blob: 1234ce656e27ea0d5d8ea01b1c297d2632fe1d08 [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"])
171 return job_info
172
Matthew Hartfb6fd362020-03-04 21:03:59 +0000173 job_info = self.scheduler.jobs.show(job_id)
174 if yaml_out_file:
175 with open(yaml_out_file, "w") as F:
176 F.write(str(job_info))
177 return job_info
178
179 def get_error_reason(self, job_id):
Matthew Hart2c2688f2020-05-26 13:09:20 +0100180 try:
181 lava_res = self.results.get_testsuite_results_yaml(job_id, 'lava')
Paul Sokolovskyf2f385d2022-01-11 00:36:31 +0300182 results = yaml.safe_load(lava_res)
Matthew Hart2c2688f2020-05-26 13:09:20 +0100183 for test in results:
184 if test['name'] == 'job':
185 return(test.get('metadata', {}).get('error_type', ''))
186 except Exception:
187 return("Unknown")
Matthew Hartfb6fd362020-03-04 21:03:59 +0000188
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100189 def get_job_state(self, job_id):
190 return self.scheduler.job_state(job_id)["job_state"]
191
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100192 def cancel_job(self, job_id):
193 """ Cancell job with id=job_id. Returns True if successfull """
194
195 return self.scheduler.jobs.cancel(job_id)
196
197 def validate_job_yaml(self, job_definition, print_err=False):
198 """ Validate a job definition syntax. Returns true is server considers
199 the syntax valid """
200
201 try:
202 with open(job_definition) as F:
203 input_yaml = F.read()
204 self.scheduler.validate_yaml(input_yaml)
205 return True
206 except Exception as E:
207 if print_err:
208 print(E)
209 return False
210
Matthew Hart110e1dc2020-05-27 17:18:55 +0100211 def device_type_from_def(self, job_data):
Paul Sokolovskyf2f385d2022-01-11 00:36:31 +0300212 def_yaml = yaml.safe_load(job_data)
Matthew Hart110e1dc2020-05-27 17:18:55 +0100213 return(def_yaml['device_type'])
214
215 def has_device_type(self, job_data):
216 d_type = self.device_type_from_def(job_data)
217 all_d = self.scheduler.devices.list()
218 for device in all_d:
219 if device['type'] == d_type:
220 if device['health'] in ['Good', 'Unknown']:
221 return(True)
222 return(False)
223
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100224 def submit_job(self, job_definition):
225 """ Will submit a yaml definition pointed by job_definition after
226 validating it againist the remote backend. Returns resulting job id,
227 and server url for job"""
228
229 try:
230 if not self.validate_job_yaml(job_definition):
Paul Sokolovsky80b9b352024-03-05 16:38:41 +0700231 _log.error("Server rejected job's syntax")
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100232 raise Exception("Invalid job")
233 with open(job_definition, "r") as F:
234 job_data = F.read()
235 except Exception as e:
236 print("Cannot submit invalid job. Check %s's content" %
237 job_definition)
238 print(e)
239 return None, None
Paul Sokolovsky0c5e8da2024-03-06 12:18:02 +0700240
241 device_type = self.device_type_from_def(job_data)
242
243 if device_type == "fvp" and os.environ.get("USE_TUXSUITE_FVP", "0") == "1":
244 output = subprocess.check_output(
245 "python3 -u -m tuxsuite test submit --no-wait --device fvp-lava --job-definition %s" % job_definition,
246 shell=True,
247 )
248
249 job_id = job_url = None
250 for l in output.decode().split("\n"):
251 _log.debug(l)
252 if l.startswith("uid:"):
253 job_id = l.split(None, 1)[1].strip()
254 job_url = "https://tuxapi.tuxsuite.com/v1/groups/tfc/projects/ci/tests/" + job_id
255 return (job_id, job_url)
256
Dean Bircha6ede7e2020-03-13 14:00:33 +0000257 try:
Dean Birch1d545c02020-05-29 14:09:21 +0100258 if self.has_device_type(job_data):
259 job_id = self.scheduler.submit_job(job_data)
260 job_url = self.server_job_prefix % job_id
261 return(job_id, job_url)
262 else:
263 raise Exception("No devices online with required device_type")
Dean Bircha6ede7e2020-03-13 14:00:33 +0000264 except Exception as e:
Paul Sokolovskyb2ca65b2024-03-11 15:07:34 +0700265 _log.exception("Exception submitting job to LAVA", e)
Dean Bircha6ede7e2020-03-13 14:00:33 +0000266 return(None, None)
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100267
268 def resubmit_job(self, job_id):
269 """ Re-submit job with provided id. Returns resulting job id,
270 and server url for job"""
271
272 job_id = self.scheduler.resubmit_job(job_id)
273 job_url = self.server_job_prefix % job_id
274 return(job_id, job_url)
275
276 def block_wait_for_job(self, job_id, timeout, poll_freq=1):
277 """ Will block code execution and wait for the job to submit.
278 Returns job status on completion """
279
280 start_t = int(time.time())
281 while(True):
282 cur_t = int(time.time())
283 if cur_t - start_t >= timeout:
284 print("Breaking because of timeout")
285 break
286 # Check if the job is not running
Dean Arnoldf1169b92020-03-11 10:14:14 +0000287 cur_status = self.get_job_state(job_id)
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100288 # If in queue or running wait
Dean Arnoldc1d81b42020-03-11 15:56:36 +0000289 if cur_status not in ["Canceling","Finished"]:
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100290 time.sleep(poll_freq)
291 else:
292 break
Dean Arnoldc1d81b42020-03-11 15:56:36 +0000293 return self.scheduler.job_health(job_id)["job_health"]
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100294
Matthew Hartfb6fd362020-03-04 21:03:59 +0000295 def block_wait_for_jobs(self, job_ids, timeout, poll_freq=10):
296 """ Wait for multiple LAVA job ids to finish and return finished list """
297
298 start_t = int(time.time())
299 finished_jobs = {}
300 while(True):
301 cur_t = int(time.time())
302 if cur_t - start_t >= timeout:
303 print("Breaking because of timeout")
304 break
305 for job_id in job_ids:
Paul Sokolovskyfb298c62022-04-29 23:15:17 +0300306 if job_id in finished_jobs:
307 continue
Matthew Hartfb6fd362020-03-04 21:03:59 +0000308 # Check if the job is not running
Paul Sokolovsky81ff0ad2022-12-29 21:47:01 +0300309 try:
310 cur_status = self.get_job_info(job_id)
Paul Sokolovskyc82f9332023-01-10 23:50:25 +0300311 except (xmlrpc.client.ProtocolError, OSError) as e:
Paul Sokolovsky81ff0ad2022-12-29 21:47:01 +0300312 # There can be transient HTTP errors, e.g. "502 Proxy Error"
Paul Sokolovskyc82f9332023-01-10 23:50:25 +0300313 # or socket timeout.
Paul Sokolovsky81ff0ad2022-12-29 21:47:01 +0300314 # Just continue with the next job, the faulted one will be
315 # re-checked on next iteration.
Paul Sokolovskyc82f9332023-01-10 23:50:25 +0300316 _log.warning("block_wait_for_jobs: %r occurred, ignore and continue", e)
Paul Sokolovsky81ff0ad2022-12-29 21:47:01 +0300317 time.sleep(2)
318 continue
Matthew Hartfb6fd362020-03-04 21:03:59 +0000319 # If in queue or running wait
320 if cur_status['state'] in ["Canceling","Finished"]:
321 cur_status['error_reason'] = self.get_error_reason(job_id)
322 finished_jobs[job_id] = cur_status
Paul Sokolovskyb06bf6f2022-12-27 13:46:24 +0300323 _log.info(
Paul Sokolovsky6e83a232024-03-11 15:30:04 +0700324 "Job %s finished in %ds with state: %s, health: %s. Remaining: %d",
Paul Sokolovskyb7a41a92022-12-28 18:06:45 +0300325 job_id, time.time() - start_t,
326 cur_status['state'],
327 cur_status['health'],
Paul Sokolovskyb06bf6f2022-12-27 13:46:24 +0300328 len(job_ids) - len(finished_jobs)
329 )
Matthew Hartfb6fd362020-03-04 21:03:59 +0000330 if len(job_ids) == len(finished_jobs):
331 break
332 else:
333 time.sleep(poll_freq)
334 if len(job_ids) == len(finished_jobs):
335 break
336 return finished_jobs
337
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100338 def test_credentials(self):
339 """ Attempt to querry the back-end and verify that the user provided
340 authentication is valid """
341
342 try:
343 self._rpc_cmd_raw("system.listMethods")
344 return True
345 except Exception as e:
346 print(e)
347 print("Credential validation failed")
348 return False
349
350
351if __name__ == "__main__":
352 pass