blob: 1e27932e1f3bb6647b36e2153a9216f5a7d464b8 [file] [log] [blame]
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +01001#!/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
8from __future__ import print_function
9
10__copyright__ = """
11/*
Xinyu Zhang82dab282022-10-09 16:33:19 +080012 * Copyright (c) 2018-2022, Arm Limited. All rights reserved.
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010013 *
14 * SPDX-License-Identifier: BSD-3-Clause
15 *
16 */
17 """
Karl Zhang08681e62020-10-30 13:56:03 +080018
19__author__ = "tf-m@lists.trustedfirmware.org"
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010020__project__ = "Trusted Firmware-M Open CI"
Xinyu Zhang06286a92021-07-22 14:00:51 +080021__version__ = "1.4.0"
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010022
23import xmlrpc.client
Paul Sokolovsky0c5e8da2024-03-06 12:18:02 +070024import os
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010025import time
Paul Sokolovskyd042d9e2024-03-11 15:15:26 +070026import json
Matthew Hartfb6fd362020-03-04 21:03:59 +000027import yaml
Matthew Hart4a4f1202020-06-12 15:52:46 +010028import requests
29import shutil
Paul Sokolovsky0c5e8da2024-03-06 12:18:02 +070030import subprocess
Paul Sokolovskyb06bf6f2022-12-27 13:46:24 +030031import logging
32
33
34_log = logging.getLogger("lavaci")
35
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010036
37class LAVA_RPC_connector(xmlrpc.client.ServerProxy, object):
38
39 def __init__(self,
40 username,
41 token,
42 hostname,
43 rest_prefix="RPC2",
44 https=False):
45
46 # If user provides hostname with http/s prefix
47 if "://" in hostname:
48 htp_pre, hostname = hostname.split("://")
49 server_addr = "%s://%s:%s@%s/%s" % (htp_pre,
50 username,
51 token,
52 hostname,
53 rest_prefix)
54 self.server_url = "%s://%s" % (htp_pre, hostname)
55 else:
56 server_addr = "%s://%s:%s@%s/%s" % ("https" if https else "http",
57 username,
58 token,
59 hostname,
60 rest_prefix)
61 self.server_url = "%s://%s" % ("https" if https else "http",
62 hostname)
63
64 self.server_job_prefix = "%s/scheduler/job/%%s" % self.server_url
Milosz Wasilewski4c4190d2020-12-15 12:56:22 +000065 self.server_api = "%s/api/v0.2/" % self.server_url
Matthew Hart4a4f1202020-06-12 15:52:46 +010066 self.server_results_prefix = "%s/results/%%s" % self.server_url
Matthew Hartc6bbbf92020-08-19 14:12:07 +010067 self.token = token
68 self.username = username
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010069 super(LAVA_RPC_connector, self).__init__(server_addr)
70
71 def _rpc_cmd_raw(self, cmd, params=None):
72 """ Run a remote comand and return the result. There is no constrain
73 check on the syntax of the command. """
74
75 cmd = "self.%s(%s)" % (cmd, params if params else "")
76 return eval(cmd)
77
Paul Sokolovskyd042d9e2024-03-11 15:15:26 +070078 @staticmethod
79 def is_tux_id(job_id):
80 job_id = str(job_id)
81 if job_id.isdigit() and len(job_id) < 22:
82 return False
83 else:
84 return True
85
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010086 def ls_cmd(self):
87 """ Return a list of supported commands """
88
89 print("\n".join(self.system.listMethods()))
90
Matthew Hart4a4f1202020-06-12 15:52:46 +010091 def fetch_file(self, url, out_file):
Matthew Hartc6bbbf92020-08-19 14:12:07 +010092 auth_params = {
93 'user': self.username,
94 'token': self.token
95 }
Paul Sokolovsky903bc432022-12-29 17:15:04 +030096 with requests.get(url, stream=True, params=auth_params) as r:
97 r.raise_for_status()
98 with open(out_file, 'wb') as f:
99 shutil.copyfileobj(r.raw, f)
100 return(out_file)
Matthew Hart4a4f1202020-06-12 15:52:46 +0100101
Paul Sokolovskyd042d9e2024-03-11 15:15:26 +0700102 def get_job_results(self, job_id, job_info, yaml_out_file):
103 if self.is_tux_id(job_id):
104 results_url = job_info["extra"]["download_url"] + "lava-results.yaml"
105 else:
106 results_url = "{}/yaml".format(self.server_results_prefix % job_id)
Matthew Hart4a4f1202020-06-12 15:52:46 +0100107 return(self.fetch_file(results_url, yaml_out_file))
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100108
Paul Sokolovskyd042d9e2024-03-11 15:15:26 +0700109 def get_job_definition(self, job_id, job_info, yaml_out_file=None):
110 if self.is_tux_id(job_id):
111 url = job_info["extra"]["download_url"] + job_info["extra"]["job_definition"]
112 with requests.get(url) as r:
113 r.raise_for_status()
114 job_def = r.text
115 else:
116 job_def = self.scheduler.jobs.definition(job_id)
117
Matthew Hartfb6fd362020-03-04 21:03:59 +0000118 if yaml_out_file:
119 with open(yaml_out_file, "w") as F:
120 F.write(str(job_def))
Paul Sokolovskyf2f385d2022-01-11 00:36:31 +0300121 def_o = yaml.safe_load(job_def)
Xinyu Zhang82dab282022-10-09 16:33:19 +0800122 return def_o
Matthew Hartfb6fd362020-03-04 21:03:59 +0000123
Matthew Hart4a4f1202020-06-12 15:52:46 +0100124 def get_job_log(self, job_id, target_out_file):
Paul Sokolovskyd042d9e2024-03-11 15:15:26 +0700125 if self.is_tux_id(job_id):
126 auth_headers = {}
127 log_url = "https://storage.tuxsuite.com/public/tfc/ci/tests/{job_id}/lava-logs.yaml".format(
128 job_id=job_id
129 )
130 else:
131 auth_headers = {"Authorization": "Token %s" % self.token}
132 log_url = "{server_url}/jobs/{job_id}/logs/".format(
133 server_url=self.server_api, job_id=job_id
134 )
Fathi Boudrac10378c2021-01-21 18:25:19 +0100135 with requests.get(log_url, stream=True, headers=auth_headers) as r:
Paul Sokolovsky903bc432022-12-29 17:15:04 +0300136 r.raise_for_status()
Fathi Boudrac10378c2021-01-21 18:25:19 +0100137 log_list = yaml.load(r.content, Loader=yaml.SafeLoader)
138 with open(target_out_file, "w") as target_out:
139 for line in log_list:
140 level = line["lvl"]
141 if (level == "target") or (level == "feedback"):
142 try:
143 target_out.write("{}\n".format(line["msg"]))
144 except UnicodeEncodeError:
145 msg = (
146 line["msg"]
147 .encode("ascii", errors="replace")
148 .decode("ascii")
149 )
150 target_out.write("{}\n".format(msg))
Matthew Hartfb6fd362020-03-04 21:03:59 +0000151
Matthew Hart4a4f1202020-06-12 15:52:46 +0100152 def get_job_config(self, job_id, config_out_file):
Paul Sokolovskyd042d9e2024-03-11 15:15:26 +0700153 if self.is_tux_id(job_id):
154 return
155
Matthew Hart4a4f1202020-06-12 15:52:46 +0100156 config_url = "{}/configuration".format(self.server_job_prefix % job_id)
157 self.fetch_file(config_url, config_out_file)
Matthew Hartfb6fd362020-03-04 21:03:59 +0000158
159 def get_job_info(self, job_id, yaml_out_file=None):
Paul Sokolovskyd042d9e2024-03-11 15:15:26 +0700160 if self.is_tux_id(job_id):
161 assert yaml_out_file is None
162 job_info = subprocess.check_output(
163 "python3 -u -m tuxsuite test get --json %s" % job_id,
164 shell=True,
165 )
166 job_info = json.loads(job_info.decode())
167 # Convert values to match LAVA output, as expected by
168 # the rest of code.
169 job_info["state"] = job_info["state"].capitalize()
170 job_info["health"] = {"pass": "Complete"}.get(job_info["result"], job_info["result"])
Paul Sokolovsky4ff31ab2024-03-21 13:36:31 +0700171 # There's no "job_name" aka "description" in Tux data, but we utilize
172 # the fact that it's included in the original name of the job definition
173 # file, that info included in the Tux data.
174 job_info["description"] = job_info["extra"]["job_definition"].split("/", 1)[1].split(".", 1)[0]
Paul Sokolovskyd042d9e2024-03-11 15:15:26 +0700175 return job_info
176
Matthew Hartfb6fd362020-03-04 21:03:59 +0000177 job_info = self.scheduler.jobs.show(job_id)
178 if yaml_out_file:
179 with open(yaml_out_file, "w") as F:
180 F.write(str(job_info))
181 return job_info
182
183 def get_error_reason(self, job_id):
Matthew Hart2c2688f2020-05-26 13:09:20 +0100184 try:
185 lava_res = self.results.get_testsuite_results_yaml(job_id, 'lava')
Paul Sokolovskyf2f385d2022-01-11 00:36:31 +0300186 results = yaml.safe_load(lava_res)
Matthew Hart2c2688f2020-05-26 13:09:20 +0100187 for test in results:
188 if test['name'] == 'job':
189 return(test.get('metadata', {}).get('error_type', ''))
190 except Exception:
191 return("Unknown")
Matthew Hartfb6fd362020-03-04 21:03:59 +0000192
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100193 def get_job_state(self, job_id):
194 return self.scheduler.job_state(job_id)["job_state"]
195
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100196 def cancel_job(self, job_id):
197 """ Cancell job with id=job_id. Returns True if successfull """
198
199 return self.scheduler.jobs.cancel(job_id)
200
201 def validate_job_yaml(self, job_definition, print_err=False):
202 """ Validate a job definition syntax. Returns true is server considers
203 the syntax valid """
204
205 try:
206 with open(job_definition) as F:
207 input_yaml = F.read()
208 self.scheduler.validate_yaml(input_yaml)
209 return True
210 except Exception as E:
211 if print_err:
212 print(E)
213 return False
214
Matthew Hart110e1dc2020-05-27 17:18:55 +0100215 def device_type_from_def(self, job_data):
Paul Sokolovskyf2f385d2022-01-11 00:36:31 +0300216 def_yaml = yaml.safe_load(job_data)
Matthew Hart110e1dc2020-05-27 17:18:55 +0100217 return(def_yaml['device_type'])
218
219 def has_device_type(self, job_data):
220 d_type = self.device_type_from_def(job_data)
221 all_d = self.scheduler.devices.list()
222 for device in all_d:
223 if device['type'] == d_type:
224 if device['health'] in ['Good', 'Unknown']:
225 return(True)
226 return(False)
227
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100228 def submit_job(self, job_definition):
229 """ Will submit a yaml definition pointed by job_definition after
230 validating it againist the remote backend. Returns resulting job id,
231 and server url for job"""
232
233 try:
234 if not self.validate_job_yaml(job_definition):
Paul Sokolovsky80b9b352024-03-05 16:38:41 +0700235 _log.error("Server rejected job's syntax")
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100236 raise Exception("Invalid job")
237 with open(job_definition, "r") as F:
238 job_data = F.read()
239 except Exception as e:
240 print("Cannot submit invalid job. Check %s's content" %
241 job_definition)
242 print(e)
243 return None, None
Paul Sokolovsky0c5e8da2024-03-06 12:18:02 +0700244
245 device_type = self.device_type_from_def(job_data)
246
247 if device_type == "fvp" and os.environ.get("USE_TUXSUITE_FVP", "0") == "1":
248 output = subprocess.check_output(
249 "python3 -u -m tuxsuite test submit --no-wait --device fvp-lava --job-definition %s" % job_definition,
250 shell=True,
251 )
252
253 job_id = job_url = None
254 for l in output.decode().split("\n"):
255 _log.debug(l)
256 if l.startswith("uid:"):
257 job_id = l.split(None, 1)[1].strip()
258 job_url = "https://tuxapi.tuxsuite.com/v1/groups/tfc/projects/ci/tests/" + job_id
259 return (job_id, job_url)
260
Dean Bircha6ede7e2020-03-13 14:00:33 +0000261 try:
Dean Birch1d545c02020-05-29 14:09:21 +0100262 if self.has_device_type(job_data):
263 job_id = self.scheduler.submit_job(job_data)
264 job_url = self.server_job_prefix % job_id
265 return(job_id, job_url)
266 else:
267 raise Exception("No devices online with required device_type")
Dean Bircha6ede7e2020-03-13 14:00:33 +0000268 except Exception as e:
Paul Sokolovskyb2ca65b2024-03-11 15:07:34 +0700269 _log.exception("Exception submitting job to LAVA", e)
Dean Bircha6ede7e2020-03-13 14:00:33 +0000270 return(None, None)
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100271
272 def resubmit_job(self, job_id):
273 """ Re-submit job with provided id. Returns resulting job id,
274 and server url for job"""
275
276 job_id = self.scheduler.resubmit_job(job_id)
277 job_url = self.server_job_prefix % job_id
278 return(job_id, job_url)
279
280 def block_wait_for_job(self, job_id, timeout, poll_freq=1):
281 """ Will block code execution and wait for the job to submit.
282 Returns job status on completion """
283
284 start_t = int(time.time())
285 while(True):
286 cur_t = int(time.time())
287 if cur_t - start_t >= timeout:
288 print("Breaking because of timeout")
289 break
290 # Check if the job is not running
Dean Arnoldf1169b92020-03-11 10:14:14 +0000291 cur_status = self.get_job_state(job_id)
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100292 # If in queue or running wait
Dean Arnoldc1d81b42020-03-11 15:56:36 +0000293 if cur_status not in ["Canceling","Finished"]:
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100294 time.sleep(poll_freq)
295 else:
296 break
Dean Arnoldc1d81b42020-03-11 15:56:36 +0000297 return self.scheduler.job_health(job_id)["job_health"]
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100298
Paul Sokolovskyf9bad0d2024-03-25 15:17:38 +0700299 def block_wait_for_jobs(self, job_ids, timeout, poll_freq=10, callback=None):
Matthew Hartfb6fd362020-03-04 21:03:59 +0000300 """ Wait for multiple LAVA job ids to finish and return finished list """
301
302 start_t = int(time.time())
303 finished_jobs = {}
304 while(True):
305 cur_t = int(time.time())
306 if cur_t - start_t >= timeout:
307 print("Breaking because of timeout")
308 break
309 for job_id in job_ids:
Paul Sokolovskyfb298c62022-04-29 23:15:17 +0300310 if job_id in finished_jobs:
311 continue
Matthew Hartfb6fd362020-03-04 21:03:59 +0000312 # Check if the job is not running
Paul Sokolovsky81ff0ad2022-12-29 21:47:01 +0300313 try:
314 cur_status = self.get_job_info(job_id)
Paul Sokolovskyc82f9332023-01-10 23:50:25 +0300315 except (xmlrpc.client.ProtocolError, OSError) as e:
Paul Sokolovsky81ff0ad2022-12-29 21:47:01 +0300316 # There can be transient HTTP errors, e.g. "502 Proxy Error"
Paul Sokolovskyc82f9332023-01-10 23:50:25 +0300317 # or socket timeout.
Paul Sokolovsky81ff0ad2022-12-29 21:47:01 +0300318 # Just continue with the next job, the faulted one will be
319 # re-checked on next iteration.
Paul Sokolovskyc82f9332023-01-10 23:50:25 +0300320 _log.warning("block_wait_for_jobs: %r occurred, ignore and continue", e)
Paul Sokolovsky81ff0ad2022-12-29 21:47:01 +0300321 time.sleep(2)
322 continue
Matthew Hartfb6fd362020-03-04 21:03:59 +0000323 # If in queue or running wait
324 if cur_status['state'] in ["Canceling","Finished"]:
325 cur_status['error_reason'] = self.get_error_reason(job_id)
326 finished_jobs[job_id] = cur_status
Paul Sokolovskyb06bf6f2022-12-27 13:46:24 +0300327 _log.info(
Paul Sokolovsky6e83a232024-03-11 15:30:04 +0700328 "Job %s finished in %ds with state: %s, health: %s. Remaining: %d",
Paul Sokolovskyb7a41a92022-12-28 18:06:45 +0300329 job_id, time.time() - start_t,
330 cur_status['state'],
331 cur_status['health'],
Paul Sokolovskyb06bf6f2022-12-27 13:46:24 +0300332 len(job_ids) - len(finished_jobs)
333 )
Paul Sokolovskyf9bad0d2024-03-25 15:17:38 +0700334 if callback:
335 callback(job_id, cur_status)
Matthew Hartfb6fd362020-03-04 21:03:59 +0000336 if len(job_ids) == len(finished_jobs):
337 break
338 else:
339 time.sleep(poll_freq)
340 if len(job_ids) == len(finished_jobs):
341 break
342 return finished_jobs
343
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100344 def test_credentials(self):
345 """ Attempt to querry the back-end and verify that the user provided
346 authentication is valid """
347
348 try:
349 self._rpc_cmd_raw("system.listMethods")
350 return True
351 except Exception as e:
352 print(e)
353 print("Credential validation failed")
354 return False
355
356
357if __name__ == "__main__":
358 pass