Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | # Copyright 2018 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Image API Service. |
| 7 | |
| 8 | The image related API endpoints should generally be found here. |
| 9 | """ |
| 10 | |
| 11 | from __future__ import print_function |
| 12 | |
| 13 | import os |
| 14 | |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 15 | from chromite.api import controller |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 16 | from chromite.api import faux |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 17 | from chromite.api import validate |
Alex Klein | e9a7dbf | 2020-10-06 18:12:12 -0600 | [diff] [blame] | 18 | from chromite.api.controller import controller_util |
David Burger | b171d65 | 2019-05-13 16:07:00 -0600 | [diff] [blame] | 19 | from chromite.api.gen.chromiumos import common_pb2 |
Will Bradley | 9bc8545 | 2019-10-10 10:48:21 -0600 | [diff] [blame] | 20 | from chromite.api.metrics import deserialize_metrics_log |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame] | 21 | from chromite.lib import cros_build_lib |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 22 | from chromite.lib import constants |
| 23 | from chromite.lib import image_lib |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 24 | from chromite.lib import cros_logging as logging |
| 25 | from chromite.scripts import pushimage |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 26 | from chromite.service import image |
Will Bradley | 9bc8545 | 2019-10-10 10:48:21 -0600 | [diff] [blame] | 27 | from chromite.utils import metrics |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 28 | |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 29 | # The image.proto ImageType enum ids. |
George Engelbrecht | c55d631 | 2021-05-05 12:11:13 -0600 | [diff] [blame^] | 30 | _BASE_ID = common_pb2.IMAGE_TYPE_BASE |
| 31 | _DEV_ID = common_pb2.IMAGE_TYPE_DEV |
| 32 | _TEST_ID = common_pb2.IMAGE_TYPE_TEST |
| 33 | _BASE_VM_ID = common_pb2.IMAGE_TYPE_BASE_VM |
| 34 | _TEST_VM_ID = common_pb2.IMAGE_TYPE_TEST_VM |
| 35 | _RECOVERY_ID = common_pb2.IMAGE_TYPE_RECOVERY |
| 36 | _FACTORY_ID = common_pb2.IMAGE_TYPE_FACTORY |
| 37 | _FIRMWARE_ID = common_pb2.IMAGE_TYPE_FIRMWARE |
| 38 | _BASE_GUEST_VM_ID = common_pb2.IMAGE_TYPE_BASE_GUEST_VM |
| 39 | _TEST_GUEST_VM_ID = common_pb2.IMAGE_TYPE_TEST_GUEST_VM |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 40 | |
| 41 | # Dict to allow easily translating names to enum ids and vice versa. |
| 42 | _IMAGE_MAPPING = { |
| 43 | _BASE_ID: constants.IMAGE_TYPE_BASE, |
| 44 | constants.IMAGE_TYPE_BASE: _BASE_ID, |
| 45 | _DEV_ID: constants.IMAGE_TYPE_DEV, |
| 46 | constants.IMAGE_TYPE_DEV: _DEV_ID, |
| 47 | _TEST_ID: constants.IMAGE_TYPE_TEST, |
| 48 | constants.IMAGE_TYPE_TEST: _TEST_ID, |
Michael Mortensen | eefe895 | 2019-08-12 15:37:15 -0600 | [diff] [blame] | 49 | _RECOVERY_ID: constants.IMAGE_TYPE_RECOVERY, |
| 50 | constants.IMAGE_TYPE_RECOVERY: _RECOVERY_ID, |
| 51 | _FACTORY_ID: constants.IMAGE_TYPE_FACTORY, |
| 52 | constants.IMAGE_TYPE_FACTORY: _FACTORY_ID, |
| 53 | _FIRMWARE_ID: constants.IMAGE_TYPE_FIRMWARE, |
| 54 | constants.IMAGE_TYPE_FIRMWARE: _FIRMWARE_ID, |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 55 | } |
| 56 | |
George Engelbrecht | 9f4f832 | 2021-03-08 12:04:17 -0700 | [diff] [blame] | 57 | # Dict to describe the prerequisite built images for each VM image type. |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 58 | _VM_IMAGE_MAPPING = { |
| 59 | _BASE_VM_ID: _IMAGE_MAPPING[_BASE_ID], |
| 60 | _TEST_VM_ID: _IMAGE_MAPPING[_TEST_ID], |
Trent Begin | 008cade | 2019-10-31 13:40:59 -0600 | [diff] [blame] | 61 | _BASE_GUEST_VM_ID: _IMAGE_MAPPING[_BASE_ID], |
| 62 | _TEST_GUEST_VM_ID: _IMAGE_MAPPING[_TEST_ID], |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 63 | } |
| 64 | |
George Engelbrecht | 9f4f832 | 2021-03-08 12:04:17 -0700 | [diff] [blame] | 65 | # Dict to describe the prerequisite built images for each mod image type. |
| 66 | _MOD_IMAGE_MAPPING = { |
| 67 | _RECOVERY_ID: _IMAGE_MAPPING[_BASE_ID], |
| 68 | } |
| 69 | |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 70 | # Supported image types for PushImage. |
| 71 | SUPPORTED_IMAGE_TYPES = { |
| 72 | common_pb2.IMAGE_TYPE_RECOVERY: constants.IMAGE_TYPE_RECOVERY, |
| 73 | common_pb2.IMAGE_TYPE_FACTORY: constants.IMAGE_TYPE_FACTORY, |
| 74 | common_pb2.IMAGE_TYPE_FIRMWARE: constants.IMAGE_TYPE_FIRMWARE, |
| 75 | common_pb2.IMAGE_TYPE_ACCESSORY_USBPD: constants.IMAGE_TYPE_ACCESSORY_USBPD, |
| 76 | common_pb2.IMAGE_TYPE_ACCESSORY_RWSIG: constants.IMAGE_TYPE_ACCESSORY_RWSIG, |
| 77 | common_pb2.IMAGE_TYPE_BASE: constants.IMAGE_TYPE_BASE, |
George Engelbrecht | 9f4f832 | 2021-03-08 12:04:17 -0700 | [diff] [blame] | 78 | common_pb2.IMAGE_TYPE_GSC_FIRMWARE: constants.IMAGE_TYPE_GSC_FIRMWARE, |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 81 | |
George Engelbrecht | 9f4f832 | 2021-03-08 12:04:17 -0700 | [diff] [blame] | 82 | def _add_image_to_proto(output_proto, path, image_type, board): |
| 83 | """Quick helper function to add a new image to the output proto.""" |
| 84 | new_image = output_proto.images.add() |
| 85 | new_image.path = path |
| 86 | new_image.type = image_type |
| 87 | new_image.build_target.name = board |
| 88 | |
| 89 | |
Michael Mortensen | 10146cf | 2019-11-19 19:59:22 -0700 | [diff] [blame] | 90 | def _CreateResponse(_input_proto, output_proto, _config): |
| 91 | """Set output_proto success field on a successful Create response.""" |
| 92 | output_proto.success = True |
| 93 | |
| 94 | |
| 95 | @faux.success(_CreateResponse) |
Michael Mortensen | 85d3840 | 2019-12-12 09:50:29 -0700 | [diff] [blame] | 96 | @faux.empty_completed_unsuccessfully_error |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 97 | @validate.require('build_target.name') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 98 | @validate.validation_complete |
Will Bradley | 9bc8545 | 2019-10-10 10:48:21 -0600 | [diff] [blame] | 99 | @metrics.collect_metrics |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 100 | def Create(input_proto, output_proto, _config): |
George Engelbrecht | 9f4f832 | 2021-03-08 12:04:17 -0700 | [diff] [blame] | 101 | """Build images. |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 102 | |
| 103 | Args: |
| 104 | input_proto (image_pb2.CreateImageRequest): The input message. |
| 105 | output_proto (image_pb2.CreateImageResult): The output message. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 106 | _config (api_config.ApiConfig): The API call config. |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 107 | """ |
| 108 | board = input_proto.build_target.name |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 109 | |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 110 | # Build the base image if no images provided. |
| 111 | to_build = input_proto.image_types or [_BASE_ID] |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 112 | |
George Engelbrecht | 9f4f832 | 2021-03-08 12:04:17 -0700 | [diff] [blame] | 113 | image_types, vm_types, mod_image_types = _ParseImagesToCreate(to_build) |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 114 | build_config = _ParseCreateBuildConfig(input_proto) |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 115 | |
| 116 | # Sorted isn't really necessary here, but it's much easier to test. |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 117 | result = image.Build( |
| 118 | board=board, images=sorted(list(image_types)), config=build_config) |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 119 | |
Alex Klein | 1bcd988 | 2019-03-19 13:25:24 -0600 | [diff] [blame] | 120 | output_proto.success = result.success |
Will Bradley | 29a49c2 | 2019-10-21 11:50:08 -0600 | [diff] [blame] | 121 | |
Alex Klein | 1bcd988 | 2019-03-19 13:25:24 -0600 | [diff] [blame] | 122 | if result.success: |
| 123 | # Success -- we need to list out the images we built in the output. |
| 124 | _PopulateBuiltImages(board, image_types, output_proto) |
Will Bradley | 29a49c2 | 2019-10-21 11:50:08 -0600 | [diff] [blame] | 125 | |
George Engelbrecht | 9f4f832 | 2021-03-08 12:04:17 -0700 | [diff] [blame] | 126 | for vm_type in vm_types: |
| 127 | is_test = vm_type in [_TEST_VM_ID, _TEST_GUEST_VM_ID] |
| 128 | try: |
| 129 | if vm_type in [_BASE_GUEST_VM_ID, _TEST_GUEST_VM_ID]: |
| 130 | vm_path = image.CreateGuestVm(board, is_test=is_test) |
| 131 | else: |
| 132 | vm_path = image.CreateVm( |
| 133 | board, disk_layout=build_config.disk_layout, is_test=is_test) |
| 134 | except image.ImageToVmError as e: |
| 135 | cros_build_lib.Die(e) |
Will Bradley | 29a49c2 | 2019-10-21 11:50:08 -0600 | [diff] [blame] | 136 | |
George Engelbrecht | 9f4f832 | 2021-03-08 12:04:17 -0700 | [diff] [blame] | 137 | _add_image_to_proto(output_proto, vm_path, vm_type, board) |
| 138 | |
| 139 | for mod_type in mod_image_types: |
| 140 | if mod_type == _RECOVERY_ID: |
| 141 | base_image_path = _GetBaseImagePath(output_proto) |
| 142 | result = image.BuildRecoveryImage(board=board, |
| 143 | image_path=base_image_path) |
| 144 | if result.success: |
| 145 | _PopulateBuiltImages(board, [_IMAGE_MAPPING[mod_type]], output_proto) |
| 146 | else: |
| 147 | cros_build_lib.Die('Failed to create recovery image.') |
| 148 | else: |
| 149 | cros_build_lib.Die('_RECOVERY_ID is the only mod_image_type.') |
Will Bradley | 29a49c2 | 2019-10-21 11:50:08 -0600 | [diff] [blame] | 150 | |
| 151 | # Read metric events log and pipe them into output_proto.events. |
| 152 | deserialize_metrics_log(output_proto.events, prefix=board) |
| 153 | return controller.RETURN_CODE_SUCCESS |
| 154 | |
Alex Klein | 1bcd988 | 2019-03-19 13:25:24 -0600 | [diff] [blame] | 155 | else: |
Alex Klein | 2557b4f | 2019-07-11 14:34:00 -0600 | [diff] [blame] | 156 | # Failure, include all of the failed packages in the output when available. |
| 157 | if not result.failed_packages: |
| 158 | return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY |
| 159 | |
Alex Klein | 1bcd988 | 2019-03-19 13:25:24 -0600 | [diff] [blame] | 160 | for package in result.failed_packages: |
| 161 | current = output_proto.failed_packages.add() |
Alex Klein | e9a7dbf | 2020-10-06 18:12:12 -0600 | [diff] [blame] | 162 | controller_util.serialize_package_info(package, current) |
Alex Klein | 1bcd988 | 2019-03-19 13:25:24 -0600 | [diff] [blame] | 163 | |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 164 | return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE |
Alex Klein | 1bcd988 | 2019-03-19 13:25:24 -0600 | [diff] [blame] | 165 | |
George Engelbrecht | 9f4f832 | 2021-03-08 12:04:17 -0700 | [diff] [blame] | 166 | def _GetBaseImagePath(output_proto): |
| 167 | """From image_pb2.CreateImageResult, return base image path or None.""" |
| 168 | ret = None |
| 169 | for i in output_proto.images: |
| 170 | if i.type == _BASE_ID: |
| 171 | ret = i.path |
| 172 | return ret |
| 173 | |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 174 | |
| 175 | def _ParseImagesToCreate(to_build): |
| 176 | """Helper function to parse the image types to build. |
| 177 | |
George Engelbrecht | 9f4f832 | 2021-03-08 12:04:17 -0700 | [diff] [blame] | 178 | This function expresses the dependencies of each image type and adds |
| 179 | the requisite image types if they're not explicitly defined. |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 180 | |
| 181 | Args: |
| 182 | to_build (list[int]): The image type list. |
| 183 | |
| 184 | Returns: |
George Engelbrecht | 9f4f832 | 2021-03-08 12:04:17 -0700 | [diff] [blame] | 185 | (set, set, set): The image, vm, and mod_image types that need to be built. |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 186 | """ |
| 187 | image_types = set() |
| 188 | vm_types = set() |
George Engelbrecht | 9f4f832 | 2021-03-08 12:04:17 -0700 | [diff] [blame] | 189 | mod_image_types = set() |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 190 | for current in to_build: |
George Engelbrecht | 9f4f832 | 2021-03-08 12:04:17 -0700 | [diff] [blame] | 191 | # Find out if it's a special case (vm, img mod), or just any old image. |
| 192 | if current in _VM_IMAGE_MAPPING: |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 193 | vm_types.add(current) |
| 194 | # Make sure we build the image required to build the VM. |
| 195 | image_types.add(_VM_IMAGE_MAPPING[current]) |
George Engelbrecht | 9f4f832 | 2021-03-08 12:04:17 -0700 | [diff] [blame] | 196 | elif current in _MOD_IMAGE_MAPPING: |
| 197 | mod_image_types.add(current) |
| 198 | image_types.add(_MOD_IMAGE_MAPPING[current]) |
| 199 | elif current in _IMAGE_MAPPING: |
| 200 | image_types.add(_IMAGE_MAPPING[current]) |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 201 | else: |
| 202 | # Not expected, but at least it will be obvious if this comes up. |
| 203 | cros_build_lib.Die( |
| 204 | "The service's known image types do not match those in image.proto. " |
| 205 | 'Unknown Enum ID: %s' % current) |
| 206 | |
Trent Begin | 008cade | 2019-10-31 13:40:59 -0600 | [diff] [blame] | 207 | # We can only build one type of these images at a time since image_to_vm.sh |
| 208 | # uses the default path if a name is not provided. |
| 209 | if vm_types.issuperset({_BASE_VM_ID, _TEST_VM_ID}): |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 210 | cros_build_lib.Die('Cannot create more than one VM.') |
| 211 | |
George Engelbrecht | 9f4f832 | 2021-03-08 12:04:17 -0700 | [diff] [blame] | 212 | return image_types, vm_types, mod_image_types |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 213 | |
| 214 | |
| 215 | def _ParseCreateBuildConfig(input_proto): |
| 216 | """Helper to parse the image build config for Create.""" |
| 217 | enable_rootfs_verification = not input_proto.disable_rootfs_verification |
| 218 | version = input_proto.version or None |
| 219 | disk_layout = input_proto.disk_layout or None |
| 220 | builder_path = input_proto.builder_path or None |
| 221 | return image.BuildConfig( |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 222 | enable_rootfs_verification=enable_rootfs_verification, |
| 223 | replace=True, |
| 224 | version=version, |
| 225 | disk_layout=disk_layout, |
| 226 | builder_path=builder_path, |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 227 | ) |
| 228 | |
Alex Klein | 1bcd988 | 2019-03-19 13:25:24 -0600 | [diff] [blame] | 229 | |
| 230 | def _PopulateBuiltImages(board, image_types, output_proto): |
| 231 | """Helper to list out built images for Create.""" |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 232 | # Build out the ImageType->ImagePath mapping in the output. |
| 233 | # We're using the default path, so just fetch that, but read the symlink so |
| 234 | # the path we're returning is somewhat more permanent. |
| 235 | latest_link = image_lib.GetLatestImageLink(board) |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame] | 236 | base_path = os.path.realpath(latest_link) |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 237 | |
| 238 | for current in image_types: |
| 239 | type_id = _IMAGE_MAPPING[current] |
| 240 | path = os.path.join(base_path, constants.IMAGE_TYPE_TO_NAME[current]) |
George Engelbrecht | 9f4f832 | 2021-03-08 12:04:17 -0700 | [diff] [blame] | 241 | _add_image_to_proto(output_proto, path, type_id, board) |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame] | 242 | |
| 243 | |
Michael Mortensen | 10146cf | 2019-11-19 19:59:22 -0700 | [diff] [blame] | 244 | def _SignerTestResponse(_input_proto, output_proto, _config): |
| 245 | """Set output_proto success field on a successful SignerTest response.""" |
| 246 | output_proto.success = True |
| 247 | return controller.RETURN_CODE_SUCCESS |
| 248 | |
| 249 | |
| 250 | @faux.success(_SignerTestResponse) |
Michael Mortensen | 85d3840 | 2019-12-12 09:50:29 -0700 | [diff] [blame] | 251 | @faux.empty_completed_unsuccessfully_error |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 252 | @validate.exists('image.path') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 253 | @validate.validation_complete |
| 254 | def SignerTest(input_proto, output_proto, _config): |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 255 | """Run image tests. |
| 256 | |
| 257 | Args: |
| 258 | input_proto (image_pb2.ImageTestRequest): The input message. |
| 259 | output_proto (image_pb2.ImageTestResult): The output message. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 260 | _config (api_config.ApiConfig): The API call config. |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 261 | """ |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 262 | image_path = input_proto.image.path |
| 263 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 264 | result = image_lib.SecurityTest(image=image_path) |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 265 | output_proto.success = result |
| 266 | if result: |
| 267 | return controller.RETURN_CODE_SUCCESS |
| 268 | else: |
| 269 | return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY |
| 270 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 271 | |
Michael Mortensen | 10146cf | 2019-11-19 19:59:22 -0700 | [diff] [blame] | 272 | def _TestResponse(_input_proto, output_proto, _config): |
| 273 | """Set output_proto success field on a successful Test response.""" |
| 274 | output_proto.success = True |
| 275 | return controller.RETURN_CODE_SUCCESS |
| 276 | |
| 277 | |
| 278 | @faux.success(_TestResponse) |
Michael Mortensen | 85d3840 | 2019-12-12 09:50:29 -0700 | [diff] [blame] | 279 | @faux.empty_completed_unsuccessfully_error |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 280 | @validate.require('build_target.name', 'result.directory') |
| 281 | @validate.exists('image.path') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 282 | def Test(input_proto, output_proto, config): |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 283 | """Run image tests. |
| 284 | |
| 285 | Args: |
| 286 | input_proto (image_pb2.ImageTestRequest): The input message. |
| 287 | output_proto (image_pb2.ImageTestResult): The output message. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 288 | config (api_config.ApiConfig): The API call config. |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 289 | """ |
| 290 | image_path = input_proto.image.path |
| 291 | board = input_proto.build_target.name |
| 292 | result_directory = input_proto.result.directory |
| 293 | |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 294 | 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] | 295 | cros_build_lib.Die( |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 296 | 'The image.path must be an existing image file with a .bin extension.') |
| 297 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 298 | if config.validate_only: |
| 299 | return controller.RETURN_CODE_VALID_INPUT |
| 300 | |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 301 | success = image.Test(board, result_directory, image_dir=image_path) |
| 302 | output_proto.success = success |
| 303 | |
| 304 | if success: |
| 305 | return controller.RETURN_CODE_SUCCESS |
| 306 | else: |
| 307 | return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 308 | |
| 309 | |
| 310 | @faux.empty_success |
| 311 | @faux.empty_completed_unsuccessfully_error |
| 312 | @validate.require('gs_image_dir', 'sysroot.build_target.name') |
| 313 | def PushImage(input_proto, _output_proto, config): |
| 314 | """Push artifacts from the archive bucket to the release bucket. |
| 315 | |
| 316 | Wraps chromite/scripts/pushimage.py. |
| 317 | |
| 318 | Args: |
| 319 | input_proto (PushImageRequest): Input proto. |
| 320 | _output_proto (PushImageResponse): Output proto. |
| 321 | config (api.config.ApiConfig): The API call config. |
| 322 | |
| 323 | Returns: |
| 324 | A controller return code (e.g. controller.RETURN_CODE_SUCCESS). |
| 325 | """ |
| 326 | sign_types = [] |
| 327 | if input_proto.sign_types: |
| 328 | for sign_type in input_proto.sign_types: |
| 329 | if sign_type not in SUPPORTED_IMAGE_TYPES: |
| 330 | logging.error('unsupported sign type %g', sign_type) |
| 331 | return controller.RETURN_CODE_INVALID_INPUT |
| 332 | sign_types.append(SUPPORTED_IMAGE_TYPES[sign_type]) |
| 333 | |
| 334 | # If configured for validation only we're done here. |
| 335 | if config.validate_only: |
| 336 | return controller.RETURN_CODE_VALID_INPUT |
| 337 | |
Jack Neus | 485a9d2 | 2020-12-21 03:15:15 +0000 | [diff] [blame] | 338 | kwargs = {} |
| 339 | if input_proto.profile.name: |
| 340 | kwargs['profile'] = input_proto.profile.name |
| 341 | if input_proto.dest_bucket: |
| 342 | kwargs['dest_bucket'] = input_proto.dest_bucket |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 343 | try: |
| 344 | pushimage.PushImage( |
| 345 | input_proto.gs_image_dir, |
| 346 | input_proto.sysroot.build_target.name, |
| 347 | dry_run=input_proto.dryrun, |
Jack Neus | 485a9d2 | 2020-12-21 03:15:15 +0000 | [diff] [blame] | 348 | sign_types=sign_types, |
| 349 | **kwargs) |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 350 | return controller.RETURN_CODE_SUCCESS |
| 351 | except Exception: |
Jack Neus | e3150a4 | 2021-01-29 17:16:36 +0000 | [diff] [blame] | 352 | logging.error('PushImage failed: ', exc_info=True) |
Jack Neus | 761e184 | 2020-12-01 18:20:11 +0000 | [diff] [blame] | 353 | return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY |