def ansible_manage_cluster(cluster_id, action): """ Perform an action on a Hadoop cluster depending on the action arg. Updates database only when starting or stopping a cluster. """ cluster = ClusterInfo.objects.get(id=cluster_id) pre_action_status = cluster.hadoop_status if action == 'format': current_hadoop_status = REVERSE_HADOOP_STATUS[cluster.hadoop_status] else: current_hadoop_status = action orka_image = OrkaImage.objects.get(image_name=cluster.os_image) chosen_image = pithos_images_uuids_properties[orka_image.image_pithos_uuid] role = chosen_image['role'] ANSIBLE_SEQUENCE = map_command_to_ansible_actions(action, chosen_image['image'], pre_action_status) cluster_name_postfix_id = '%s%s%s' % (cluster.cluster_name, '-', cluster_id) hosts_filename = os.getcwd( ) + '/' + ansible_hosts_prefix + cluster_name_postfix_id.replace(" ", "_") if isfile(hosts_filename): state = '%s %s' % (HADOOP_STATUS_ACTIONS[action][1], cluster.cluster_name) current_task.update_state(state=state) db_hadoop_update(cluster_id, 'Pending', state) debug_file_name = "create_cluster_debug_" + hosts_filename.split( ansible_hosts_prefix, 1)[1] + ".log" ansible_log = " >> " + os.path.join(os.getcwd(), debug_file_name) ansible_code_generic = 'ansible-playbook -i {0} {1} {2} -e "choose_role={3} manage_cluster={3}" -t'.format( hosts_filename, ansible_playbook, ansible_verbosity, role) for hadoop_action in ANSIBLE_SEQUENCE: ansible_code = '{0} {1} {2}'.format(ansible_code_generic, hadoop_action, ansible_log) execute_ansible_playbook(ansible_code) msg = 'Cluster %s %s' % (cluster.cluster_name, HADOOP_STATUS_ACTIONS[action][2]) db_hadoop_update(cluster_id, current_hadoop_status, msg) return msg else: msg = 'Ansible hosts file [%s] does not exist' % hosts_filename raise RuntimeError(msg)
def ansible_manage_cluster(cluster_id, action): """ Perform an action on a Hadoop cluster depending on the action arg. Updates database only when starting or stopping a cluster. """ cluster = ClusterInfo.objects.get(id=cluster_id) pre_action_status = cluster.hadoop_status if action == 'format': current_hadoop_status = REVERSE_HADOOP_STATUS[cluster.hadoop_status] else: current_hadoop_status = action orka_image = OrkaImage.objects.get(image_name=cluster.os_image) chosen_image = pithos_images_uuids_properties[orka_image.image_pithos_uuid] role = chosen_image['role'] ANSIBLE_SEQUENCE = map_command_to_ansible_actions(action, chosen_image['image'], pre_action_status) cluster_name_postfix_id = '%s%s%s' % (cluster.cluster_name, '-', cluster_id) hosts_filename = os.getcwd() + '/' + ansible_hosts_prefix + cluster_name_postfix_id.replace(" ", "_") if isfile(hosts_filename): state = '%s %s' %(HADOOP_STATUS_ACTIONS[action][1], cluster.cluster_name) current_task.update_state(state=state) db_hadoop_update(cluster_id, 'Pending', state) debug_file_name = "create_cluster_debug_" + hosts_filename.split(ansible_hosts_prefix, 1)[1] + ".log" ansible_log = " >> " + os.path.join(os.getcwd(), debug_file_name) ansible_code_generic = 'ansible-playbook -i {0} {1} {2} -e "choose_role={3} manage_cluster={3}" -t'.format(hosts_filename, ansible_playbook, ansible_verbosity, role) for hadoop_action in ANSIBLE_SEQUENCE: ansible_code = '{0} {1} {2}'.format(ansible_code_generic, hadoop_action, ansible_log) execute_ansible_playbook(ansible_code) msg = 'Cluster %s %s' %(cluster.cluster_name, HADOOP_STATUS_ACTIONS[action][2]) db_hadoop_update(cluster_id, current_hadoop_status, msg) return msg else: msg = 'Ansible hosts file [%s] does not exist' % hosts_filename raise RuntimeError(msg)
def ansible_manage_cluster(cluster_id, action): """ Perform an action on a Hadoop cluster depending on the action arg. Updates database only when starting or stopping a cluster. """ cluster = ClusterInfo.objects.get(id=cluster_id) pre_action_status = cluster.hadoop_status cluster_status = cluster.cluster_status if action == 'format': current_hadoop_status = REVERSE_HADOOP_STATUS[cluster.hadoop_status] else: current_hadoop_status = action image_tags = get_image_category(image_name=cluster.os_image) decoded_image_tags = decode_json(image_tags['ansible_cluster_config_tags']) role = decoded_image_tags['role'] ANSIBLE_SEQUENCE = map_command_to_ansible_actions(action, image_tags, pre_action_status) cluster_name_postfix_id = '%s%s%s' % (cluster.cluster_name, '-', cluster_id) hosts_filename = os.getcwd() + '/' + ansible_hosts_prefix + cluster_name_postfix_id.replace(" ", "_") if isfile(hosts_filename): state = '%s %s' %(HADOOP_STATUS_ACTIONS[action][1], cluster.cluster_name) current_task.update_state(state=state) db_hadoop_update(cluster_id, 'Pending', state) debug_file_name = "create_cluster_debug_" + hosts_filename.split(ansible_hosts_prefix, 1)[1] + ".log" ansible_log = " >> " + os.path.join(LOGS_PATH, debug_file_name)#change ansible_code_generic = 'ansible-playbook -i {0} {1} {2} -e "choose_role={3} manage_cluster={3}" -t'.format(hosts_filename, ansible_playbook, ansible_verbosity, role) for hadoop_action in ANSIBLE_SEQUENCE: ansible_code = '{0} {1} {2}'.format(ansible_code_generic, hadoop_action, ansible_log) try: execute_ansible_playbook(ansible_code) except Exception, e: msg = str(e.args[0]) db_hadoop_update(cluster_id, 'undefined', msg) raise RuntimeError(msg) msg = 'Cluster %s %s' %(cluster.cluster_name, HADOOP_STATUS_ACTIONS[action][2]) db_hadoop_update(cluster_id, current_hadoop_status, msg) return msg