Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | """ lava_rpc_connector.py: |
| 4 | |
| 5 | class that extends xmlrpc in order to add LAVA specific functionality. |
| 6 | Used in managing communication with the back-end. """ |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | |
| 10 | __copyright__ = """ |
| 11 | /* |
Xinyu Zhang | 82dab28 | 2022-10-09 16:33:19 +0800 | [diff] [blame] | 12 | * Copyright (c) 2018-2022, Arm Limited. All rights reserved. |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 13 | * |
| 14 | * SPDX-License-Identifier: BSD-3-Clause |
| 15 | * |
| 16 | */ |
| 17 | """ |
Karl Zhang | 08681e6 | 2020-10-30 13:56:03 +0800 | [diff] [blame] | 18 | |
| 19 | __author__ = "tf-m@lists.trustedfirmware.org" |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 20 | __project__ = "Trusted Firmware-M Open CI" |
Xinyu Zhang | 06286a9 | 2021-07-22 14:00:51 +0800 | [diff] [blame] | 21 | __version__ = "1.4.0" |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 22 | |
| 23 | import xmlrpc.client |
Paul Sokolovsky | 0c5e8da | 2024-03-06 12:18:02 +0700 | [diff] [blame^] | 24 | import os |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 25 | import time |
Matthew Hart | fb6fd36 | 2020-03-04 21:03:59 +0000 | [diff] [blame] | 26 | import yaml |
Matthew Hart | 4a4f120 | 2020-06-12 15:52:46 +0100 | [diff] [blame] | 27 | import requests |
| 28 | import shutil |
Paul Sokolovsky | 0c5e8da | 2024-03-06 12:18:02 +0700 | [diff] [blame^] | 29 | import subprocess |
Paul Sokolovsky | b06bf6f | 2022-12-27 13:46:24 +0300 | [diff] [blame] | 30 | import logging |
| 31 | |
| 32 | |
| 33 | _log = logging.getLogger("lavaci") |
| 34 | |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 35 | |
| 36 | class LAVA_RPC_connector(xmlrpc.client.ServerProxy, object): |
| 37 | |
| 38 | def __init__(self, |
| 39 | username, |
| 40 | token, |
| 41 | hostname, |
| 42 | rest_prefix="RPC2", |
| 43 | https=False): |
| 44 | |
| 45 | # If user provides hostname with http/s prefix |
| 46 | if "://" in hostname: |
| 47 | htp_pre, hostname = hostname.split("://") |
| 48 | server_addr = "%s://%s:%s@%s/%s" % (htp_pre, |
| 49 | username, |
| 50 | token, |
| 51 | hostname, |
| 52 | rest_prefix) |
| 53 | self.server_url = "%s://%s" % (htp_pre, hostname) |
| 54 | else: |
| 55 | server_addr = "%s://%s:%s@%s/%s" % ("https" if https else "http", |
| 56 | username, |
| 57 | token, |
| 58 | hostname, |
| 59 | rest_prefix) |
| 60 | self.server_url = "%s://%s" % ("https" if https else "http", |
| 61 | hostname) |
| 62 | |
| 63 | self.server_job_prefix = "%s/scheduler/job/%%s" % self.server_url |
Milosz Wasilewski | 4c4190d | 2020-12-15 12:56:22 +0000 | [diff] [blame] | 64 | self.server_api = "%s/api/v0.2/" % self.server_url |
Matthew Hart | 4a4f120 | 2020-06-12 15:52:46 +0100 | [diff] [blame] | 65 | self.server_results_prefix = "%s/results/%%s" % self.server_url |
Matthew Hart | c6bbbf9 | 2020-08-19 14:12:07 +0100 | [diff] [blame] | 66 | self.token = token |
| 67 | self.username = username |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 68 | super(LAVA_RPC_connector, self).__init__(server_addr) |
| 69 | |
| 70 | def _rpc_cmd_raw(self, cmd, params=None): |
| 71 | """ Run a remote comand and return the result. There is no constrain |
| 72 | check on the syntax of the command. """ |
| 73 | |
| 74 | cmd = "self.%s(%s)" % (cmd, params if params else "") |
| 75 | return eval(cmd) |
| 76 | |
| 77 | def ls_cmd(self): |
| 78 | """ Return a list of supported commands """ |
| 79 | |
| 80 | print("\n".join(self.system.listMethods())) |
| 81 | |
Matthew Hart | 4a4f120 | 2020-06-12 15:52:46 +0100 | [diff] [blame] | 82 | def fetch_file(self, url, out_file): |
Matthew Hart | c6bbbf9 | 2020-08-19 14:12:07 +0100 | [diff] [blame] | 83 | auth_params = { |
| 84 | 'user': self.username, |
| 85 | 'token': self.token |
| 86 | } |
Paul Sokolovsky | 903bc43 | 2022-12-29 17:15:04 +0300 | [diff] [blame] | 87 | with requests.get(url, stream=True, params=auth_params) as r: |
| 88 | r.raise_for_status() |
| 89 | with open(out_file, 'wb') as f: |
| 90 | shutil.copyfileobj(r.raw, f) |
| 91 | return(out_file) |
Matthew Hart | 4a4f120 | 2020-06-12 15:52:46 +0100 | [diff] [blame] | 92 | |
| 93 | def get_job_results(self, job_id, yaml_out_file): |
| 94 | results_url = "{}/yaml".format(self.server_results_prefix % job_id) |
| 95 | return(self.fetch_file(results_url, yaml_out_file)) |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 96 | |
Matthew Hart | fb6fd36 | 2020-03-04 21:03:59 +0000 | [diff] [blame] | 97 | def get_job_definition(self, job_id, yaml_out_file=None): |
| 98 | job_def = self.scheduler.jobs.definition(job_id) |
| 99 | if yaml_out_file: |
| 100 | with open(yaml_out_file, "w") as F: |
| 101 | F.write(str(job_def)) |
Paul Sokolovsky | f2f385d | 2022-01-11 00:36:31 +0300 | [diff] [blame] | 102 | def_o = yaml.safe_load(job_def) |
Xinyu Zhang | 82dab28 | 2022-10-09 16:33:19 +0800 | [diff] [blame] | 103 | return def_o |
Matthew Hart | fb6fd36 | 2020-03-04 21:03:59 +0000 | [diff] [blame] | 104 | |
Matthew Hart | 4a4f120 | 2020-06-12 15:52:46 +0100 | [diff] [blame] | 105 | def get_job_log(self, job_id, target_out_file): |
Milosz Wasilewski | 4c4190d | 2020-12-15 12:56:22 +0000 | [diff] [blame] | 106 | auth_headers = {"Authorization": "Token %s" % self.token} |
| 107 | log_url = "{server_url}/jobs/{job_id}/logs/".format( |
| 108 | server_url=self.server_api, job_id=job_id |
| 109 | ) |
Fathi Boudra | c10378c | 2021-01-21 18:25:19 +0100 | [diff] [blame] | 110 | with requests.get(log_url, stream=True, headers=auth_headers) as r: |
Paul Sokolovsky | 903bc43 | 2022-12-29 17:15:04 +0300 | [diff] [blame] | 111 | r.raise_for_status() |
Fathi Boudra | c10378c | 2021-01-21 18:25:19 +0100 | [diff] [blame] | 112 | log_list = yaml.load(r.content, Loader=yaml.SafeLoader) |
| 113 | with open(target_out_file, "w") as target_out: |
| 114 | for line in log_list: |
| 115 | level = line["lvl"] |
| 116 | if (level == "target") or (level == "feedback"): |
| 117 | try: |
| 118 | target_out.write("{}\n".format(line["msg"])) |
| 119 | except UnicodeEncodeError: |
| 120 | msg = ( |
| 121 | line["msg"] |
| 122 | .encode("ascii", errors="replace") |
| 123 | .decode("ascii") |
| 124 | ) |
| 125 | target_out.write("{}\n".format(msg)) |
Matthew Hart | fb6fd36 | 2020-03-04 21:03:59 +0000 | [diff] [blame] | 126 | |
Matthew Hart | 4a4f120 | 2020-06-12 15:52:46 +0100 | [diff] [blame] | 127 | def get_job_config(self, job_id, config_out_file): |
| 128 | config_url = "{}/configuration".format(self.server_job_prefix % job_id) |
| 129 | self.fetch_file(config_url, config_out_file) |
Matthew Hart | fb6fd36 | 2020-03-04 21:03:59 +0000 | [diff] [blame] | 130 | |
| 131 | def get_job_info(self, job_id, yaml_out_file=None): |
| 132 | job_info = self.scheduler.jobs.show(job_id) |
| 133 | if yaml_out_file: |
| 134 | with open(yaml_out_file, "w") as F: |
| 135 | F.write(str(job_info)) |
| 136 | return job_info |
| 137 | |
| 138 | def get_error_reason(self, job_id): |
Matthew Hart | 2c2688f | 2020-05-26 13:09:20 +0100 | [diff] [blame] | 139 | try: |
| 140 | lava_res = self.results.get_testsuite_results_yaml(job_id, 'lava') |
Paul Sokolovsky | f2f385d | 2022-01-11 00:36:31 +0300 | [diff] [blame] | 141 | results = yaml.safe_load(lava_res) |
Matthew Hart | 2c2688f | 2020-05-26 13:09:20 +0100 | [diff] [blame] | 142 | for test in results: |
| 143 | if test['name'] == 'job': |
| 144 | return(test.get('metadata', {}).get('error_type', '')) |
| 145 | except Exception: |
| 146 | return("Unknown") |
Matthew Hart | fb6fd36 | 2020-03-04 21:03:59 +0000 | [diff] [blame] | 147 | |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 148 | def get_job_state(self, job_id): |
| 149 | return self.scheduler.job_state(job_id)["job_state"] |
| 150 | |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 151 | def cancel_job(self, job_id): |
| 152 | """ Cancell job with id=job_id. Returns True if successfull """ |
| 153 | |
| 154 | return self.scheduler.jobs.cancel(job_id) |
| 155 | |
| 156 | def validate_job_yaml(self, job_definition, print_err=False): |
| 157 | """ Validate a job definition syntax. Returns true is server considers |
| 158 | the syntax valid """ |
| 159 | |
| 160 | try: |
| 161 | with open(job_definition) as F: |
| 162 | input_yaml = F.read() |
| 163 | self.scheduler.validate_yaml(input_yaml) |
| 164 | return True |
| 165 | except Exception as E: |
| 166 | if print_err: |
| 167 | print(E) |
| 168 | return False |
| 169 | |
Matthew Hart | 110e1dc | 2020-05-27 17:18:55 +0100 | [diff] [blame] | 170 | def device_type_from_def(self, job_data): |
Paul Sokolovsky | f2f385d | 2022-01-11 00:36:31 +0300 | [diff] [blame] | 171 | def_yaml = yaml.safe_load(job_data) |
Matthew Hart | 110e1dc | 2020-05-27 17:18:55 +0100 | [diff] [blame] | 172 | return(def_yaml['device_type']) |
| 173 | |
| 174 | def has_device_type(self, job_data): |
| 175 | d_type = self.device_type_from_def(job_data) |
| 176 | all_d = self.scheduler.devices.list() |
| 177 | for device in all_d: |
| 178 | if device['type'] == d_type: |
| 179 | if device['health'] in ['Good', 'Unknown']: |
| 180 | return(True) |
| 181 | return(False) |
| 182 | |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 183 | def submit_job(self, job_definition): |
| 184 | """ Will submit a yaml definition pointed by job_definition after |
| 185 | validating it againist the remote backend. Returns resulting job id, |
| 186 | and server url for job""" |
| 187 | |
| 188 | try: |
| 189 | if not self.validate_job_yaml(job_definition): |
Paul Sokolovsky | 80b9b35 | 2024-03-05 16:38:41 +0700 | [diff] [blame] | 190 | _log.error("Server rejected job's syntax") |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 191 | raise Exception("Invalid job") |
| 192 | with open(job_definition, "r") as F: |
| 193 | job_data = F.read() |
| 194 | except Exception as e: |
| 195 | print("Cannot submit invalid job. Check %s's content" % |
| 196 | job_definition) |
| 197 | print(e) |
| 198 | return None, None |
Paul Sokolovsky | 0c5e8da | 2024-03-06 12:18:02 +0700 | [diff] [blame^] | 199 | |
| 200 | device_type = self.device_type_from_def(job_data) |
| 201 | |
| 202 | if device_type == "fvp" and os.environ.get("USE_TUXSUITE_FVP", "0") == "1": |
| 203 | output = subprocess.check_output( |
| 204 | "python3 -u -m tuxsuite test submit --no-wait --device fvp-lava --job-definition %s" % job_definition, |
| 205 | shell=True, |
| 206 | ) |
| 207 | |
| 208 | job_id = job_url = None |
| 209 | for l in output.decode().split("\n"): |
| 210 | _log.debug(l) |
| 211 | if l.startswith("uid:"): |
| 212 | job_id = l.split(None, 1)[1].strip() |
| 213 | job_url = "https://tuxapi.tuxsuite.com/v1/groups/tfc/projects/ci/tests/" + job_id |
| 214 | return (job_id, job_url) |
| 215 | |
Dean Birch | a6ede7e | 2020-03-13 14:00:33 +0000 | [diff] [blame] | 216 | try: |
Dean Birch | 1d545c0 | 2020-05-29 14:09:21 +0100 | [diff] [blame] | 217 | if self.has_device_type(job_data): |
| 218 | job_id = self.scheduler.submit_job(job_data) |
| 219 | job_url = self.server_job_prefix % job_id |
| 220 | return(job_id, job_url) |
| 221 | else: |
| 222 | raise Exception("No devices online with required device_type") |
Dean Birch | a6ede7e | 2020-03-13 14:00:33 +0000 | [diff] [blame] | 223 | except Exception as e: |
Paul Sokolovsky | b2ca65b | 2024-03-11 15:07:34 +0700 | [diff] [blame] | 224 | _log.exception("Exception submitting job to LAVA", e) |
Dean Birch | a6ede7e | 2020-03-13 14:00:33 +0000 | [diff] [blame] | 225 | return(None, None) |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 226 | |
| 227 | def resubmit_job(self, job_id): |
| 228 | """ Re-submit job with provided id. Returns resulting job id, |
| 229 | and server url for job""" |
| 230 | |
| 231 | job_id = self.scheduler.resubmit_job(job_id) |
| 232 | job_url = self.server_job_prefix % job_id |
| 233 | return(job_id, job_url) |
| 234 | |
| 235 | def block_wait_for_job(self, job_id, timeout, poll_freq=1): |
| 236 | """ Will block code execution and wait for the job to submit. |
| 237 | Returns job status on completion """ |
| 238 | |
| 239 | start_t = int(time.time()) |
| 240 | while(True): |
| 241 | cur_t = int(time.time()) |
| 242 | if cur_t - start_t >= timeout: |
| 243 | print("Breaking because of timeout") |
| 244 | break |
| 245 | # Check if the job is not running |
Dean Arnold | f1169b9 | 2020-03-11 10:14:14 +0000 | [diff] [blame] | 246 | cur_status = self.get_job_state(job_id) |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 247 | # If in queue or running wait |
Dean Arnold | c1d81b4 | 2020-03-11 15:56:36 +0000 | [diff] [blame] | 248 | if cur_status not in ["Canceling","Finished"]: |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 249 | time.sleep(poll_freq) |
| 250 | else: |
| 251 | break |
Dean Arnold | c1d81b4 | 2020-03-11 15:56:36 +0000 | [diff] [blame] | 252 | return self.scheduler.job_health(job_id)["job_health"] |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 253 | |
Matthew Hart | fb6fd36 | 2020-03-04 21:03:59 +0000 | [diff] [blame] | 254 | def block_wait_for_jobs(self, job_ids, timeout, poll_freq=10): |
| 255 | """ Wait for multiple LAVA job ids to finish and return finished list """ |
| 256 | |
| 257 | start_t = int(time.time()) |
| 258 | finished_jobs = {} |
| 259 | while(True): |
| 260 | cur_t = int(time.time()) |
| 261 | if cur_t - start_t >= timeout: |
| 262 | print("Breaking because of timeout") |
| 263 | break |
| 264 | for job_id in job_ids: |
Paul Sokolovsky | fb298c6 | 2022-04-29 23:15:17 +0300 | [diff] [blame] | 265 | if job_id in finished_jobs: |
| 266 | continue |
Matthew Hart | fb6fd36 | 2020-03-04 21:03:59 +0000 | [diff] [blame] | 267 | # Check if the job is not running |
Paul Sokolovsky | 81ff0ad | 2022-12-29 21:47:01 +0300 | [diff] [blame] | 268 | try: |
| 269 | cur_status = self.get_job_info(job_id) |
Paul Sokolovsky | c82f933 | 2023-01-10 23:50:25 +0300 | [diff] [blame] | 270 | except (xmlrpc.client.ProtocolError, OSError) as e: |
Paul Sokolovsky | 81ff0ad | 2022-12-29 21:47:01 +0300 | [diff] [blame] | 271 | # There can be transient HTTP errors, e.g. "502 Proxy Error" |
Paul Sokolovsky | c82f933 | 2023-01-10 23:50:25 +0300 | [diff] [blame] | 272 | # or socket timeout. |
Paul Sokolovsky | 81ff0ad | 2022-12-29 21:47:01 +0300 | [diff] [blame] | 273 | # Just continue with the next job, the faulted one will be |
| 274 | # re-checked on next iteration. |
Paul Sokolovsky | c82f933 | 2023-01-10 23:50:25 +0300 | [diff] [blame] | 275 | _log.warning("block_wait_for_jobs: %r occurred, ignore and continue", e) |
Paul Sokolovsky | 81ff0ad | 2022-12-29 21:47:01 +0300 | [diff] [blame] | 276 | time.sleep(2) |
| 277 | continue |
Matthew Hart | fb6fd36 | 2020-03-04 21:03:59 +0000 | [diff] [blame] | 278 | # If in queue or running wait |
| 279 | if cur_status['state'] in ["Canceling","Finished"]: |
| 280 | cur_status['error_reason'] = self.get_error_reason(job_id) |
| 281 | finished_jobs[job_id] = cur_status |
Paul Sokolovsky | b06bf6f | 2022-12-27 13:46:24 +0300 | [diff] [blame] | 282 | _log.info( |
Paul Sokolovsky | 6e83a23 | 2024-03-11 15:30:04 +0700 | [diff] [blame] | 283 | "Job %s finished in %ds with state: %s, health: %s. Remaining: %d", |
Paul Sokolovsky | b7a41a9 | 2022-12-28 18:06:45 +0300 | [diff] [blame] | 284 | job_id, time.time() - start_t, |
| 285 | cur_status['state'], |
| 286 | cur_status['health'], |
Paul Sokolovsky | b06bf6f | 2022-12-27 13:46:24 +0300 | [diff] [blame] | 287 | len(job_ids) - len(finished_jobs) |
| 288 | ) |
Matthew Hart | fb6fd36 | 2020-03-04 21:03:59 +0000 | [diff] [blame] | 289 | if len(job_ids) == len(finished_jobs): |
| 290 | break |
| 291 | else: |
| 292 | time.sleep(poll_freq) |
| 293 | if len(job_ids) == len(finished_jobs): |
| 294 | break |
| 295 | return finished_jobs |
| 296 | |
Minos Galanakis | f4ca6ac | 2017-12-11 02:39:21 +0100 | [diff] [blame] | 297 | def test_credentials(self): |
| 298 | """ Attempt to querry the back-end and verify that the user provided |
| 299 | authentication is valid """ |
| 300 | |
| 301 | try: |
| 302 | self._rpc_cmd_raw("system.listMethods") |
| 303 | return True |
| 304 | except Exception as e: |
| 305 | print(e) |
| 306 | print("Credential validation failed") |
| 307 | return False |
| 308 | |
| 309 | |
| 310 | if __name__ == "__main__": |
| 311 | pass |