Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 1 | # Copyright 2018 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Image API Service. |
| 6 | |
| 7 | The image related API endpoints should generally be found here. |
| 8 | """ |
| 9 | |
Jack Neus | 5e56fef | 2021-06-18 16:57:28 +0000 | [diff] [blame] | 10 | import functools |
Chris McDonald | 1672ddb | 2021-07-21 11:48:23 -0600 | [diff] [blame] | 11 | import logging |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 12 | import os |
Jack Neus | 94a35be | 2021-09-10 15:18:06 +0000 | [diff] [blame] | 13 | from pathlib import Path |
Kevin Shelton | 5b21c8b | 2022-04-04 17:04:13 -0700 | [diff] [blame] | 14 | from typing import List, NamedTuple, Set, TYPE_CHECKING, Union |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 15 | |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 16 | from chromite.api import controller |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 17 | from chromite.api import faux |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 18 | from chromite.api import validate |
Alex Klein | e9a7dbf | 2020-10-06 18:12:12 -0600 | [diff] [blame] | 19 | from chromite.api.controller import controller_util |
David Burger | b171d65 | 2019-05-13 16:07:00 -0600 | [diff] [blame] | 20 | from chromite.api.gen.chromiumos import common_pb2 |
Will Bradley | 9bc8545 | 2019-10-10 10:48:21 -0600 | [diff] [blame] | 21 | from chromite.api.metrics import deserialize_metrics_log |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 22 | from chromite.lib import build_target_lib |
| 23 | from chromite.lib import chroot_lib |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 24 | from chromite.lib import constants |
Chris McDonald | 1672ddb | 2021-07-21 11:48:23 -0600 | [diff] [blame] | 25 | from chromite.lib import cros_build_lib |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 26 | from chromite.lib import image_lib |
Alex Klein | aef4194 | 2022-04-19 14:13:17 -0600 | [diff] [blame] | 27 | from chromite.lib import metrics_lib |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 28 | from chromite.lib import sysroot_lib |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 29 | from chromite.scripts import pushimage |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 30 | from chromite.service import image |
Mike Frysinger | 1cc8f1f | 2022-04-28 22:40:40 -0400 | [diff] [blame] | 31 | from chromite.service import packages as packages_service |
| 32 | |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 33 | |
Kevin Shelton | 5b21c8b | 2022-04-04 17:04:13 -0700 | [diff] [blame] | 34 | if TYPE_CHECKING: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 35 | from chromite.api import api_config |
| 36 | from chromite.api.gen.chromite.api import image_pb2 |
Kevin Shelton | 5b21c8b | 2022-04-04 17:04:13 -0700 | [diff] [blame] | 37 | |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 38 | # The image.proto ImageType enum ids. |
George Engelbrecht | c55d631 | 2021-05-05 12:11:13 -0600 | [diff] [blame] | 39 | _BASE_ID = common_pb2.IMAGE_TYPE_BASE |
| 40 | _DEV_ID = common_pb2.IMAGE_TYPE_DEV |
| 41 | _TEST_ID = common_pb2.IMAGE_TYPE_TEST |
| 42 | _BASE_VM_ID = common_pb2.IMAGE_TYPE_BASE_VM |
| 43 | _TEST_VM_ID = common_pb2.IMAGE_TYPE_TEST_VM |
| 44 | _RECOVERY_ID = common_pb2.IMAGE_TYPE_RECOVERY |
| 45 | _FACTORY_ID = common_pb2.IMAGE_TYPE_FACTORY |
| 46 | _FIRMWARE_ID = common_pb2.IMAGE_TYPE_FIRMWARE |
| 47 | _BASE_GUEST_VM_ID = common_pb2.IMAGE_TYPE_BASE_GUEST_VM |
| 48 | _TEST_GUEST_VM_ID = common_pb2.IMAGE_TYPE_TEST_GUEST_VM |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 49 | |
| 50 | # Dict to allow easily translating names to enum ids and vice versa. |
| 51 | _IMAGE_MAPPING = { |
| 52 | _BASE_ID: constants.IMAGE_TYPE_BASE, |
| 53 | constants.IMAGE_TYPE_BASE: _BASE_ID, |
| 54 | _DEV_ID: constants.IMAGE_TYPE_DEV, |
| 55 | constants.IMAGE_TYPE_DEV: _DEV_ID, |
| 56 | _TEST_ID: constants.IMAGE_TYPE_TEST, |
| 57 | constants.IMAGE_TYPE_TEST: _TEST_ID, |
Michael Mortensen | eefe895 | 2019-08-12 15:37:15 -0600 | [diff] [blame] | 58 | _RECOVERY_ID: constants.IMAGE_TYPE_RECOVERY, |
| 59 | constants.IMAGE_TYPE_RECOVERY: _RECOVERY_ID, |
Alex Klein | 9039a95 | 2021-07-27 13:52:39 -0600 | [diff] [blame] | 60 | _FACTORY_ID: constants.IMAGE_TYPE_FACTORY_SHIM, |
| 61 | constants.IMAGE_TYPE_FACTORY_SHIM: _FACTORY_ID, |
Michael Mortensen | eefe895 | 2019-08-12 15:37:15 -0600 | [diff] [blame] | 62 | _FIRMWARE_ID: constants.IMAGE_TYPE_FIRMWARE, |
| 63 | constants.IMAGE_TYPE_FIRMWARE: _FIRMWARE_ID, |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 64 | } |
| 65 | |
George Engelbrecht | 9f4f832 | 2021-03-08 12:04:17 -0700 | [diff] [blame] | 66 | # Dict to describe the prerequisite built images for each VM image type. |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 67 | _VM_IMAGE_MAPPING = { |
| 68 | _BASE_VM_ID: _IMAGE_MAPPING[_BASE_ID], |
| 69 | _TEST_VM_ID: _IMAGE_MAPPING[_TEST_ID], |
Trent Begin | 008cade | 2019-10-31 13:40:59 -0600 | [diff] [blame] | 70 | _BASE_GUEST_VM_ID: _IMAGE_MAPPING[_BASE_ID], |
| 71 | _TEST_GUEST_VM_ID: _IMAGE_MAPPING[_TEST_ID], |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 72 | } |
| 73 | |
George Engelbrecht | 9f4f832 | 2021-03-08 12:04:17 -0700 | [diff] [blame] | 74 | # Dict to describe the prerequisite built images for each mod image type. |
| 75 | _MOD_IMAGE_MAPPING = { |
| 76 | _RECOVERY_ID: _IMAGE_MAPPING[_BASE_ID], |
| 77 | } |
| 78 | |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 79 | # Supported image types for PushImage. |
| 80 | SUPPORTED_IMAGE_TYPES = { |
| 81 | common_pb2.IMAGE_TYPE_RECOVERY: constants.IMAGE_TYPE_RECOVERY, |
| 82 | common_pb2.IMAGE_TYPE_FACTORY: constants.IMAGE_TYPE_FACTORY, |
| 83 | common_pb2.IMAGE_TYPE_FIRMWARE: constants.IMAGE_TYPE_FIRMWARE, |
| 84 | common_pb2.IMAGE_TYPE_ACCESSORY_USBPD: constants.IMAGE_TYPE_ACCESSORY_USBPD, |
| 85 | common_pb2.IMAGE_TYPE_ACCESSORY_RWSIG: constants.IMAGE_TYPE_ACCESSORY_RWSIG, |
Evan Benn | 4d06110 | 2022-02-14 12:50:45 +1100 | [diff] [blame] | 86 | common_pb2.IMAGE_TYPE_HPS_FIRMWARE: constants.IMAGE_TYPE_HPS_FIRMWARE, |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 87 | common_pb2.IMAGE_TYPE_BASE: constants.IMAGE_TYPE_BASE, |
George Engelbrecht | 9f4f832 | 2021-03-08 12:04:17 -0700 | [diff] [blame] | 88 | common_pb2.IMAGE_TYPE_GSC_FIRMWARE: constants.IMAGE_TYPE_GSC_FIRMWARE, |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Alex Klein | 27978a4 | 2021-07-27 14:18:10 -0600 | [diff] [blame] | 91 | # Built image directory symlink names. These names allow specifying a static |
| 92 | # location for creation to simplify later archival stages. In practice, this |
| 93 | # sets the symlink argument to build_packages. |
| 94 | # Core are the build/dev/test images. |
| 95 | # Use "latest" until we do a better job of passing through image directories, |
| 96 | # e.g. for artifacts. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 97 | LOCATION_CORE = "latest" |
Alex Klein | 27978a4 | 2021-07-27 14:18:10 -0600 | [diff] [blame] | 98 | # The factory_install image. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 99 | LOCATION_FACTORY = "factory_shim" |
Alex Klein | 27978a4 | 2021-07-27 14:18:10 -0600 | [diff] [blame] | 100 | |
| 101 | |
| 102 | class ImageTypes(NamedTuple): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 103 | """Parsed image types.""" |
Alex Klein | 27978a4 | 2021-07-27 14:18:10 -0600 | [diff] [blame] | 104 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 105 | images: Set[str] |
| 106 | vms: Set[int] |
| 107 | mod_images: Set[int] |
Alex Klein | 27978a4 | 2021-07-27 14:18:10 -0600 | [diff] [blame] | 108 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 109 | @property |
| 110 | def core_images(self) -> List[str]: |
| 111 | """The core images (base/dev/test) as a list.""" |
| 112 | return list(self.images - {_IMAGE_MAPPING[_FACTORY_ID]}) or [] |
Alex Klein | 27978a4 | 2021-07-27 14:18:10 -0600 | [diff] [blame] | 113 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 114 | @property |
| 115 | def has_factory(self) -> bool: |
| 116 | """Whether the factory image is present.""" |
| 117 | return _IMAGE_MAPPING[_FACTORY_ID] in self.images |
| 118 | |
| 119 | @property |
| 120 | def factory(self) -> List[str]: |
| 121 | """A list with the factory type if set.""" |
| 122 | return [_IMAGE_MAPPING[_FACTORY_ID]] if self.has_factory else [] |
Alex Klein | 27978a4 | 2021-07-27 14:18:10 -0600 | [diff] [blame] | 123 | |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 124 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 125 | def _add_image_to_proto( |
| 126 | output_proto, path: Union["Path", str], image_type: int, board: str |
| 127 | ): |
| 128 | """Quick helper function to add a new image to the output proto.""" |
| 129 | new_image = output_proto.images.add() |
| 130 | new_image.path = str(path) |
| 131 | new_image.type = image_type |
| 132 | new_image.build_target.name = board |
George Engelbrecht | 9f4f832 | 2021-03-08 12:04:17 -0700 | [diff] [blame] | 133 | |
| 134 | |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 135 | def ExampleGetResponse(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 136 | """Give an example response to assemble upstream in caller artifacts.""" |
| 137 | uabs = common_pb2.UploadedArtifactsByService |
| 138 | cabs = common_pb2.ArtifactsByService |
| 139 | return uabs.Sysroot( |
| 140 | artifacts=[ |
| 141 | uabs.Image.ArtifactPaths( |
| 142 | artifact_type=cabs.Image.ArtifactType.DLC_IMAGE, |
| 143 | paths=[ |
| 144 | common_pb2.Path( |
| 145 | path="/tmp/dlc/dlc.img", |
| 146 | location=common_pb2.Path.OUTSIDE, |
| 147 | ) |
| 148 | ], |
| 149 | ) |
| 150 | ] |
| 151 | ) |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 152 | |
| 153 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 154 | def GetArtifacts( |
| 155 | in_proto: common_pb2.ArtifactsByService.Image, |
| 156 | chroot: chroot_lib.Chroot, |
| 157 | sysroot_class: sysroot_lib.Sysroot, |
| 158 | build_target: build_target_lib.BuildTarget, |
| 159 | output_dir, |
| 160 | ) -> list: |
| 161 | """Builds and copies images to specified output_dir. |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 162 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 163 | Copies (after optionally bundling) all required images into the output_dir, |
| 164 | returning a mapping of image type to a list of (output_dir) paths to |
| 165 | the desired files. Note that currently it is only processing one image (DLC), |
| 166 | but the future direction is to process all required images. Required images |
| 167 | are located within output_artifact.artifact_type. |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 168 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 169 | Args: |
| 170 | in_proto: Proto request defining reqs. |
| 171 | chroot: The chroot proto used for these artifacts. |
| 172 | sysroot_class: The sysroot proto used for these artifacts. |
| 173 | build_target: The build target used for these artifacts. |
| 174 | output_dir: The path to write artifacts to. |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 175 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 176 | Returns: |
| 177 | A list of dictionary mappings of ArtifactType to list of paths. |
| 178 | """ |
| 179 | base_path = chroot.full_path(sysroot_class.path) |
| 180 | board = build_target.name |
| 181 | factory_shim_location = Path( |
| 182 | image_lib.GetLatestImageLink(board, pointer=LOCATION_FACTORY) |
| 183 | ) |
Jack Neus | 5e56fef | 2021-06-18 16:57:28 +0000 | [diff] [blame] | 184 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 185 | generated = [] |
| 186 | dlc_func = functools.partial(image.copy_dlc_image, base_path) |
| 187 | license_func = functools.partial( |
| 188 | image.copy_license_credits, board, symlink=LOCATION_CORE |
| 189 | ) |
| 190 | factory_image_func = functools.partial( |
| 191 | image.create_factory_image_zip, |
| 192 | chroot, |
| 193 | sysroot_class, |
| 194 | factory_shim_location, |
| 195 | packages_service.determine_full_version(), |
| 196 | ) |
| 197 | stripped_packags_func = functools.partial( |
| 198 | image.create_stripped_packages_tar, |
| 199 | chroot, |
| 200 | build_target, |
| 201 | ) |
| 202 | artifact_types = { |
| 203 | in_proto.ArtifactType.DLC_IMAGE: dlc_func, |
| 204 | in_proto.ArtifactType.LICENSE_CREDITS: license_func, |
| 205 | in_proto.ArtifactType.FACTORY_IMAGE: factory_image_func, |
| 206 | in_proto.ArtifactType.STRIPPED_PACKAGES: stripped_packags_func, |
| 207 | } |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 208 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 209 | for output_artifact in in_proto.output_artifacts: |
| 210 | for artifact_type, func in artifact_types.items(): |
| 211 | if artifact_type in output_artifact.artifact_types: |
| 212 | result = func(output_dir) |
| 213 | if result: |
| 214 | generated.append( |
| 215 | { |
| 216 | "paths": [result] |
| 217 | if isinstance(result, str) |
| 218 | else result, |
| 219 | "type": artifact_type, |
| 220 | } |
| 221 | ) |
George Engelbrecht | c9a8e81 | 2021-06-16 18:14:17 -0600 | [diff] [blame] | 222 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 223 | return generated |
Pi-Hsun Shih | 8d9c8e4 | 2021-06-30 03:27:28 +0000 | [diff] [blame] | 224 | |
Alex Klein | caace39 | 2021-07-26 14:28:13 -0600 | [diff] [blame] | 225 | |
Michael Mortensen | 10146cf | 2019-11-19 19:59:22 -0700 | [diff] [blame] | 226 | def _CreateResponse(_input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 227 | """Set output_proto success field on a successful Create response.""" |
| 228 | output_proto.success = True |
Michael Mortensen | 10146cf | 2019-11-19 19:59:22 -0700 | [diff] [blame] | 229 | |
| 230 | |
| 231 | @faux.success(_CreateResponse) |
Michael Mortensen | 85d3840 | 2019-12-12 09:50:29 -0700 | [diff] [blame] | 232 | @faux.empty_completed_unsuccessfully_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 233 | @validate.require("build_target.name") |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 234 | @validate.validation_complete |
Alex Klein | aef4194 | 2022-04-19 14:13:17 -0600 | [diff] [blame] | 235 | @metrics_lib.collect_metrics |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 236 | def Create( |
| 237 | input_proto: "image_pb2.CreateImageRequest", |
| 238 | output_proto: "image_pb2.CreateImageResult", |
| 239 | _config: "api_config.ApiConfig", |
| 240 | ): |
| 241 | """Build images. |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 242 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 243 | Args: |
| 244 | input_proto: The input message. |
| 245 | output_proto: The output message. |
| 246 | _config: The API call config. |
| 247 | """ |
| 248 | board = input_proto.build_target.name |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 249 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 250 | # Build the base image if no images provided. |
| 251 | to_build = input_proto.image_types or [_BASE_ID] |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 252 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 253 | image_types = _ParseImagesToCreate(to_build) |
| 254 | build_config = _ParseCreateBuildConfig(input_proto) |
| 255 | factory_build_config = build_config._replace( |
| 256 | symlink=LOCATION_FACTORY, output_dir_suffix=LOCATION_FACTORY |
| 257 | ) |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 258 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 259 | # Try building the core and factory images. |
| 260 | # Sorted isn't really necessary here, but it's much easier to test. |
| 261 | core_result = image.Build( |
| 262 | board, sorted(image_types.core_images), config=build_config |
| 263 | ) |
| 264 | logging.debug("Core Result Images: %s", core_result.images) |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 265 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 266 | factory_result = image.Build( |
| 267 | board, image_types.factory, config=factory_build_config |
| 268 | ) |
| 269 | logging.debug("Factory Result Images: %s", factory_result.images) |
Will Bradley | 29a49c2 | 2019-10-21 11:50:08 -0600 | [diff] [blame] | 270 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 271 | # A successful run will have no images missing, will have run at least one |
| 272 | # of the two image sets, and neither attempt errored. The no error condition |
| 273 | # should be redundant with no missing images, but is cheap insurance. |
| 274 | all_built = core_result.all_built and factory_result.all_built |
| 275 | one_ran = core_result.build_run or factory_result.build_run |
| 276 | no_errors = not core_result.run_error and not factory_result.run_error |
| 277 | output_proto.success = success = all_built and one_ran and no_errors |
Will Bradley | 29a49c2 | 2019-10-21 11:50:08 -0600 | [diff] [blame] | 278 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 279 | if success: |
| 280 | # Success! We need to record the images we built in the output. |
| 281 | all_images = {**core_result.images, **factory_result.images} |
| 282 | for img_name, img_path in all_images.items(): |
| 283 | _add_image_to_proto( |
| 284 | output_proto, img_path, _IMAGE_MAPPING[img_name], board |
| 285 | ) |
Alex Klein | 27978a4 | 2021-07-27 14:18:10 -0600 | [diff] [blame] | 286 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 287 | # Build and record VMs as necessary. |
| 288 | for vm_type in image_types.vms: |
| 289 | is_test = vm_type in [_TEST_VM_ID, _TEST_GUEST_VM_ID] |
| 290 | img_type = _IMAGE_MAPPING[_TEST_ID if is_test else _BASE_ID] |
| 291 | img_dir = core_result.images[img_type].parent.resolve() |
| 292 | try: |
| 293 | if vm_type in [_BASE_GUEST_VM_ID, _TEST_GUEST_VM_ID]: |
| 294 | vm_path = image.CreateGuestVm( |
| 295 | board, is_test=is_test, image_dir=img_dir |
| 296 | ) |
| 297 | else: |
| 298 | vm_path = image.CreateVm( |
| 299 | board, |
| 300 | disk_layout=build_config.disk_layout, |
| 301 | is_test=is_test, |
| 302 | image_dir=img_dir, |
| 303 | ) |
| 304 | except image.ImageToVmError as e: |
| 305 | cros_build_lib.Die(e) |
Will Bradley | 29a49c2 | 2019-10-21 11:50:08 -0600 | [diff] [blame] | 306 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 307 | _add_image_to_proto(output_proto, vm_path, vm_type, board) |
George Engelbrecht | 9f4f832 | 2021-03-08 12:04:17 -0700 | [diff] [blame] | 308 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 309 | # Build and record any mod images. |
| 310 | for mod_type in image_types.mod_images: |
| 311 | if mod_type == _RECOVERY_ID: |
| 312 | base_image_path = core_result.images[constants.IMAGE_TYPE_BASE] |
| 313 | # For ChromeOS Flex special case. |
| 314 | if build_config.base_is_recovery: |
| 315 | result = image.CopyBaseToRecovery( |
| 316 | board=board, image_path=base_image_path |
| 317 | ) |
| 318 | else: |
| 319 | result = image.BuildRecoveryImage( |
| 320 | board=board, image_path=base_image_path |
| 321 | ) |
| 322 | if result.all_built: |
| 323 | _add_image_to_proto( |
| 324 | output_proto, |
| 325 | result.images[_IMAGE_MAPPING[mod_type]], |
| 326 | mod_type, |
| 327 | board, |
| 328 | ) |
| 329 | else: |
| 330 | cros_build_lib.Die("Failed to create recovery image.") |
| 331 | else: |
| 332 | cros_build_lib.Die("_RECOVERY_ID is the only mod_image_type.") |
Will Bradley | 29a49c2 | 2019-10-21 11:50:08 -0600 | [diff] [blame] | 333 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 334 | # Read metric events log and pipe them into output_proto.events. |
| 335 | deserialize_metrics_log(output_proto.events, prefix=board) |
| 336 | return controller.RETURN_CODE_SUCCESS |
Will Bradley | 29a49c2 | 2019-10-21 11:50:08 -0600 | [diff] [blame] | 337 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 338 | else: |
| 339 | # Failure, include all of the failed packages in the output when available. |
| 340 | packages = core_result.failed_packages + factory_result.failed_packages |
| 341 | if not packages: |
| 342 | return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY |
Alex Klein | 2557b4f | 2019-07-11 14:34:00 -0600 | [diff] [blame] | 343 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 344 | for package in packages: |
| 345 | current = output_proto.failed_packages.add() |
| 346 | controller_util.serialize_package_info(package, current) |
Alex Klein | 1bcd988 | 2019-03-19 13:25:24 -0600 | [diff] [blame] | 347 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 348 | return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE |
Alex Klein | 1bcd988 | 2019-03-19 13:25:24 -0600 | [diff] [blame] | 349 | |
Alex Klein | caace39 | 2021-07-26 14:28:13 -0600 | [diff] [blame] | 350 | |
Alex Klein | 27978a4 | 2021-07-27 14:18:10 -0600 | [diff] [blame] | 351 | def _ParseImagesToCreate(to_build: List[int]) -> ImageTypes: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 352 | """Helper function to parse the image types to build. |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 353 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 354 | This function expresses the dependencies of each image type and adds |
| 355 | the requisite image types if they're not explicitly defined. |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 356 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 357 | Args: |
| 358 | to_build: The image type list. |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 359 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 360 | Returns: |
| 361 | ImageTypes: The parsed images to build. |
| 362 | """ |
| 363 | image_types = set() |
| 364 | vm_types = set() |
| 365 | mod_image_types = set() |
| 366 | for current in to_build: |
| 367 | # Find out if it's a special case (vm, img mod), or just any old image. |
| 368 | if current in _VM_IMAGE_MAPPING: |
| 369 | vm_types.add(current) |
| 370 | # Make sure we build the image required to build the VM. |
| 371 | image_types.add(_VM_IMAGE_MAPPING[current]) |
| 372 | elif current in _MOD_IMAGE_MAPPING: |
| 373 | mod_image_types.add(current) |
| 374 | image_types.add(_MOD_IMAGE_MAPPING[current]) |
| 375 | elif current in _IMAGE_MAPPING: |
| 376 | image_types.add(_IMAGE_MAPPING[current]) |
| 377 | else: |
| 378 | # Not expected, but at least it will be obvious if this comes up. |
| 379 | cros_build_lib.Die( |
| 380 | "The service's known image types do not match those in image.proto. " |
| 381 | "Unknown Enum ID: %s" % current |
| 382 | ) |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 383 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 384 | # We can only build one type of these images at a time since image_to_vm.sh |
| 385 | # uses the default path if a name is not provided. |
| 386 | if vm_types.issuperset({_BASE_VM_ID, _TEST_VM_ID}): |
| 387 | cros_build_lib.Die("Cannot create more than one VM.") |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 388 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 389 | return ImageTypes( |
| 390 | images=image_types, vms=vm_types, mod_images=mod_image_types |
| 391 | ) |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 392 | |
| 393 | |
| 394 | def _ParseCreateBuildConfig(input_proto): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 395 | """Helper to parse the image build config for Create.""" |
| 396 | enable_rootfs_verification = not input_proto.disable_rootfs_verification |
| 397 | version = input_proto.version or None |
| 398 | disk_layout = input_proto.disk_layout or None |
| 399 | builder_path = input_proto.builder_path or None |
| 400 | base_is_recovery = input_proto.base_is_recovery or False |
| 401 | return image.BuildConfig( |
| 402 | enable_rootfs_verification=enable_rootfs_verification, |
| 403 | replace=True, |
| 404 | version=version, |
| 405 | disk_layout=disk_layout, |
| 406 | builder_path=builder_path, |
| 407 | symlink=LOCATION_CORE, |
| 408 | base_is_recovery=base_is_recovery, |
| 409 | ) |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 410 | |
Alex Klein | 1bcd988 | 2019-03-19 13:25:24 -0600 | [diff] [blame] | 411 | |
Michael Mortensen | 10146cf | 2019-11-19 19:59:22 -0700 | [diff] [blame] | 412 | def _SignerTestResponse(_input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 413 | """Set output_proto success field on a successful SignerTest response.""" |
| 414 | output_proto.success = True |
| 415 | return controller.RETURN_CODE_SUCCESS |
Michael Mortensen | 10146cf | 2019-11-19 19:59:22 -0700 | [diff] [blame] | 416 | |
| 417 | |
| 418 | @faux.success(_SignerTestResponse) |
Michael Mortensen | 85d3840 | 2019-12-12 09:50:29 -0700 | [diff] [blame] | 419 | @faux.empty_completed_unsuccessfully_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 420 | @validate.exists("image.path") |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 421 | @validate.validation_complete |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 422 | def SignerTest( |
| 423 | input_proto: "image_pb2.ImageTestRequest", |
| 424 | output_proto: "image_pb2.ImageTestRequest", |
| 425 | _config: "api_config.ApiConfig", |
| 426 | ): |
| 427 | """Run image tests. |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 428 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 429 | Args: |
| 430 | input_proto: The input message. |
| 431 | output_proto: The output message. |
| 432 | _config: The API call config. |
| 433 | """ |
| 434 | image_path = input_proto.image.path |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 435 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 436 | result = image_lib.SecurityTest(image=image_path) |
| 437 | output_proto.success = result |
| 438 | if result: |
| 439 | return controller.RETURN_CODE_SUCCESS |
| 440 | else: |
| 441 | return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 442 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 443 | |
Michael Mortensen | 10146cf | 2019-11-19 19:59:22 -0700 | [diff] [blame] | 444 | def _TestResponse(_input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 445 | """Set output_proto success field on a successful Test response.""" |
| 446 | output_proto.success = True |
| 447 | return controller.RETURN_CODE_SUCCESS |
Michael Mortensen | 10146cf | 2019-11-19 19:59:22 -0700 | [diff] [blame] | 448 | |
| 449 | |
| 450 | @faux.success(_TestResponse) |
Michael Mortensen | 85d3840 | 2019-12-12 09:50:29 -0700 | [diff] [blame] | 451 | @faux.empty_completed_unsuccessfully_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 452 | @validate.require("build_target.name", "result.directory") |
| 453 | @validate.exists("image.path") |
| 454 | def Test( |
| 455 | input_proto: "image_pb2.ImageTestRequest", |
| 456 | output_proto: "image_pb2.ImageTestResult", |
| 457 | config: "api_config.ApiConfig", |
| 458 | ): |
| 459 | """Run image tests. |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 460 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 461 | Args: |
| 462 | input_proto: The input message. |
| 463 | output_proto: The output message. |
| 464 | config: The API call config. |
| 465 | """ |
| 466 | image_path = input_proto.image.path |
| 467 | board = input_proto.build_target.name |
| 468 | result_directory = input_proto.result.directory |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 469 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 470 | if not os.path.isfile(image_path) or not image_path.endswith(".bin"): |
| 471 | cros_build_lib.Die( |
| 472 | "The image.path must be an existing image file with a .bin extension." |
| 473 | ) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 474 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 475 | if config.validate_only: |
| 476 | return controller.RETURN_CODE_VALID_INPUT |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 477 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 478 | success = image.Test(board, result_directory, image_dir=image_path) |
| 479 | output_proto.success = success |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 480 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 481 | if success: |
| 482 | return controller.RETURN_CODE_SUCCESS |
| 483 | else: |
| 484 | return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 485 | |
| 486 | |
| 487 | @faux.empty_success |
| 488 | @faux.empty_completed_unsuccessfully_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 489 | @validate.require("gs_image_dir", "sysroot.build_target.name") |
| 490 | def PushImage( |
| 491 | input_proto: "image_pb2.PushImageRequest", |
| 492 | _output_proto: "image_pb2.PushImageResponse", |
| 493 | config: "api.config.ApiConfig", |
| 494 | ): |
| 495 | """Push artifacts from the archive bucket to the release bucket. |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 496 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 497 | Wraps chromite/scripts/pushimage.py. |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 498 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 499 | Args: |
| 500 | input_proto: Input proto. |
| 501 | _output_proto: Output proto. |
| 502 | config: The API call config. |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 503 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 504 | Returns: |
| 505 | A controller return code (e.g. controller.RETURN_CODE_SUCCESS). |
| 506 | """ |
| 507 | sign_types = [] |
| 508 | if input_proto.sign_types: |
| 509 | for sign_type in input_proto.sign_types: |
| 510 | if sign_type not in SUPPORTED_IMAGE_TYPES: |
| 511 | logging.error("unsupported sign type %g", sign_type) |
| 512 | return controller.RETURN_CODE_INVALID_INPUT |
| 513 | sign_types.append(SUPPORTED_IMAGE_TYPES[sign_type]) |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 514 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 515 | # If configured for validation only we're done here. |
| 516 | if config.validate_only: |
| 517 | return controller.RETURN_CODE_VALID_INPUT |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 518 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 519 | kwargs = {} |
| 520 | if input_proto.profile.name: |
| 521 | kwargs["profile"] = input_proto.profile.name |
| 522 | if input_proto.dest_bucket: |
| 523 | kwargs["dest_bucket"] = input_proto.dest_bucket |
| 524 | if input_proto.channels: |
| 525 | kwargs["force_channels"] = [ |
| 526 | common_pb2.Channel.Name(channel).lower()[len("channel_") :] |
| 527 | for channel in input_proto.channels |
| 528 | ] |
| 529 | try: |
| 530 | channel_to_uris = pushimage.PushImage( |
| 531 | input_proto.gs_image_dir, |
| 532 | input_proto.sysroot.build_target.name, |
| 533 | dry_run=input_proto.dryrun, |
| 534 | sign_types=sign_types, |
| 535 | **kwargs, |
| 536 | ) |
| 537 | except Exception: |
| 538 | logging.error("PushImage failed: ", exc_info=True) |
| 539 | return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY |
| 540 | if channel_to_uris: |
| 541 | for uris in channel_to_uris.values(): |
| 542 | for uri in uris: |
| 543 | _output_proto.instructions.add().instructions_file_path = uri |
| 544 | return controller.RETURN_CODE_SUCCESS |