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 |
Mike Frysinger | ef94e4c | 2020-02-10 23:59:54 -0500 | [diff] [blame] | 14 | import sys |
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 |
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 |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 24 | from chromite.service import image |
Will Bradley | 9bc8545 | 2019-10-10 10:48:21 -0600 | [diff] [blame] | 25 | from chromite.utils import metrics |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 26 | |
Mike Frysinger | ef94e4c | 2020-02-10 23:59:54 -0500 | [diff] [blame] | 27 | |
| 28 | assert sys.version_info >= (3, 6), 'This module requires Python 3.6+' |
| 29 | |
| 30 | |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 31 | # The image.proto ImageType enum ids. |
David Burger | b171d65 | 2019-05-13 16:07:00 -0600 | [diff] [blame] | 32 | _BASE_ID = common_pb2.BASE |
| 33 | _DEV_ID = common_pb2.DEV |
| 34 | _TEST_ID = common_pb2.TEST |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 35 | _BASE_VM_ID = common_pb2.BASE_VM |
| 36 | _TEST_VM_ID = common_pb2.TEST_VM |
Michael Mortensen | eefe895 | 2019-08-12 15:37:15 -0600 | [diff] [blame] | 37 | _RECOVERY_ID = common_pb2.RECOVERY |
| 38 | _FACTORY_ID = common_pb2.FACTORY |
| 39 | _FIRMWARE_ID = common_pb2.FIRMWARE |
Trent Begin | 008cade | 2019-10-31 13:40:59 -0600 | [diff] [blame] | 40 | _BASE_GUEST_VM_ID = common_pb2.BASE_GUEST_VM |
| 41 | _TEST_GUEST_VM_ID = common_pb2.TEST_GUEST_VM |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 42 | |
| 43 | # Dict to allow easily translating names to enum ids and vice versa. |
| 44 | _IMAGE_MAPPING = { |
| 45 | _BASE_ID: constants.IMAGE_TYPE_BASE, |
| 46 | constants.IMAGE_TYPE_BASE: _BASE_ID, |
| 47 | _DEV_ID: constants.IMAGE_TYPE_DEV, |
| 48 | constants.IMAGE_TYPE_DEV: _DEV_ID, |
| 49 | _TEST_ID: constants.IMAGE_TYPE_TEST, |
| 50 | constants.IMAGE_TYPE_TEST: _TEST_ID, |
Michael Mortensen | eefe895 | 2019-08-12 15:37:15 -0600 | [diff] [blame] | 51 | _RECOVERY_ID: constants.IMAGE_TYPE_RECOVERY, |
| 52 | constants.IMAGE_TYPE_RECOVERY: _RECOVERY_ID, |
| 53 | _FACTORY_ID: constants.IMAGE_TYPE_FACTORY, |
| 54 | constants.IMAGE_TYPE_FACTORY: _FACTORY_ID, |
| 55 | _FIRMWARE_ID: constants.IMAGE_TYPE_FIRMWARE, |
| 56 | constants.IMAGE_TYPE_FIRMWARE: _FIRMWARE_ID, |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 59 | _VM_IMAGE_MAPPING = { |
| 60 | _BASE_VM_ID: _IMAGE_MAPPING[_BASE_ID], |
| 61 | _TEST_VM_ID: _IMAGE_MAPPING[_TEST_ID], |
Trent Begin | 008cade | 2019-10-31 13:40:59 -0600 | [diff] [blame] | 62 | _BASE_GUEST_VM_ID: _IMAGE_MAPPING[_BASE_ID], |
| 63 | _TEST_GUEST_VM_ID: _IMAGE_MAPPING[_TEST_ID], |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 64 | } |
| 65 | |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 66 | |
Michael Mortensen | 10146cf | 2019-11-19 19:59:22 -0700 | [diff] [blame] | 67 | def _CreateResponse(_input_proto, output_proto, _config): |
| 68 | """Set output_proto success field on a successful Create response.""" |
| 69 | output_proto.success = True |
| 70 | |
| 71 | |
| 72 | @faux.success(_CreateResponse) |
Michael Mortensen | 85d3840 | 2019-12-12 09:50:29 -0700 | [diff] [blame] | 73 | @faux.empty_completed_unsuccessfully_error |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 74 | @validate.require('build_target.name') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 75 | @validate.validation_complete |
Will Bradley | 9bc8545 | 2019-10-10 10:48:21 -0600 | [diff] [blame] | 76 | @metrics.collect_metrics |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 77 | def Create(input_proto, output_proto, _config): |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 78 | """Build an image. |
| 79 | |
| 80 | Args: |
| 81 | input_proto (image_pb2.CreateImageRequest): The input message. |
| 82 | output_proto (image_pb2.CreateImageResult): The output message. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 83 | _config (api_config.ApiConfig): The API call config. |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 84 | """ |
| 85 | board = input_proto.build_target.name |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 86 | |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 87 | # Build the base image if no images provided. |
| 88 | to_build = input_proto.image_types or [_BASE_ID] |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 89 | |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 90 | image_types, vm_types = _ParseImagesToCreate(to_build) |
| 91 | build_config = _ParseCreateBuildConfig(input_proto) |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 92 | |
| 93 | # Sorted isn't really necessary here, but it's much easier to test. |
Alex Klein | 1bcd988 | 2019-03-19 13:25:24 -0600 | [diff] [blame] | 94 | result = image.Build(board=board, images=sorted(list(image_types)), |
| 95 | config=build_config) |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 96 | |
Alex Klein | 1bcd988 | 2019-03-19 13:25:24 -0600 | [diff] [blame] | 97 | output_proto.success = result.success |
Will Bradley | 29a49c2 | 2019-10-21 11:50:08 -0600 | [diff] [blame] | 98 | |
Alex Klein | 1bcd988 | 2019-03-19 13:25:24 -0600 | [diff] [blame] | 99 | if result.success: |
| 100 | # Success -- we need to list out the images we built in the output. |
| 101 | _PopulateBuiltImages(board, image_types, output_proto) |
Will Bradley | 29a49c2 | 2019-10-21 11:50:08 -0600 | [diff] [blame] | 102 | |
| 103 | if vm_types: |
Trent Begin | 008cade | 2019-10-31 13:40:59 -0600 | [diff] [blame] | 104 | for vm_type in vm_types: |
| 105 | is_test = vm_type in [_TEST_VM_ID, _TEST_GUEST_VM_ID] |
| 106 | try: |
| 107 | if vm_type in [_BASE_GUEST_VM_ID, _TEST_GUEST_VM_ID]: |
| 108 | vm_path = image.CreateGuestVm(board, is_test=is_test) |
| 109 | else: |
Sean Abraham | 8754534 | 2020-06-03 14:24:19 -0600 | [diff] [blame] | 110 | vm_path = image.CreateVm( |
| 111 | board, disk_layout=build_config.disk_layout, is_test=is_test) |
Trent Begin | 008cade | 2019-10-31 13:40:59 -0600 | [diff] [blame] | 112 | except image.ImageToVmError as e: |
| 113 | cros_build_lib.Die(e) |
Will Bradley | 29a49c2 | 2019-10-21 11:50:08 -0600 | [diff] [blame] | 114 | |
Trent Begin | 008cade | 2019-10-31 13:40:59 -0600 | [diff] [blame] | 115 | new_image = output_proto.images.add() |
| 116 | new_image.path = vm_path |
| 117 | new_image.type = vm_type |
| 118 | new_image.build_target.name = board |
Will Bradley | 29a49c2 | 2019-10-21 11:50:08 -0600 | [diff] [blame] | 119 | |
| 120 | # Read metric events log and pipe them into output_proto.events. |
| 121 | deserialize_metrics_log(output_proto.events, prefix=board) |
| 122 | return controller.RETURN_CODE_SUCCESS |
| 123 | |
Alex Klein | 1bcd988 | 2019-03-19 13:25:24 -0600 | [diff] [blame] | 124 | else: |
Alex Klein | 2557b4f | 2019-07-11 14:34:00 -0600 | [diff] [blame] | 125 | # Failure, include all of the failed packages in the output when available. |
| 126 | if not result.failed_packages: |
| 127 | return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY |
| 128 | |
Alex Klein | 1bcd988 | 2019-03-19 13:25:24 -0600 | [diff] [blame] | 129 | for package in result.failed_packages: |
| 130 | current = output_proto.failed_packages.add() |
| 131 | current.category = package.category |
| 132 | current.package_name = package.package |
| 133 | if package.version: |
| 134 | current.version = package.version |
| 135 | |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 136 | return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE |
Alex Klein | 1bcd988 | 2019-03-19 13:25:24 -0600 | [diff] [blame] | 137 | |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 138 | |
| 139 | def _ParseImagesToCreate(to_build): |
| 140 | """Helper function to parse the image types to build. |
| 141 | |
| 142 | This function exists just to clean up the Create function. |
| 143 | |
| 144 | Args: |
| 145 | to_build (list[int]): The image type list. |
| 146 | |
| 147 | Returns: |
| 148 | (set, set): The image and vm types, respectively, that need to be built. |
| 149 | """ |
| 150 | image_types = set() |
| 151 | vm_types = set() |
| 152 | for current in to_build: |
| 153 | if current in _IMAGE_MAPPING: |
| 154 | image_types.add(_IMAGE_MAPPING[current]) |
| 155 | elif current in _VM_IMAGE_MAPPING: |
| 156 | vm_types.add(current) |
| 157 | # Make sure we build the image required to build the VM. |
| 158 | image_types.add(_VM_IMAGE_MAPPING[current]) |
| 159 | else: |
| 160 | # Not expected, but at least it will be obvious if this comes up. |
| 161 | cros_build_lib.Die( |
| 162 | "The service's known image types do not match those in image.proto. " |
| 163 | 'Unknown Enum ID: %s' % current) |
| 164 | |
Trent Begin | 008cade | 2019-10-31 13:40:59 -0600 | [diff] [blame] | 165 | # We can only build one type of these images at a time since image_to_vm.sh |
| 166 | # uses the default path if a name is not provided. |
| 167 | if vm_types.issuperset({_BASE_VM_ID, _TEST_VM_ID}): |
Alex Klein | 21b9502 | 2019-05-09 14:14:46 -0600 | [diff] [blame] | 168 | cros_build_lib.Die('Cannot create more than one VM.') |
| 169 | |
| 170 | return image_types, vm_types |
| 171 | |
| 172 | |
| 173 | def _ParseCreateBuildConfig(input_proto): |
| 174 | """Helper to parse the image build config for Create.""" |
| 175 | enable_rootfs_verification = not input_proto.disable_rootfs_verification |
| 176 | version = input_proto.version or None |
| 177 | disk_layout = input_proto.disk_layout or None |
| 178 | builder_path = input_proto.builder_path or None |
| 179 | return image.BuildConfig( |
| 180 | enable_rootfs_verification=enable_rootfs_verification, replace=True, |
| 181 | version=version, disk_layout=disk_layout, builder_path=builder_path, |
| 182 | ) |
| 183 | |
Alex Klein | 1bcd988 | 2019-03-19 13:25:24 -0600 | [diff] [blame] | 184 | |
| 185 | def _PopulateBuiltImages(board, image_types, output_proto): |
| 186 | """Helper to list out built images for Create.""" |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 187 | # Build out the ImageType->ImagePath mapping in the output. |
| 188 | # We're using the default path, so just fetch that, but read the symlink so |
| 189 | # the path we're returning is somewhat more permanent. |
| 190 | latest_link = image_lib.GetLatestImageLink(board) |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame] | 191 | base_path = os.path.realpath(latest_link) |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 192 | |
| 193 | for current in image_types: |
| 194 | type_id = _IMAGE_MAPPING[current] |
| 195 | path = os.path.join(base_path, constants.IMAGE_TYPE_TO_NAME[current]) |
| 196 | |
| 197 | new_image = output_proto.images.add() |
| 198 | new_image.path = path |
| 199 | new_image.type = type_id |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame] | 200 | new_image.build_target.name = board |
| 201 | |
| 202 | |
Michael Mortensen | 10146cf | 2019-11-19 19:59:22 -0700 | [diff] [blame] | 203 | def _SignerTestResponse(_input_proto, output_proto, _config): |
| 204 | """Set output_proto success field on a successful SignerTest response.""" |
| 205 | output_proto.success = True |
| 206 | return controller.RETURN_CODE_SUCCESS |
| 207 | |
| 208 | |
| 209 | @faux.success(_SignerTestResponse) |
Michael Mortensen | 85d3840 | 2019-12-12 09:50:29 -0700 | [diff] [blame] | 210 | @faux.empty_completed_unsuccessfully_error |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 211 | @validate.exists('image.path') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 212 | @validate.validation_complete |
| 213 | def SignerTest(input_proto, output_proto, _config): |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 214 | """Run image tests. |
| 215 | |
| 216 | Args: |
| 217 | input_proto (image_pb2.ImageTestRequest): The input message. |
| 218 | output_proto (image_pb2.ImageTestResult): The output message. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 219 | _config (api_config.ApiConfig): The API call config. |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 220 | """ |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 221 | image_path = input_proto.image.path |
| 222 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 223 | result = image_lib.SecurityTest(image=image_path) |
Michael Mortensen | c83c995 | 2019-08-05 12:15:12 -0600 | [diff] [blame] | 224 | output_proto.success = result |
| 225 | if result: |
| 226 | return controller.RETURN_CODE_SUCCESS |
| 227 | else: |
| 228 | return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY |
| 229 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 230 | |
Michael Mortensen | 10146cf | 2019-11-19 19:59:22 -0700 | [diff] [blame] | 231 | def _TestResponse(_input_proto, output_proto, _config): |
| 232 | """Set output_proto success field on a successful Test response.""" |
| 233 | output_proto.success = True |
| 234 | return controller.RETURN_CODE_SUCCESS |
| 235 | |
| 236 | |
| 237 | @faux.success(_TestResponse) |
Michael Mortensen | 85d3840 | 2019-12-12 09:50:29 -0700 | [diff] [blame] | 238 | @faux.empty_completed_unsuccessfully_error |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 239 | @validate.require('build_target.name', 'result.directory') |
| 240 | @validate.exists('image.path') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 241 | def Test(input_proto, output_proto, config): |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 242 | """Run image tests. |
| 243 | |
| 244 | Args: |
| 245 | input_proto (image_pb2.ImageTestRequest): The input message. |
| 246 | output_proto (image_pb2.ImageTestResult): The output message. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 247 | config (api_config.ApiConfig): The API call config. |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 248 | """ |
| 249 | image_path = input_proto.image.path |
| 250 | board = input_proto.build_target.name |
| 251 | result_directory = input_proto.result.directory |
| 252 | |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 253 | 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] | 254 | cros_build_lib.Die( |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 255 | 'The image.path must be an existing image file with a .bin extension.') |
| 256 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 257 | if config.validate_only: |
| 258 | return controller.RETURN_CODE_VALID_INPUT |
| 259 | |
Alex Klein | 8cb365a | 2019-05-15 16:24:53 -0600 | [diff] [blame] | 260 | success = image.Test(board, result_directory, image_dir=image_path) |
| 261 | output_proto.success = success |
| 262 | |
| 263 | if success: |
| 264 | return controller.RETURN_CODE_SUCCESS |
| 265 | else: |
| 266 | return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY |