blob: b28700e826d1fe0d9f4911f955be0d938e050901 [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/*
11 * Copyright (c) 2020, Arm Limited. All rights reserved.
12 *
13 * SPDX-License-Identifier: BSD-3-Clause
14 *
15 */
16 """
17
18import os
19import glob
20import sys
21import shutil
22import argparse
23from copy import deepcopy
24from collections import OrderedDict
25from jinja2 import Environment, FileSystemLoader
26from lava_helper_configs import *
27
28try:
29 from tfm_ci_pylib.utils import (
30 save_json,
31 load_json,
32 sort_dict,
33 load_yaml,
34 test,
35 print_test,
36 )
37 from tfm_ci_pylib.lava_rpc_connector import LAVA_RPC_connector
38except ImportError:
39 dir_path = os.path.dirname(os.path.realpath(__file__))
40 sys.path.append(os.path.join(dir_path, "../"))
41 from tfm_ci_pylib.utils import (
42 save_json,
43 load_json,
44 sort_dict,
45 load_yaml,
46 test,
47 print_test,
48 )
49 from tfm_ci_pylib.lava_rpc_connector import LAVA_RPC_connector
50
51
52def test_lava_dispatch_credentials(user_args):
53 """ Will validate if provided token/credentials are valid. It will return
54 a valid connection or exit program if not"""
55
56 # Collect the authentication tokens
57 try:
58 if user_args.token_from_env:
59 usr = os.environ['LAVA_USER']
60 secret = os.environ['LAVA_TOKEN']
61 elif user_args.lava_token and user_args.lava_user:
62 usr = user_args.lava_user
63 secret = user_args.lava_token
64
65 # Do not submit job without complete credentials
66 if not len(usr) or not len(secret):
67 raise Exception("Credentials not set")
68
69 lava = LAVA_RPC_connector(usr,
70 secret,
71 user_args.lava_url)
72
73 # Test the credentials againist the backend
74 if not lava.test_credentials():
75 raise Exception("Server rejected user authentication")
76 except Exception as e:
77 print("Credential validation failed with : %s" % e)
78 print("Did you set set --lava-token, --lava-user?")
79 sys.exit(1)
80 return lava
81
82def list_files_from_dir(user_args):
83 file_list = []
84 for filename in glob.iglob(user_args.job_dir + '**/*.yaml', recursive=True):
85 file_list.append(filename)
86 print("Found job {}".format(filename))
87 return file_list
88
89def lava_dispatch(user_args):
90 """ Submit a job to LAVA backend, block untill it is completed, and
91 fetch the results files if successful. If not, calls sys exit with 1
92 return code """
93
94 lava = test_lava_dispatch_credentials(user_args)
95 file_list = list_files_from_dir(user_args)
96 job_id_list = []
97 for job_file in file_list:
98 job_id, job_url = lava.submit_job(job_file)
99
100 # The reason of failure will be reported to user by LAVA_RPC_connector
101 if job_id is None and job_url is None:
102 print("Job failed")
103 else:
104 print("Job submitted at: " + job_url)
105 job_id_list.append(job_id)
106
Matthew Hartfb6fd362020-03-04 21:03:59 +0000107 print("JOBS: {}".format(",".join(str(x) for x in job_id_list)))
Dean Bircha6ede7e2020-03-13 14:00:33 +0000108
109def main(user_args):
110 lava_dispatch(user_args)
111
112
113def get_cmd_args():
114 """ Parse command line arguments """
115
116 # Parse command line arguments to override config
117 parser = argparse.ArgumentParser(description="Lava Create Jobs")
118 cmdargs = parser.add_argument_group("Create LAVA Jobs")
119
120 # Configuration control
121 cmdargs.add_argument(
122 "--lava-url", dest="lava_url", action="store", help="LAVA lab URL (without RPC2)"
123 )
124 cmdargs.add_argument(
125 "--job-dir", dest="job_dir", action="store", help="LAVA jobs directory"
126 )
127 cmdargs.add_argument(
128 "--lava-token", dest="lava_token", action="store", help="LAVA auth token"
129 )
130 cmdargs.add_argument(
131 "--lava-user", dest="lava_user", action="store", help="LAVA username"
132 )
133 cmdargs.add_argument(
134 "--use-env", dest="token_from_env", action="store_true", default=False, help="LAVA username"
135 )
136 cmdargs.add_argument(
137 "--lava-timeout", dest="dispatch_timeout", action="store", default=3600, help="LAVA username"
138 )
139 return parser.parse_args()
140
141
142if __name__ == "__main__":
143 main(get_cmd_args())