Reply
New Contributor
Posts: 1
Registered: ‎09-06-2017

Needed help on CM API 'stop_roles'

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'

 

Highlighted
Posts: 1,565
Kudos: 287
Solutions: 239
Registered: ‎07-31-2013

Re: Needed help on CM API 'stop_roles'

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)
Backline Customer Operations Engineer
Announcements