blob: 3c913181cf7669a7bd2574db75c56e7323613874 [file] [log] [blame]
Dean Bircha6ede7e2020-03-13 14:00:33 +00001#!/usr/bin/env python3
2
3from __future__ import print_function
4
5"""
6Script for submitting multiple LAVA definitions
7"""
8
9__copyright__ = """
10/*
Xinyu Zhangf2b7cbf2021-05-18 20:17:34 +080011 * Copyright (c) 2020-2021, Arm Limited. All rights reserved.
Dean Bircha6ede7e2020-03-13 14:00:33 +000012 *
13 * SPDX-License-Identifier: BSD-3-Clause
14 *
15 */
16 """
17
18import os
19import glob
20import sys
Dean Bircha6ede7e2020-03-13 14:00:33 +000021import argparse
Dean Bircha6ede7e2020-03-13 14:00:33 +000022from lava_helper_configs import *
23
24try:
25 from tfm_ci_pylib.utils import (
26 save_json,
27 load_json,
28 sort_dict,
29 load_yaml,
30 test,
31 print_test,
32 )
33 from tfm_ci_pylib.lava_rpc_connector import LAVA_RPC_connector
34except ImportError:
35 dir_path = os.path.dirname(os.path.realpath(__file__))
36 sys.path.append(os.path.join(dir_path, "../"))
37 from tfm_ci_pylib.utils import (
38 save_json,
39 load_json,
40 sort_dict,
41 load_yaml,
42 test,
43 print_test,
44 )
45 from tfm_ci_pylib.lava_rpc_connector import LAVA_RPC_connector
46
47
48def test_lava_dispatch_credentials(user_args):
49 """ Will validate if provided token/credentials are valid. It will return
50 a valid connection or exit program if not"""
51
52 # Collect the authentication tokens
53 try:
54 if user_args.token_from_env:
55 usr = os.environ['LAVA_USER']
56 secret = os.environ['LAVA_TOKEN']
57 elif user_args.lava_token and user_args.lava_user:
58 usr = user_args.lava_user
59 secret = user_args.lava_token
60
61 # Do not submit job without complete credentials
62 if not len(usr) or not len(secret):
63 raise Exception("Credentials not set")
64
65 lava = LAVA_RPC_connector(usr,
66 secret,
67 user_args.lava_url)
68
69 # Test the credentials againist the backend
70 if not lava.test_credentials():
71 raise Exception("Server rejected user authentication")
72 except Exception as e:
73 print("Credential validation failed with : %s" % e)
74 print("Did you set set --lava-token, --lava-user?")
75 sys.exit(1)
76 return lava
77
Xinyu Zhangf2b7cbf2021-05-18 20:17:34 +080078def list_files_from_dir(user_args, job_dir=""):
79 if job_dir == "":
80 job_dir = user_args.job_dir
Dean Bircha6ede7e2020-03-13 14:00:33 +000081 file_list = []
Xinyu Zhangf2b7cbf2021-05-18 20:17:34 +080082 for filename in glob.iglob(job_dir + '**/*.yaml', recursive=True):
Dean Bircha6ede7e2020-03-13 14:00:33 +000083 file_list.append(filename)
84 print("Found job {}".format(filename))
85 return file_list
86
Xinyu Zhangf2b7cbf2021-05-18 20:17:34 +080087def lava_dispatch(user_args, job_dir=""):
Dean Bircha6ede7e2020-03-13 14:00:33 +000088 """ Submit a job to LAVA backend, block untill it is completed, and
89 fetch the results files if successful. If not, calls sys exit with 1
90 return code """
91
Xinyu Zhangf2b7cbf2021-05-18 20:17:34 +080092 if job_dir == "":
93 job_dir = user_args.job_dir
94
Dean Bircha6ede7e2020-03-13 14:00:33 +000095 lava = test_lava_dispatch_credentials(user_args)
Xinyu Zhangf2b7cbf2021-05-18 20:17:34 +080096 file_list = list_files_from_dir(user_args, job_dir)
Dean Bircha6ede7e2020-03-13 14:00:33 +000097 job_id_list = []
98 for job_file in file_list:
99 job_id, job_url = lava.submit_job(job_file)
100
101 # The reason of failure will be reported to user by LAVA_RPC_connector
102 if job_id is None and job_url is None:
103 print("Job failed")
104 else:
105 print("Job submitted at: " + job_url)
106 job_id_list.append(job_id)
107
Xinyu Zhangf2b7cbf2021-05-18 20:17:34 +0800108 return job_id_list
Dean Bircha6ede7e2020-03-13 14:00:33 +0000109
110def main(user_args):
Xinyu Zhangf2b7cbf2021-05-18 20:17:34 +0800111 job_id_list = lava_dispatch(user_args)
112 print("JOBS: {}".format(",".join(str(x) for x in job_id_list)))
Dean Bircha6ede7e2020-03-13 14:00:33 +0000113
114
115def get_cmd_args():
116 """ Parse command line arguments """
117
118 # Parse command line arguments to override config
119 parser = argparse.ArgumentParser(description="Lava Create Jobs")
120 cmdargs = parser.add_argument_group("Create LAVA Jobs")
121
122 # Configuration control
123 cmdargs.add_argument(
124 "--lava-url", dest="lava_url", action="store", help="LAVA lab URL (without RPC2)"
125 )
126 cmdargs.add_argument(
127 "--job-dir", dest="job_dir", action="store", help="LAVA jobs directory"
128 )
129 cmdargs.add_argument(
130 "--lava-token", dest="lava_token", action="store", help="LAVA auth token"
131 )
132 cmdargs.add_argument(
133 "--lava-user", dest="lava_user", action="store", help="LAVA username"
134 )
135 cmdargs.add_argument(
136 "--use-env", dest="token_from_env", action="store_true", default=False, help="LAVA username"
137 )
138 cmdargs.add_argument(
139 "--lava-timeout", dest="dispatch_timeout", action="store", default=3600, help="LAVA username"
140 )
141 return parser.parse_args()
142
143
144if __name__ == "__main__":
145 main(get_cmd_args())