blob: 37e602ea0af3f53df9b146e2d717bf56d5038ec5 [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
Matthew Hartfb6fd362020-03-04 21:03:59 +000026import yaml
Matthew Hart4a4f1202020-06-12 15:52:46 +010027import requests
28import shutil
Paul Sokolovsky0c5e8da2024-03-06 12:18:02 +070029import subprocess
Paul Sokolovskyb06bf6f2022-12-27 13:46:24 +030030import logging
31
32
33_log = logging.getLogger("lavaci")
34
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010035
36class 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 Wasilewski4c4190d2020-12-15 12:56:22 +000064 self.server_api = "%s/api/v0.2/" % self.server_url
Matthew Hart4a4f1202020-06-12 15:52:46 +010065 self.server_results_prefix = "%s/results/%%s" % self.server_url
Matthew Hartc6bbbf92020-08-19 14:12:07 +010066 self.token = token
67 self.username = username
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +010068 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 Hart4a4f1202020-06-12 15:52:46 +010082 def fetch_file(self, url, out_file):
Matthew Hartc6bbbf92020-08-19 14:12:07 +010083 auth_params = {
84 'user': self.username,
85 'token': self.token
86 }
Paul Sokolovsky903bc432022-12-29 17:15:04 +030087 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 Hart4a4f1202020-06-12 15:52:46 +010092
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 Galanakisf4ca6ac2017-12-11 02:39:21 +010096
Matthew Hartfb6fd362020-03-04 21:03:59 +000097 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 Sokolovskyf2f385d2022-01-11 00:36:31 +0300102 def_o = yaml.safe_load(job_def)
Xinyu Zhang82dab282022-10-09 16:33:19 +0800103 return def_o
Matthew Hartfb6fd362020-03-04 21:03:59 +0000104
Matthew Hart4a4f1202020-06-12 15:52:46 +0100105 def get_job_log(self, job_id, target_out_file):
Milosz Wasilewski4c4190d2020-12-15 12:56:22 +0000106 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 Boudrac10378c2021-01-21 18:25:19 +0100110 with requests.get(log_url, stream=True, headers=auth_headers) as r:
Paul Sokolovsky903bc432022-12-29 17:15:04 +0300111 r.raise_for_status()
Fathi Boudrac10378c2021-01-21 18:25:19 +0100112 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 Hartfb6fd362020-03-04 21:03:59 +0000126
Matthew Hart4a4f1202020-06-12 15:52:46 +0100127 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 Hartfb6fd362020-03-04 21:03:59 +0000130
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 Hart2c2688f2020-05-26 13:09:20 +0100139 try:
140 lava_res = self.results.get_testsuite_results_yaml(job_id, 'lava')
Paul Sokolovskyf2f385d2022-01-11 00:36:31 +0300141 results = yaml.safe_load(lava_res)
Matthew Hart2c2688f2020-05-26 13:09:20 +0100142 for test in results:
143 if test['name'] == 'job':
144 return(test.get('metadata', {}).get('error_type', ''))
145 except Exception:
146 return("Unknown")
Matthew Hartfb6fd362020-03-04 21:03:59 +0000147
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100148 def get_job_state(self, job_id):
149 return self.scheduler.job_state(job_id)["job_state"]
150
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100151 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 Hart110e1dc2020-05-27 17:18:55 +0100170 def device_type_from_def(self, job_data):
Paul Sokolovskyf2f385d2022-01-11 00:36:31 +0300171 def_yaml = yaml.safe_load(job_data)
Matthew Hart110e1dc2020-05-27 17:18:55 +0100172 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 Galanakisf4ca6ac2017-12-11 02:39:21 +0100183 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 Sokolovsky80b9b352024-03-05 16:38:41 +0700190 _log.error("Server rejected job's syntax")
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100191 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 Sokolovsky0c5e8da2024-03-06 12:18:02 +0700199
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 Bircha6ede7e2020-03-13 14:00:33 +0000216 try:
Dean Birch1d545c02020-05-29 14:09:21 +0100217 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 Bircha6ede7e2020-03-13 14:00:33 +0000223 except Exception as e:
Paul Sokolovskyb2ca65b2024-03-11 15:07:34 +0700224 _log.exception("Exception submitting job to LAVA", e)
Dean Bircha6ede7e2020-03-13 14:00:33 +0000225 return(None, None)
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100226
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 Arnoldf1169b92020-03-11 10:14:14 +0000246 cur_status = self.get_job_state(job_id)
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100247 # If in queue or running wait
Dean Arnoldc1d81b42020-03-11 15:56:36 +0000248 if cur_status not in ["Canceling","Finished"]:
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100249 time.sleep(poll_freq)
250 else:
251 break
Dean Arnoldc1d81b42020-03-11 15:56:36 +0000252 return self.scheduler.job_health(job_id)["job_health"]
Minos Galanakisf4ca6ac2017-12-11 02:39:21 +0100253
Matthew Hartfb6fd362020-03-04 21:03:59 +0000254 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 Sokolovskyfb298c62022-04-29 23:15:17 +0300265 if job_id in finished_jobs:
266 continue
Matthew Hartfb6fd362020-03-04 21:03:59 +0000267 # Check if the job is not running
Paul Sokolovsky81ff0ad2022-12-29 21:47:01 +0300268 try:
269 cur_status = self.get_job_info(job_id)
Paul Sokolovskyc82f9332023-01-10 23:50:25 +0300270 except (xmlrpc.client.ProtocolError, OSError) as e:
Paul Sokolovsky81ff0ad2022-12-29 21:47:01 +0300271 # There can be transient HTTP errors, e.g. "502 Proxy Error"
Paul Sokolovskyc82f9332023-01-10 23:50:25 +0300272 # or socket timeout.
Paul Sokolovsky81ff0ad2022-12-29 21:47:01 +0300273 # Just continue with the next job, the faulted one will be
274 # re-checked on next iteration.
Paul Sokolovskyc82f9332023-01-10 23:50:25 +0300275 _log.warning("block_wait_for_jobs: %r occurred, ignore and continue", e)
Paul Sokolovsky81ff0ad2022-12-29 21:47:01 +0300276 time.sleep(2)
277 continue
Matthew Hartfb6fd362020-03-04 21:03:59 +0000278 # 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 Sokolovskyb06bf6f2022-12-27 13:46:24 +0300282 _log.info(
Paul Sokolovsky6e83a232024-03-11 15:30:04 +0700283 "Job %s finished in %ds with state: %s, health: %s. Remaining: %d",
Paul Sokolovskyb7a41a92022-12-28 18:06:45 +0300284 job_id, time.time() - start_t,
285 cur_status['state'],
286 cur_status['health'],
Paul Sokolovskyb06bf6f2022-12-27 13:46:24 +0300287 len(job_ids) - len(finished_jobs)
288 )
Matthew Hartfb6fd362020-03-04 21:03:59 +0000289 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 Galanakisf4ca6ac2017-12-11 02:39:21 +0100297 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
310if __name__ == "__main__":
311 pass