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