Dean Birch | a6ede7e | 2020-03-13 14:00:33 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | from __future__ import print_function |
| 4 | |
| 5 | """ |
| 6 | Script 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 | |
| 18 | import os |
| 19 | import glob |
| 20 | import sys |
Dean Birch | a6ede7e | 2020-03-13 14:00:33 +0000 | [diff] [blame] | 21 | import argparse |
Dean Birch | a6ede7e | 2020-03-13 14:00:33 +0000 | [diff] [blame] | 22 | from lava_helper_configs import * |
| 23 | |
| 24 | try: |
| 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 |
| 34 | except 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 | |
| 48 | def 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 | |
| 78 | def list_files_from_dir(user_args): |
| 79 | file_list = [] |
| 80 | for filename in glob.iglob(user_args.job_dir + '**/*.yaml', recursive=True): |
| 81 | file_list.append(filename) |
| 82 | print("Found job {}".format(filename)) |
| 83 | return file_list |
| 84 | |
| 85 | def lava_dispatch(user_args): |
| 86 | """ Submit a job to LAVA backend, block untill it is completed, and |
| 87 | fetch the results files if successful. If not, calls sys exit with 1 |
| 88 | return code """ |
| 89 | |
| 90 | lava = test_lava_dispatch_credentials(user_args) |
| 91 | file_list = list_files_from_dir(user_args) |
| 92 | job_id_list = [] |
| 93 | for job_file in file_list: |
| 94 | job_id, job_url = lava.submit_job(job_file) |
| 95 | |
| 96 | # The reason of failure will be reported to user by LAVA_RPC_connector |
| 97 | if job_id is None and job_url is None: |
| 98 | print("Job failed") |
| 99 | else: |
| 100 | print("Job submitted at: " + job_url) |
| 101 | job_id_list.append(job_id) |
| 102 | |
Matthew Hart | fb6fd36 | 2020-03-04 21:03:59 +0000 | [diff] [blame] | 103 | print("JOBS: {}".format(",".join(str(x) for x in job_id_list))) |
Dean Birch | a6ede7e | 2020-03-13 14:00:33 +0000 | [diff] [blame] | 104 | |
| 105 | def main(user_args): |
| 106 | lava_dispatch(user_args) |
| 107 | |
| 108 | |
| 109 | def get_cmd_args(): |
| 110 | """ Parse command line arguments """ |
| 111 | |
| 112 | # Parse command line arguments to override config |
| 113 | parser = argparse.ArgumentParser(description="Lava Create Jobs") |
| 114 | cmdargs = parser.add_argument_group("Create LAVA Jobs") |
| 115 | |
| 116 | # Configuration control |
| 117 | cmdargs.add_argument( |
| 118 | "--lava-url", dest="lava_url", action="store", help="LAVA lab URL (without RPC2)" |
| 119 | ) |
| 120 | cmdargs.add_argument( |
| 121 | "--job-dir", dest="job_dir", action="store", help="LAVA jobs directory" |
| 122 | ) |
| 123 | cmdargs.add_argument( |
| 124 | "--lava-token", dest="lava_token", action="store", help="LAVA auth token" |
| 125 | ) |
| 126 | cmdargs.add_argument( |
| 127 | "--lava-user", dest="lava_user", action="store", help="LAVA username" |
| 128 | ) |
| 129 | cmdargs.add_argument( |
| 130 | "--use-env", dest="token_from_env", action="store_true", default=False, help="LAVA username" |
| 131 | ) |
| 132 | cmdargs.add_argument( |
| 133 | "--lava-timeout", dest="dispatch_timeout", action="store", default=3600, help="LAVA username" |
| 134 | ) |
| 135 | return parser.parse_args() |
| 136 | |
| 137 | |
| 138 | if __name__ == "__main__": |
| 139 | main(get_cmd_args()) |