def yarn_start():
    '''
    Starts yarn services (nodemanager, resourcemanager)
    :return: Returns success if yarn_start_cmd command executes successfully or error message is shown
    '''
    log.info("\nStarting Yarn\n")
    header_key = request.headers.get('API-KEY')
    api_status = check_apiKey(header_key)
    if api_status == 'success':
        result = run_services.run_bigdata_services(
            yarn_start_cmd, 'Started Yarn Services',
            'Error Starting Yarn Services')
        update_rm_info()
        return result
    else:
        return api_status
def nm_stop(cluster_id):
    ip = get_system_ip()
    sql = "select id, status, web_port, rpyc_port from yarn_yarn where ip='%s' and type=0 and cluster_id=%d" % (
        ip, cluster_id)
    rs = run_services.get_service_status(sql)
    id = rs[0]
    status = rs[1]
    web_port = rs[2]
    rpyc_port = rs[3]
    if status == "RUNNING":
        result = run_services.run_bigdata_services(
            nm_stop_cmd, 'Node manager Stopped', 'Error Stopping Node manager')
        update_rm_info()
        return run_services.confirm_stop(result, "yarn_yarn", id, web_port,
                                         rpyc_port)
    else:
        return '{"success": 1, "msg": ["Node Manager Already Stopped!!!"]}'
def rm_start(cluster_id):
    ip = get_system_ip()
    sql = "select id, status, web_port, rpyc_port from yarn_yarn where ip='%s' and type=1 and cluster_id=%d" % (
        ip, cluster_id)
    rs = run_services.get_service_status(sql)
    id = rs[0]
    status = rs[1]
    web_port = rs[2]
    rpyc_port = rs[3]
    if status != "RUNNING":
        result = run_services.run_bigdata_services(
            rm_start_cmd, 'Resource manager Started',
            'Error Starting Resource manager')
        update_rm_info()
        return run_services.confirm_start(result, "yarn_yarn", id, web_port,
                                          rpyc_port)
    else:
        return '{"success": 1, "msg": ["Resource Manager Already Running!!!"]}'
def yarn_stop():
    log.info("\nStopping Yarn\n")
    header_key = request.headers.get('API-KEY')
    api_status = check_apiKey(header_key)
    if api_status == 'success':
        conn = get_postgres_connection()
        cur = conn.cursor()
        sql = "select status from yarn_yarn where type=1"
        cur.execute(sql)
        rows = cur.fetchall()
        cur.close()
        conn.close()
        status_list = [' '.join(item) for item in rows]
        if "RUNNING" in status_list:
            result = run_services.run_bigdata_services(
                yarn_stop_cmd, 'Stopped Yarn Services',
                'Error Stopping Yarn Services')
            if json.loads(result)["success"]:
                update_rm_info()
                return result
        else:
            return '{"success": 1}'
    else:
        return api_status