blob: cffea030c8da28df7166842561d69077c9cf9bb1 [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
Alex Klein2275d692019-04-23 16:04:12 -060020from chromite.lib import chroot_lib
Evan Hernandezf388cbf2019-04-01 11:15:23 -060021from chromite.lib import constants
22from chromite.lib import cros_build_lib
Alex Klein2275d692019-04-23 16:04:12 -060023from chromite.lib import sysroot_lib
24from chromite.service import artifacts
Greg Edelstondc941072021-08-11 12:32:30 -060025from chromite.service import test
Evan Hernandezf388cbf2019-04-01 11:15:23 -060026
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')
228
229
230@faux.success(_BundleTestUpdatePayloadsResponse)
231@faux.empty_error
Alex Klein231d2da2019-07-22 16:44:45 -0600232@validate.require('build_target.name', 'output_dir')
233@validate.exists('output_dir')
234@validate.validation_complete
Varun Somani04dccd72021-10-09 01:06:11 +0000235def BundleTestUpdatePayloads(
236 input_proto: artifacts_pb2.BundleRequest,
237 output_proto: artifacts_pb2.BundleResponse,
238 _config: 'api_config.ApiConfig'):
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600239 """Generate minimal update payloads for the build target for testing.
240
241 Args:
Varun Somani04dccd72021-10-09 01:06:11 +0000242 input_proto: The input proto.
243 output_proto: The output proto.
244 _config: The API call config.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600245 """
246 target = input_proto.build_target.name
247 output_dir = input_proto.output_dir
248 build_root = constants.SOURCE_ROOT
249
250 # Use the first available image to create the update payload.
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600251 img_dir = _GetImageDir(build_root, target)
Alex Kleind2bf1462019-10-24 16:37:04 -0600252 if img_dir is None:
253 return None
254
Alex Kleincb541e82019-06-26 15:06:11 -0600255 img_types = [constants.IMAGE_TYPE_TEST, constants.IMAGE_TYPE_DEV,
256 constants.IMAGE_TYPE_BASE]
257 img_names = [constants.IMAGE_TYPE_TO_NAME[t] for t in img_types]
Mike Frysinger66ce4132019-07-17 22:52:52 -0400258 img_paths = [os.path.join(img_dir, x) for x in img_names]
Mike Frysingera552be42018-08-17 14:39:32 -0400259 valid_images = [x for x in img_paths if os.path.exists(x)]
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600260
Alex Kleincb541e82019-06-26 15:06:11 -0600261 if not valid_images:
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600262 cros_build_lib.Die(
263 'Expected to find an image of type among %r for target "%s" '
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600264 'at path %s.', img_types, target, img_dir)
Alex Kleincb541e82019-06-26 15:06:11 -0600265 image = valid_images[0]
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600266
Alex Kleincb541e82019-06-26 15:06:11 -0600267 payloads = artifacts.BundleTestUpdatePayloads(image, output_dir)
268 for payload in payloads:
269 output_proto.artifacts.add().path = payload
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600270
271
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700272def _BundleAutotestFilesResponse(input_proto, output_proto, _config):
273 """Add test autotest files to a successful response."""
274 output_proto.artifacts.add().path = os.path.join(input_proto.output_dir,
275 'autotest-a.tar.gz')
276
277
278@faux.success(_BundleAutotestFilesResponse)
279@faux.empty_error
Alex Klein231d2da2019-07-22 16:44:45 -0600280@validate.require('output_dir')
281@validate.exists('output_dir')
Varun Somani04dccd72021-10-09 01:06:11 +0000282def BundleAutotestFiles(
283 input_proto: artifacts_pb2.BundleRequest,
284 output_proto: artifacts_pb2.BundleResponse,
285 config: 'api_config.ApiConfig'):
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600286 """Tar the autotest files for a build target.
287
288 Args:
Varun Somani04dccd72021-10-09 01:06:11 +0000289 input_proto: The input proto.
290 output_proto: The output proto.
291 config: The API call config.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600292 """
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600293 output_dir = input_proto.output_dir
Alex Klein238d8862019-05-07 11:32:46 -0600294 target = input_proto.build_target.name
Alex Kleine21a0952019-08-23 16:08:16 -0600295 chroot = controller_util.ParseChroot(input_proto.chroot)
296
Alex Klein238d8862019-05-07 11:32:46 -0600297 if target:
Alex Kleine21a0952019-08-23 16:08:16 -0600298 sysroot_path = os.path.join('/build', target)
Alex Klein238d8862019-05-07 11:32:46 -0600299 else:
300 # New style call, use chroot and sysroot.
Alex Klein238d8862019-05-07 11:32:46 -0600301 sysroot_path = input_proto.sysroot.path
302 if not sysroot_path:
303 cros_build_lib.Die('sysroot.path is required.')
304
Alex Kleine21a0952019-08-23 16:08:16 -0600305 sysroot = sysroot_lib.Sysroot(sysroot_path)
Alex Klein238d8862019-05-07 11:32:46 -0600306
Alex Klein231d2da2019-07-22 16:44:45 -0600307 # TODO(saklein): Switch to the validate_only decorator when legacy handling
308 # is removed.
309 if config.validate_only:
310 return controller.RETURN_CODE_VALID_INPUT
311
Alex Kleine21a0952019-08-23 16:08:16 -0600312 if not sysroot.Exists(chroot=chroot):
Alex Klein238d8862019-05-07 11:32:46 -0600313 cros_build_lib.Die('Sysroot path must exist: %s', sysroot.path)
314
315 try:
316 # Note that this returns the full path to *multiple* tarballs.
Alex Kleine21a0952019-08-23 16:08:16 -0600317 archives = artifacts.BundleAutotestFiles(chroot, sysroot, output_dir)
Alex Klein238d8862019-05-07 11:32:46 -0600318 except artifacts.Error as e:
Alex Klein03890312020-06-30 09:59:50 -0600319 logging.warning(e)
320 return
Alex Klein238d8862019-05-07 11:32:46 -0600321
322 for archive in archives.values():
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600323 output_proto.artifacts.add().path = archive
324
325
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700326def _BundleTastFilesResponse(input_proto, output_proto, _config):
327 """Add test tast files to a successful response."""
328 output_proto.artifacts.add().path = os.path.join(input_proto.output_dir,
329 'tast_bundles.tar.gz')
330
331
332@faux.success(_BundleTastFilesResponse)
333@faux.empty_error
Alex Kleinb9d810b2019-07-01 12:38:02 -0600334@validate.require('output_dir')
Alex Klein231d2da2019-07-22 16:44:45 -0600335@validate.exists('output_dir')
Varun Somani04dccd72021-10-09 01:06:11 +0000336def BundleTastFiles(
337 input_proto: artifacts_pb2.BundleRequest,
338 output_proto: artifacts_pb2.BundleResponse,
339 config: 'api_config.ApiConfig'):
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600340 """Tar the tast files for a build target.
341
342 Args:
Varun Somani04dccd72021-10-09 01:06:11 +0000343 input_proto: The input proto.
344 output_proto: The output proto.
345 config: The API call config.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600346 """
347 target = input_proto.build_target.name
348 output_dir = input_proto.output_dir
349 build_root = constants.SOURCE_ROOT
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600350
Alex Kleinb9d810b2019-07-01 12:38:02 -0600351 chroot = controller_util.ParseChroot(input_proto.chroot)
352 sysroot_path = input_proto.sysroot.path
353
354 # TODO(saklein) Cleanup legacy handling after it has been switched over.
355 if target:
356 # Legacy handling.
Alex Klein171da612019-08-06 14:00:42 -0600357 chroot = chroot_lib.Chroot(path=os.path.join(build_root, 'chroot'))
Alex Kleinb9d810b2019-07-01 12:38:02 -0600358 sysroot_path = os.path.join('/build', target)
359
360 # New handling - chroot & sysroot based.
361 # TODO(saklein) Switch this to the require decorator when legacy is removed.
362 if not sysroot_path:
363 cros_build_lib.Die('sysroot.path is required.')
364
Alex Klein231d2da2019-07-22 16:44:45 -0600365 # TODO(saklein): Switch to the validation_complete decorator when legacy
366 # handling is removed.
367 if config.validate_only:
368 return controller.RETURN_CODE_VALID_INPUT
369
Alex Kleinb9d810b2019-07-01 12:38:02 -0600370 sysroot = sysroot_lib.Sysroot(sysroot_path)
Alex Klein231d2da2019-07-22 16:44:45 -0600371 if not sysroot.Exists(chroot=chroot):
Alex Kleinb9d810b2019-07-01 12:38:02 -0600372 cros_build_lib.Die('Sysroot must exist.')
373
374 archive = artifacts.BundleTastFiles(chroot, sysroot, output_dir)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600375
LaMont Jonesb9793cd2020-06-11 08:14:46 -0600376 if archive:
377 output_proto.artifacts.add().path = archive
378 else:
379 logging.warning('Found no tast files for %s.', target)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600380
381
Fergus Dall34d74e12020-09-29 15:52:32 +1000382def BundlePinnedGuestImages(_input_proto, _output_proto, _config):
383 # TODO(crbug/1034529): Remove this endpoint
384 pass
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700385
Fergus Dall34d74e12020-09-29 15:52:32 +1000386def FetchPinnedGuestImageUris(_input_proto, _output_proto, _config):
387 # TODO(crbug/1034529): Remove this endpoint
388 pass
Alex Klein7bf0ecb2019-06-25 10:04:15 -0600389
390
Greg Edelstondc941072021-08-11 12:32:30 -0600391def _FetchMetadataResponse(_input_proto, output_proto, _config):
392 """Populate the output_proto with sample data."""
393 for fp in ('/metadata/foo.txt', '/metadata/bar.jsonproto'):
394 output_proto.filepaths.add(path=common_pb2.Path(
395 path=fp, location=common_pb2.Path.OUTSIDE))
396 return controller.RETURN_CODE_SUCCESS
397
398
399@faux.success(_FetchMetadataResponse)
400@faux.empty_error
401@validate.exists('chroot.path')
402@validate.require('sysroot.path')
403@validate.validation_complete
Varun Somani04dccd72021-10-09 01:06:11 +0000404def FetchMetadata(
405 input_proto: artifacts_pb2.FetchMetadataRequest,
406 output_proto: artifacts_pb2.FetchMetadataResponse,
407 _config: 'api_config.ApiConfig'):
Greg Edelstondc941072021-08-11 12:32:30 -0600408 """FetchMetadata returns the paths to all build/test metadata files.
409
410 This implements ArtifactsService.FetchMetadata.
411
412 Args:
Varun Somani04dccd72021-10-09 01:06:11 +0000413 input_proto: The input proto.
414 output_proto: The output proto.
415 config: The API call config.
Greg Edelstondc941072021-08-11 12:32:30 -0600416 """
417 chroot = controller_util.ParseChroot(input_proto.chroot)
418 sysroot = controller_util.ParseSysroot(input_proto.sysroot)
419 for path in test.FindAllMetadataFiles(chroot, sysroot):
420 output_proto.filepaths.add(
421 path=common_pb2.Path(path=path, location=common_pb2.Path.OUTSIDE))
422 return controller.RETURN_CODE_SUCCESS
423
424
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700425def _BundleFirmwareResponse(input_proto, output_proto, _config):
426 """Add test firmware image files to a successful response."""
427 output_proto.artifacts.add().path = os.path.join(
428 input_proto.output_dir, 'firmware.tar.gz')
429
430
431@faux.success(_BundleFirmwareResponse)
432@faux.empty_error
Michael Mortensen38675192019-06-28 16:52:55 +0000433@validate.require('output_dir', 'sysroot.path')
Alex Klein231d2da2019-07-22 16:44:45 -0600434@validate.exists('output_dir')
435@validate.validation_complete
Varun Somani04dccd72021-10-09 01:06:11 +0000436def BundleFirmware(
437 input_proto: artifacts_pb2.BundleRequest,
438 output_proto: artifacts_pb2.BundleResponse,
439 _config: 'api_config.ApiConfig'):
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600440 """Tar the firmware images for a build target.
441
442 Args:
Varun Somani04dccd72021-10-09 01:06:11 +0000443 input_proto: The input proto.
444 output_proto: The output proto.
445 _config: The API call config.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600446 """
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600447 output_dir = input_proto.output_dir
Michael Mortensen38675192019-06-28 16:52:55 +0000448 chroot = controller_util.ParseChroot(input_proto.chroot)
449 sysroot_path = input_proto.sysroot.path
450 sysroot = sysroot_lib.Sysroot(sysroot_path)
Alex Klein231d2da2019-07-22 16:44:45 -0600451
452 if not chroot.exists():
453 cros_build_lib.Die('Chroot does not exist: %s', chroot.path)
454 elif not sysroot.Exists(chroot=chroot):
455 cros_build_lib.Die('Sysroot does not exist: %s',
456 chroot.full_path(sysroot.path))
457
Michael Mortensen38675192019-06-28 16:52:55 +0000458 archive = artifacts.BuildFirmwareArchive(chroot, sysroot, output_dir)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600459
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600460 if archive is None:
George Engelbrecht9e41e172021-11-18 17:04:22 -0700461 logging.warning(
Michael Mortensen38675192019-06-28 16:52:55 +0000462 'Could not create firmware archive. No firmware found for %s.',
463 sysroot_path)
George Engelbrecht9e41e172021-11-18 17:04:22 -0700464 return
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600465
Alex Klein231d2da2019-07-22 16:44:45 -0600466 output_proto.artifacts.add().path = archive
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600467
468
Yicheng Liea1181f2020-09-22 11:51:10 -0700469def _BundleFpmcuUnittestsResponse(input_proto, output_proto, _config):
470 """Add fingerprint MCU unittest binaries to a successful response."""
471 output_proto.artifacts.add().path = os.path.join(
472 input_proto.output_dir, 'fpmcu_unittests.tar.gz')
473
474
475@faux.success(_BundleFpmcuUnittestsResponse)
476@faux.empty_error
477@validate.require('output_dir', 'sysroot.path')
478@validate.exists('output_dir')
479@validate.validation_complete
Varun Somani04dccd72021-10-09 01:06:11 +0000480def BundleFpmcuUnittests(
481 input_proto: artifacts_pb2.BundleRequest,
482 output_proto: artifacts_pb2.BundleResponse,
483 _config: 'api_config.ApiConfig'):
Yicheng Liea1181f2020-09-22 11:51:10 -0700484 """Tar the fingerprint MCU unittest binaries for a build target.
485
486 Args:
Varun Somani04dccd72021-10-09 01:06:11 +0000487 input_proto: The input proto.
488 output_proto: The output proto.
489 _config: The API call config.
Yicheng Liea1181f2020-09-22 11:51:10 -0700490 """
491 output_dir = input_proto.output_dir
492 chroot = controller_util.ParseChroot(input_proto.chroot)
493 sysroot_path = input_proto.sysroot.path
494 sysroot = sysroot_lib.Sysroot(sysroot_path)
495
496 if not chroot.exists():
497 cros_build_lib.Die('Chroot does not exist: %s', chroot.path)
498 elif not sysroot.Exists(chroot=chroot):
499 cros_build_lib.Die('Sysroot does not exist: %s',
500 chroot.full_path(sysroot.path))
501
502 archive = artifacts.BundleFpmcuUnittests(chroot, sysroot, output_dir)
503
504 if archive is None:
505 logging.warning(
506 'No fpmcu unittests found for %s.', sysroot_path)
507 return
508
509 output_proto.artifacts.add().path = archive
510
511
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700512def _BundleEbuildLogsResponse(input_proto, output_proto, _config):
513 """Add test log files to a successful response."""
514 output_proto.artifacts.add().path = os.path.join(
515 input_proto.output_dir, 'ebuild-logs.tar.gz')
516
517
518@faux.success(_BundleEbuildLogsResponse)
519@faux.empty_error
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600520@validate.exists('output_dir')
Varun Somani04dccd72021-10-09 01:06:11 +0000521def BundleEbuildLogs(
522 input_proto: artifacts_pb2.BundleRequest,
523 output_proto: artifacts_pb2.BundleResponse,
524 config: 'api_config.ApiConfig'):
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600525 """Tar the ebuild logs for a build target.
526
527 Args:
Varun Somani04dccd72021-10-09 01:06:11 +0000528 input_proto: The input proto.
529 output_proto: The output proto.
530 config: The API call config.
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600531 """
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600532 output_dir = input_proto.output_dir
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600533 sysroot_path = input_proto.sysroot.path
534 chroot = controller_util.ParseChroot(input_proto.chroot)
Evan Hernandeza478d802019-04-08 15:08:24 -0600535
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600536 # TODO(mmortensen) Cleanup legacy handling after it has been switched over.
537 target = input_proto.build_target.name
538 if target:
539 # Legacy handling.
540 build_root = constants.SOURCE_ROOT
Alex Klein171da612019-08-06 14:00:42 -0600541 chroot = chroot_lib.Chroot(path=os.path.join(build_root, 'chroot'))
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600542 sysroot_path = os.path.join('/build', target)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600543
Alex Klein231d2da2019-07-22 16:44:45 -0600544 # TODO(saklein): Switch to validation_complete decorator after legacy
545 # handling has been cleaned up.
546 if config.validate_only:
547 return controller.RETURN_CODE_VALID_INPUT
548
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600549 sysroot = sysroot_lib.Sysroot(sysroot_path)
550 archive = artifacts.BundleEBuildLogsTarball(chroot, sysroot, output_dir)
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600551 if archive is None:
552 cros_build_lib.Die(
Michael Mortensen3f382cb2019-07-29 13:21:49 -0600553 'Could not create ebuild logs archive. No logs found for %s.',
554 sysroot.path)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600555 output_proto.artifacts.add().path = os.path.join(output_dir, archive)
Alex Klein6504eca2019-04-18 15:37:56 -0600556
557
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700558def _BundleChromeOSConfigResponse(input_proto, output_proto, _config):
559 """Add test config files to a successful response."""
560 output_proto.artifacts.add().path = os.path.join(
561 input_proto.output_dir, 'config.yaml')
562
563
564@faux.success(_BundleChromeOSConfigResponse)
565@faux.empty_error
Andrew Lamb811aead2019-08-12 10:25:05 -0600566@validate.exists('output_dir')
567@validate.validation_complete
Varun Somani04dccd72021-10-09 01:06:11 +0000568def BundleChromeOSConfig(
569 input_proto: artifacts_pb2.BundleRequest,
570 output_proto: artifacts_pb2.BundleResponse,
571 _config: 'api_config.ApiConfig'):
Andrew Lamb811aead2019-08-12 10:25:05 -0600572 """Output the ChromeOS Config payload for a build target.
573
574 Args:
Varun Somani04dccd72021-10-09 01:06:11 +0000575 input_proto: The input proto.
576 output_proto: The output proto.
577 _config: The API call config.
Andrew Lamb811aead2019-08-12 10:25:05 -0600578 """
579 output_dir = input_proto.output_dir
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600580 sysroot_path = input_proto.sysroot.path
Andrew Lamb811aead2019-08-12 10:25:05 -0600581 chroot = controller_util.ParseChroot(input_proto.chroot)
582
Andrew Lamb67bd68f2019-08-15 09:09:15 -0600583 # TODO(mmortensen) Cleanup legacy handling after it has been switched over.
584 target = input_proto.build_target.name
585 if target:
586 # Legacy handling.
587 build_root = constants.SOURCE_ROOT
588 chroot = chroot_lib.Chroot(path=os.path.join(build_root, 'chroot'))
589 sysroot_path = os.path.join('/build', target)
590
591 sysroot = sysroot_lib.Sysroot(sysroot_path)
Andrew Lamb811aead2019-08-12 10:25:05 -0600592 chromeos_config = artifacts.BundleChromeOSConfig(chroot, sysroot, output_dir)
Alex Klein383a7a32021-12-07 16:01:19 -0700593 if not chromeos_config:
594 return
595
Andrew Lamb811aead2019-08-12 10:25:05 -0600596 output_proto.artifacts.add().path = os.path.join(output_dir, chromeos_config)
597
598
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700599def _BundleSimpleChromeArtifactsResponse(input_proto, output_proto, _config):
600 """Add test simple chrome files to a successful response."""
601 output_proto.artifacts.add().path = os.path.join(
602 input_proto.output_dir, 'simple_chrome.txt')
603
604
605@faux.success(_BundleSimpleChromeArtifactsResponse)
606@faux.empty_error
Alex Klein231d2da2019-07-22 16:44:45 -0600607@validate.require('output_dir', 'sysroot.build_target.name', 'sysroot.path')
608@validate.exists('output_dir')
609@validate.validation_complete
610def BundleSimpleChromeArtifacts(input_proto, output_proto, _config):
Alex Klein2275d692019-04-23 16:04:12 -0600611 """Create the simple chrome artifacts."""
Alex Klein2275d692019-04-23 16:04:12 -0600612 sysroot_path = input_proto.sysroot.path
Alex Klein2275d692019-04-23 16:04:12 -0600613 output_dir = input_proto.output_dir
614
Alex Klein2275d692019-04-23 16:04:12 -0600615 # Build out the argument instances.
Alex Klein26e472b2020-03-10 14:35:01 -0600616 build_target = controller_util.ParseBuildTarget(
617 input_proto.sysroot.build_target)
618 chroot = controller_util.ParseChroot(input_proto.chroot)
Alex Klein2275d692019-04-23 16:04:12 -0600619 # Sysroot.path needs to be the fully qualified path, including the chroot.
620 full_sysroot_path = os.path.join(chroot.path, sysroot_path.lstrip(os.sep))
621 sysroot = sysroot_lib.Sysroot(full_sysroot_path)
622
623 # Quick sanity check that the sysroot exists before we go on.
624 if not sysroot.Exists():
625 cros_build_lib.Die('The sysroot does not exist.')
626
627 try:
628 results = artifacts.BundleSimpleChromeArtifacts(chroot, sysroot,
629 build_target, output_dir)
630 except artifacts.Error as e:
631 cros_build_lib.Die('Error %s raised in BundleSimpleChromeArtifacts: %s',
632 type(e), e)
633
634 for file_name in results:
635 output_proto.artifacts.add().path = file_name
636
637
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700638def _BundleVmFilesResponse(input_proto, output_proto, _config):
639 """Add test vm files to a successful response."""
640 output_proto.artifacts.add().path = os.path.join(
641 input_proto.output_dir, 'f1.tar')
642
643
644@faux.success(_BundleVmFilesResponse)
645@faux.empty_error
Michael Mortensen51f06722019-07-18 09:55:50 -0600646@validate.require('chroot.path', 'test_results_dir', 'output_dir')
Alex Klein231d2da2019-07-22 16:44:45 -0600647@validate.exists('output_dir')
648@validate.validation_complete
Varun Somani04dccd72021-10-09 01:06:11 +0000649def BundleVmFiles(
650 input_proto: artifacts_pb2.BundleVmFilesRequest,
651 output_proto: artifacts_pb2.BundleResponse,
652 _config: 'api_config.ApiConfig'):
Alex Klein6504eca2019-04-18 15:37:56 -0600653 """Tar VM disk and memory files.
654
655 Args:
Varun Somani04dccd72021-10-09 01:06:11 +0000656 input_proto: The input proto.
657 output_proto: The output proto.
658 _config: The API call config.
Alex Klein6504eca2019-04-18 15:37:56 -0600659 """
Michael Mortensen51f06722019-07-18 09:55:50 -0600660 chroot = controller_util.ParseChroot(input_proto.chroot)
661 test_results_dir = input_proto.test_results_dir
Alex Klein6504eca2019-04-18 15:37:56 -0600662 output_dir = input_proto.output_dir
663
Michael Mortensen51f06722019-07-18 09:55:50 -0600664 archives = artifacts.BundleVmFiles(
665 chroot, test_results_dir, output_dir)
Alex Klein6504eca2019-04-18 15:37:56 -0600666 for archive in archives:
667 output_proto.artifacts.add().path = archive
Tiancong Wangc4805b72019-06-11 12:12:03 -0700668
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700669def _ExportCpeReportResponse(input_proto, output_proto, _config):
670 """Add test cpe results to a successful response."""
671 output_proto.artifacts.add().path = os.path.join(
672 input_proto.output_dir, 'cpe_report.txt')
673 output_proto.artifacts.add().path = os.path.join(
674 input_proto.output_dir, 'cpe_warnings.txt')
675
676
677@faux.success(_ExportCpeReportResponse)
678@faux.empty_error
Alex Klein0b1cbfc2019-08-14 10:09:58 -0600679@validate.exists('output_dir')
Varun Somani04dccd72021-10-09 01:06:11 +0000680def ExportCpeReport(
681 input_proto: artifacts_pb2.BundleRequest,
682 output_proto: artifacts_pb2.BundleResponse,
683 config: 'api_config.ApiConfig'):
Alex Klein0b1cbfc2019-08-14 10:09:58 -0600684 """Export a CPE report.
685
686 Args:
Varun Somani04dccd72021-10-09 01:06:11 +0000687 input_proto: The input proto.
688 output_proto: The output proto.
689 config: The API call config.
Alex Klein0b1cbfc2019-08-14 10:09:58 -0600690 """
691 chroot = controller_util.ParseChroot(input_proto.chroot)
692 output_dir = input_proto.output_dir
693
694 if input_proto.build_target.name:
695 # Legacy handling - use the default sysroot path for the build target.
696 build_target = controller_util.ParseBuildTarget(input_proto.build_target)
697 sysroot = sysroot_lib.Sysroot(build_target.root)
698 elif input_proto.sysroot.path:
699 sysroot = sysroot_lib.Sysroot(input_proto.sysroot.path)
700 else:
701 # TODO(saklein): Switch to validate decorators once legacy handling can be
702 # cleaned up.
703 cros_build_lib.Die('sysroot.path is required.')
704
705 if config.validate_only:
706 return controller.RETURN_CODE_VALID_INPUT
707
708 cpe_result = artifacts.GenerateCpeReport(chroot, sysroot, output_dir)
709
710 output_proto.artifacts.add().path = cpe_result.report
711 output_proto.artifacts.add().path = cpe_result.warnings
Shao-Chuan Leea44dddc2020-10-30 17:16:55 +0900712
713
714def _BundleGceTarballResponse(input_proto, output_proto, _config):
715 """Add artifact tarball to a successful response."""
716 output_proto.artifacts.add().path = os.path.join(input_proto.output_dir,
717 constants.TEST_IMAGE_GCE_TAR)
718
719
720@faux.success(_BundleGceTarballResponse)
721@faux.empty_error
722@validate.require('build_target.name', 'output_dir')
723@validate.exists('output_dir')
724@validate.validation_complete
Varun Somani04dccd72021-10-09 01:06:11 +0000725def BundleGceTarball(
726 input_proto: artifacts_pb2.BundleRequest,
727 output_proto: artifacts_pb2.BundleResponse,
728 _config: 'api_config.ApiConfig'):
Shao-Chuan Leea44dddc2020-10-30 17:16:55 +0900729 """Bundle the test image into a tarball suitable for importing into GCE.
730
731 Args:
Varun Somani04dccd72021-10-09 01:06:11 +0000732 input_proto: The input proto.
733 output_proto: The output proto.
734 _config: The API call config.
Shao-Chuan Leea44dddc2020-10-30 17:16:55 +0900735 """
736 target = input_proto.build_target.name
737 output_dir = input_proto.output_dir
738 image_dir = _GetImageDir(constants.SOURCE_ROOT, target)
739 if image_dir is None:
740 return None
741
742 tarball = artifacts.BundleGceTarball(output_dir, image_dir)
743 output_proto.artifacts.add().path = tarball