Skip to content

Instantly share code, notes, and snippets.

@andrewstevenson
Created September 11, 2016 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewstevenson/27b6eefe728f48e42bda3399abd745cf to your computer and use it in GitHub Desktop.
Save andrewstevenson/27b6eefe728f48e42bda3399abd745cf to your computer and use it in GitHub Desktop.
from cm_api.api_client import ApiResource, ApiException
cm_host = "my_cm_host"
cluster_name = "Cloudera QuickStart"
service_name = "flume"
role_group_name = "Logging"
role_type = "AGENT"
config_file = "/Users/andrew/agent.conf"
host_name = "quickstart.cloudera"
agent_name = "appLogger"
CMD_TIMEOUT = 420
## Get api handle
api = ApiResource(cm_host, 7180, "cloudera", "cloudera")
## Get cluster
cluster = api.get_cluster(cluster_name)
## Get service
service = cluster.get_service(service_name)
## Create role group on service
print "Checking if role group %s exists. It will be created if not." % role_group_name
try:
service.create_role_config_group(role_group_name, role_group_name, role_type)
print " Role group %s created." % role_group_name
except ApiException as err:
print " Role group %s already exists." % role_group_name
## Update role group configuration
role_group = service.get_role_config_group(role_group_name)
## Read the config file
f = open(config_file,'r')
flume_conf = ""
while 1:
line = f.readline()
if not line:break
flume_conf += line
f.close()
## Update the role group configuration
print "Updating role group configuration."
role_group.update_config({"agent_config_file" : flume_conf})
# Add host to role group
print "Checking if role group '%s' exists. It will be created if not." % role_group_name
try:
host_id = api.get_host(host_name).hostId
role = service.create_role(role_type = role_type_name, role_name = agent_name, host_id = host_id)
print " Created role '%s'" % agent_name
except ApiException as err:
print " Role '%s' already exists." % agent_name
## Add the agent to the role group
print "Adding agent '%s' on host '%s' to role group '%s'" % (agent_name, host_name, role_group_name)
try:
role_group.move_roles([role.name])
print " Added agent '%s' on host '%s' to role group '%s'." % (agent_name, host_name, role_group_name)
except ApiException as err:
print " Failed to move agent '%s' on host '%s' to role group '%s'. '%s'"
% (agent_name, host_name, role_group_name, err.message)
## Restart the roles
print "Restarting agent %s on host %s in role group %s" % (agent_name, host_name, role_group_name)
try:
role = service.get_role(agent_name)
cmds = service.restart_roles(role.name)
for cmd in cmds:
print " Waiting for restart..."
cmd.wait(CMD_TIMEOUT)
print "Agents restarted."
except ApiException as err:
"Failed to restart agent %s on host %s in role group %s. %s"
% (agent_name, host_name, role_group_name, err.message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment