blob: 95d348c3a4d0782da2cc82011ba8bcf5ab0eaf3d [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2019 The ChromiumOS Authors
Evan Hernandezf388cbf2019-04-01 11:15:23 -06002# 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:
Alex Klein1699fab2022-09-08 08:46:06 -060028 from chromite.api import api_config
29
Evan Hernandezf388cbf2019-04-01 11:15:23 -060030
George Engelbrechtc9a8e812021-06-16 18:14:17 -060031class RegisteredGet(NamedTuple):
Alex Kleinb6847e22022-11-07 10:44:48 -070032 """A registered function for calling Get on an artifact type."""
Alex Klein1699fab2022-09-08 08:46:06 -060033
34 output_proto: artifacts_pb2.GetResponse
35 artifact_dict: Any
LaMont Jones0f5171b2021-01-29 12:28:56 -070036
37
Alex Kleinb6847e22022-11-07 10:44:48 -070038def ExampleGetResponse(_input_proto, _output_proto, _config) -> Optional[int]:
Alex Klein1699fab2022-09-08 08:46:06 -060039 """Give an example GetResponse with a minimal coverage set."""
40 _output_proto = artifacts_pb2.GetResponse(
41 artifacts=common_pb2.UploadedArtifactsByService(
42 image=image_controller.ExampleGetResponse(),
43 sysroot=sysroot_controller.ExampleGetResponse(),
44 )
45 )
46 return controller.RETURN_CODE_SUCCESS
George Engelbrechtc9a8e812021-06-16 18:14:17 -060047
48
LaMont Jones0f5171b2021-01-29 12:28:56 -070049@faux.empty_error
George Engelbrechtc9a8e812021-06-16 18:14:17 -060050@faux.success(ExampleGetResponse)
Alex Klein1699fab2022-09-08 08:46:06 -060051@validate.exists("result_path.path.path")
LaMont Jones0f5171b2021-01-29 12:28:56 -070052@validate.validation_complete
Varun Somani04dccd72021-10-09 01:06:11 +000053def Get(
54 input_proto: artifacts_pb2.GetRequest,
55 output_proto: artifacts_pb2.GetResponse,
Alex Klein1699fab2022-09-08 08:46:06 -060056 _config: "api_config.ApiConfig",
Alex Kleinb6847e22022-11-07 10:44:48 -070057) -> Optional[int]:
Alex Klein1699fab2022-09-08 08:46:06 -060058 """Get all artifacts.
LaMont Jones0f5171b2021-01-29 12:28:56 -070059
Alex Klein1699fab2022-09-08 08:46:06 -060060 Get all artifacts for the build.
LaMont Jones0f5171b2021-01-29 12:28:56 -070061
Alex Klein1699fab2022-09-08 08:46:06 -060062 Note: As the individual artifact_type bundlers are added here, they *must*
63 stop uploading it via the individual bundler function.
Alex Klein1699fab2022-09-08 08:46:06 -060064 """
65 output_dir = input_proto.result_path.path.path
Jaques Clapauchf616bcd2021-04-09 20:14:40 +000066
Alex Klein1699fab2022-09-08 08:46:06 -060067 sysroot = controller_util.ParseSysroot(input_proto.sysroot)
68 # This endpoint does not currently support any artifacts that are built
69 # without a sysroot being present.
70 if not sysroot.path:
71 return controller.RETURN_CODE_SUCCESS
72
73 chroot = controller_util.ParseChroot(input_proto.chroot)
74 build_target = controller_util.ParseBuildTarget(
75 input_proto.sysroot.build_target
76 )
77
78 # 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,
84 chroot,
85 sysroot,
86 build_target,
87 output_dir,
88 ),
89 ),
90 RegisteredGet(
91 output_proto.artifacts.sysroot,
92 sysroot_controller.GetArtifacts(
93 input_proto.artifact_info.sysroot,
94 chroot,
95 sysroot,
96 build_target,
97 output_dir,
98 ),
99 ),
100 RegisteredGet(
101 output_proto.artifacts.test,
102 test_controller.GetArtifacts(
103 input_proto.artifact_info.test,
104 chroot,
105 sysroot,
106 build_target,
107 output_dir,
108 ),
109 ),
110 ]
111
112 for get_res in get_res_list:
113 for artifact_dict in get_res.artifact_dict:
Jack Neus26b94672022-10-27 17:33:21 +0000114 kwargs = {}
115 # TODO(b/255838545): Remove the kwargs funkness when these fields
116 # have been added for all services.
117 if "failed" in artifact_dict:
118 kwargs["failed"] = artifact_dict.get("failed", False)
119 kwargs["failure_reason"] = artifact_dict.get("failure_reason")
Alex Klein1699fab2022-09-08 08:46:06 -0600120 get_res.output_proto.artifacts.add(
121 artifact_type=artifact_dict["type"],
122 paths=[
123 common_pb2.Path(
124 path=x, location=common_pb2.Path.Location.OUTSIDE
125 )
Jack Neus26b94672022-10-27 17:33:21 +0000126 for x in artifact_dict.get("paths", [])
Alex Klein1699fab2022-09-08 08:46:06 -0600127 ],
Jack Neus26b94672022-10-27 17:33:21 +0000128 **kwargs,
Alex Klein1699fab2022-09-08 08:46:06 -0600129 )
LaMont Jones8b88e9d2021-06-28 16:37:34 -0600130 return controller.RETURN_CODE_SUCCESS
131
LaMont Jones0f5171b2021-01-29 12:28:56 -0700132
Alex Kleinb6847e22022-11-07 10:44:48 -0700133def _BuildSetupResponse(_input_proto, output_proto, _config) -> None:
Alex Klein1699fab2022-09-08 08:46:06 -0600134 """Just return POINTLESS for now."""
Alex Kleinb6847e22022-11-07 10:44:48 -0700135 # All the artifact types we support claim that the build is POINTLESS.
Alex Klein1699fab2022-09-08 08:46:06 -0600136 output_proto.build_relevance = artifacts_pb2.BuildSetupResponse.POINTLESS
LaMont Jones58362a42021-02-04 17:40:08 -0700137
138
139@faux.success(_BuildSetupResponse)
140@faux.empty_error
141@validate.validation_complete
Varun Somani04dccd72021-10-09 01:06:11 +0000142def BuildSetup(
143 _input_proto: artifacts_pb2.GetRequest,
144 output_proto: artifacts_pb2.GetResponse,
Alex Klein1699fab2022-09-08 08:46:06 -0600145 _config: "api_config.ApiConfig",
Alex Kleinb6847e22022-11-07 10:44:48 -0700146) -> Optional[int]:
Alex Klein1699fab2022-09-08 08:46:06 -0600147 """Setup anything needed for building artifacts
LaMont Jones58362a42021-02-04 17:40:08 -0700148
Alex Klein1699fab2022-09-08 08:46:06 -0600149 If any artifact types require steps prior to building the package, they go
150 here. For example, see ToolchainService/PrepareForBuild.
LaMont Jones58362a42021-02-04 17:40:08 -0700151
Alex Klein1699fab2022-09-08 08:46:06 -0600152 Note: crbug/1034529 introduces this method as a noop. As the individual
153 artifact_type bundlers are added here, they *must* stop uploading it via the
154 individual bundler function.
Alex Klein1699fab2022-09-08 08:46:06 -0600155 """
156 # If any artifact_type says "NEEDED", the return is NEEDED.
157 # Otherwise, if any artifact_type says "UNKNOWN", the return is UNKNOWN.
158 # Otherwise, the return is POINTLESS.
159 output_proto.build_relevance = artifacts_pb2.BuildSetupResponse.POINTLESS
160 return controller.RETURN_CODE_SUCCESS
LaMont Jones58362a42021-02-04 17:40:08 -0700161
162
Varun Somani04dccd72021-10-09 01:06:11 +0000163def _GetImageDir(build_root: str, target: str) -> Optional[str]:
Alex Klein1699fab2022-09-08 08:46:06 -0600164 """Return path containing images for the given build target.
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600165
Alex Klein1699fab2022-09-08 08:46:06 -0600166 TODO(saklein) Expand image_lib.GetLatestImageLink to support this use case.
Alex Kleine2612a02019-04-18 13:51:06 -0600167
Alex Klein1699fab2022-09-08 08:46:06 -0600168 Args:
Alex Klein611dddd2022-10-11 17:02:01 -0600169 build_root: Path to checkout where build occurs.
170 target: Name of the build target.
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600171
Alex Klein1699fab2022-09-08 08:46:06 -0600172 Returns:
Alex Klein611dddd2022-10-11 17:02:01 -0600173 Path to the latest directory containing target images or None.
Alex Klein1699fab2022-09-08 08:46:06 -0600174 """
175 image_dir = os.path.join(build_root, "src/build/images", target, "latest")
176 if not os.path.exists(image_dir):
177 logging.warning(
178 "Expected to find image output for target %s at %s, but "
179 "path does not exist",
180 target,
181 image_dir,
182 )
183 return None
Alex Kleind2bf1462019-10-24 16:37:04 -0600184
Alex Klein1699fab2022-09-08 08:46:06 -0600185 return image_dir
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600186
187
Alex Kleinb6847e22022-11-07 10:44:48 -0700188def _BundleImageArchivesResponse(input_proto, output_proto, _config) -> None:
Alex Klein1699fab2022-09-08 08:46:06 -0600189 """Add artifact paths to a successful response."""
Brian Norris2711f3a2023-07-18 11:09:00 -0700190 output_proto.artifacts.add(
191 artifact_path=common_pb2.Path(
192 path=os.path.join(
193 input_proto.result_path.path.path, "path0.tar.xz"
194 ),
195 location=common_pb2.Path.OUTSIDE,
196 )
Alex Klein1699fab2022-09-08 08:46:06 -0600197 )
Brian Norris2711f3a2023-07-18 11:09:00 -0700198 output_proto.artifacts.add(
199 artifact_path=common_pb2.Path(
200 path=os.path.join(
201 input_proto.result_path.path.path, "path1.tar.xz"
202 ),
203 location=common_pb2.Path.OUTSIDE,
204 )
Alex Klein1699fab2022-09-08 08:46:06 -0600205 )
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700206
207
208@faux.success(_BundleImageArchivesResponse)
209@faux.empty_error
Hsin-Yi Wang09663da2023-06-28 21:40:24 +0800210@validate.require("sysroot.build_target.name", "sysroot.path")
Brian Norris2711f3a2023-07-18 11:09:00 -0700211@validate.exists("result_path.path.path")
Alex Kleind91e95a2019-09-17 10:39:02 -0600212@validate.validation_complete
Alex Kleinb6847e22022-11-07 10:44:48 -0700213def BundleImageArchives(
214 input_proto: artifacts_pb2.BundleRequest,
215 output_proto: artifacts_pb2.BundleResponse,
216 _config: "api_config.ApiConfig",
217) -> Optional[int]:
Alex Klein1699fab2022-09-08 08:46:06 -0600218 """Create a .tar.xz archive for each image that has been created."""
Hsin-Yi Wang09663da2023-06-28 21:40:24 +0800219 build_target = controller_util.ParseBuildTarget(
220 input_proto.sysroot.build_target
221 )
222 chroot = controller_util.ParseChroot(input_proto.chroot)
223 sysroot = controller_util.ParseSysroot(input_proto.sysroot)
Brian Norris2711f3a2023-07-18 11:09:00 -0700224 output_dir = input_proto.result_path.path.path
Alex Klein1699fab2022-09-08 08:46:06 -0600225 image_dir = _GetImageDir(constants.SOURCE_ROOT, build_target.name)
226 if image_dir is None:
227 return
Alex Kleind91e95a2019-09-17 10:39:02 -0600228
Hsin-Yi Wang09663da2023-06-28 21:40:24 +0800229 if not sysroot.Exists(chroot=chroot):
230 logging.warning("Sysroot does not exist: %s", sysroot.path)
231
232 archives = artifacts.ArchiveImages(chroot, sysroot, image_dir, output_dir)
Alex Kleind91e95a2019-09-17 10:39:02 -0600233
Alex Klein1699fab2022-09-08 08:46:06 -0600234 for archive in archives:
Brian Norris2711f3a2023-07-18 11:09:00 -0700235 output_proto.artifacts.add(
236 artifact_path=common_pb2.Path(
237 path=os.path.join(output_dir, archive),
238 location=common_pb2.Path.OUTSIDE,
239 )
240 )
Alex Kleind91e95a2019-09-17 10:39:02 -0600241
242
Alex Kleinb6847e22022-11-07 10:44:48 -0700243def _BundleImageZipResponse(input_proto, output_proto, _config) -> None:
Alex Klein1699fab2022-09-08 08:46:06 -0600244 """Add artifact zip files to a successful response."""
Brian Norris2711f3a2023-07-18 11:09:00 -0700245 output_proto.artifacts.add(
246 artifact_path=common_pb2.Path(
247 path=os.path.join(input_proto.result_path.path.path, "image.zip"),
248 location=common_pb2.Path.OUTSIDE,
249 )
Alex Klein1699fab2022-09-08 08:46:06 -0600250 )
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700251
252
253@faux.success(_BundleImageZipResponse)
254@faux.empty_error
Brian Norris2711f3a2023-07-18 11:09:00 -0700255@validate.require("build_target.name", "result_path.path.path")
256@validate.exists("result_path.path.path")
Alex Klein231d2da2019-07-22 16:44:45 -0600257@validate.validation_complete
Varun Somani04dccd72021-10-09 01:06:11 +0000258def BundleImageZip(
259 input_proto: artifacts_pb2.BundleRequest,
260 output_proto: artifacts_pb2.BundleResponse,
Alex Klein1699fab2022-09-08 08:46:06 -0600261 _config: "api_config.ApiConfig",
Alex Kleinb6847e22022-11-07 10:44:48 -0700262) -> Optional[int]:
263 """Bundle image.zip."""
Alex Klein1699fab2022-09-08 08:46:06 -0600264 target = input_proto.build_target.name
Brian Norris2711f3a2023-07-18 11:09:00 -0700265 output_dir = input_proto.result_path.path.path
Alex Klein1699fab2022-09-08 08:46:06 -0600266 image_dir = _GetImageDir(constants.SOURCE_ROOT, target)
267 if image_dir is None:
Alex Kleinb6847e22022-11-07 10:44:48 -0700268 logging.warning("Image build directory not found.")
Alex Klein1699fab2022-09-08 08:46:06 -0600269 return None
Alex Klein231d2da2019-07-22 16:44:45 -0600270
Alex Klein1699fab2022-09-08 08:46:06 -0600271 archive = artifacts.BundleImageZip(output_dir, image_dir)
Brian Norris2711f3a2023-07-18 11:09:00 -0700272 output_proto.artifacts.add(
273 artifact_path=common_pb2.Path(
274 path=os.path.join(output_dir, archive),
275 location=common_pb2.Path.OUTSIDE,
276 )
277 )
Evan Hernandez9f125ac2019-04-08 17:18:47 -0600278
279
Alex Kleinb6847e22022-11-07 10:44:48 -0700280def _BundleTestUpdatePayloadsResponse(
281 input_proto, output_proto, _config
282) -> None:
Alex Klein1699fab2022-09-08 08:46:06 -0600283 """Add test payload files to a successful response."""
Brian Norris2711f3a2023-07-18 11:09:00 -0700284 output_proto.artifacts.add(
285 artifact_path=common_pb2.Path(
286 path=os.path.join(
287 input_proto.result_path.path.path, "payload1.bin"
288 ),
289 location=common_pb2.Path.OUTSIDE,
290 )
Alex Klein1699fab2022-09-08 08:46:06 -0600291 )
Brian Norris2711f3a2023-07-18 11:09:00 -0700292 output_proto.artifacts.add(
293 artifact_path=common_pb2.Path(
294 path=os.path.join(
295 input_proto.result_path.path.path, "payload1.json"
296 ),
297 location=common_pb2.Path.OUTSIDE,
298 )
Alex Klein1699fab2022-09-08 08:46:06 -0600299 )
Brian Norris2711f3a2023-07-18 11:09:00 -0700300 output_proto.artifacts.add(
301 artifact_path=common_pb2.Path(
302 path=os.path.join(
303 input_proto.result_path.path.path, "payload1.log"
304 ),
305 location=common_pb2.Path.OUTSIDE,
306 )
Alex Klein1699fab2022-09-08 08:46:06 -0600307 )
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700308
309
310@faux.success(_BundleTestUpdatePayloadsResponse)
311@faux.empty_error
Brian Norris2711f3a2023-07-18 11:09:00 -0700312@validate.require("build_target.name", "result_path.path.path")
313@validate.exists("result_path.path.path")
Alex Klein231d2da2019-07-22 16:44:45 -0600314@validate.validation_complete
Varun Somani04dccd72021-10-09 01:06:11 +0000315def BundleTestUpdatePayloads(
316 input_proto: artifacts_pb2.BundleRequest,
317 output_proto: artifacts_pb2.BundleResponse,
Alex Klein1699fab2022-09-08 08:46:06 -0600318 _config: "api_config.ApiConfig",
Alex Kleinb6847e22022-11-07 10:44:48 -0700319) -> Optional[int]:
320 """Generate minimal update payloads for the build target for testing."""
Alex Klein1699fab2022-09-08 08:46:06 -0600321 target = input_proto.build_target.name
Brian Norris2711f3a2023-07-18 11:09:00 -0700322 output_dir = input_proto.result_path.path.path
Brian Norrisdd2e7e62023-06-16 14:07:32 -0700323 chroot = controller_util.ParseChroot(input_proto.chroot)
Alex Klein1699fab2022-09-08 08:46:06 -0600324 build_root = constants.SOURCE_ROOT
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600325
Alex Klein1699fab2022-09-08 08:46:06 -0600326 # Use the first available image to create the update payload.
327 img_dir = _GetImageDir(build_root, target)
328 if img_dir is None:
329 return None
Alex Kleind2bf1462019-10-24 16:37:04 -0600330
Alex Klein1699fab2022-09-08 08:46:06 -0600331 img_types = [
332 constants.IMAGE_TYPE_TEST,
333 constants.IMAGE_TYPE_DEV,
334 constants.IMAGE_TYPE_BASE,
335 ]
336 img_names = [constants.IMAGE_TYPE_TO_NAME[t] for t in img_types]
337 img_paths = [os.path.join(img_dir, x) for x in img_names]
338 valid_images = [x for x in img_paths if os.path.exists(x)]
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600339
Alex Klein1699fab2022-09-08 08:46:06 -0600340 if not valid_images:
341 cros_build_lib.Die(
342 'Expected to find an image of type among %r for target "%s" '
343 "at path %s.",
344 img_types,
345 target,
346 img_dir,
347 )
348 image = valid_images[0]
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600349
Brian Norrisdd2e7e62023-06-16 14:07:32 -0700350 payloads = artifacts.BundleTestUpdatePayloads(chroot, image, output_dir)
Alex Klein1699fab2022-09-08 08:46:06 -0600351 for payload in payloads:
Brian Norris2711f3a2023-07-18 11:09:00 -0700352 output_proto.artifacts.add(
353 artifact_path=common_pb2.Path(
354 path=payload, location=common_pb2.Path.OUTSIDE
355 ),
356 )
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600357
358
Alex Kleinb6847e22022-11-07 10:44:48 -0700359def _BundleAutotestFilesResponse(input_proto, output_proto, _config) -> None:
Alex Klein1699fab2022-09-08 08:46:06 -0600360 """Add test autotest files to a successful response."""
Brian Norris2711f3a2023-07-18 11:09:00 -0700361 output_proto.artifacts.add(
362 artifact_path=common_pb2.Path(
363 path=os.path.join(
364 input_proto.result_path.path.path, "autotest-a.tar.gz"
365 ),
366 location=common_pb2.Path.OUTSIDE,
367 )
Alex Klein1699fab2022-09-08 08:46:06 -0600368 )
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700369
370
371@faux.success(_BundleAutotestFilesResponse)
372@faux.empty_error
Alex Klein1699fab2022-09-08 08:46:06 -0600373@validate.require("sysroot.path")
Brian Norris2711f3a2023-07-18 11:09:00 -0700374@validate.exists("result_path.path.path")
Alex Klein036833d2022-06-01 13:05:01 -0600375@validate.validation_complete
Alex Klein1699fab2022-09-08 08:46:06 -0600376def BundleAutotestFiles(
377 input_proto: artifacts_pb2.BundleRequest,
378 output_proto: artifacts_pb2.BundleResponse,
379 _config: "api_config.ApiConfig",
Alex Kleinb6847e22022-11-07 10:44:48 -0700380) -> Optional[int]:
Alex Klein1699fab2022-09-08 08:46:06 -0600381 """Tar the autotest files for a build target."""
Brian Norris2711f3a2023-07-18 11:09:00 -0700382 output_dir = input_proto.result_path.path.path
Alex Klein1699fab2022-09-08 08:46:06 -0600383 chroot = controller_util.ParseChroot(input_proto.chroot)
384 sysroot = controller_util.ParseSysroot(input_proto.sysroot)
Alex Klein231d2da2019-07-22 16:44:45 -0600385
Alex Klein1699fab2022-09-08 08:46:06 -0600386 if not sysroot.Exists(chroot=chroot):
387 logging.warning("Sysroot does not exist: %s", sysroot.path)
388 return
Alex Klein238d8862019-05-07 11:32:46 -0600389
Alex Klein1699fab2022-09-08 08:46:06 -0600390 try:
391 # Note that this returns the full path to *multiple* tarballs.
392 archives = artifacts.BundleAutotestFiles(chroot, sysroot, output_dir)
393 except artifacts.Error as e:
394 logging.warning(e)
395 return
Alex Klein238d8862019-05-07 11:32:46 -0600396
Alex Klein1699fab2022-09-08 08:46:06 -0600397 for archive in archives.values():
Brian Norris2711f3a2023-07-18 11:09:00 -0700398 output_proto.artifacts.add(
399 artifact_path=common_pb2.Path(
400 path=archive, location=common_pb2.Path.OUTSIDE
401 )
402 )
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600403
404
Alex Kleinb6847e22022-11-07 10:44:48 -0700405def _BundleTastFilesResponse(input_proto, output_proto, _config) -> None:
Alex Klein1699fab2022-09-08 08:46:06 -0600406 """Add test tast files to a successful response."""
Brian Norris2711f3a2023-07-18 11:09:00 -0700407 output_proto.artifacts.add(
408 artifact_path=common_pb2.Path(
409 path=os.path.join(
410 input_proto.result_path.path.path, "tast_bundles.tar.gz"
411 ),
412 location=common_pb2.Path.OUTSIDE,
413 )
Alex Klein1699fab2022-09-08 08:46:06 -0600414 )
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700415
416
417@faux.success(_BundleTastFilesResponse)
418@faux.empty_error
Alex Klein1699fab2022-09-08 08:46:06 -0600419@validate.require("sysroot.path")
Brian Norris2711f3a2023-07-18 11:09:00 -0700420@validate.exists("result_path.path.path")
Alex Klein036833d2022-06-01 13:05:01 -0600421@validate.validation_complete
Alex Klein1699fab2022-09-08 08:46:06 -0600422def BundleTastFiles(
423 input_proto: artifacts_pb2.BundleRequest,
424 output_proto: artifacts_pb2.BundleResponse,
425 _config: "api_config.ApiConfig",
Alex Kleinb6847e22022-11-07 10:44:48 -0700426) -> Optional[int]:
Alex Klein1699fab2022-09-08 08:46:06 -0600427 """Tar the tast files for a build target."""
Brian Norris2711f3a2023-07-18 11:09:00 -0700428 output_dir = input_proto.result_path.path.path
Alex Klein1699fab2022-09-08 08:46:06 -0600429 chroot = controller_util.ParseChroot(input_proto.chroot)
430 sysroot = controller_util.ParseSysroot(input_proto.sysroot)
Alex Kleinb9d810b2019-07-01 12:38:02 -0600431
Alex Klein1699fab2022-09-08 08:46:06 -0600432 if not sysroot.Exists(chroot=chroot):
433 logging.warning("Sysroot does not exist: %s", sysroot.path)
434 return
Alex Kleinb9d810b2019-07-01 12:38:02 -0600435
Alex Klein1699fab2022-09-08 08:46:06 -0600436 archive = artifacts.BundleTastFiles(chroot, sysroot, output_dir)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600437
Alex Klein1699fab2022-09-08 08:46:06 -0600438 if not archive:
439 logging.warning("Found no tast files for %s.", sysroot.path)
440 return
Alex Klein036833d2022-06-01 13:05:01 -0600441
Brian Norris2711f3a2023-07-18 11:09:00 -0700442 output_proto.artifacts.add(
443 artifact_path=common_pb2.Path(
444 path=archive, location=common_pb2.Path.OUTSIDE
445 )
446 )
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600447
448
Fergus Dall34d74e12020-09-29 15:52:32 +1000449def BundlePinnedGuestImages(_input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600450 # TODO(crbug/1034529): Remove this endpoint
451 pass
452
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700453
Fergus Dall34d74e12020-09-29 15:52:32 +1000454def FetchPinnedGuestImageUris(_input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600455 # TODO(crbug/1034529): Remove this endpoint
456 pass
Alex Klein7bf0ecb2019-06-25 10:04:15 -0600457
458
Alex Kleinb6847e22022-11-07 10:44:48 -0700459def _FetchMetadataResponse(
460 _input_proto, output_proto, _config
461) -> Optional[int]:
Alex Klein1699fab2022-09-08 08:46:06 -0600462 """Populate the output_proto with sample data."""
463 for fp in ("/metadata/foo.txt", "/metadata/bar.jsonproto"):
464 output_proto.filepaths.add(
465 path=common_pb2.Path(path=fp, location=common_pb2.Path.OUTSIDE)
466 )
467 return controller.RETURN_CODE_SUCCESS
Greg Edelstondc941072021-08-11 12:32:30 -0600468
469
470@faux.success(_FetchMetadataResponse)
471@faux.empty_error
Alex Klein1699fab2022-09-08 08:46:06 -0600472@validate.exists("chroot.path")
473@validate.require("sysroot.path")
Greg Edelstondc941072021-08-11 12:32:30 -0600474@validate.validation_complete
Varun Somani04dccd72021-10-09 01:06:11 +0000475def FetchMetadata(
476 input_proto: artifacts_pb2.FetchMetadataRequest,
477 output_proto: artifacts_pb2.FetchMetadataResponse,
Alex Klein1699fab2022-09-08 08:46:06 -0600478 _config: "api_config.ApiConfig",
Alex Kleinb6847e22022-11-07 10:44:48 -0700479) -> Optional[int]:
Alex Klein1699fab2022-09-08 08:46:06 -0600480 """FetchMetadata returns the paths to all build/test metadata files.
Greg Edelstondc941072021-08-11 12:32:30 -0600481
Alex Klein1699fab2022-09-08 08:46:06 -0600482 This implements ArtifactsService.FetchMetadata.
Alex Klein1699fab2022-09-08 08:46:06 -0600483 """
484 chroot = controller_util.ParseChroot(input_proto.chroot)
485 sysroot = controller_util.ParseSysroot(input_proto.sysroot)
486 for path in test.FindAllMetadataFiles(chroot, sysroot):
487 output_proto.filepaths.add(
488 path=common_pb2.Path(path=path, location=common_pb2.Path.OUTSIDE)
489 )
490 return controller.RETURN_CODE_SUCCESS
Greg Edelstondc941072021-08-11 12:32:30 -0600491
492
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700493def _BundleFirmwareResponse(input_proto, output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600494 """Add test firmware image files to a successful response."""
Brian Norris2711f3a2023-07-18 11:09:00 -0700495 output_proto.artifacts.add(
496 artifact_path=common_pb2.Path(
497 path=os.path.join(
498 input_proto.result_path.path.path, "firmware.tar.gz"
499 ),
500 location=common_pb2.Path.OUTSIDE,
501 )
Alex Klein1699fab2022-09-08 08:46:06 -0600502 )
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700503
504
505@faux.success(_BundleFirmwareResponse)
506@faux.empty_error
Alex Klein1699fab2022-09-08 08:46:06 -0600507@validate.require("sysroot.path")
Brian Norris2711f3a2023-07-18 11:09:00 -0700508@validate.exists("result_path.path.path")
Alex Klein231d2da2019-07-22 16:44:45 -0600509@validate.validation_complete
Alex Klein1699fab2022-09-08 08:46:06 -0600510def BundleFirmware(
511 input_proto: artifacts_pb2.BundleRequest,
512 output_proto: artifacts_pb2.BundleResponse,
513 _config: "api_config.ApiConfig",
Alex Kleinb6847e22022-11-07 10:44:48 -0700514) -> Optional[int]:
Alex Klein1699fab2022-09-08 08:46:06 -0600515 """Tar the firmware images for a build target."""
Brian Norris2711f3a2023-07-18 11:09:00 -0700516 output_dir = input_proto.result_path.path.path
Alex Klein1699fab2022-09-08 08:46:06 -0600517 chroot = controller_util.ParseChroot(input_proto.chroot)
518 sysroot = controller_util.ParseSysroot(input_proto.sysroot)
Alex Klein231d2da2019-07-22 16:44:45 -0600519
Alex Klein1699fab2022-09-08 08:46:06 -0600520 if not chroot.exists():
521 logging.warning("Chroot does not exist: %s", chroot.path)
522 return
523 elif not sysroot.Exists(chroot=chroot):
524 logging.warning("Sysroot does not exist: %s", sysroot.path)
525 return
Alex Klein231d2da2019-07-22 16:44:45 -0600526
Alex Klein1699fab2022-09-08 08:46:06 -0600527 archive = artifacts.BuildFirmwareArchive(chroot, sysroot, output_dir)
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600528
Alex Klein1699fab2022-09-08 08:46:06 -0600529 if not archive:
530 logging.warning(
531 "Could not create firmware archive. No firmware found for %s.",
532 sysroot.path,
533 )
534 return
Evan Hernandez9a5d3122019-04-09 10:51:23 -0600535
Brian Norris2711f3a2023-07-18 11:09:00 -0700536 output_proto.artifacts.add(
537 artifact_path=common_pb2.Path(
538 path=archive, location=common_pb2.Path.OUTSIDE
539 )
540 )
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600541
542
Alex Kleinb6847e22022-11-07 10:44:48 -0700543def _BundleFpmcuUnittestsResponse(input_proto, output_proto, _config) -> None:
Alex Klein1699fab2022-09-08 08:46:06 -0600544 """Add fingerprint MCU unittest binaries to a successful response."""
Brian Norris2711f3a2023-07-18 11:09:00 -0700545 output_proto.artifacts.add(
546 artifact_path=common_pb2.Path(
547 path=os.path.join(
548 input_proto.result_path.path.path, "fpmcu_unittests.tar.gz"
549 ),
550 location=common_pb2.Path.OUTSIDE,
551 )
Alex Klein1699fab2022-09-08 08:46:06 -0600552 )
Yicheng Liea1181f2020-09-22 11:51:10 -0700553
554
555@faux.success(_BundleFpmcuUnittestsResponse)
556@faux.empty_error
Alex Klein1699fab2022-09-08 08:46:06 -0600557@validate.require("sysroot.path")
Brian Norris2711f3a2023-07-18 11:09:00 -0700558@validate.exists("result_path.path.path")
Yicheng Liea1181f2020-09-22 11:51:10 -0700559@validate.validation_complete
Alex Klein1699fab2022-09-08 08:46:06 -0600560def BundleFpmcuUnittests(
561 input_proto: artifacts_pb2.BundleRequest,
562 output_proto: artifacts_pb2.BundleResponse,
563 _config: "api_config.ApiConfig",
Alex Kleinb6847e22022-11-07 10:44:48 -0700564) -> Optional[int]:
565 """Tar the fingerprint MCU unittest binaries for a build target."""
Brian Norris2711f3a2023-07-18 11:09:00 -0700566 output_dir = input_proto.result_path.path.path
Alex Klein1699fab2022-09-08 08:46:06 -0600567 chroot = controller_util.ParseChroot(input_proto.chroot)
568 sysroot = controller_util.ParseSysroot(input_proto.sysroot)
Yicheng Liea1181f2020-09-22 11:51:10 -0700569
Alex Klein1699fab2022-09-08 08:46:06 -0600570 if not chroot.exists():
571 logging.warning("Chroot does not exist: %s", chroot.path)
572 return
573 elif not sysroot.Exists(chroot=chroot):
574 logging.warning("Sysroot does not exist: %s", sysroot.path)
575 return
Yicheng Liea1181f2020-09-22 11:51:10 -0700576
Alex Klein1699fab2022-09-08 08:46:06 -0600577 archive = artifacts.BundleFpmcuUnittests(chroot, sysroot, output_dir)
Yicheng Liea1181f2020-09-22 11:51:10 -0700578
Alex Klein1699fab2022-09-08 08:46:06 -0600579 if not archive:
580 logging.warning("No fpmcu unittests found for %s.", sysroot.path)
581 return
Yicheng Liea1181f2020-09-22 11:51:10 -0700582
Brian Norris2711f3a2023-07-18 11:09:00 -0700583 output_proto.artifacts.add(
584 artifact_path=common_pb2.Path(
585 path=archive, location=common_pb2.Path.OUTSIDE
586 )
587 )
Yicheng Liea1181f2020-09-22 11:51:10 -0700588
589
Alex Kleinb6847e22022-11-07 10:44:48 -0700590def _BundleEbuildLogsResponse(input_proto, output_proto, _config) -> None:
Alex Klein1699fab2022-09-08 08:46:06 -0600591 """Add test log files to a successful response."""
Brian Norris2711f3a2023-07-18 11:09:00 -0700592 output_proto.artifacts.add(
593 artifact_path=common_pb2.Path(
594 path=os.path.join(
595 input_proto.result_path.path.path, "ebuild-logs.tar.gz"
596 ),
597 location=common_pb2.Path.OUTSIDE,
598 )
Alex Klein1699fab2022-09-08 08:46:06 -0600599 )
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700600
601
602@faux.success(_BundleEbuildLogsResponse)
603@faux.empty_error
Alex Klein1699fab2022-09-08 08:46:06 -0600604@validate.require("sysroot.path")
Brian Norris2711f3a2023-07-18 11:09:00 -0700605@validate.exists("result_path.path.path")
Alex Klein036833d2022-06-01 13:05:01 -0600606@validate.validation_complete
Alex Klein1699fab2022-09-08 08:46:06 -0600607def BundleEbuildLogs(
608 input_proto: artifacts_pb2.BundleRequest,
609 output_proto: artifacts_pb2.BundleResponse,
610 _config: "api_config.ApiConfig",
Alex Kleinb6847e22022-11-07 10:44:48 -0700611) -> Optional[int]:
Alex Klein1699fab2022-09-08 08:46:06 -0600612 """Tar the ebuild logs for a build target."""
Brian Norris2711f3a2023-07-18 11:09:00 -0700613 output_dir = input_proto.result_path.path.path
Alex Klein1699fab2022-09-08 08:46:06 -0600614 chroot = controller_util.ParseChroot(input_proto.chroot)
615 sysroot = controller_util.ParseSysroot(input_proto.sysroot)
Evan Hernandeza478d802019-04-08 15:08:24 -0600616
Alex Klein1699fab2022-09-08 08:46:06 -0600617 if not sysroot.Exists(chroot=chroot):
618 logging.warning("Sysroot does not exist: %s", sysroot.path)
619 return
Evan Hernandezf388cbf2019-04-01 11:15:23 -0600620
Alex Klein1699fab2022-09-08 08:46:06 -0600621 archive = artifacts.BundleEBuildLogsTarball(chroot, sysroot, output_dir)
Alex Klein036833d2022-06-01 13:05:01 -0600622
Alex Klein1699fab2022-09-08 08:46:06 -0600623 if not archive:
624 logging.warning(
625 "Could not create ebuild logs archive. No logs found for %s.",
626 sysroot.path,
627 )
628 return
Alex Klein036833d2022-06-01 13:05:01 -0600629
Brian Norris2711f3a2023-07-18 11:09:00 -0700630 output_proto.artifacts.add(
631 artifact_path=common_pb2.Path(
632 path=os.path.join(output_dir, archive),
633 location=common_pb2.Path.OUTSIDE,
634 )
635 )
Alex Klein6504eca2019-04-18 15:37:56 -0600636
637
Alex Kleinb6847e22022-11-07 10:44:48 -0700638def _BundleChromeOSConfigResponse(input_proto, output_proto, _config) -> None:
Alex Klein1699fab2022-09-08 08:46:06 -0600639 """Add test config files to a successful response."""
Brian Norris2711f3a2023-07-18 11:09:00 -0700640 output_proto.artifacts.add(
641 artifact_path=common_pb2.Path(
642 path=os.path.join(input_proto.result_path.path.path, "config.yaml"),
643 location=common_pb2.Path.OUTSIDE,
644 )
Alex Klein1699fab2022-09-08 08:46:06 -0600645 )
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700646
647
648@faux.success(_BundleChromeOSConfigResponse)
649@faux.empty_error
Alex Klein1699fab2022-09-08 08:46:06 -0600650@validate.require("sysroot.path")
Brian Norris2711f3a2023-07-18 11:09:00 -0700651@validate.exists("result_path.path.path")
Andrew Lamb811aead2019-08-12 10:25:05 -0600652@validate.validation_complete
Varun Somani04dccd72021-10-09 01:06:11 +0000653def BundleChromeOSConfig(
654 input_proto: artifacts_pb2.BundleRequest,
655 output_proto: artifacts_pb2.BundleResponse,
Alex Klein1699fab2022-09-08 08:46:06 -0600656 _config: "api_config.ApiConfig",
Alex Kleinb6847e22022-11-07 10:44:48 -0700657) -> Optional[int]:
Alex Klein1699fab2022-09-08 08:46:06 -0600658 """Output the ChromeOS Config payload for a build target."""
Brian Norris2711f3a2023-07-18 11:09:00 -0700659 output_dir = input_proto.result_path.path.path
Alex Klein1699fab2022-09-08 08:46:06 -0600660 chroot = controller_util.ParseChroot(input_proto.chroot)
661 sysroot = controller_util.ParseSysroot(input_proto.sysroot)
Andrew Lamb811aead2019-08-12 10:25:05 -0600662
Alex Klein1699fab2022-09-08 08:46:06 -0600663 chromeos_config = artifacts.BundleChromeOSConfig(
664 chroot, sysroot, output_dir
665 )
Alex Klein2d8333c2022-06-01 13:29:01 -0600666
Alex Klein1699fab2022-09-08 08:46:06 -0600667 if not chromeos_config:
668 logging.warning(
669 "Could not create ChromeOS Config for %s.", sysroot.path
670 )
671 return
Alex Klein383a7a32021-12-07 16:01:19 -0700672
Brian Norris2711f3a2023-07-18 11:09:00 -0700673 output_proto.artifacts.add(
674 artifact_path=common_pb2.Path(
675 path=os.path.join(output_dir, chromeos_config),
676 location=common_pb2.Path.OUTSIDE,
677 )
Alex Klein1699fab2022-09-08 08:46:06 -0600678 )
Andrew Lamb811aead2019-08-12 10:25:05 -0600679
680
Alex Kleinb6847e22022-11-07 10:44:48 -0700681def _BundleSimpleChromeArtifactsResponse(
682 input_proto, output_proto, _config
683) -> None:
Alex Klein1699fab2022-09-08 08:46:06 -0600684 """Add test simple chrome files to a successful response."""
Brian Norris2711f3a2023-07-18 11:09:00 -0700685 output_proto.artifacts.add(
686 artifact_path=common_pb2.Path(
687 path=os.path.join(
688 input_proto.result_path.path.path, "simple_chrome.txt"
689 ),
690 location=common_pb2.Path.OUTSIDE,
691 )
Alex Klein1699fab2022-09-08 08:46:06 -0600692 )
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700693
694
695@faux.success(_BundleSimpleChromeArtifactsResponse)
696@faux.empty_error
Brian Norris2711f3a2023-07-18 11:09:00 -0700697@validate.require(
698 "result_path.path.path", "sysroot.build_target.name", "sysroot.path"
699)
700@validate.exists("result_path.path.path")
Alex Klein231d2da2019-07-22 16:44:45 -0600701@validate.validation_complete
Alex Kleinb6847e22022-11-07 10:44:48 -0700702def BundleSimpleChromeArtifacts(
703 input_proto, output_proto, _config
704) -> Optional[int]:
Alex Klein1699fab2022-09-08 08:46:06 -0600705 """Create the simple chrome artifacts."""
706 sysroot_path = input_proto.sysroot.path
Brian Norris2711f3a2023-07-18 11:09:00 -0700707 output_dir = input_proto.result_path.path.path
Alex Klein2275d692019-04-23 16:04:12 -0600708
Alex Klein1699fab2022-09-08 08:46:06 -0600709 # Build out the argument instances.
710 build_target = controller_util.ParseBuildTarget(
711 input_proto.sysroot.build_target
712 )
713 chroot = controller_util.ParseChroot(input_proto.chroot)
714 # Sysroot.path needs to be the fully qualified path, including the chroot.
Brian Norrisd3e391e2023-06-30 09:53:11 -0700715 full_sysroot_path = chroot.full_path(sysroot_path)
Alex Klein1699fab2022-09-08 08:46:06 -0600716 sysroot = sysroot_lib.Sysroot(full_sysroot_path)
Alex Klein2275d692019-04-23 16:04:12 -0600717
Alex Klein1699fab2022-09-08 08:46:06 -0600718 # Check that the sysroot exists before we go on.
719 if not sysroot.Exists():
Alex Kleinb6847e22022-11-07 10:44:48 -0700720 logging.warning("The sysroot does not exist.")
721 return
Alex Klein2275d692019-04-23 16:04:12 -0600722
Alex Klein1699fab2022-09-08 08:46:06 -0600723 try:
724 results = artifacts.BundleSimpleChromeArtifacts(
725 chroot, sysroot, build_target, output_dir
726 )
727 except artifacts.Error as e:
Alex Kleinb6847e22022-11-07 10:44:48 -0700728 logging.warning(
Alex Klein1699fab2022-09-08 08:46:06 -0600729 "Error %s raised in BundleSimpleChromeArtifacts: %s", type(e), e
730 )
Alex Kleinb6847e22022-11-07 10:44:48 -0700731 return
Alex Klein2275d692019-04-23 16:04:12 -0600732
Alex Klein1699fab2022-09-08 08:46:06 -0600733 for file_name in results:
Brian Norris2711f3a2023-07-18 11:09:00 -0700734 output_proto.artifacts.add(
735 artifact_path=common_pb2.Path(
736 path=file_name, location=common_pb2.Path.OUTSIDE
737 )
738 )
Alex Klein2275d692019-04-23 16:04:12 -0600739
740
Alex Kleinb6847e22022-11-07 10:44:48 -0700741def _BundleVmFilesResponse(input_proto, output_proto, _config) -> None:
Alex Klein1699fab2022-09-08 08:46:06 -0600742 """Add test vm files to a successful response."""
743 output_proto.artifacts.add().path = os.path.join(
744 input_proto.output_dir, "f1.tar"
745 )
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700746
747
748@faux.success(_BundleVmFilesResponse)
749@faux.empty_error
Alex Klein1699fab2022-09-08 08:46:06 -0600750@validate.require("chroot.path", "test_results_dir", "output_dir")
751@validate.exists("output_dir")
Alex Klein231d2da2019-07-22 16:44:45 -0600752@validate.validation_complete
Varun Somani04dccd72021-10-09 01:06:11 +0000753def BundleVmFiles(
754 input_proto: artifacts_pb2.BundleVmFilesRequest,
755 output_proto: artifacts_pb2.BundleResponse,
Alex Klein1699fab2022-09-08 08:46:06 -0600756 _config: "api_config.ApiConfig",
Alex Kleinb6847e22022-11-07 10:44:48 -0700757) -> None:
758 """Tar VM disk and memory files."""
Alex Klein1699fab2022-09-08 08:46:06 -0600759 chroot = controller_util.ParseChroot(input_proto.chroot)
760 test_results_dir = input_proto.test_results_dir
761 output_dir = input_proto.output_dir
Alex Klein6504eca2019-04-18 15:37:56 -0600762
Alex Klein1699fab2022-09-08 08:46:06 -0600763 archives = artifacts.BundleVmFiles(chroot, test_results_dir, output_dir)
764 for archive in archives:
765 output_proto.artifacts.add().path = archive
766
Tiancong Wangc4805b72019-06-11 12:12:03 -0700767
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700768def _ExportCpeReportResponse(input_proto, output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600769 """Add test cpe results to a successful response."""
Brian Norris2711f3a2023-07-18 11:09:00 -0700770 output_proto.artifacts.add(
771 artifact_path=common_pb2.Path(
772 path=os.path.join(
773 input_proto.result_path.path.path, "cpe_report.txt"
774 ),
775 location=common_pb2.Path.OUTSIDE,
776 )
Alex Klein1699fab2022-09-08 08:46:06 -0600777 )
Brian Norris2711f3a2023-07-18 11:09:00 -0700778 output_proto.artifacts.add(
779 artifact_path=common_pb2.Path(
780 path=os.path.join(
781 input_proto.result_path.path.path, "cpe_warnings.txt"
782 ),
783 location=common_pb2.Path.OUTSIDE,
784 )
Alex Klein1699fab2022-09-08 08:46:06 -0600785 )
Michael Mortensen2d6a2402019-11-26 13:40:40 -0700786
787
788@faux.success(_ExportCpeReportResponse)
789@faux.empty_error
Alex Klein1699fab2022-09-08 08:46:06 -0600790@validate.require("sysroot.path")
Brian Norris2711f3a2023-07-18 11:09:00 -0700791@validate.exists("result_path.path.path")
Alex Klein036833d2022-06-01 13:05:01 -0600792@validate.validation_complete
Alex Klein1699fab2022-09-08 08:46:06 -0600793def ExportCpeReport(
794 input_proto: artifacts_pb2.BundleRequest,
795 output_proto: artifacts_pb2.BundleResponse,
796 _config: "api_config.ApiConfig",
Alex Kleinb6847e22022-11-07 10:44:48 -0700797) -> Optional[int]:
Alex Klein1699fab2022-09-08 08:46:06 -0600798 """Export a CPE report."""
799 chroot = controller_util.ParseChroot(input_proto.chroot)
800 sysroot = controller_util.ParseSysroot(input_proto.sysroot)
Brian Norris2711f3a2023-07-18 11:09:00 -0700801 output_dir = input_proto.result_path.path.path
Alex Klein0b1cbfc2019-08-14 10:09:58 -0600802
Alex Klein1699fab2022-09-08 08:46:06 -0600803 if not sysroot.Exists(chroot=chroot):
804 logging.warning("Sysroot does not exist: %s", sysroot.path)
805 return
Alex Klein0b1cbfc2019-08-14 10:09:58 -0600806
Alex Klein1699fab2022-09-08 08:46:06 -0600807 cpe_result = artifacts.GenerateCpeReport(chroot, sysroot, output_dir)
Alex Klein0b1cbfc2019-08-14 10:09:58 -0600808
Brian Norris2711f3a2023-07-18 11:09:00 -0700809 output_proto.artifacts.add(
810 artifact_path=common_pb2.Path(
811 path=cpe_result.report, location=common_pb2.Path.OUTSIDE
812 )
813 )
814 output_proto.artifacts.add(
815 artifact_path=common_pb2.Path(
816 path=cpe_result.warnings, location=common_pb2.Path.OUTSIDE
817 )
818 )
Shao-Chuan Leea44dddc2020-10-30 17:16:55 +0900819
820
Alex Kleinb6847e22022-11-07 10:44:48 -0700821def _BundleGceTarballResponse(input_proto, output_proto, _config) -> None:
Alex Klein1699fab2022-09-08 08:46:06 -0600822 """Add artifact tarball to a successful response."""
Brian Norris2711f3a2023-07-18 11:09:00 -0700823 output_proto.artifacts.add(
824 artifact_path=common_pb2.Path(
825 path=os.path.join(
826 input_proto.result_path.path.path, constants.TEST_IMAGE_GCE_TAR
827 ),
828 location=common_pb2.Path.OUTSIDE,
829 )
Alex Klein1699fab2022-09-08 08:46:06 -0600830 )
Shao-Chuan Leea44dddc2020-10-30 17:16:55 +0900831
832
833@faux.success(_BundleGceTarballResponse)
834@faux.empty_error
Brian Norris2711f3a2023-07-18 11:09:00 -0700835@validate.require("build_target.name", "result_path.path.path")
836@validate.exists("result_path.path.path")
Shao-Chuan Leea44dddc2020-10-30 17:16:55 +0900837@validate.validation_complete
Varun Somani04dccd72021-10-09 01:06:11 +0000838def BundleGceTarball(
839 input_proto: artifacts_pb2.BundleRequest,
840 output_proto: artifacts_pb2.BundleResponse,
Alex Klein1699fab2022-09-08 08:46:06 -0600841 _config: "api_config.ApiConfig",
Alex Kleinb6847e22022-11-07 10:44:48 -0700842) -> Optional[int]:
843 """Bundle the test image into a tarball suitable for importing into GCE."""
Alex Klein1699fab2022-09-08 08:46:06 -0600844 target = input_proto.build_target.name
Brian Norris2711f3a2023-07-18 11:09:00 -0700845 output_dir = input_proto.result_path.path.path
Alex Klein1699fab2022-09-08 08:46:06 -0600846 image_dir = _GetImageDir(constants.SOURCE_ROOT, target)
847 if image_dir is None:
848 return None
Shao-Chuan Leea44dddc2020-10-30 17:16:55 +0900849
Alex Klein1699fab2022-09-08 08:46:06 -0600850 tarball = artifacts.BundleGceTarball(output_dir, image_dir)
Brian Norris2711f3a2023-07-18 11:09:00 -0700851 output_proto.artifacts.add(
852 artifact_path=common_pb2.Path(
853 path=tarball, location=common_pb2.Path.OUTSIDE
854 )
855 )