Created 09-06-2017 08:11 AM
Hi,
Can someone help me?
I am not able to stop roles for node specified in “xyz_host” file
host_file = open("xyz_host", "r") host_l = host_file.readlines() host_list = map(str.strip, host_l) #print host_list cdh5 = None for c in api.get_all_clusters(): print c.name print c.version if c.name == "cluster": if c.version == "CDH5": cdh5 = c for s in cdh5.get_all_services(): print s for r in s.get_all_roles(): print r current_host = api.get_host(r.hostRef.hostId).hostname if current_host in host_list: cmd = r.stop_roles(r.hostRef.hostId)
I am failing in below step.
File "./xyz.py", line 33, in <module> cmd = r.stop_roles(r.hostRef.hostId) AttributeError: 'ApiRole' object has no attribute 'stop_roles'
Created 11-07-2017 11:25 PM
Are you following the epydoc of CM API Python bindings for your development? It can be found here: http://cloudera.github.io/cm_api/epydoc/5.13.0/index.html
Looking over your snippet's logic, it seems like you want to stop a role if its on some specified hosts. The API call you want at the end therefore should just be instructing the role to stop itself, not stop_roles (which is available only on the ApiService object, not on the ApiRole or ApiHost objects)
I'd rewrite your API calls as below, with the above in mind:
host_file = open("xyz_host", "r") host_l = host_file.readlines() host_list = map(str.strip, host_l) #print host_list cdh5 = None for c in api.get_all_clusters(): print c.name print c.version if c.name == "cluster": if c.version == "CDH5": cdh5 = c for s in cdh5.get_all_services(): roles = s.get_all_roles() filtered_role_names = [role.name for role in roles if api.get_host(role.hostRef.hostId).hostname in host_list] s.stop_roles(filtered_role_names)
Created 10-18-2018 12:48 PM
Harsh,
I have tried your solution but running into the following error:
Traceback (most recent call last):
File "testing.py", line 34, in <module>
s.stop_roles(filtered_role_names)
File "/usr/lib/python2.6/site-packages/cm_api/endpoints/services.py", line 734, in stop_roles
return self._role_cmd('stop', role_names)
File "/usr/lib/python2.6/site-packages/cm_api/endpoints/services.py", line 128, in _role_cmd
data=roles, api_version=api_version)
File "/usr/lib/python2.6/site-packages/cm_api/endpoints/types.py", line 362, in _post
api_version)
File "/usr/lib/python2.6/site-packages/cm_api/endpoints/types.py", line 380, in _call
api_version)
File "/usr/lib/python2.6/site-packages/cm_api/endpoints/types.py", line 137, in call
ret = method(path, data=data, params=params)
File "/usr/lib/python2.6/site-packages/cm_api/resource.py", line 148, in post
self._make_headers(contenttype))
File "/usr/lib/python2.6/site-packages/cm_api/resource.py", line 73, in invoke
headers=headers)
File "/usr/lib/python2.6/site-packages/cm_api/http_client.py", line 174, in execute
raise self._exc_class(ex)
cm_api.api_client.ApiException: Can not deserialize instance of java.lang.String out of START_ARRAY token
at [Source: org.apache.cxf.transport.http.AbstractHTTPDestination$1@4211c08b; line: 1, column: 12] (through reference chain: com.cloudera.api.model.ApiRoleNameList["items"]) (error 400)
Please find below the code:
#!/usr/bin/python
# Get a handle to the API client
import time
import datetime
import os
from cm_api.api_client import ApiResource
cred_file = open("cred", "r")
cred_l = cred_file.readlines()
cred_list = map(str.strip, cred_l)
cm_host = cred_list[0]
api = ApiResource(cm_host, version=1, username=cred_list[1], password=cred_list[2])
host_file = open("hadoop_host_file", "r")
host_l = host_file.readlines()
host_list = map(str.strip, host_l)
# Get a list of all clusters
cdh5 = None
for c in api.get_all_clusters():
print c.name
print c.version
if c.name == "Cluster 1":
if c.version == "CDH5":
cdh5 = c
for s in cdh5.get_all_services():
roles = s.get_all_roles()
# print roles
filtered_role_names = [role.name for role in roles if api.get_host(role.hostRef.hostId).hostname in host_list]
print filtered_role_names
s.stop_roles(filtered_role_names)
I am not sure if I am missing something. Any pointers are helpful.