blob: e255e86e2e90eb19bc3844da9bf814c2170a1853 [file] [log] [blame]
Evan Hernandezf388cbf2019-04-01 11:15:23 -06001# Copyright 2019 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Implements ArtifactService."""
6
Chris McDonald1672ddb2021-07-21 11:48:23 -06007import logging
Evan Hernandezf388cbf2019-04-01 11:15:23 -06008import os
Varun Somani04dccd72021-10-09 01:06:11 +00009from typing import Any, NamedTuple, Optional, TYPE_CHECKING
Evan Hernandezf388cbf2019-04-01 11:15:23 -060010
Alex Klein231d2da2019-07-22 16:44:45 -060011from chromite.api import controller
Alex Klein076841b2019-08-29 15:19:39 -060012from chromite.api import faux
Alex Klein2b236722019-06-19 15:44:26 -060013from chromite.api import validate
Alex Klein238d8862019-05-07 11:32:46 -060014from chromite.api.controller import controller_util
George Engelbrechtc9a8e812021-06-16 18:14:17 -060015from chromite.api.controller import image as image_controller
16from chromite.api.controller import sysroot as sysroot_controller
David Wellingc1433c22021-06-25 16:29:48 +000017from chromite.api.controller import test as test_controller
LaMont Jones58362a42021-02-04 17:40:08 -070018from chromite.api.gen.chromite.api import artifacts_pb2
Jaques Clapauchf616bcd2021-04-09 20:14:40 +000019from chromite.api.gen.chromiumos import common_pb2
Evan Hernandezf388cbf2019-04-01 11:15:23 -060020from chromite.lib import constants
21from chromite.lib import cros_build_lib
Alex Klein2275d692019-04-23 16:04:12 -060022from chromite.lib import sysroot_lib
23from chromite.service import artifacts
Greg Edelstondc941072021-08-11 12:32:30 -060024from chromite.service import test
Evan Hernandezf388cbf2019-04-01 11:15:23 -060025
Mike Frysinger1cc8f1f2022-04-28 22:40:40 -040026
Varun Somani04dccd72021-10-09 01:06:11 +000027if TYPE_CHECKING:
28 from chromite.api import api_config
Evan Hernandezf388cbf2019-04-01 11:15:23 -060029
George Engelbrechtc9a8e812021-06-16 18:14:17 -060030class RegisteredGet(NamedTuple):
31 """An registered function for calling Get on an artifact type."""
32 output_proto: artifacts_pb2.GetResponse
33 artifact_dict: Any
LaMont Jones0f5171b2021-01-29 12:28:56 -070034
35
George Engelbrechtc9a8e812021-06-16 18:14:17 -060036def ExampleGetResponse(_input_proto, _output_proto, _config):
37 """Give an example GetResponse with a minimal coverage set."""
38 _output_proto = artifacts_pb2.GetResponse(
39 artifacts=common_pb2.UploadedArtifactsByService(
40 image=image_controller.ExampleGetResponse(),
41 sysroot=sysroot_controller.ExampleGetResponse(),
42 ))
43 return controller.RETURN_CODE_SUCCESS
44
45
LaMont Jones0f5171b2021-01-29 12:28:56 -070046@faux.empty_error
George Engelbrechtc9a8e812021-06-16 18:14:17 -060047@faux.success(ExampleGetResponse)
LaMont Jones0f5171b2021-01-29 12:28:56 -070048@validate.exists('result_path.path.path')
49@validate.validation_complete
Varun Somani04dccd72021-10-09 01:06:11 +000050def Get(
51 input_proto: artifacts_pb2.GetRequest,
52 output_proto: artifacts_pb2.GetResponse,
53 _config: 'api_config.ApiConfig'):
LaMont Jones0f5171b2021-01-29 12:28:56 -070054 """Get all artifacts.
55
56 Get all artifacts for the build.
57
George Engelbrechtc9a8e812021-06-16 18:14:17 -060058 Note: As the individual artifact_type bundlers are added here, they *must*
59 stop uploading it via the individual bundler function.
LaMont Jones0f5171b2021-01-29 12:28:56 -070060
61 Args:
Varun Somani04dccd72021-10-09 01:06:11 +000062 input_proto: The input proto.
63 output_proto: The output proto.
64 _config: The API call config.
LaMont Jones0f5171b2021-01-29 12:28:56 -070065 """
Jaques Clapauchf616bcd2021-04-09 20:14:40 +000066 output_dir = input_proto.result_path.path.path
67
George Engelbrechtc9a8e812021-06-16 18:14:17 -060068 sysroot = controller_util.ParseSysroot(input_proto.sysroot)
LaMont Jones8b88e9d2021-06-28 16:37:34 -060069 # This endpoint does not currently support any artifacts that are built
70 # without a sysroot being present.
71 if not sysroot.path:
72 return controller.RETURN_CODE_SUCCESS
73
George Engelbrechtc9a8e812021-06-16 18:14:17 -060074 chroot = controller_util.ParseChroot(input_proto.chroot)
75 build_target = controller_util.ParseBuildTarget(
76 input_proto.sysroot.build_target)
Jaques Clapauchf616bcd2021-04-09 20:14:40 +000077
George Engelbrechtc9a8e812021-06-16 18:14:17 -060078 # A list of RegisteredGet tuples (input proto, output proto, get results).
79 get_res_list = [
80 RegisteredGet(
81 output_proto.artifacts.image,
82 image_controller.GetArtifacts(
83 input_proto.artifact_info.image, chroot, sysroot, build_target,
84 output_dir)),
85 RegisteredGet(
86 output_proto.artifacts.sysroot,
87 sysroot_controller.GetArtifacts(
88 input_proto.artifact_info.sysroot, chroot, sysroot, build_target,
David Wellingc1433c22021-06-25 16:29:48 +000089 output_dir)),
90 RegisteredGet(
91 output_proto.artifacts.test,
92 test_controller.GetArtifacts(
Jack Neusc9707c32021-07-23 21:48:54 +000093 input_proto.artifact_info.test, chroot, sysroot, build_target,
94 output_dir)),
George Engelbrechtc9a8e812021-06-16 18:14:17 -060095 ]
Jaques Clapauchf616bcd2021-04-09 20:14:40 +000096
George Engelbrechtc9a8e812021-06-16 18:14:17 -060097 for get_res in get_res_list:
98 for artifact_dict in get_res.artifact_dict:
99 get_res.output_proto.artifacts.add(
100 artifact_type=artifact_dict['type'],
101 paths=[
102 common_pb2.Path(
103 path=x, location=common_pb2.Path.Location.OUTSIDE)
104 for x in artifact_dict['paths']
105 ])
LaMont Jones0f5171b2021-01-29 12:28:56 -0700106 return controller.RETURN_CODE_SUCCESS
107
108
LaMont Jones58362a42021-02-04 17:40:08 -0700109def _BuildSetupResponse(_input_proto, output_proto, _config):
110 """Just return POINTLESS for now."""
111 # All of the artifact types we support claim that the build is POINTLESS.
112 output_proto.build_relevance = artifacts_pb2.BuildSetupResponse.POINTLESS
113
114
115@faux.success(_BuildSetupResponse)
116@faux.empty_error
117@validate.validation_complete
Varun Somani04dccd72021-10-09 01:06:11 +0000118def BuildSetup(
119 _input_proto: artifacts_pb2.GetRequest,
120 output_proto: artifacts_pb2.GetResponse,
121 _config: 'api_config.ApiConfig'):
122
LaMont Jones58362a42021-02-04 17:40:08 -0700123 """Setup anything needed for building artifacts
124
125 If any artifact types require steps prior to building the package, they go
126 here. For example, see ToolchainService/PrepareForBuild.
127
128 Note: crbug/1034529 introduces this method as a noop. As the individual
129 artifact_type bundlers are added here, they *must* stop uploading it via the
130 individual bundler function.
131
132 Args:
Varun Somani04dccd72021-10-09 01:06:11 +0000133 _input_proto: The input proto.
134 output_proto: The output proto.
135 _config: The API call config.
LaMont Jones58362a42021-02-04 17:40:08 -0700136 """
137 # If any artifact_type says "NEEDED", the return is NEEDED.
138 # Otherwise, if any artifact_type says "UNKNOWN", the return is UNKNOWN.
139 # Otherwise, the return is POINTLESS.
140 output_proto.build_relevance = artifacts_pb2.BuildSetupResponse.POINTLESS
141 return controller.RETURN_CODE_SUCCESS
142
143
Varun Somani04dccd72021-10-09 01:06:11 +0000144def _GetImageDir(build_root: str, target: str) -> Optional[str]:
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600145 """Return path containing images for the given build target.
146
Alex Kleine2612a02019-04-18 13:51:06 -0600147 TODO(saklein) Expand image_lib.GetLatestImageLink to support this use case.
148
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600149 Args:
Varun Somani04dccd72021-10-09 01:06:11 +0000150 build_root: Path to checkout where build occurs.
151 target: Name of the build target.
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600152
153 Returns:
Alex Kleind2bf1462019-10-24 16:37:04 -0600154 Path to the latest directory containing target images or None.
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600155 """
156 image_dir = os.path.join(build_root, 'src/build/images', target, 'latest')
157 if not os.path.exists(image_dir):
Alex Kleind2bf1462019-10-24 16:37:04 -0600158 logging.warning('Expected to find image output for target %s at %s, but '
159 'path does not exist', target, image_dir)
160 return None
161
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600162 return image_dir
163
164
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700165def _BundleImageArchivesResponse(input_proto, output_proto, _config):
166 """Add artifact paths to a successful response."""
167 output_proto.artifacts.add().path = os.path.join(input_proto.output_dir,
168 'path0.tar.xz')
169 output_proto.artifacts.add().path = os.path.join(input_proto.output_dir,
170 'path1.tar.xz')
171
172
173@faux.success(_BundleImageArchivesResponse)
174@faux.empty_error
Alex Kleind91e95a2019-09-17 10:39:02 -0600175@validate.require('build_target.name')
176@validate.exists('output_dir')
177@validate.validation_complete
178def BundleImageArchives(input_proto, output_proto, _config):
179 """Create a .tar.xz archive for each image that has been created."""
180 build_target = controller_util.ParseBuildTarget(input_proto.build_target)
181 output_dir = input_proto.output_dir
182 image_dir = _GetImageDir(constants.SOURCE_ROOT, build_target.name)
Alex Kleind2bf1462019-10-24 16:37:04 -0600183 if image_dir is None:
184 return
Alex Kleind91e95a2019-09-17 10:39:02 -0600185
186 archives = artifacts.ArchiveImages(image_dir, output_dir)
187
188 for archive in archives:
189 output_proto.artifacts.add().path = os.path.join(output_dir, archive)
190
191
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700192def _BundleImageZipResponse(input_proto, output_proto, _config):
193 """Add artifact zip files to a successful response."""
194 output_proto.artifacts.add().path = os.path.join(input_proto.output_dir,
195 'image.zip')
196
197
198@faux.success(_BundleImageZipResponse)
199@faux.empty_error
Michael Mortensen01910922019-07-24 14:48:10 -0600200@validate.require('build_target.name', 'output_dir')
Alex Klein231d2da2019-07-22 16:44:45 -0600201@validate.exists('output_dir')
202@validate.validation_complete
Varun Somani04dccd72021-10-09 01:06:11 +0000203def BundleImageZip(
204 input_proto: artifacts_pb2.BundleRequest,
205 output_proto: artifacts_pb2.BundleResponse,
206 _config: 'api_config.ApiConfig'):
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600207 """Bundle image.zip.
208
209 Args:
Varun Somani04dccd72021-10-09 01:06:11 +0000210 input_proto: The input proto.
211 output_proto: The output proto.
212 _config: The API call config.
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600213 """
214 target = input_proto.build_target.name
215 output_dir = input_proto.output_dir
216 image_dir = _GetImageDir(constants.SOURCE_ROOT, target)
Alex Kleind2bf1462019-10-24 16:37:04 -0600217 if image_dir is None:
218 return None
Alex Klein231d2da2019-07-22 16:44:45 -0600219
Michael Mortensen01910922019-07-24 14:48:10 -0600220 archive = artifacts.BundleImageZip(output_dir, image_dir)
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600221 output_proto.artifacts.add().path = os.path.join(output_dir, archive)
222
223
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700224def _BundleTestUpdatePayloadsResponse(input_proto, output_proto, _config):
225 """Add test payload files to a successful response."""
226 output_proto.artifacts.add().path = os.path.join(input_proto.output_dir,
227 'payload1.bin')
George Engelbrechtf0239d52022-04-06 13:09:33 -0600228 output_proto.artifacts.add().path = os.path.join(input_proto.output_dir,
229 'payload1.json')
230 output_proto.artifacts.add().path = os.path.join(input_proto.output_dir,
231 'payload1.log')
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700232
233
234@faux.success(_BundleTestUpdatePayloadsResponse)
235@faux.empty_error
Alex Klein231d2da2019-07-22 16:44:45 -0600236@validate.require('build_target.name', 'output_dir')
237@validate.exists('output_dir')
238@validate.validation_complete
Varun Somani04dccd72021-10-09 01:06:11 +0000239def BundleTestUpdatePayloads(
240 input_proto: artifacts_pb2.BundleRequest,
241 output_proto: artifacts_pb2.BundleResponse,
242 _config: 'api_config.ApiConfig'):
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600243 """Generate minimal update payloads for the build target for testing.
244
245 Args:
Varun Somani04dccd72021-10-09 01:06:11 +0000246 input_proto: The input proto.
247 output_proto: The output proto.
248 _config: The API call config.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600249 """
250 target = input_proto.build_target.name
251 output_dir = input_proto.output_dir
252 build_root = constants.SOURCE_ROOT
253
254 # Use the first available image to create the update payload.
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600255 img_dir = _GetImageDir(build_root, target)
Alex Kleind2bf1462019-10-24 16:37:04 -0600256 if img_dir is None:
257 return None
258
Alex Kleincb541e82019-06-26 15:06:11 -0600259 img_types = [constants.IMAGE_TYPE_TEST, constants.IMAGE_TYPE_DEV,
260 constants.IMAGE_TYPE_BASE]
261 img_names = [constants.IMAGE_TYPE_TO_NAME[t] for t in img_types]
Mike Frysinger66ce4132019-07-17 22:52:52 -0400262 img_paths = [os.path.join(img_dir, x) for x in img_names]
Mike Frysingera552be42018-08-17 14:39:32 -0400263 valid_images = [x for x in img_paths if os.path.exists(x)]
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600264
Alex Kleincb541e82019-06-26 15:06:11 -0600265 if not valid_images:
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600266 cros_build_lib.Die(
267 'Expected to find an image of type among %r for target "%s" '
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600268 'at path %s.', img_types, target, img_dir)
Alex Kleincb541e82019-06-26 15:06:11 -0600269 image = valid_images[0]
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600270
Alex Kleincb541e82019-06-26 15:06:11 -0600271 payloads = artifacts.BundleTestUpdatePayloads(image, output_dir)
272 for payload in payloads:
273 output_proto.artifacts.add().path = payload
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600274
275
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700276def _BundleAutotestFilesResponse(input_proto, output_proto, _config):
277 """Add test autotest files to a successful response."""
278 output_proto.artifacts.add().path = os.path.join(input_proto.output_dir,
279 'autotest-a.tar.gz')
280
281
282@faux.success(_BundleAutotestFilesResponse)
283@faux.empty_error
Alex Klein036833d2022-06-01 13:05:01 -0600284@validate.require('sysroot.path')
Alex Klein231d2da2019-07-22 16:44:45 -0600285@validate.exists('output_dir')
Alex Klein036833d2022-06-01 13:05:01 -0600286@validate.validation_complete
287def BundleAutotestFiles(input_proto: artifacts_pb2.BundleRequest,
288 output_proto: artifacts_pb2.BundleResponse,
289 _config: 'api_config.ApiConfig') -> None:
290 """Tar the autotest files for a build target."""
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600291 output_dir = input_proto.output_dir
Alex Kleine21a0952019-08-23 16:08:16 -0600292 chroot = controller_util.ParseChroot(input_proto.chroot)
Alex Klein036833d2022-06-01 13:05:01 -0600293 sysroot = controller_util.ParseSysroot(input_proto.sysroot)
Alex Klein231d2da2019-07-22 16:44:45 -0600294
Alex Kleine21a0952019-08-23 16:08:16 -0600295 if not sysroot.Exists(chroot=chroot):
Alex Klein036833d2022-06-01 13:05:01 -0600296 logging.warning('Sysroot does not exist: %s', sysroot.path)
297 return
Alex Klein238d8862019-05-07 11:32:46 -0600298
299 try:
300 # Note that this returns the full path to *multiple* tarballs.
Alex Kleine21a0952019-08-23 16:08:16 -0600301 archives = artifacts.BundleAutotestFiles(chroot, sysroot, output_dir)
Alex Klein238d8862019-05-07 11:32:46 -0600302 except artifacts.Error as e:
Alex Klein03890312020-06-30 09:59:50 -0600303 logging.warning(e)
304 return
Alex Klein238d8862019-05-07 11:32:46 -0600305
306 for archive in archives.values():
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600307 output_proto.artifacts.add().path = archive
308
309
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700310def _BundleTastFilesResponse(input_proto, output_proto, _config):
311 """Add test tast files to a successful response."""
312 output_proto.artifacts.add().path = os.path.join(input_proto.output_dir,
313 'tast_bundles.tar.gz')
314
315
316@faux.success(_BundleTastFilesResponse)
317@faux.empty_error
Alex Klein036833d2022-06-01 13:05:01 -0600318@validate.require('sysroot.path')
Alex Klein231d2da2019-07-22 16:44:45 -0600319@validate.exists('output_dir')
Alex Klein036833d2022-06-01 13:05:01 -0600320@validate.validation_complete
321def BundleTastFiles(input_proto: artifacts_pb2.BundleRequest,
322 output_proto: artifacts_pb2.BundleResponse,
323 _config: 'api_config.ApiConfig') -> None:
324 """Tar the tast files for a build target."""
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600325 output_dir = input_proto.output_dir
Alex Kleinb9d810b2019-07-01 12:38:02 -0600326 chroot = controller_util.ParseChroot(input_proto.chroot)
Alex Klein036833d2022-06-01 13:05:01 -0600327 sysroot = controller_util.ParseSysroot(input_proto.sysroot)
Alex Kleinb9d810b2019-07-01 12:38:02 -0600328
Alex Klein231d2da2019-07-22 16:44:45 -0600329 if not sysroot.Exists(chroot=chroot):
Alex Klein036833d2022-06-01 13:05:01 -0600330 logging.warning('Sysroot does not exist: %s', sysroot.path)
331 return
Alex Kleinb9d810b2019-07-01 12:38:02 -0600332
333 archive = artifacts.BundleTastFiles(chroot, sysroot, output_dir)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600334
Alex Klein036833d2022-06-01 13:05:01 -0600335 if not archive:
336 logging.warning('Found no tast files for %s.', sysroot.path)
337 return
338
339 output_proto.artifacts.add().path = archive
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600340
341
Fergus Dall34d74e12020-09-29 15:52:32 +1000342def BundlePinnedGuestImages(_input_proto, _output_proto, _config):
343 # TODO(crbug/1034529): Remove this endpoint
344 pass
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700345
Fergus Dall34d74e12020-09-29 15:52:32 +1000346def FetchPinnedGuestImageUris(_input_proto, _output_proto, _config):
347 # TODO(crbug/1034529): Remove this endpoint
348 pass
Alex Klein7bf0ecb2019-06-25 10:04:15 -0600349
350
Greg Edelstondc941072021-08-11 12:32:30 -0600351def _FetchMetadataResponse(_input_proto, output_proto, _config):
352 """Populate the output_proto with sample data."""
353 for fp in ('/metadata/foo.txt', '/metadata/bar.jsonproto'):
354 output_proto.filepaths.add(path=common_pb2.Path(
355 path=fp, location=common_pb2.Path.OUTSIDE))
356 return controller.RETURN_CODE_SUCCESS
357
358
359@faux.success(_FetchMetadataResponse)
360@faux.empty_error
361@validate.exists('chroot.path')
362@validate.require('sysroot.path')
363@validate.validation_complete
Varun Somani04dccd72021-10-09 01:06:11 +0000364def FetchMetadata(
365 input_proto: artifacts_pb2.FetchMetadataRequest,
366 output_proto: artifacts_pb2.FetchMetadataResponse,
367 _config: 'api_config.ApiConfig'):
Greg Edelstondc941072021-08-11 12:32:30 -0600368 """FetchMetadata returns the paths to all build/test metadata files.
369
370 This implements ArtifactsService.FetchMetadata.
371
372 Args:
Varun Somani04dccd72021-10-09 01:06:11 +0000373 input_proto: The input proto.
374 output_proto: The output proto.
375 config: The API call config.
Greg Edelstondc941072021-08-11 12:32:30 -0600376 """
377 chroot = controller_util.ParseChroot(input_proto.chroot)
378 sysroot = controller_util.ParseSysroot(input_proto.sysroot)
379 for path in test.FindAllMetadataFiles(chroot, sysroot):
380 output_proto.filepaths.add(
381 path=common_pb2.Path(path=path, location=common_pb2.Path.OUTSIDE))
382 return controller.RETURN_CODE_SUCCESS
383
384
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700385def _BundleFirmwareResponse(input_proto, output_proto, _config):
386 """Add test firmware image files to a successful response."""
387 output_proto.artifacts.add().path = os.path.join(
388 input_proto.output_dir, 'firmware.tar.gz')
389
390
391@faux.success(_BundleFirmwareResponse)
392@faux.empty_error
Alex Klein2d8333c2022-06-01 13:29:01 -0600393@validate.require('sysroot.path')
Alex Klein231d2da2019-07-22 16:44:45 -0600394@validate.exists('output_dir')
395@validate.validation_complete
Alex Klein2d8333c2022-06-01 13:29:01 -0600396def BundleFirmware(input_proto: artifacts_pb2.BundleRequest,
397 output_proto: artifacts_pb2.BundleResponse,
398 _config: 'api_config.ApiConfig') -> None:
399 """Tar the firmware images for a build target."""
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600400 output_dir = input_proto.output_dir
Michael Mortensen38675192019-06-28 16:52:55 +0000401 chroot = controller_util.ParseChroot(input_proto.chroot)
Alex Klein2d8333c2022-06-01 13:29:01 -0600402 sysroot = controller_util.ParseSysroot(input_proto.sysroot)
Alex Klein231d2da2019-07-22 16:44:45 -0600403
404 if not chroot.exists():
Alex Klein2d8333c2022-06-01 13:29:01 -0600405 logging.warning('Chroot does not exist: %s', chroot.path)
406 return
Alex Klein231d2da2019-07-22 16:44:45 -0600407 elif not sysroot.Exists(chroot=chroot):
Alex Klein2d8333c2022-06-01 13:29:01 -0600408 logging.warning('Sysroot does not exist: %s', sysroot.path)
409 return
Alex Klein231d2da2019-07-22 16:44:45 -0600410
Michael Mortensen38675192019-06-28 16:52:55 +0000411 archive = artifacts.BuildFirmwareArchive(chroot, sysroot, output_dir)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600412
Alex Klein2d8333c2022-06-01 13:29:01 -0600413 if not archive:
George Engelbrecht9e41e172021-11-18 17:04:22 -0700414 logging.warning(
Michael Mortensen38675192019-06-28 16:52:55 +0000415 'Could not create firmware archive. No firmware found for %s.',
Alex Klein2d8333c2022-06-01 13:29:01 -0600416 sysroot.path)
George Engelbrecht9e41e172021-11-18 17:04:22 -0700417 return
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600418
Alex Klein231d2da2019-07-22 16:44:45 -0600419 output_proto.artifacts.add().path = archive
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600420
421
Yicheng Liea1181f2020-09-22 11:51:10 -0700422def _BundleFpmcuUnittestsResponse(input_proto, output_proto, _config):
423 """Add fingerprint MCU unittest binaries to a successful response."""
424 output_proto.artifacts.add().path = os.path.join(
425 input_proto.output_dir, 'fpmcu_unittests.tar.gz')
426
427
428@faux.success(_BundleFpmcuUnittestsResponse)
429@faux.empty_error
Alex Klein2d8333c2022-06-01 13:29:01 -0600430@validate.require('sysroot.path')
Yicheng Liea1181f2020-09-22 11:51:10 -0700431@validate.exists('output_dir')
432@validate.validation_complete
Alex Klein2d8333c2022-06-01 13:29:01 -0600433def BundleFpmcuUnittests(input_proto: artifacts_pb2.BundleRequest,
434 output_proto: artifacts_pb2.BundleResponse,
435 _config: 'api_config.ApiConfig') -> None:
Yicheng Liea1181f2020-09-22 11:51:10 -0700436 """Tar the fingerprint MCU unittest binaries for a build target.
437
438 Args:
Varun Somani04dccd72021-10-09 01:06:11 +0000439 input_proto: The input proto.
440 output_proto: The output proto.
441 _config: The API call config.
Yicheng Liea1181f2020-09-22 11:51:10 -0700442 """
443 output_dir = input_proto.output_dir
444 chroot = controller_util.ParseChroot(input_proto.chroot)
Alex Klein2d8333c2022-06-01 13:29:01 -0600445 sysroot = controller_util.ParseSysroot(input_proto.sysroot)
Yicheng Liea1181f2020-09-22 11:51:10 -0700446
447 if not chroot.exists():
Alex Klein2d8333c2022-06-01 13:29:01 -0600448 logging.warning('Chroot does not exist: %s', chroot.path)
449 return
Yicheng Liea1181f2020-09-22 11:51:10 -0700450 elif not sysroot.Exists(chroot=chroot):
Alex Klein2d8333c2022-06-01 13:29:01 -0600451 logging.warning('Sysroot does not exist: %s', sysroot.path)
452 return
Yicheng Liea1181f2020-09-22 11:51:10 -0700453
454 archive = artifacts.BundleFpmcuUnittests(chroot, sysroot, output_dir)
455
Alex Klein2d8333c2022-06-01 13:29:01 -0600456 if not archive:
457 logging.warning('No fpmcu unittests found for %s.', sysroot.path)
Yicheng Liea1181f2020-09-22 11:51:10 -0700458 return
459
460 output_proto.artifacts.add().path = archive
461
462
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700463def _BundleEbuildLogsResponse(input_proto, output_proto, _config):
464 """Add test log files to a successful response."""
465 output_proto.artifacts.add().path = os.path.join(
466 input_proto.output_dir, 'ebuild-logs.tar.gz')
467
468
469@faux.success(_BundleEbuildLogsResponse)
470@faux.empty_error
Alex Klein036833d2022-06-01 13:05:01 -0600471@validate.require('sysroot.path')
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600472@validate.exists('output_dir')
Alex Klein036833d2022-06-01 13:05:01 -0600473@validate.validation_complete
474def BundleEbuildLogs(input_proto: artifacts_pb2.BundleRequest,
475 output_proto: artifacts_pb2.BundleResponse,
476 _config: 'api_config.ApiConfig') -> None:
477 """Tar the ebuild logs for a build target."""
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600478 output_dir = input_proto.output_dir
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600479 chroot = controller_util.ParseChroot(input_proto.chroot)
Alex Klein036833d2022-06-01 13:05:01 -0600480 sysroot = controller_util.ParseSysroot(input_proto.sysroot)
Evan Hernandeza478d802019-04-08 15:08:24 -0600481
Alex Klein036833d2022-06-01 13:05:01 -0600482 if not sysroot.Exists(chroot=chroot):
483 logging.warning('Sysroot does not exist: %s', sysroot.path)
484 return
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600485
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600486 archive = artifacts.BundleEBuildLogsTarball(chroot, sysroot, output_dir)
Alex Klein036833d2022-06-01 13:05:01 -0600487
488 if not archive:
489 logging.warning(
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600490 'Could not create ebuild logs archive. No logs found for %s.',
491 sysroot.path)
Alex Klein036833d2022-06-01 13:05:01 -0600492 return
493
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600494 output_proto.artifacts.add().path = os.path.join(output_dir, archive)
Alex Klein6504eca2019-04-18 15:37:56 -0600495
496
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700497def _BundleChromeOSConfigResponse(input_proto, output_proto, _config):
498 """Add test config files to a successful response."""
499 output_proto.artifacts.add().path = os.path.join(
500 input_proto.output_dir, 'config.yaml')
501
502
503@faux.success(_BundleChromeOSConfigResponse)
504@faux.empty_error
Alex Klein2d8333c2022-06-01 13:29:01 -0600505@validate.require('sysroot.path')
Andrew Lamb811aead2019-08-12 10:25:05 -0600506@validate.exists('output_dir')
507@validate.validation_complete
Varun Somani04dccd72021-10-09 01:06:11 +0000508def BundleChromeOSConfig(
509 input_proto: artifacts_pb2.BundleRequest,
510 output_proto: artifacts_pb2.BundleResponse,
Alex Klein2d8333c2022-06-01 13:29:01 -0600511 _config: 'api_config.ApiConfig') -> None:
512 """Output the ChromeOS Config payload for a build target."""
Andrew Lamb811aead2019-08-12 10:25:05 -0600513 output_dir = input_proto.output_dir
Andrew Lamb811aead2019-08-12 10:25:05 -0600514 chroot = controller_util.ParseChroot(input_proto.chroot)
Alex Klein2d8333c2022-06-01 13:29:01 -0600515 sysroot = controller_util.ParseSysroot(input_proto.sysroot)
Andrew Lamb811aead2019-08-12 10:25:05 -0600516
517 chromeos_config = artifacts.BundleChromeOSConfig(chroot, sysroot, output_dir)
Alex Klein2d8333c2022-06-01 13:29:01 -0600518
Alex Klein383a7a32021-12-07 16:01:19 -0700519 if not chromeos_config:
Alex Klein2d8333c2022-06-01 13:29:01 -0600520 logging.warning('Could not create ChromeOS Config for %s.', sysroot.path)
Alex Klein383a7a32021-12-07 16:01:19 -0700521 return
522
Andrew Lamb811aead2019-08-12 10:25:05 -0600523 output_proto.artifacts.add().path = os.path.join(output_dir, chromeos_config)
524
525
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700526def _BundleSimpleChromeArtifactsResponse(input_proto, output_proto, _config):
527 """Add test simple chrome files to a successful response."""
528 output_proto.artifacts.add().path = os.path.join(
529 input_proto.output_dir, 'simple_chrome.txt')
530
531
532@faux.success(_BundleSimpleChromeArtifactsResponse)
533@faux.empty_error
Alex Klein231d2da2019-07-22 16:44:45 -0600534@validate.require('output_dir', 'sysroot.build_target.name', 'sysroot.path')
535@validate.exists('output_dir')
536@validate.validation_complete
537def BundleSimpleChromeArtifacts(input_proto, output_proto, _config):
Alex Klein2275d692019-04-23 16:04:12 -0600538 """Create the simple chrome artifacts."""
Alex Klein2275d692019-04-23 16:04:12 -0600539 sysroot_path = input_proto.sysroot.path
Alex Klein2275d692019-04-23 16:04:12 -0600540 output_dir = input_proto.output_dir
541
Alex Klein2275d692019-04-23 16:04:12 -0600542 # Build out the argument instances.
Alex Klein26e472b2020-03-10 14:35:01 -0600543 build_target = controller_util.ParseBuildTarget(
544 input_proto.sysroot.build_target)
545 chroot = controller_util.ParseChroot(input_proto.chroot)
Alex Klein2275d692019-04-23 16:04:12 -0600546 # Sysroot.path needs to be the fully qualified path, including the chroot.
547 full_sysroot_path = os.path.join(chroot.path, sysroot_path.lstrip(os.sep))
548 sysroot = sysroot_lib.Sysroot(full_sysroot_path)
549
550 # Quick sanity check that the sysroot exists before we go on.
551 if not sysroot.Exists():
552 cros_build_lib.Die('The sysroot does not exist.')
553
554 try:
555 results = artifacts.BundleSimpleChromeArtifacts(chroot, sysroot,
556 build_target, output_dir)
557 except artifacts.Error as e:
558 cros_build_lib.Die('Error %s raised in BundleSimpleChromeArtifacts: %s',
559 type(e), e)
560
561 for file_name in results:
562 output_proto.artifacts.add().path = file_name
563
564
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700565def _BundleVmFilesResponse(input_proto, output_proto, _config):
566 """Add test vm files to a successful response."""
567 output_proto.artifacts.add().path = os.path.join(
568 input_proto.output_dir, 'f1.tar')
569
570
571@faux.success(_BundleVmFilesResponse)
572@faux.empty_error
Michael Mortensen51f06722019-07-18 09:55:50 -0600573@validate.require('chroot.path', 'test_results_dir', 'output_dir')
Alex Klein231d2da2019-07-22 16:44:45 -0600574@validate.exists('output_dir')
575@validate.validation_complete
Varun Somani04dccd72021-10-09 01:06:11 +0000576def BundleVmFiles(
577 input_proto: artifacts_pb2.BundleVmFilesRequest,
578 output_proto: artifacts_pb2.BundleResponse,
579 _config: 'api_config.ApiConfig'):
Alex Klein6504eca2019-04-18 15:37:56 -0600580 """Tar VM disk and memory files.
581
582 Args:
Varun Somani04dccd72021-10-09 01:06:11 +0000583 input_proto: The input proto.
584 output_proto: The output proto.
585 _config: The API call config.
Alex Klein6504eca2019-04-18 15:37:56 -0600586 """
Michael Mortensen51f06722019-07-18 09:55:50 -0600587 chroot = controller_util.ParseChroot(input_proto.chroot)
588 test_results_dir = input_proto.test_results_dir
Alex Klein6504eca2019-04-18 15:37:56 -0600589 output_dir = input_proto.output_dir
590
Michael Mortensen51f06722019-07-18 09:55:50 -0600591 archives = artifacts.BundleVmFiles(
592 chroot, test_results_dir, output_dir)
Alex Klein6504eca2019-04-18 15:37:56 -0600593 for archive in archives:
594 output_proto.artifacts.add().path = archive
Tiancong Wangc4805b72019-06-11 12:12:03 -0700595
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700596def _ExportCpeReportResponse(input_proto, output_proto, _config):
597 """Add test cpe results to a successful response."""
598 output_proto.artifacts.add().path = os.path.join(
599 input_proto.output_dir, 'cpe_report.txt')
600 output_proto.artifacts.add().path = os.path.join(
601 input_proto.output_dir, 'cpe_warnings.txt')
602
603
604@faux.success(_ExportCpeReportResponse)
605@faux.empty_error
Alex Klein036833d2022-06-01 13:05:01 -0600606@validate.require('sysroot.path')
Alex Klein0b1cbfc2019-08-14 10:09:58 -0600607@validate.exists('output_dir')
Alex Klein036833d2022-06-01 13:05:01 -0600608@validate.validation_complete
609def ExportCpeReport(input_proto: artifacts_pb2.BundleRequest,
610 output_proto: artifacts_pb2.BundleResponse,
611 _config: 'api_config.ApiConfig') -> None:
612 """Export a CPE report."""
Alex Klein0b1cbfc2019-08-14 10:09:58 -0600613 chroot = controller_util.ParseChroot(input_proto.chroot)
Alex Klein036833d2022-06-01 13:05:01 -0600614 sysroot = controller_util.ParseSysroot(input_proto.sysroot)
Alex Klein0b1cbfc2019-08-14 10:09:58 -0600615 output_dir = input_proto.output_dir
616
Alex Klein036833d2022-06-01 13:05:01 -0600617 if not sysroot.Exists(chroot=chroot):
618 logging.warning('Sysroot does not exist: %s', sysroot.path)
619 return
Alex Klein0b1cbfc2019-08-14 10:09:58 -0600620
621 cpe_result = artifacts.GenerateCpeReport(chroot, sysroot, output_dir)
622
623 output_proto.artifacts.add().path = cpe_result.report
624 output_proto.artifacts.add().path = cpe_result.warnings
Shao-Chuan Leea44dddc2020-10-30 17:16:55 +0900625
626
627def _BundleGceTarballResponse(input_proto, output_proto, _config):
628 """Add artifact tarball to a successful response."""
629 output_proto.artifacts.add().path = os.path.join(input_proto.output_dir,
630 constants.TEST_IMAGE_GCE_TAR)
631
632
633@faux.success(_BundleGceTarballResponse)
634@faux.empty_error
635@validate.require('build_target.name', 'output_dir')
636@validate.exists('output_dir')
637@validate.validation_complete
Varun Somani04dccd72021-10-09 01:06:11 +0000638def BundleGceTarball(
639 input_proto: artifacts_pb2.BundleRequest,
640 output_proto: artifacts_pb2.BundleResponse,
641 _config: 'api_config.ApiConfig'):
Shao-Chuan Leea44dddc2020-10-30 17:16:55 +0900642 """Bundle the test image into a tarball suitable for importing into GCE.
643
644 Args:
Varun Somani04dccd72021-10-09 01:06:11 +0000645 input_proto: The input proto.
646 output_proto: The output proto.
647 _config: The API call config.
Shao-Chuan Leea44dddc2020-10-30 17:16:55 +0900648 """
649 target = input_proto.build_target.name
650 output_dir = input_proto.output_dir
651 image_dir = _GetImageDir(constants.SOURCE_ROOT, target)
652 if image_dir is None:
653 return None
654
655 tarball = artifacts.BundleGceTarball(output_dir, image_dir)
656 output_proto.artifacts.add().path = tarball