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 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 __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 __parse_one_node(node):
    row = "%s,%s" % (mac_to_id(node["server_id"]), node["role"])
    for nic in node['nics']:
        if nic['enable'] is True:
            if nic.get('allocated_ip', None) != None:
                row = row + ',"%s,%s"' % (nic['mac_address'].upper(), nic['allocated_ip'])
            else:
                row = row + ',"%s,%s"' % (nic['mac_address'].upper(), 'dhcp')
        else:
            row = row + ',"%s,%s"' % (nic['mac_address'].upper(), 'null')

    for i in range(max_nics - len(node['nics'])):
        row = row + ','

    return row
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 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 __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 __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)