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