blob: fcc3ff67d222df976594f145b9f5cee9b9602d5f [file] [log] [blame]
Matthew Hartfb6fd362020-03-04 21:03:59 +00001#!/usr/bin/env python3
2
3from __future__ import print_function
4
5__copyright__ = """
6/*
7 * Copyright (c) 2020, Arm Limited. All rights reserved.
8 *
9 * SPDX-License-Identifier: BSD-3-Clause
10 *
11 */
12 """
13
14"""
15Script for create LAVA definitions from a single tf-m-build-config
16jenkins Job.
17"""
18
19import os
20import sys
21import shutil
22import argparse
23from copy import deepcopy
24from collections import OrderedDict
25from jinja2 import Environment, FileSystemLoader
26from lava_helper_configs import *
27
28try:
29 from tfm_ci_pylib.lava_rpc_connector import LAVA_RPC_connector
30except ImportError:
31 dir_path = os.path.dirname(os.path.realpath(__file__))
32 sys.path.append(os.path.join(dir_path, "../"))
33 from tfm_ci_pylib.lava_rpc_connector import LAVA_RPC_connector
34
35
36def load_config_overrides(user_args, config_key):
37 """ Load a configuration from multiple locations and override it with
38 user provided arguements """
39
40 print("Using built-in config: %s" % config_key)
41 try:
42 config = lava_gen_config_map[config_key]
43 except KeyError:
44 print("No template found for config: %s" % config_key)
45 sys.exit(1)
46
47 config["build_no"] = user_args.build_no
Dean Birch5d2dc572020-05-29 13:15:59 +010048 config["artifact_store_url"] = user_args.jenkins_build_url
Matthew Hartfb6fd362020-03-04 21:03:59 +000049
50 # Add the template folder
51 config["templ"] = os.path.join(user_args.template_dir, config["templ"])
52 return config
53
54
55def get_artifact_url(artifact_store_url, params, filename):
56 platform = params['platform']
57 if params['device_type'] == 'fvp':
58 platform = 'fvp'
59 return "{}/artifact/trusted-firmware-m/build/install/outputs/{}/{}".format(
60 artifact_store_url.rstrip('/'), platform, filename,
61 )
62
63
64def get_recovery_url(recovery_store_url, recovery):
Dean Birch5d2dc572020-05-29 13:15:59 +010065 return "{}/{}".format(recovery_store_url.rstrip('/'), recovery)
Matthew Hartfb6fd362020-03-04 21:03:59 +000066
67
68def get_job_name(name, params, job):
69 return "{}_{}_{}_{}_{}_{}_{}_{}".format(
70 name,
71 job,
72 params["platform"],
73 params["build_no"],
74 params["compiler"],
75 params["build_type"],
76 params["boot_type"],
77 params["name"],
78 )
79
80
81def get_build_name(params):
82 return "{}_{}_{}_{}_{}".format(
83 params["platform"],
84 params["compiler"],
85 params["name"],
86 params["build_type"],
87 params["boot_type"],
88 )
89
90
91def generate_test_definitions(config, work_dir, user_args):
92 """ Get a dictionary configuration, and an existing jinja2 template
93 and generate a LAVA compatbile yaml definition """
94
95 template_loader = FileSystemLoader(searchpath=work_dir)
96 template_env = Environment(loader=template_loader)
Dean Birch5d2dc572020-05-29 13:15:59 +010097 recovery_store_url = config.get('recovery_store_url', '')
Matthew Hartfb6fd362020-03-04 21:03:59 +000098 build_no = user_args.build_no
Dean Birch5d2dc572020-05-29 13:15:59 +010099 artifact_store_url = config["artifact_store_url"]
Matthew Hartfb6fd362020-03-04 21:03:59 +0000100 template_file = config.pop("templ")
101
102 definitions = {}
103
104 for platform, recovery in config["platforms"].items():
105 if platform != user_args.platform:
106 continue
107 recovery_image_url = get_recovery_url(recovery_store_url, recovery)
108 for compiler in config["compilers"]:
109 if compiler != user_args.compiler:
110 continue
111 for build_type in config["build_types"]:
112 if build_type != user_args.build_type:
113 continue
114 for boot_type in config["boot_types"]:
115 bl2_string = "BL2" if user_args.bl2 else "NOBL2"
116 if boot_type != bl2_string:
117 continue
118 for test_name, test_dict in config["tests"].items():
119 if "Config{}".format(test_name) != user_args.proj_config:
120 continue
121 params = {
122 "device_type": config["device_type"],
123 "job_timeout": config["job_timeout"],
124 "action_timeout": config["action_timeout"],
125 "monitor_timeout": config["monitor_timeout"],
126 "poweroff_timeout": config["poweroff_timeout"],
127 "compiler": compiler,
128 "build_type": build_type,
129 "build_no": build_no,
130 "boot_type": boot_type,
131 "name": test_name,
132 "test": test_dict,
133 "platform": platform,
134 "recovery_image_url": recovery_image_url,
135 "data_bin_offset": config.get('data_bin_offset', ''),
136 "docker_prefix": vars(user_args).get('docker_prefix', ''),
137 "license_variable": vars(user_args).get('license_variable', ''),
138 "build_job_url": artifact_store_url,
Matthew Hart2c2688f2020-05-26 13:09:20 +0100139 "cpu0_baseline": config.get("cpu0_baseline", 0),
140 "cpu0_initvtor_s": config.get("cpu0_initvtor_s", "0x10000000")
Matthew Hartfb6fd362020-03-04 21:03:59 +0000141 }
142 params.update(
143 {
144 "firmware_url": get_artifact_url(
145 artifact_store_url,
146 params,
147 test_dict["binaries"]["firmware"],
148 ),
149 "bootloader_url": get_artifact_url(
150 artifact_store_url,
151 params,
152 test_dict["binaries"]["bootloader"],
153 ),
154 }
155 )
156 params.update(
157 {
158 "job_name": get_job_name(
159 config["job_name"], params, user_args.jenkins_job,
160 ),
161 "build_name": get_build_name(params)
162 }
163 )
164
165 definition = template_env.get_template(template_file).render(
166 params
167 )
168 definitions.update({params["job_name"]: definition})
169 return definitions
170
171
172def generate_lava_job_defs(user_args, config):
173 """ Create a LAVA test job definition file """
174
175 # Evaluate current directory
176 work_dir = os.path.abspath(os.path.dirname(__file__))
177
178 # If a single platform is requested and it exists in the platform
179 if user_args.platform and user_args.platform in config["platforms"]:
180 # Only test this platform
181 platform = user_args.platform
182 config["platforms"] = {platform: config["platforms"][platform]}
Matthew Hartfb6fd362020-03-04 21:03:59 +0000183 # Generate the ouptut definition
184 definitions = generate_test_definitions(config, work_dir, user_args)
185
186 # Write it into a file
187 out_dir = os.path.abspath(user_args.lava_def_output)
188 os.makedirs(out_dir, exist_ok=True)
189 for name, definition in definitions.items():
190 out_file = os.path.join(out_dir, "{}{}".format(name, ".yaml"))
191 with open(out_file, "w") as F:
192 F.write(definition)
193 print("Definition created at %s" % out_file)
194
195
196def main(user_args):
197 user_args.template_dir = "jinja2_templates"
198 config_keys = lava_gen_config_map.keys()
199 if user_args.config_key:
200 config_keys = [user_args.config_key]
201 for config_key in config_keys:
202 config = load_config_overrides(user_args, config_key)
203 generate_lava_job_defs(user_args, config)
204
205
206def get_cmd_args():
207 """ Parse command line arguments """
208
209 # Parse command line arguments to override config
210 parser = argparse.ArgumentParser(description="Lava Create Jobs")
211 cmdargs = parser.add_argument_group("Create LAVA Jobs")
212
213 # Configuration control
214 cmdargs.add_argument(
215 "--config-name",
216 dest="config_key",
217 action="store",
218 help="Select built-in configuration by name",
219 )
220 cmdargs.add_argument(
221 "--build-number",
222 dest="build_no",
223 action="store",
224 default="lastSuccessfulBuild",
225 help="JENKINS Build number selector. " "Default: lastSuccessfulBuild",
226 )
227 cmdargs.add_argument(
228 "--output-dir",
229 dest="lava_def_output",
230 action="store",
231 default="job_results",
232 help="Set LAVA compatible .yaml output file",
233 )
234 cmdargs.add_argument(
235 "--platform",
236 dest="platform",
237 action="store",
238 help="Override platform.Only the provided one " "will be tested",
239 )
240 cmdargs.add_argument(
241 "--compiler",
242 dest="compiler",
243 action="store",
244 help="Compiler to build definitions for",
245 )
246 cmdargs.add_argument(
247 "--jenkins-build-url",
248 dest="jenkins_build_url",
249 action="store",
250 help="Set the Jenkins URL",
251 )
252 cmdargs.add_argument(
253 "--jenkins-job",
254 dest="jenkins_job",
255 action="store",
256 default="tf-m-build-config",
257 help="Set the jenkins job name",
258 )
259 cmdargs.add_argument(
260 "--proj-config", dest="proj_config", action="store", help="Proj config"
261 )
262 cmdargs.add_argument(
263 "--build-type", dest="build_type", action="store", help="Build type"
264 )
265 cmdargs.add_argument(
266 "--docker-prefix", dest="docker_prefix", action="store", help="Prefix string for the FVP docker registry location"
267 )
268 cmdargs.add_argument(
269 "--license-variable", dest="license_variable", action="store", help="License string for Fastmodels"
270 )
271 cmdargs.add_argument("--bl2", dest="bl2", action="store_true", help="BL2")
Matthew Hart2c2688f2020-05-26 13:09:20 +0100272 cmdargs.add_argument(
273 "--psa-api-suite", dest="psa_suite", action="store", help="PSA API Suite name"
274 )
Matthew Hartfb6fd362020-03-04 21:03:59 +0000275 return parser.parse_args()
276
277
278if __name__ == "__main__":
279 main(get_cmd_args())