def __create_metadata_to_nodes(deployment_id, mac_table, deployment_parameters): tor_gateway = deployment_parameters['tor_gateway'] tor_netmask = deployment_parameters['tor_netmask'] tor_dns = deployment_parameters['tor_dns'] servers_map = {} nodes, status_info = razor("/api/collections/nodes", "get") nodes = nodes["items"] for node in nodes: node_name = node["name"] one_node_info, status_code = razor("/api/collections/nodes/%s" % (node_name), "get") servers_map[mac_to_id(one_node_info["facts"]["macaddress"])] = node_name for to_deploy_node in mac_table: node_name = servers_map[to_deploy_node["server_id"]] id_mac = to_deploy_node["server_id"].upper() host_name, status = node_assigner("/deployment/%s/register" % deployment_id, "post", pay_load = {"mac_address":id_mac}) nics = to_deploy_node["nics"] ip_dic = {} for nic in nics: ip, status = node_assigner("/deployment/%s/ippool/allocate" % deployment_id, "post", pay_load = {"mac_address":nic['mac_address']}) ip_dic[nic['mac_address']] = ip['ip'] headers = {'content-type': 'application/json'} metadata = {'deployment_name': deployment_id, 'id_macaddress': id_mac, 'to_allocate_hostname': host_name["role"], 'to_allocate_ip': ip_dic, 'tor_netmask': tor_netmask, 'tor_gateway': tor_gateway, 'tor_dns': tor_dns} for key in metadata: request_data = {"node": node_name, "key": key, "value": metadata[key]} razor("/api/commands/update-node-metadata", "post", pay_load=json.dumps(request_data), headers=headers)
def __get_node_name_by_id(server_id): nodes, status_info = razor("/api/collections/nodes", "get") nodes = nodes["items"] for node in nodes: one_node_info, status_code = razor("/api/collections/nodes/%s" % (node['name']), "get") if mac_to_id(one_node_info["facts"]["macaddress"]) == server_id: return node["name"] return None
def __create_metadata_to_node(server_id, os_parameters): node_name = __get_node_name_by_id(server_id) if node_name is None: logger.error("Can't find node with mac address: %s" % server_id) headers = {'content-type': 'application/json'} metadata = {'id_macaddress': server_id.upper(), 'to_allocate_hostname': os_parameters['hostname'], 'to_allocate_ip': os_parameters['ip_address'], } for key in metadata: request_data = {"node": node_name, "key": key, "value": metadata[key]} razor("/api/commands/update-node-metadata", "post", pay_load=json.dumps(request_data), headers=headers)
def del_openstack_deployment(deployment_id): delete_task(deployment_id) headers = {'content-type': 'application/json'} command = {} command["name"] = "openstack-" + deployment_id + "-policy" text, status_code = razor("/api/commands/delete-policy", "post", pay_load=json.dumps(command), headers=headers) command = {} command["name"] = "openstack-" + deployment_id + "-tag" text, status_code = razor("/api/commands/delete-tag", "post", pay_load=json.dumps(command), headers=headers) text, status_code = node_assigner("/deployment/%s" % deployment_id, "delete") text, status_code = process_controller("/openstack/deployments/%s" % deployment_id, "delete") nodes, status_info = razor("/api/collections/nodes", "get") nodes = nodes["items"] for node in nodes: node_name = node["name"] one_node_info, status_code = razor("/api/collections/nodes/%s" % (node_name), "get") if "metadata" in one_node_info and "deployment_name" in one_node_info["metadata"].keys() and one_node_info["metadata"]["deployment_name"] == deployment_id: command = {} command["node"] = one_node_info["name"] command["key"] = "deployment_name" text, status_code = razor("/api/commands/remove-node-metadata", "post", pay_load=json.dumps(command), headers=headers) command["key"] = "id_macaddress" text, status_code = razor("/api/commands/remove-node-metadata", "post", pay_load=json.dumps(command), headers=headers) command["key"] = "to_allocate_hostname" text, status_code = razor("/api/commands/remove-node-metadata", "post", pay_load=json.dumps(command), headers=headers) command["key"] = "to_allocate_ip" text, status_code = razor("/api/commands/remove-node-metadata", "post", pay_load=json.dumps(command), headers=headers) return "Deleted"
def __send_reinstall_command(mac_table): servers_map = {} nodes, status_info = razor("/api/collections/nodes", "get") nodes = nodes["items"] for node in nodes: node_name = node["name"] one_node_info, status_code = razor("/api/collections/nodes/%s" % (node_name), "get") servers_map[mac_to_id(one_node_info["facts"]["macaddress"])] = node_name for to_deploy_node in mac_table: node_name = servers_map[to_deploy_node["server_id"]] request_data = {} request_data["name"] = node_name headers = {'content-type': 'application/json'} razor("/api/commands/reinstall-node", "post", pay_load=json.dumps(request_data), headers=headers)
def activate_ipmi(server_id): post_data = json.loads(request.data) nodes, status_code = razor("/api/collections/nodes", "get") nodes = nodes["items"] for node in nodes: node_name = node["name"] one_node_info, status_code = razor("/api/collections/nodes/%s" % (node_name), "get") if mac_to_id(one_node_info["facts"]["macaddress"]) == server_id: post_data.update({'name': node_name}) break headers = {'content-type': 'application/json'} res, status_code = razor("/api/commands/set-node-ipmi-credentials", "post", pay_load=json.dumps(post_data), headers=headers) return make_response(res['result'], status_code)
def baremetal_server_list(): servers = {} server_num = 0 IPMI_num = 0 started_num = 0 shutdown_num = 0 server_list = [] mac_ip_dic = scan_macs() nodes, status_code = razor("/api/collections/nodes", "get") nodes = nodes["items"] for node in nodes: one_server = {} node_name = node["name"] logger.info("Querying node: %s" % node_name) one_node_info, status_code = razor("/api/collections/nodes/%s" % (node_name), "get") one_server["id"] = mac_to_id(one_node_info["facts"]["macaddress"]) one_server["is_ipmi"] = one_node_info.get("ipmi", False) if one_server["is_ipmi"]: IPMI_num += 1 one_server['node_name'] = node_name one_server["current_role"] = __get_current_role(one_node_info) one_server["current_deployment"] = __get_current_deployment(one_node_info) one_server["networks"] = __get_node_networks(one_node_info) one_server["status"] = __check_if_install(node_name) and check_server_status(one_node_info) or 'Installing' one_server["hostname"] = __get_node_hostname(one_node_info) one_server["ip_address"] = mac_ip_dic.get(one_node_info["facts"]["macaddress"], 'Unknown') if one_server["status"] == "Started": started_num += 1 elif one_server["status"] == "Shutdown": shutdown_num += 1 server_list.append(one_server) server_num += 1 servers["server_num"] = server_num servers["IPMI_num"] = IPMI_num servers["Started"] = started_num servers["shutdown"] = shutdown_num servers["server_list"] = server_list return json.dumps(servers)
def server_status(server_id): nodes, status_code = razor("/api/collections/nodes", "get") nodes = nodes["items"] for node in nodes: one_server = {} node_name = node["name"] logger.info("Querying node: %s" % node_name) one_node_info, status_code = razor("/api/collections/nodes/%s" % (node_name), "get") if mac_to_id(one_node_info["facts"]["macaddress"]) == server_id: status = {} status["server_id"] = server_id status["status"] = check_server_status(one_node_info) return json.dumps(status) return make_response("Cannot find the server!", 404)
def __create_razor_tags(task_id, server_id): logger.info("Creating tag in Razor: %s" % server_id) command = {} command["name"] = 'oprs-' + server_id + "-tag" command["rule"] = ["=", ["fact", "macaddress"], id_to_mac(server_id)] headers = {'content-type': 'application/json'} text, status_code = razor("/api/commands/create-tag", "post", pay_load=json.dumps(command), headers=headers) __log_status(task_id, 'Create_Razor_Tag', status_code) return command["name"]
def start_server_api(server_id): nodes, status_code = razor("/api/collections/nodes", "get") nodes = nodes["items"] for node in nodes: node_name = node["name"] one_node_info, status_code = razor("/api/collections/nodes/%s" % (node_name), "get") if mac_to_id(one_node_info["facts"]["macaddress"]) == server_id: ipmi_host = one_node_info["ipmi_hostname"] ipmi_username = one_node_info["ipmi_username"] ipmi_password = one_node_info["ipmi_password"] output = {} output["server_id"] = server_id output["status"] = str(start_server(ipmi_host, ipmi_username, ipmi_password)) return json.dumps(output) output = {} output["server_id"] = server_id output["status"] = str(False) return json.dumps(output)
def __check_if_all_install(mac_table, servers_map): all_installed = True for node in mac_table: node_name = servers_map[node["server_id"]] one_node_info, status_code = razor("/api/collections/nodes/%s" % (node_name), "get") all_installed = all_installed and "stage" in one_node_info["state"].keys() and one_node_info["state"]["stage"] == "boot_local" if not all_installed: return all_installed return all_installed
def __update_razor_tags(task_id, mac_table, deployment_id): logger.info("Updating tag in Razor: %s" % deployment_id) command = {} command["name"] = 'openstack-' + deployment_id + "-tag" text, status_code = razor("/api/collections/tags/%s" % (command["name"]), "get") command["rule"] = text["rule"] for mac in mac_table: one_rule = [] one_rule.append("=") one_rule.append(["fact", "macaddress"]) mac_address = id_to_mac(str(mac["server_id"])) one_rule.append(mac_address) command["rule"].append(one_rule) command["force"] = True headers = {'content-type': 'application/json'} text, status_code = razor("/api/commands/update-tag-rule", "post", pay_load=json.dumps(command), headers=headers) __log_status(task_id, status[4], status_code) return command["name"]
def __restart_servers(task_id, mac_table): servers_map = {} nodes, status_code = razor("/api/collections/nodes", "get") nodes = nodes['items'] is_sucessful = True for node in nodes: node_name = node["name"] one_node_info, status_code = razor("/api/collections/nodes/%s" % (node_name), "get") if one_node_info["ipmi"]: ipmi_infos = {} ipmi_infos["host"] = one_node_info["ipmi_hostname"] ipmi_infos["username"] = one_node_info["ipmi_username"] ipmi_infos["password"] = one_node_info["ipmi_password"] servers_map[mac_to_id(one_node_info["facts"]["macaddress"])] = ipmi_infos for node in mac_table: ipmi_infos = servers_map[node["server_id"]] is_sucessful = is_sucessful and restart_server(ipmi_infos["host"], ipmi_infos["username"], ipmi_infos["password"]) __log_status(task_id, status[6], is_sucessful)
def __create_razor_policies(task_id, tag_id, server_id, os_parameters): logger.info("Creating policy in Razor: %s" % server_id) policies, status_code = razor("/api/collections/policies", "get") policy_list = policies["items"] top_policy = policy_list[0]["name"] command = {} command["name"] = "oprs-" + server_id + "-policy" command["repo"] = os_parameters['repo'] command["task"] = os_parameters['task'] command["broker"] = "mco" command["hostname"] = os_parameters['hostname'] command["root_password"] = "******" command["tags"] = [tag_id] command["before"] = top_policy headers = {'content-type': 'application/json'} text, status_code = razor("/api/commands/create-policy", "post", pay_load=json.dumps(command), headers=headers) __log_status(task_id, 'Create_Razor_Policy', status_code) return command["name"]
def __install_operation_system(task_id, mac_table): __send_reinstall_command(mac_table) __restart_servers(task_id, mac_table) servers_map = {} nodes, status_code = razor("/api/collections/nodes", "get") nodes = nodes["items"] for node in nodes: node_name = node["name"] one_node_info, status_code = razor("/api/collections/nodes/%s" % (node_name), "get") servers_map[mac_to_id(one_node_info["facts"]["macaddress"])] = node_name total_time = 0 all_installed = True while not __check_if_all_install(mac_table, servers_map): time.sleep(each_check_span) total_time += each_check_span if total_time >= time_out_to_install_os: all_installed = False break __log_status(task_id, status[7], all_installed)
def __create_razor_policies(task_id, tag_id, node_assigner_url, deployment_id): logger.info("Creating policy in Razor: %s" % deployment_id) command = {} command["name"] = "openstack-" + deployment_id + "-policy" command["repo"] = "centos7_mini" command["task"] = "centos7_sh" command["broker"] = "mco" command["hostname"] = "${ref:node_assigner:{fact:\"macaddress\", deployment:\"%s\", url:\"%s\"}}" % (deployment_id, node_assigner_url) command["root_password"] = "******" command["tags"] = [] command["tags"].append(tag_id) headers = {'content-type': 'application/json'} text, status_code = razor("/api/commands/create-policy", "post", pay_load=json.dumps(command), headers=headers) __log_status(task_id, status[5], status_code) return command["name"]
def __get_task_by_repo(repo): one_repo, status_code = razor("/api/collections/repos/%s" % (repo), "get") return one_repo['task']['name']
def __check_if_install(node_name): one_node_info, status_code = razor("/api/collections/nodes/%s" % (node_name), "get") if_installed = "stage" in one_node_info["state"].keys() and one_node_info["state"]["stage"] == "boot_local" return if_installed
def __restart_server(task_id, node_name): one_node_info, status_code = razor("/api/collections/nodes/%s" % (node_name), "get") if not one_node_info["ipmi"]: logger.error('Node: %s does not have IPMI info, can not be restarted' % node_name) is_sucessful = restart_server(one_node_info["ipmi_hostname"], one_node_info["ipmi_username"], one_node_info["ipmi_password"]) __log_status(task_id, "Restart_Server", is_sucessful)
def __send_reinstall_command(node_name): request_data = {} request_data["name"] = node_name headers = {'content-type': 'application/json'} razor("/api/commands/reinstall-node", "post", pay_load=json.dumps(request_data), headers=headers)
def __check_razor_env(): brokers, status_code = razor("/api/collections/brokers", "get") tasks, status_code = razor("/api/collections/tasks", "get") repos, status_code = razor("/api/collections/repos", "get") return (bool(filter(lambda x: x['name']=="mco", brokers["items"])) and bool(filter(lambda x: x['name']=="centos7_sh", tasks["items"])) and bool(filter(lambda x: x['name']=="centos7_mini", repos["items"])))
def __clear_razor_tag(server_id): headers = {'content-type': 'application/json'} command = {} command["name"] = 'oprs-' + server_id + "-tag" text, status_code = razor("/api/commands/delete-tag", "post", pay_load=json.dumps(command), headers=headers)
def get_repos(): repos, status_code = razor("/api/collections/repos", "get") repos = repos["items"] repo_list = map(lambda x: x['name'], repos) return json.dumps({"repos": repo_list})