Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2019 The ChromiumOS Authors |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 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 McDonald | 1672ddb | 2021-07-21 11:48:23 -0600 | [diff] [blame] | 7 | import logging |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 8 | import os |
Varun Somani | 04dccd7 | 2021-10-09 01:06:11 +0000 | [diff] [blame] | 9 | from typing import Any, NamedTuple, Optional, TYPE_CHECKING |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 10 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 11 | from chromite.api import controller |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 12 | from chromite.api import faux |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 13 | from chromite.api import validate |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 14 | from chromite.api.controller import controller_util |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 15 | from chromite.api.controller import image as image_controller |
| 16 | from chromite.api.controller import sysroot as sysroot_controller |
David Welling | c1433c2 | 2021-06-25 16:29:48 +0000 | [diff] [blame] | 17 | from chromite.api.controller import test as test_controller |
LaMont Jones | 58362a4 | 2021-02-04 17:40:08 -0700 | [diff] [blame] | 18 | from chromite.api.gen.chromite.api import artifacts_pb2 |
Jaques Clapauch | f616bcd | 2021-04-09 20:14:40 +0000 | [diff] [blame] | 19 | from chromite.api.gen.chromiumos import common_pb2 |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 20 | from chromite.lib import constants |
| 21 | from chromite.lib import cros_build_lib |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 22 | from chromite.lib import sysroot_lib |
| 23 | from chromite.service import artifacts |
Greg Edelston | dc94107 | 2021-08-11 12:32:30 -0600 | [diff] [blame] | 24 | from chromite.service import test |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 25 | |
Mike Frysinger | 1cc8f1f | 2022-04-28 22:40:40 -0400 | [diff] [blame] | 26 | |
Varun Somani | 04dccd7 | 2021-10-09 01:06:11 +0000 | [diff] [blame] | 27 | if TYPE_CHECKING: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 28 | from chromite.api import api_config |
| 29 | |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 30 | |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 31 | class RegisteredGet(NamedTuple): |
Alex Klein | b6847e2 | 2022-11-07 10:44:48 -0700 | [diff] [blame] | 32 | """A registered function for calling Get on an artifact type.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 33 | |
| 34 | output_proto: artifacts_pb2.GetResponse |
| 35 | artifact_dict: Any |
LaMont Jones | 0f5171b | 2021-01-29 12:28:56 -0700 | [diff] [blame] | 36 | |
| 37 | |
Alex Klein | b6847e2 | 2022-11-07 10:44:48 -0700 | [diff] [blame] | 38 | def ExampleGetResponse(_input_proto, _output_proto, _config) -> Optional[int]: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 39 | """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 Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 47 | |
| 48 | |
LaMont Jones | 0f5171b | 2021-01-29 12:28:56 -0700 | [diff] [blame] | 49 | @faux.empty_error |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 50 | @faux.success(ExampleGetResponse) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 51 | @validate.exists("result_path.path.path") |
LaMont Jones | 0f5171b | 2021-01-29 12:28:56 -0700 | [diff] [blame] | 52 | @validate.validation_complete |
Varun Somani | 04dccd7 | 2021-10-09 01:06:11 +0000 | [diff] [blame] | 53 | def Get( |
| 54 | input_proto: artifacts_pb2.GetRequest, |
| 55 | output_proto: artifacts_pb2.GetResponse, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 56 | _config: "api_config.ApiConfig", |
Alex Klein | b6847e2 | 2022-11-07 10:44:48 -0700 | [diff] [blame] | 57 | ) -> Optional[int]: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 58 | """Get all artifacts. |
LaMont Jones | 0f5171b | 2021-01-29 12:28:56 -0700 | [diff] [blame] | 59 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 60 | Get all artifacts for the build. |
LaMont Jones | 0f5171b | 2021-01-29 12:28:56 -0700 | [diff] [blame] | 61 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 62 | Note: As the individual artifact_type bundlers are added here, they *must* |
| 63 | stop uploading it via the individual bundler function. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 64 | """ |
| 65 | output_dir = input_proto.result_path.path.path |
Jaques Clapauch | f616bcd | 2021-04-09 20:14:40 +0000 | [diff] [blame] | 66 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 67 | 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 Neus | 26b9467 | 2022-10-27 17:33:21 +0000 | [diff] [blame] | 114 | 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 Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 120 | 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 Neus | 26b9467 | 2022-10-27 17:33:21 +0000 | [diff] [blame] | 126 | for x in artifact_dict.get("paths", []) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 127 | ], |
Jack Neus | 26b9467 | 2022-10-27 17:33:21 +0000 | [diff] [blame] | 128 | **kwargs, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 129 | ) |
LaMont Jones | 8b88e9d | 2021-06-28 16:37:34 -0600 | [diff] [blame] | 130 | return controller.RETURN_CODE_SUCCESS |
| 131 | |
LaMont Jones | 0f5171b | 2021-01-29 12:28:56 -0700 | [diff] [blame] | 132 | |
Alex Klein | b6847e2 | 2022-11-07 10:44:48 -0700 | [diff] [blame] | 133 | def _BuildSetupResponse(_input_proto, output_proto, _config) -> None: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 134 | """Just return POINTLESS for now.""" |
Alex Klein | b6847e2 | 2022-11-07 10:44:48 -0700 | [diff] [blame] | 135 | # All the artifact types we support claim that the build is POINTLESS. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 136 | output_proto.build_relevance = artifacts_pb2.BuildSetupResponse.POINTLESS |
LaMont Jones | 58362a4 | 2021-02-04 17:40:08 -0700 | [diff] [blame] | 137 | |
| 138 | |
| 139 | @faux.success(_BuildSetupResponse) |
| 140 | @faux.empty_error |
| 141 | @validate.validation_complete |
Varun Somani | 04dccd7 | 2021-10-09 01:06:11 +0000 | [diff] [blame] | 142 | def BuildSetup( |
| 143 | _input_proto: artifacts_pb2.GetRequest, |
| 144 | output_proto: artifacts_pb2.GetResponse, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 145 | _config: "api_config.ApiConfig", |
Alex Klein | b6847e2 | 2022-11-07 10:44:48 -0700 | [diff] [blame] | 146 | ) -> Optional[int]: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 147 | """Setup anything needed for building artifacts |
LaMont Jones | 58362a4 | 2021-02-04 17:40:08 -0700 | [diff] [blame] | 148 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 149 | If any artifact types require steps prior to building the package, they go |
| 150 | here. For example, see ToolchainService/PrepareForBuild. |
LaMont Jones | 58362a4 | 2021-02-04 17:40:08 -0700 | [diff] [blame] | 151 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 152 | 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 Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 155 | """ |
| 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 Jones | 58362a4 | 2021-02-04 17:40:08 -0700 | [diff] [blame] | 161 | |
| 162 | |
Varun Somani | 04dccd7 | 2021-10-09 01:06:11 +0000 | [diff] [blame] | 163 | def _GetImageDir(build_root: str, target: str) -> Optional[str]: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 164 | """Return path containing images for the given build target. |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 165 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 166 | TODO(saklein) Expand image_lib.GetLatestImageLink to support this use case. |
Alex Klein | e2612a0 | 2019-04-18 13:51:06 -0600 | [diff] [blame] | 167 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 168 | Args: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 169 | build_root: Path to checkout where build occurs. |
| 170 | target: Name of the build target. |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 171 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 172 | Returns: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 173 | Path to the latest directory containing target images or None. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 174 | """ |
| 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 Klein | d2bf146 | 2019-10-24 16:37:04 -0600 | [diff] [blame] | 184 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 185 | return image_dir |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 186 | |
| 187 | |
Alex Klein | b6847e2 | 2022-11-07 10:44:48 -0700 | [diff] [blame] | 188 | def _BundleImageArchivesResponse(input_proto, output_proto, _config) -> None: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 189 | """Add artifact paths to a successful response.""" |
| 190 | output_proto.artifacts.add().path = os.path.join( |
| 191 | input_proto.output_dir, "path0.tar.xz" |
| 192 | ) |
| 193 | output_proto.artifacts.add().path = os.path.join( |
| 194 | input_proto.output_dir, "path1.tar.xz" |
| 195 | ) |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 196 | |
| 197 | |
| 198 | @faux.success(_BundleImageArchivesResponse) |
| 199 | @faux.empty_error |
Hsin-Yi Wang | 09663da | 2023-06-28 21:40:24 +0800 | [diff] [blame] | 200 | @validate.require("sysroot.build_target.name", "sysroot.path") |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 201 | @validate.exists("output_dir") |
Alex Klein | d91e95a | 2019-09-17 10:39:02 -0600 | [diff] [blame] | 202 | @validate.validation_complete |
Alex Klein | b6847e2 | 2022-11-07 10:44:48 -0700 | [diff] [blame] | 203 | def BundleImageArchives( |
| 204 | input_proto: artifacts_pb2.BundleRequest, |
| 205 | output_proto: artifacts_pb2.BundleResponse, |
| 206 | _config: "api_config.ApiConfig", |
| 207 | ) -> Optional[int]: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 208 | """Create a .tar.xz archive for each image that has been created.""" |
Hsin-Yi Wang | 09663da | 2023-06-28 21:40:24 +0800 | [diff] [blame] | 209 | build_target = controller_util.ParseBuildTarget( |
| 210 | input_proto.sysroot.build_target |
| 211 | ) |
| 212 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 213 | sysroot = controller_util.ParseSysroot(input_proto.sysroot) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 214 | output_dir = input_proto.output_dir |
| 215 | image_dir = _GetImageDir(constants.SOURCE_ROOT, build_target.name) |
| 216 | if image_dir is None: |
| 217 | return |
Alex Klein | d91e95a | 2019-09-17 10:39:02 -0600 | [diff] [blame] | 218 | |
Hsin-Yi Wang | 09663da | 2023-06-28 21:40:24 +0800 | [diff] [blame] | 219 | if not sysroot.Exists(chroot=chroot): |
| 220 | logging.warning("Sysroot does not exist: %s", sysroot.path) |
| 221 | |
| 222 | archives = artifacts.ArchiveImages(chroot, sysroot, image_dir, output_dir) |
Alex Klein | d91e95a | 2019-09-17 10:39:02 -0600 | [diff] [blame] | 223 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 224 | for archive in archives: |
| 225 | output_proto.artifacts.add().path = os.path.join(output_dir, archive) |
Alex Klein | d91e95a | 2019-09-17 10:39:02 -0600 | [diff] [blame] | 226 | |
| 227 | |
Alex Klein | b6847e2 | 2022-11-07 10:44:48 -0700 | [diff] [blame] | 228 | def _BundleImageZipResponse(input_proto, output_proto, _config) -> None: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 229 | """Add artifact zip files to a successful response.""" |
| 230 | output_proto.artifacts.add().path = os.path.join( |
| 231 | input_proto.output_dir, "image.zip" |
| 232 | ) |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 233 | |
| 234 | |
| 235 | @faux.success(_BundleImageZipResponse) |
| 236 | @faux.empty_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 237 | @validate.require("build_target.name", "output_dir") |
| 238 | @validate.exists("output_dir") |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 239 | @validate.validation_complete |
Varun Somani | 04dccd7 | 2021-10-09 01:06:11 +0000 | [diff] [blame] | 240 | def BundleImageZip( |
| 241 | input_proto: artifacts_pb2.BundleRequest, |
| 242 | output_proto: artifacts_pb2.BundleResponse, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 243 | _config: "api_config.ApiConfig", |
Alex Klein | b6847e2 | 2022-11-07 10:44:48 -0700 | [diff] [blame] | 244 | ) -> Optional[int]: |
| 245 | """Bundle image.zip.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 246 | target = input_proto.build_target.name |
| 247 | output_dir = input_proto.output_dir |
| 248 | image_dir = _GetImageDir(constants.SOURCE_ROOT, target) |
| 249 | if image_dir is None: |
Alex Klein | b6847e2 | 2022-11-07 10:44:48 -0700 | [diff] [blame] | 250 | logging.warning("Image build directory not found.") |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 251 | return None |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 252 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 253 | archive = artifacts.BundleImageZip(output_dir, image_dir) |
| 254 | output_proto.artifacts.add().path = os.path.join(output_dir, archive) |
Evan Hernandez | 9f125ac | 2019-04-08 17:18:47 -0600 | [diff] [blame] | 255 | |
| 256 | |
Alex Klein | b6847e2 | 2022-11-07 10:44:48 -0700 | [diff] [blame] | 257 | def _BundleTestUpdatePayloadsResponse( |
| 258 | input_proto, output_proto, _config |
| 259 | ) -> None: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 260 | """Add test payload files to a successful response.""" |
| 261 | output_proto.artifacts.add().path = os.path.join( |
| 262 | input_proto.output_dir, "payload1.bin" |
| 263 | ) |
| 264 | output_proto.artifacts.add().path = os.path.join( |
| 265 | input_proto.output_dir, "payload1.json" |
| 266 | ) |
| 267 | output_proto.artifacts.add().path = os.path.join( |
| 268 | input_proto.output_dir, "payload1.log" |
| 269 | ) |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 270 | |
| 271 | |
| 272 | @faux.success(_BundleTestUpdatePayloadsResponse) |
| 273 | @faux.empty_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 274 | @validate.require("build_target.name", "output_dir") |
| 275 | @validate.exists("output_dir") |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 276 | @validate.validation_complete |
Varun Somani | 04dccd7 | 2021-10-09 01:06:11 +0000 | [diff] [blame] | 277 | def BundleTestUpdatePayloads( |
| 278 | input_proto: artifacts_pb2.BundleRequest, |
| 279 | output_proto: artifacts_pb2.BundleResponse, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 280 | _config: "api_config.ApiConfig", |
Alex Klein | b6847e2 | 2022-11-07 10:44:48 -0700 | [diff] [blame] | 281 | ) -> Optional[int]: |
| 282 | """Generate minimal update payloads for the build target for testing.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 283 | target = input_proto.build_target.name |
| 284 | output_dir = input_proto.output_dir |
Brian Norris | dd2e7e6 | 2023-06-16 14:07:32 -0700 | [diff] [blame] | 285 | chroot = controller_util.ParseChroot(input_proto.chroot) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 286 | build_root = constants.SOURCE_ROOT |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 287 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 288 | # Use the first available image to create the update payload. |
| 289 | img_dir = _GetImageDir(build_root, target) |
| 290 | if img_dir is None: |
| 291 | return None |
Alex Klein | d2bf146 | 2019-10-24 16:37:04 -0600 | [diff] [blame] | 292 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 293 | img_types = [ |
| 294 | constants.IMAGE_TYPE_TEST, |
| 295 | constants.IMAGE_TYPE_DEV, |
| 296 | constants.IMAGE_TYPE_BASE, |
| 297 | ] |
| 298 | img_names = [constants.IMAGE_TYPE_TO_NAME[t] for t in img_types] |
| 299 | img_paths = [os.path.join(img_dir, x) for x in img_names] |
| 300 | valid_images = [x for x in img_paths if os.path.exists(x)] |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 301 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 302 | if not valid_images: |
| 303 | cros_build_lib.Die( |
| 304 | 'Expected to find an image of type among %r for target "%s" ' |
| 305 | "at path %s.", |
| 306 | img_types, |
| 307 | target, |
| 308 | img_dir, |
| 309 | ) |
| 310 | image = valid_images[0] |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 311 | |
Brian Norris | dd2e7e6 | 2023-06-16 14:07:32 -0700 | [diff] [blame] | 312 | payloads = artifacts.BundleTestUpdatePayloads(chroot, image, output_dir) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 313 | for payload in payloads: |
| 314 | output_proto.artifacts.add().path = payload |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 315 | |
| 316 | |
Alex Klein | b6847e2 | 2022-11-07 10:44:48 -0700 | [diff] [blame] | 317 | def _BundleAutotestFilesResponse(input_proto, output_proto, _config) -> None: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 318 | """Add test autotest files to a successful response.""" |
| 319 | output_proto.artifacts.add().path = os.path.join( |
| 320 | input_proto.output_dir, "autotest-a.tar.gz" |
| 321 | ) |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 322 | |
| 323 | |
| 324 | @faux.success(_BundleAutotestFilesResponse) |
| 325 | @faux.empty_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 326 | @validate.require("sysroot.path") |
| 327 | @validate.exists("output_dir") |
Alex Klein | 036833d | 2022-06-01 13:05:01 -0600 | [diff] [blame] | 328 | @validate.validation_complete |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 329 | def BundleAutotestFiles( |
| 330 | input_proto: artifacts_pb2.BundleRequest, |
| 331 | output_proto: artifacts_pb2.BundleResponse, |
| 332 | _config: "api_config.ApiConfig", |
Alex Klein | b6847e2 | 2022-11-07 10:44:48 -0700 | [diff] [blame] | 333 | ) -> Optional[int]: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 334 | """Tar the autotest files for a build target.""" |
| 335 | output_dir = input_proto.output_dir |
| 336 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 337 | sysroot = controller_util.ParseSysroot(input_proto.sysroot) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 338 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 339 | if not sysroot.Exists(chroot=chroot): |
| 340 | logging.warning("Sysroot does not exist: %s", sysroot.path) |
| 341 | return |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 342 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 343 | try: |
| 344 | # Note that this returns the full path to *multiple* tarballs. |
| 345 | archives = artifacts.BundleAutotestFiles(chroot, sysroot, output_dir) |
| 346 | except artifacts.Error as e: |
| 347 | logging.warning(e) |
| 348 | return |
Alex Klein | 238d886 | 2019-05-07 11:32:46 -0600 | [diff] [blame] | 349 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 350 | for archive in archives.values(): |
| 351 | output_proto.artifacts.add().path = archive |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 352 | |
| 353 | |
Alex Klein | b6847e2 | 2022-11-07 10:44:48 -0700 | [diff] [blame] | 354 | def _BundleTastFilesResponse(input_proto, output_proto, _config) -> None: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 355 | """Add test tast files to a successful response.""" |
| 356 | output_proto.artifacts.add().path = os.path.join( |
| 357 | input_proto.output_dir, "tast_bundles.tar.gz" |
| 358 | ) |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 359 | |
| 360 | |
| 361 | @faux.success(_BundleTastFilesResponse) |
| 362 | @faux.empty_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 363 | @validate.require("sysroot.path") |
| 364 | @validate.exists("output_dir") |
Alex Klein | 036833d | 2022-06-01 13:05:01 -0600 | [diff] [blame] | 365 | @validate.validation_complete |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 366 | def BundleTastFiles( |
| 367 | input_proto: artifacts_pb2.BundleRequest, |
| 368 | output_proto: artifacts_pb2.BundleResponse, |
| 369 | _config: "api_config.ApiConfig", |
Alex Klein | b6847e2 | 2022-11-07 10:44:48 -0700 | [diff] [blame] | 370 | ) -> Optional[int]: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 371 | """Tar the tast files for a build target.""" |
| 372 | output_dir = input_proto.output_dir |
| 373 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 374 | sysroot = controller_util.ParseSysroot(input_proto.sysroot) |
Alex Klein | b9d810b | 2019-07-01 12:38:02 -0600 | [diff] [blame] | 375 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 376 | if not sysroot.Exists(chroot=chroot): |
| 377 | logging.warning("Sysroot does not exist: %s", sysroot.path) |
| 378 | return |
Alex Klein | b9d810b | 2019-07-01 12:38:02 -0600 | [diff] [blame] | 379 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 380 | archive = artifacts.BundleTastFiles(chroot, sysroot, output_dir) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 381 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 382 | if not archive: |
| 383 | logging.warning("Found no tast files for %s.", sysroot.path) |
| 384 | return |
Alex Klein | 036833d | 2022-06-01 13:05:01 -0600 | [diff] [blame] | 385 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 386 | output_proto.artifacts.add().path = archive |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 387 | |
| 388 | |
Fergus Dall | 34d74e1 | 2020-09-29 15:52:32 +1000 | [diff] [blame] | 389 | def BundlePinnedGuestImages(_input_proto, _output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 390 | # TODO(crbug/1034529): Remove this endpoint |
| 391 | pass |
| 392 | |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 393 | |
Fergus Dall | 34d74e1 | 2020-09-29 15:52:32 +1000 | [diff] [blame] | 394 | def FetchPinnedGuestImageUris(_input_proto, _output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 395 | # TODO(crbug/1034529): Remove this endpoint |
| 396 | pass |
Alex Klein | 7bf0ecb | 2019-06-25 10:04:15 -0600 | [diff] [blame] | 397 | |
| 398 | |
Alex Klein | b6847e2 | 2022-11-07 10:44:48 -0700 | [diff] [blame] | 399 | def _FetchMetadataResponse( |
| 400 | _input_proto, output_proto, _config |
| 401 | ) -> Optional[int]: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 402 | """Populate the output_proto with sample data.""" |
| 403 | for fp in ("/metadata/foo.txt", "/metadata/bar.jsonproto"): |
| 404 | output_proto.filepaths.add( |
| 405 | path=common_pb2.Path(path=fp, location=common_pb2.Path.OUTSIDE) |
| 406 | ) |
| 407 | return controller.RETURN_CODE_SUCCESS |
Greg Edelston | dc94107 | 2021-08-11 12:32:30 -0600 | [diff] [blame] | 408 | |
| 409 | |
| 410 | @faux.success(_FetchMetadataResponse) |
| 411 | @faux.empty_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 412 | @validate.exists("chroot.path") |
| 413 | @validate.require("sysroot.path") |
Greg Edelston | dc94107 | 2021-08-11 12:32:30 -0600 | [diff] [blame] | 414 | @validate.validation_complete |
Varun Somani | 04dccd7 | 2021-10-09 01:06:11 +0000 | [diff] [blame] | 415 | def FetchMetadata( |
| 416 | input_proto: artifacts_pb2.FetchMetadataRequest, |
| 417 | output_proto: artifacts_pb2.FetchMetadataResponse, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 418 | _config: "api_config.ApiConfig", |
Alex Klein | b6847e2 | 2022-11-07 10:44:48 -0700 | [diff] [blame] | 419 | ) -> Optional[int]: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 420 | """FetchMetadata returns the paths to all build/test metadata files. |
Greg Edelston | dc94107 | 2021-08-11 12:32:30 -0600 | [diff] [blame] | 421 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 422 | This implements ArtifactsService.FetchMetadata. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 423 | """ |
| 424 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 425 | sysroot = controller_util.ParseSysroot(input_proto.sysroot) |
| 426 | for path in test.FindAllMetadataFiles(chroot, sysroot): |
| 427 | output_proto.filepaths.add( |
| 428 | path=common_pb2.Path(path=path, location=common_pb2.Path.OUTSIDE) |
| 429 | ) |
| 430 | return controller.RETURN_CODE_SUCCESS |
Greg Edelston | dc94107 | 2021-08-11 12:32:30 -0600 | [diff] [blame] | 431 | |
| 432 | |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 433 | def _BundleFirmwareResponse(input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 434 | """Add test firmware image files to a successful response.""" |
| 435 | output_proto.artifacts.add().path = os.path.join( |
| 436 | input_proto.output_dir, "firmware.tar.gz" |
| 437 | ) |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 438 | |
| 439 | |
| 440 | @faux.success(_BundleFirmwareResponse) |
| 441 | @faux.empty_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 442 | @validate.require("sysroot.path") |
| 443 | @validate.exists("output_dir") |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 444 | @validate.validation_complete |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 445 | def BundleFirmware( |
| 446 | input_proto: artifacts_pb2.BundleRequest, |
| 447 | output_proto: artifacts_pb2.BundleResponse, |
| 448 | _config: "api_config.ApiConfig", |
Alex Klein | b6847e2 | 2022-11-07 10:44:48 -0700 | [diff] [blame] | 449 | ) -> Optional[int]: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 450 | """Tar the firmware images for a build target.""" |
| 451 | output_dir = input_proto.output_dir |
| 452 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 453 | sysroot = controller_util.ParseSysroot(input_proto.sysroot) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 454 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 455 | if not chroot.exists(): |
| 456 | logging.warning("Chroot does not exist: %s", chroot.path) |
| 457 | return |
| 458 | elif not sysroot.Exists(chroot=chroot): |
| 459 | logging.warning("Sysroot does not exist: %s", sysroot.path) |
| 460 | return |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 461 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 462 | archive = artifacts.BuildFirmwareArchive(chroot, sysroot, output_dir) |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 463 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 464 | if not archive: |
| 465 | logging.warning( |
| 466 | "Could not create firmware archive. No firmware found for %s.", |
| 467 | sysroot.path, |
| 468 | ) |
| 469 | return |
Evan Hernandez | 9a5d312 | 2019-04-09 10:51:23 -0600 | [diff] [blame] | 470 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 471 | output_proto.artifacts.add().path = archive |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 472 | |
| 473 | |
Alex Klein | b6847e2 | 2022-11-07 10:44:48 -0700 | [diff] [blame] | 474 | def _BundleFpmcuUnittestsResponse(input_proto, output_proto, _config) -> None: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 475 | """Add fingerprint MCU unittest binaries to a successful response.""" |
| 476 | output_proto.artifacts.add().path = os.path.join( |
| 477 | input_proto.output_dir, "fpmcu_unittests.tar.gz" |
| 478 | ) |
Yicheng Li | ea1181f | 2020-09-22 11:51:10 -0700 | [diff] [blame] | 479 | |
| 480 | |
| 481 | @faux.success(_BundleFpmcuUnittestsResponse) |
| 482 | @faux.empty_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 483 | @validate.require("sysroot.path") |
| 484 | @validate.exists("output_dir") |
Yicheng Li | ea1181f | 2020-09-22 11:51:10 -0700 | [diff] [blame] | 485 | @validate.validation_complete |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 486 | def BundleFpmcuUnittests( |
| 487 | input_proto: artifacts_pb2.BundleRequest, |
| 488 | output_proto: artifacts_pb2.BundleResponse, |
| 489 | _config: "api_config.ApiConfig", |
Alex Klein | b6847e2 | 2022-11-07 10:44:48 -0700 | [diff] [blame] | 490 | ) -> Optional[int]: |
| 491 | """Tar the fingerprint MCU unittest binaries for a build target.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 492 | output_dir = input_proto.output_dir |
| 493 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 494 | sysroot = controller_util.ParseSysroot(input_proto.sysroot) |
Yicheng Li | ea1181f | 2020-09-22 11:51:10 -0700 | [diff] [blame] | 495 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 496 | if not chroot.exists(): |
| 497 | logging.warning("Chroot does not exist: %s", chroot.path) |
| 498 | return |
| 499 | elif not sysroot.Exists(chroot=chroot): |
| 500 | logging.warning("Sysroot does not exist: %s", sysroot.path) |
| 501 | return |
Yicheng Li | ea1181f | 2020-09-22 11:51:10 -0700 | [diff] [blame] | 502 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 503 | archive = artifacts.BundleFpmcuUnittests(chroot, sysroot, output_dir) |
Yicheng Li | ea1181f | 2020-09-22 11:51:10 -0700 | [diff] [blame] | 504 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 505 | if not archive: |
| 506 | logging.warning("No fpmcu unittests found for %s.", sysroot.path) |
| 507 | return |
Yicheng Li | ea1181f | 2020-09-22 11:51:10 -0700 | [diff] [blame] | 508 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 509 | output_proto.artifacts.add().path = archive |
Yicheng Li | ea1181f | 2020-09-22 11:51:10 -0700 | [diff] [blame] | 510 | |
| 511 | |
Alex Klein | b6847e2 | 2022-11-07 10:44:48 -0700 | [diff] [blame] | 512 | def _BundleEbuildLogsResponse(input_proto, output_proto, _config) -> None: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 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 | ) |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 517 | |
| 518 | |
| 519 | @faux.success(_BundleEbuildLogsResponse) |
| 520 | @faux.empty_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 521 | @validate.require("sysroot.path") |
| 522 | @validate.exists("output_dir") |
Alex Klein | 036833d | 2022-06-01 13:05:01 -0600 | [diff] [blame] | 523 | @validate.validation_complete |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 524 | def BundleEbuildLogs( |
| 525 | input_proto: artifacts_pb2.BundleRequest, |
| 526 | output_proto: artifacts_pb2.BundleResponse, |
| 527 | _config: "api_config.ApiConfig", |
Alex Klein | b6847e2 | 2022-11-07 10:44:48 -0700 | [diff] [blame] | 528 | ) -> Optional[int]: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 529 | """Tar the ebuild logs for a build target.""" |
| 530 | output_dir = input_proto.output_dir |
| 531 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 532 | sysroot = controller_util.ParseSysroot(input_proto.sysroot) |
Evan Hernandez | a478d80 | 2019-04-08 15:08:24 -0600 | [diff] [blame] | 533 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 534 | if not sysroot.Exists(chroot=chroot): |
| 535 | logging.warning("Sysroot does not exist: %s", sysroot.path) |
| 536 | return |
Evan Hernandez | f388cbf | 2019-04-01 11:15:23 -0600 | [diff] [blame] | 537 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 538 | archive = artifacts.BundleEBuildLogsTarball(chroot, sysroot, output_dir) |
Alex Klein | 036833d | 2022-06-01 13:05:01 -0600 | [diff] [blame] | 539 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 540 | if not archive: |
| 541 | logging.warning( |
| 542 | "Could not create ebuild logs archive. No logs found for %s.", |
| 543 | sysroot.path, |
| 544 | ) |
| 545 | return |
Alex Klein | 036833d | 2022-06-01 13:05:01 -0600 | [diff] [blame] | 546 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 547 | output_proto.artifacts.add().path = os.path.join(output_dir, archive) |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 548 | |
| 549 | |
Alex Klein | b6847e2 | 2022-11-07 10:44:48 -0700 | [diff] [blame] | 550 | def _BundleChromeOSConfigResponse(input_proto, output_proto, _config) -> None: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 551 | """Add test config files to a successful response.""" |
| 552 | output_proto.artifacts.add().path = os.path.join( |
| 553 | input_proto.output_dir, "config.yaml" |
| 554 | ) |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 555 | |
| 556 | |
| 557 | @faux.success(_BundleChromeOSConfigResponse) |
| 558 | @faux.empty_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 559 | @validate.require("sysroot.path") |
| 560 | @validate.exists("output_dir") |
Andrew Lamb | 811aead | 2019-08-12 10:25:05 -0600 | [diff] [blame] | 561 | @validate.validation_complete |
Varun Somani | 04dccd7 | 2021-10-09 01:06:11 +0000 | [diff] [blame] | 562 | def BundleChromeOSConfig( |
| 563 | input_proto: artifacts_pb2.BundleRequest, |
| 564 | output_proto: artifacts_pb2.BundleResponse, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 565 | _config: "api_config.ApiConfig", |
Alex Klein | b6847e2 | 2022-11-07 10:44:48 -0700 | [diff] [blame] | 566 | ) -> Optional[int]: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 567 | """Output the ChromeOS Config payload for a build target.""" |
| 568 | output_dir = input_proto.output_dir |
| 569 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 570 | sysroot = controller_util.ParseSysroot(input_proto.sysroot) |
Andrew Lamb | 811aead | 2019-08-12 10:25:05 -0600 | [diff] [blame] | 571 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 572 | chromeos_config = artifacts.BundleChromeOSConfig( |
| 573 | chroot, sysroot, output_dir |
| 574 | ) |
Alex Klein | 2d8333c | 2022-06-01 13:29:01 -0600 | [diff] [blame] | 575 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 576 | if not chromeos_config: |
| 577 | logging.warning( |
| 578 | "Could not create ChromeOS Config for %s.", sysroot.path |
| 579 | ) |
| 580 | return |
Alex Klein | 383a7a3 | 2021-12-07 16:01:19 -0700 | [diff] [blame] | 581 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 582 | output_proto.artifacts.add().path = os.path.join( |
| 583 | output_dir, chromeos_config |
| 584 | ) |
Andrew Lamb | 811aead | 2019-08-12 10:25:05 -0600 | [diff] [blame] | 585 | |
| 586 | |
Alex Klein | b6847e2 | 2022-11-07 10:44:48 -0700 | [diff] [blame] | 587 | def _BundleSimpleChromeArtifactsResponse( |
| 588 | input_proto, output_proto, _config |
| 589 | ) -> None: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 590 | """Add test simple chrome files to a successful response.""" |
| 591 | output_proto.artifacts.add().path = os.path.join( |
| 592 | input_proto.output_dir, "simple_chrome.txt" |
| 593 | ) |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 594 | |
| 595 | |
| 596 | @faux.success(_BundleSimpleChromeArtifactsResponse) |
| 597 | @faux.empty_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 598 | @validate.require("output_dir", "sysroot.build_target.name", "sysroot.path") |
| 599 | @validate.exists("output_dir") |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 600 | @validate.validation_complete |
Alex Klein | b6847e2 | 2022-11-07 10:44:48 -0700 | [diff] [blame] | 601 | def BundleSimpleChromeArtifacts( |
| 602 | input_proto, output_proto, _config |
| 603 | ) -> Optional[int]: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 604 | """Create the simple chrome artifacts.""" |
| 605 | sysroot_path = input_proto.sysroot.path |
| 606 | output_dir = input_proto.output_dir |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 607 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 608 | # Build out the argument instances. |
| 609 | build_target = controller_util.ParseBuildTarget( |
| 610 | input_proto.sysroot.build_target |
| 611 | ) |
| 612 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 613 | # Sysroot.path needs to be the fully qualified path, including the chroot. |
Brian Norris | d3e391e | 2023-06-30 09:53:11 -0700 | [diff] [blame^] | 614 | full_sysroot_path = chroot.full_path(sysroot_path) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 615 | sysroot = sysroot_lib.Sysroot(full_sysroot_path) |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 616 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 617 | # Check that the sysroot exists before we go on. |
| 618 | if not sysroot.Exists(): |
Alex Klein | b6847e2 | 2022-11-07 10:44:48 -0700 | [diff] [blame] | 619 | logging.warning("The sysroot does not exist.") |
| 620 | return |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 621 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 622 | try: |
| 623 | results = artifacts.BundleSimpleChromeArtifacts( |
| 624 | chroot, sysroot, build_target, output_dir |
| 625 | ) |
| 626 | except artifacts.Error as e: |
Alex Klein | b6847e2 | 2022-11-07 10:44:48 -0700 | [diff] [blame] | 627 | logging.warning( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 628 | "Error %s raised in BundleSimpleChromeArtifacts: %s", type(e), e |
| 629 | ) |
Alex Klein | b6847e2 | 2022-11-07 10:44:48 -0700 | [diff] [blame] | 630 | return |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 631 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 632 | for file_name in results: |
| 633 | output_proto.artifacts.add().path = file_name |
Alex Klein | 2275d69 | 2019-04-23 16:04:12 -0600 | [diff] [blame] | 634 | |
| 635 | |
Alex Klein | b6847e2 | 2022-11-07 10:44:48 -0700 | [diff] [blame] | 636 | def _BundleVmFilesResponse(input_proto, output_proto, _config) -> None: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 637 | """Add test vm files to a successful response.""" |
| 638 | output_proto.artifacts.add().path = os.path.join( |
| 639 | input_proto.output_dir, "f1.tar" |
| 640 | ) |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 641 | |
| 642 | |
| 643 | @faux.success(_BundleVmFilesResponse) |
| 644 | @faux.empty_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 645 | @validate.require("chroot.path", "test_results_dir", "output_dir") |
| 646 | @validate.exists("output_dir") |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 647 | @validate.validation_complete |
Varun Somani | 04dccd7 | 2021-10-09 01:06:11 +0000 | [diff] [blame] | 648 | def BundleVmFiles( |
| 649 | input_proto: artifacts_pb2.BundleVmFilesRequest, |
| 650 | output_proto: artifacts_pb2.BundleResponse, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 651 | _config: "api_config.ApiConfig", |
Alex Klein | b6847e2 | 2022-11-07 10:44:48 -0700 | [diff] [blame] | 652 | ) -> None: |
| 653 | """Tar VM disk and memory files.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 654 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 655 | test_results_dir = input_proto.test_results_dir |
| 656 | output_dir = input_proto.output_dir |
Alex Klein | 6504eca | 2019-04-18 15:37:56 -0600 | [diff] [blame] | 657 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 658 | archives = artifacts.BundleVmFiles(chroot, test_results_dir, output_dir) |
| 659 | for archive in archives: |
| 660 | output_proto.artifacts.add().path = archive |
| 661 | |
Tiancong Wang | c4805b7 | 2019-06-11 12:12:03 -0700 | [diff] [blame] | 662 | |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 663 | def _ExportCpeReportResponse(input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 664 | """Add test cpe results to a successful response.""" |
| 665 | output_proto.artifacts.add().path = os.path.join( |
| 666 | input_proto.output_dir, "cpe_report.txt" |
| 667 | ) |
| 668 | output_proto.artifacts.add().path = os.path.join( |
| 669 | input_proto.output_dir, "cpe_warnings.txt" |
| 670 | ) |
Michael Mortensen | 2d6a240 | 2019-11-26 13:40:40 -0700 | [diff] [blame] | 671 | |
| 672 | |
| 673 | @faux.success(_ExportCpeReportResponse) |
| 674 | @faux.empty_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 675 | @validate.require("sysroot.path") |
| 676 | @validate.exists("output_dir") |
Alex Klein | 036833d | 2022-06-01 13:05:01 -0600 | [diff] [blame] | 677 | @validate.validation_complete |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 678 | def ExportCpeReport( |
| 679 | input_proto: artifacts_pb2.BundleRequest, |
| 680 | output_proto: artifacts_pb2.BundleResponse, |
| 681 | _config: "api_config.ApiConfig", |
Alex Klein | b6847e2 | 2022-11-07 10:44:48 -0700 | [diff] [blame] | 682 | ) -> Optional[int]: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 683 | """Export a CPE report.""" |
| 684 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 685 | sysroot = controller_util.ParseSysroot(input_proto.sysroot) |
| 686 | output_dir = input_proto.output_dir |
Alex Klein | 0b1cbfc | 2019-08-14 10:09:58 -0600 | [diff] [blame] | 687 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 688 | if not sysroot.Exists(chroot=chroot): |
| 689 | logging.warning("Sysroot does not exist: %s", sysroot.path) |
| 690 | return |
Alex Klein | 0b1cbfc | 2019-08-14 10:09:58 -0600 | [diff] [blame] | 691 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 692 | cpe_result = artifacts.GenerateCpeReport(chroot, sysroot, output_dir) |
Alex Klein | 0b1cbfc | 2019-08-14 10:09:58 -0600 | [diff] [blame] | 693 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 694 | output_proto.artifacts.add().path = cpe_result.report |
| 695 | output_proto.artifacts.add().path = cpe_result.warnings |
Shao-Chuan Lee | a44dddc | 2020-10-30 17:16:55 +0900 | [diff] [blame] | 696 | |
| 697 | |
Alex Klein | b6847e2 | 2022-11-07 10:44:48 -0700 | [diff] [blame] | 698 | def _BundleGceTarballResponse(input_proto, output_proto, _config) -> None: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 699 | """Add artifact tarball to a successful response.""" |
| 700 | output_proto.artifacts.add().path = os.path.join( |
| 701 | input_proto.output_dir, constants.TEST_IMAGE_GCE_TAR |
| 702 | ) |
Shao-Chuan Lee | a44dddc | 2020-10-30 17:16:55 +0900 | [diff] [blame] | 703 | |
| 704 | |
| 705 | @faux.success(_BundleGceTarballResponse) |
| 706 | @faux.empty_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 707 | @validate.require("build_target.name", "output_dir") |
| 708 | @validate.exists("output_dir") |
Shao-Chuan Lee | a44dddc | 2020-10-30 17:16:55 +0900 | [diff] [blame] | 709 | @validate.validation_complete |
Varun Somani | 04dccd7 | 2021-10-09 01:06:11 +0000 | [diff] [blame] | 710 | def BundleGceTarball( |
| 711 | input_proto: artifacts_pb2.BundleRequest, |
| 712 | output_proto: artifacts_pb2.BundleResponse, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 713 | _config: "api_config.ApiConfig", |
Alex Klein | b6847e2 | 2022-11-07 10:44:48 -0700 | [diff] [blame] | 714 | ) -> Optional[int]: |
| 715 | """Bundle the test image into a tarball suitable for importing into GCE.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 716 | target = input_proto.build_target.name |
| 717 | output_dir = input_proto.output_dir |
| 718 | image_dir = _GetImageDir(constants.SOURCE_ROOT, target) |
| 719 | if image_dir is None: |
| 720 | return None |
Shao-Chuan Lee | a44dddc | 2020-10-30 17:16:55 +0900 | [diff] [blame] | 721 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 722 | tarball = artifacts.BundleGceTarball(output_dir, image_dir) |
| 723 | output_proto.artifacts.add().path = tarball |