blob: ab66f1172ff0a9c132adda47a2171a7edd10c99e [file] [log] [blame]
Alex Klein2966e302019-01-17 13:29:38 -07001# 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
7The image related API endpoints should generally be found here.
8"""
9
Alex Klein27978a42021-07-27 14:18:10 -060010import copy
Jack Neus5e56fef2021-06-18 16:57:28 +000011import functools
Chris McDonald1672ddb2021-07-21 11:48:23 -060012import logging
Alex Klein2966e302019-01-17 13:29:38 -070013import os
Alex Kleinf5dc2632021-08-31 16:35:06 -060014from typing import List, NamedTuple, Set, TYPE_CHECKING, Union
Alex Klein2966e302019-01-17 13:29:38 -070015
Alex Klein8cb365a2019-05-15 16:24:53 -060016from chromite.api import controller
Alex Klein076841b2019-08-29 15:19:39 -060017from chromite.api import faux
Alex Klein2b236722019-06-19 15:44:26 -060018from chromite.api import validate
Alex Kleine9a7dbf2020-10-06 18:12:12 -060019from chromite.api.controller import controller_util
David Burgerb171d652019-05-13 16:07:00 -060020from chromite.api.gen.chromiumos import common_pb2
Will Bradley9bc85452019-10-10 10:48:21 -060021from chromite.api.metrics import deserialize_metrics_log
George Engelbrechtc9a8e812021-06-16 18:14:17 -060022from chromite.lib import build_target_lib
23from chromite.lib import chroot_lib
Alex Klein56355682019-02-07 10:36:54 -070024from chromite.lib import constants
Chris McDonald1672ddb2021-07-21 11:48:23 -060025from chromite.lib import cros_build_lib
Alex Klein56355682019-02-07 10:36:54 -070026from chromite.lib import image_lib
George Engelbrechtc9a8e812021-06-16 18:14:17 -060027from chromite.lib import sysroot_lib
Jack Neus761e1842020-12-01 18:20:11 +000028from chromite.scripts import pushimage
Alex Kleinb7cdbe62019-02-22 11:41:32 -070029from chromite.service import image
Will Bradley9bc85452019-10-10 10:48:21 -060030from chromite.utils import metrics
Alex Klein2966e302019-01-17 13:29:38 -070031
Alex Kleinf5dc2632021-08-31 16:35:06 -060032if TYPE_CHECKING:
33 from pathlib import Path
34
Alex Klein56355682019-02-07 10:36:54 -070035# The image.proto ImageType enum ids.
George Engelbrechtc55d6312021-05-05 12:11:13 -060036_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 Klein56355682019-02-07 10:36:54 -070046
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 Mortenseneefe8952019-08-12 15:37:15 -060055 _RECOVERY_ID: constants.IMAGE_TYPE_RECOVERY,
56 constants.IMAGE_TYPE_RECOVERY: _RECOVERY_ID,
Alex Klein9039a952021-07-27 13:52:39 -060057 _FACTORY_ID: constants.IMAGE_TYPE_FACTORY_SHIM,
58 constants.IMAGE_TYPE_FACTORY_SHIM: _FACTORY_ID,
Michael Mortenseneefe8952019-08-12 15:37:15 -060059 _FIRMWARE_ID: constants.IMAGE_TYPE_FIRMWARE,
60 constants.IMAGE_TYPE_FIRMWARE: _FIRMWARE_ID,
Alex Klein56355682019-02-07 10:36:54 -070061}
62
George Engelbrecht9f4f8322021-03-08 12:04:17 -070063# Dict to describe the prerequisite built images for each VM image type.
Alex Klein21b95022019-05-09 14:14:46 -060064_VM_IMAGE_MAPPING = {
65 _BASE_VM_ID: _IMAGE_MAPPING[_BASE_ID],
66 _TEST_VM_ID: _IMAGE_MAPPING[_TEST_ID],
Trent Begin008cade2019-10-31 13:40:59 -060067 _BASE_GUEST_VM_ID: _IMAGE_MAPPING[_BASE_ID],
68 _TEST_GUEST_VM_ID: _IMAGE_MAPPING[_TEST_ID],
Alex Klein21b95022019-05-09 14:14:46 -060069}
70
George Engelbrecht9f4f8322021-03-08 12:04:17 -070071# 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 Neus761e1842020-12-01 18:20:11 +000076# Supported image types for PushImage.
77SUPPORTED_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 Engelbrecht9f4f8322021-03-08 12:04:17 -070084 common_pb2.IMAGE_TYPE_GSC_FIRMWARE: constants.IMAGE_TYPE_GSC_FIRMWARE,
Jack Neus761e1842020-12-01 18:20:11 +000085}
86
Alex Klein27978a42021-07-27 14:18:10 -060087# 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.
93LOCATION_CORE = 'latest'
94# The factory_install image.
95LOCATION_FACTORY = 'factory_shim'
96
97
98class 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 Klein56355682019-02-07 10:36:54 -0700119
Alex Kleinf5dc2632021-08-31 16:35:06 -0600120def _add_image_to_proto(output_proto, path: Union['Path', str], image_type: int,
121 board: str):
George Engelbrecht9f4f8322021-03-08 12:04:17 -0700122 """Quick helper function to add a new image to the output proto."""
123 new_image = output_proto.images.add()
Alex Kleinf5dc2632021-08-31 16:35:06 -0600124 new_image.path = str(path)
George Engelbrecht9f4f8322021-03-08 12:04:17 -0700125 new_image.type = image_type
126 new_image.build_target.name = board
127
128
George Engelbrechtc9a8e812021-06-16 18:14:17 -0600129def 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
143def GetArtifacts(in_proto: common_pb2.ArtifactsByService.Image,
Alex Kleincaace392021-07-26 14:28:13 -0600144 chroot: chroot_lib.Chroot, sysroot_class: sysroot_lib.Sysroot,
145 build_target: build_target_lib.BuildTarget,
146 output_dir) -> list:
George Engelbrechtc9a8e812021-06-16 18:14:17 -0600147 """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 Shih8d9c8e42021-06-30 03:27:28 +0000165 base_path = chroot.full_path(sysroot_class.path)
Jack Neus5e56fef2021-06-18 16:57:28 +0000166 board = build_target.name
167
168 generated = []
169 dlc_func = functools.partial(image.copy_dlc_image, base_path)
Alex Klein27978a42021-07-27 14:18:10 -0600170 license_func = functools.partial(
171 image.copy_license_credits, board, symlink=LOCATION_CORE)
Jack Neus5e56fef2021-06-18 16:57:28 +0000172 artifact_types = {
Alex Kleincaace392021-07-26 14:28:13 -0600173 in_proto.ArtifactType.DLC_IMAGE: dlc_func,
174 in_proto.ArtifactType.LICENSE_CREDITS: license_func,
Jack Neus5e56fef2021-06-18 16:57:28 +0000175 }
George Engelbrechtc9a8e812021-06-16 18:14:17 -0600176
177 for output_artifact in in_proto.output_artifacts:
Jack Neus5e56fef2021-06-18 16:57:28 +0000178 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 Engelbrechtc9a8e812021-06-16 18:14:17 -0600186
Jack Neus5e56fef2021-06-18 16:57:28 +0000187 return generated
Pi-Hsun Shih8d9c8e42021-06-30 03:27:28 +0000188
Alex Kleincaace392021-07-26 14:28:13 -0600189
Michael Mortensen10146cf2019-11-19 19:59:22 -0700190def _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 Mortensen85d38402019-12-12 09:50:29 -0700196@faux.empty_completed_unsuccessfully_error
Alex Klein2b236722019-06-19 15:44:26 -0600197@validate.require('build_target.name')
Alex Klein231d2da2019-07-22 16:44:45 -0600198@validate.validation_complete
Will Bradley9bc85452019-10-10 10:48:21 -0600199@metrics.collect_metrics
Alex Klein231d2da2019-07-22 16:44:45 -0600200def Create(input_proto, output_proto, _config):
George Engelbrecht9f4f8322021-03-08 12:04:17 -0700201 """Build images.
Alex Klein56355682019-02-07 10:36:54 -0700202
203 Args:
204 input_proto (image_pb2.CreateImageRequest): The input message.
205 output_proto (image_pb2.CreateImageResult): The output message.
Alex Klein231d2da2019-07-22 16:44:45 -0600206 _config (api_config.ApiConfig): The API call config.
Alex Klein56355682019-02-07 10:36:54 -0700207 """
208 board = input_proto.build_target.name
Alex Klein56355682019-02-07 10:36:54 -0700209
Alex Klein56355682019-02-07 10:36:54 -0700210 # Build the base image if no images provided.
211 to_build = input_proto.image_types or [_BASE_ID]
Alex Klein56355682019-02-07 10:36:54 -0700212
Alex Klein27978a42021-07-27 14:18:10 -0600213 image_types = _ParseImagesToCreate(to_build)
Alex Klein21b95022019-05-09 14:14:46 -0600214 build_config = _ParseCreateBuildConfig(input_proto)
Alex Klein27978a42021-07-27 14:18:10 -0600215 factory_build_config = copy.copy(build_config)
216 build_config.symlink = LOCATION_CORE
217 factory_build_config.symlink = LOCATION_FACTORY
Alex Klein56355682019-02-07 10:36:54 -0700218
Alex Klein27978a42021-07-27 14:18:10 -0600219 # Try building the core and factory images.
Alex Klein56355682019-02-07 10:36:54 -0700220 # Sorted isn't really necessary here, but it's much easier to test.
Alex Klein27978a42021-07-27 14:18:10 -0600221 core_result = image.Build(
222 board, sorted(image_types.core_images), config=build_config)
223 logging.debug('Core Result Images: %s', core_result.images)
Alex Klein56355682019-02-07 10:36:54 -0700224
Alex Klein27978a42021-07-27 14:18:10 -0600225 factory_result = image.Build(
226 board, image_types.factory, config=factory_build_config)
227 logging.debug('Factory Result Images: %s', factory_result.images)
Will Bradley29a49c22019-10-21 11:50:08 -0600228
Alex Klein27978a42021-07-27 14:18:10 -0600229 # A successful run will have no images missing, will have run at least one
230 # of the two image sets, and neither attempt errored. The no error condition
231 # should be redundant with no missing images, but is cheap insurance.
232 all_built = core_result.all_built and factory_result.all_built
233 one_ran = core_result.build_run or factory_result.build_run
234 no_errors = not core_result.run_error and not factory_result.run_error
235 output_proto.success = success = all_built and one_ran and no_errors
Will Bradley29a49c22019-10-21 11:50:08 -0600236
Alex Klein27978a42021-07-27 14:18:10 -0600237 if success:
238 # Success! We need to record the images we built in the output.
239 all_images = {**core_result.images, **factory_result.images}
240 for img_name, img_path in all_images.items():
Alex Kleinf5dc2632021-08-31 16:35:06 -0600241 _add_image_to_proto(output_proto, img_path, _IMAGE_MAPPING[img_name],
Alex Klein27978a42021-07-27 14:18:10 -0600242 board)
243
244 # Build and record VMs as necessary.
245 for vm_type in image_types.vms:
George Engelbrecht9f4f8322021-03-08 12:04:17 -0700246 is_test = vm_type in [_TEST_VM_ID, _TEST_GUEST_VM_ID]
Alex Klein27978a42021-07-27 14:18:10 -0600247 img_type = _IMAGE_MAPPING[_TEST_ID if is_test else _BASE_ID]
248 img_dir = core_result.images[img_type].parent.resolve()
George Engelbrecht9f4f8322021-03-08 12:04:17 -0700249 try:
250 if vm_type in [_BASE_GUEST_VM_ID, _TEST_GUEST_VM_ID]:
Alex Klein27978a42021-07-27 14:18:10 -0600251 vm_path = image.CreateGuestVm(
252 board, is_test=is_test, image_dir=img_dir)
George Engelbrecht9f4f8322021-03-08 12:04:17 -0700253 else:
254 vm_path = image.CreateVm(
Alex Klein27978a42021-07-27 14:18:10 -0600255 board,
256 disk_layout=build_config.disk_layout,
257 is_test=is_test,
258 image_dir=img_dir)
George Engelbrecht9f4f8322021-03-08 12:04:17 -0700259 except image.ImageToVmError as e:
260 cros_build_lib.Die(e)
Will Bradley29a49c22019-10-21 11:50:08 -0600261
George Engelbrecht9f4f8322021-03-08 12:04:17 -0700262 _add_image_to_proto(output_proto, vm_path, vm_type, board)
263
Alex Klein27978a42021-07-27 14:18:10 -0600264 # Build and record any mod images.
265 for mod_type in image_types.mod_images:
George Engelbrecht9f4f8322021-03-08 12:04:17 -0700266 if mod_type == _RECOVERY_ID:
Alex Klein27978a42021-07-27 14:18:10 -0600267 base_image_path = core_result.images[constants.IMAGE_TYPE_BASE]
Alex Kleincaace392021-07-26 14:28:13 -0600268 result = image.BuildRecoveryImage(
269 board=board, image_path=base_image_path)
Alex Klein27978a42021-07-27 14:18:10 -0600270 if result.all_built:
271 _add_image_to_proto(output_proto,
272 result.images[_IMAGE_MAPPING[mod_type]], mod_type,
273 board)
George Engelbrecht9f4f8322021-03-08 12:04:17 -0700274 else:
275 cros_build_lib.Die('Failed to create recovery image.')
276 else:
277 cros_build_lib.Die('_RECOVERY_ID is the only mod_image_type.')
Will Bradley29a49c22019-10-21 11:50:08 -0600278
279 # Read metric events log and pipe them into output_proto.events.
280 deserialize_metrics_log(output_proto.events, prefix=board)
281 return controller.RETURN_CODE_SUCCESS
282
Alex Klein1bcd9882019-03-19 13:25:24 -0600283 else:
Alex Klein2557b4f2019-07-11 14:34:00 -0600284 # Failure, include all of the failed packages in the output when available.
Alex Klein27978a42021-07-27 14:18:10 -0600285 packages = core_result.failed_packages + factory_result.failed_packages
286 if not packages:
Alex Klein2557b4f2019-07-11 14:34:00 -0600287 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
288
Alex Klein27978a42021-07-27 14:18:10 -0600289 for package in packages:
Alex Klein1bcd9882019-03-19 13:25:24 -0600290 current = output_proto.failed_packages.add()
Alex Kleine9a7dbf2020-10-06 18:12:12 -0600291 controller_util.serialize_package_info(package, current)
Alex Klein1bcd9882019-03-19 13:25:24 -0600292
Alex Klein8cb365a2019-05-15 16:24:53 -0600293 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
Alex Klein1bcd9882019-03-19 13:25:24 -0600294
Alex Kleincaace392021-07-26 14:28:13 -0600295
Alex Klein27978a42021-07-27 14:18:10 -0600296def _ParseImagesToCreate(to_build: List[int]) -> ImageTypes:
Alex Klein21b95022019-05-09 14:14:46 -0600297 """Helper function to parse the image types to build.
298
George Engelbrecht9f4f8322021-03-08 12:04:17 -0700299 This function expresses the dependencies of each image type and adds
300 the requisite image types if they're not explicitly defined.
Alex Klein21b95022019-05-09 14:14:46 -0600301
302 Args:
Alex Klein27978a42021-07-27 14:18:10 -0600303 to_build: The image type list.
Alex Klein21b95022019-05-09 14:14:46 -0600304
305 Returns:
Alex Klein27978a42021-07-27 14:18:10 -0600306 ImageTypes: The parsed images to build.
Alex Klein21b95022019-05-09 14:14:46 -0600307 """
308 image_types = set()
309 vm_types = set()
George Engelbrecht9f4f8322021-03-08 12:04:17 -0700310 mod_image_types = set()
Alex Klein21b95022019-05-09 14:14:46 -0600311 for current in to_build:
George Engelbrecht9f4f8322021-03-08 12:04:17 -0700312 # Find out if it's a special case (vm, img mod), or just any old image.
313 if current in _VM_IMAGE_MAPPING:
Alex Klein21b95022019-05-09 14:14:46 -0600314 vm_types.add(current)
315 # Make sure we build the image required to build the VM.
316 image_types.add(_VM_IMAGE_MAPPING[current])
George Engelbrecht9f4f8322021-03-08 12:04:17 -0700317 elif current in _MOD_IMAGE_MAPPING:
318 mod_image_types.add(current)
319 image_types.add(_MOD_IMAGE_MAPPING[current])
320 elif current in _IMAGE_MAPPING:
321 image_types.add(_IMAGE_MAPPING[current])
Alex Klein21b95022019-05-09 14:14:46 -0600322 else:
323 # Not expected, but at least it will be obvious if this comes up.
324 cros_build_lib.Die(
325 "The service's known image types do not match those in image.proto. "
326 'Unknown Enum ID: %s' % current)
327
Trent Begin008cade2019-10-31 13:40:59 -0600328 # We can only build one type of these images at a time since image_to_vm.sh
329 # uses the default path if a name is not provided.
330 if vm_types.issuperset({_BASE_VM_ID, _TEST_VM_ID}):
Alex Klein21b95022019-05-09 14:14:46 -0600331 cros_build_lib.Die('Cannot create more than one VM.')
332
Alex Klein27978a42021-07-27 14:18:10 -0600333 return ImageTypes(
334 images=image_types, vms=vm_types, mod_images=mod_image_types)
Alex Klein21b95022019-05-09 14:14:46 -0600335
336
337def _ParseCreateBuildConfig(input_proto):
338 """Helper to parse the image build config for Create."""
339 enable_rootfs_verification = not input_proto.disable_rootfs_verification
340 version = input_proto.version or None
341 disk_layout = input_proto.disk_layout or None
342 builder_path = input_proto.builder_path or None
343 return image.BuildConfig(
Jack Neus761e1842020-12-01 18:20:11 +0000344 enable_rootfs_verification=enable_rootfs_verification,
345 replace=True,
346 version=version,
347 disk_layout=disk_layout,
348 builder_path=builder_path,
Alex Klein21b95022019-05-09 14:14:46 -0600349 )
350
Alex Klein1bcd9882019-03-19 13:25:24 -0600351
Michael Mortensen10146cf2019-11-19 19:59:22 -0700352def _SignerTestResponse(_input_proto, output_proto, _config):
353 """Set output_proto success field on a successful SignerTest response."""
354 output_proto.success = True
355 return controller.RETURN_CODE_SUCCESS
356
357
358@faux.success(_SignerTestResponse)
Michael Mortensen85d38402019-12-12 09:50:29 -0700359@faux.empty_completed_unsuccessfully_error
Michael Mortensenc83c9952019-08-05 12:15:12 -0600360@validate.exists('image.path')
Alex Klein231d2da2019-07-22 16:44:45 -0600361@validate.validation_complete
362def SignerTest(input_proto, output_proto, _config):
Michael Mortensenc83c9952019-08-05 12:15:12 -0600363 """Run image tests.
364
365 Args:
366 input_proto (image_pb2.ImageTestRequest): The input message.
367 output_proto (image_pb2.ImageTestResult): The output message.
Alex Klein231d2da2019-07-22 16:44:45 -0600368 _config (api_config.ApiConfig): The API call config.
Michael Mortensenc83c9952019-08-05 12:15:12 -0600369 """
Michael Mortensenc83c9952019-08-05 12:15:12 -0600370 image_path = input_proto.image.path
371
Alex Klein231d2da2019-07-22 16:44:45 -0600372 result = image_lib.SecurityTest(image=image_path)
Michael Mortensenc83c9952019-08-05 12:15:12 -0600373 output_proto.success = result
374 if result:
375 return controller.RETURN_CODE_SUCCESS
376 else:
377 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
378
Alex Klein076841b2019-08-29 15:19:39 -0600379
Michael Mortensen10146cf2019-11-19 19:59:22 -0700380def _TestResponse(_input_proto, output_proto, _config):
381 """Set output_proto success field on a successful Test response."""
382 output_proto.success = True
383 return controller.RETURN_CODE_SUCCESS
384
385
386@faux.success(_TestResponse)
Michael Mortensen85d38402019-12-12 09:50:29 -0700387@faux.empty_completed_unsuccessfully_error
Alex Klein2b236722019-06-19 15:44:26 -0600388@validate.require('build_target.name', 'result.directory')
389@validate.exists('image.path')
Alex Klein231d2da2019-07-22 16:44:45 -0600390def Test(input_proto, output_proto, config):
Alex Klein2966e302019-01-17 13:29:38 -0700391 """Run image tests.
392
393 Args:
394 input_proto (image_pb2.ImageTestRequest): The input message.
395 output_proto (image_pb2.ImageTestResult): The output message.
Alex Klein231d2da2019-07-22 16:44:45 -0600396 config (api_config.ApiConfig): The API call config.
Alex Klein2966e302019-01-17 13:29:38 -0700397 """
398 image_path = input_proto.image.path
399 board = input_proto.build_target.name
400 result_directory = input_proto.result.directory
401
Alex Klein2966e302019-01-17 13:29:38 -0700402 if not os.path.isfile(image_path) or not image_path.endswith('.bin'):
Alex Klein4f0eb432019-05-02 13:56:04 -0600403 cros_build_lib.Die(
Alex Klein2966e302019-01-17 13:29:38 -0700404 'The image.path must be an existing image file with a .bin extension.')
405
Alex Klein231d2da2019-07-22 16:44:45 -0600406 if config.validate_only:
407 return controller.RETURN_CODE_VALID_INPUT
408
Alex Klein8cb365a2019-05-15 16:24:53 -0600409 success = image.Test(board, result_directory, image_dir=image_path)
410 output_proto.success = success
411
412 if success:
413 return controller.RETURN_CODE_SUCCESS
414 else:
415 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
Jack Neus761e1842020-12-01 18:20:11 +0000416
417
418@faux.empty_success
419@faux.empty_completed_unsuccessfully_error
420@validate.require('gs_image_dir', 'sysroot.build_target.name')
421def PushImage(input_proto, _output_proto, config):
422 """Push artifacts from the archive bucket to the release bucket.
423
424 Wraps chromite/scripts/pushimage.py.
425
426 Args:
427 input_proto (PushImageRequest): Input proto.
428 _output_proto (PushImageResponse): Output proto.
429 config (api.config.ApiConfig): The API call config.
430
431 Returns:
432 A controller return code (e.g. controller.RETURN_CODE_SUCCESS).
433 """
434 sign_types = []
435 if input_proto.sign_types:
436 for sign_type in input_proto.sign_types:
437 if sign_type not in SUPPORTED_IMAGE_TYPES:
438 logging.error('unsupported sign type %g', sign_type)
439 return controller.RETURN_CODE_INVALID_INPUT
440 sign_types.append(SUPPORTED_IMAGE_TYPES[sign_type])
441
442 # If configured for validation only we're done here.
443 if config.validate_only:
444 return controller.RETURN_CODE_VALID_INPUT
445
Jack Neus485a9d22020-12-21 03:15:15 +0000446 kwargs = {}
447 if input_proto.profile.name:
448 kwargs['profile'] = input_proto.profile.name
449 if input_proto.dest_bucket:
450 kwargs['dest_bucket'] = input_proto.dest_bucket
Jack Neus761e1842020-12-01 18:20:11 +0000451 try:
452 pushimage.PushImage(
453 input_proto.gs_image_dir,
454 input_proto.sysroot.build_target.name,
455 dry_run=input_proto.dryrun,
Jack Neus485a9d22020-12-21 03:15:15 +0000456 sign_types=sign_types,
457 **kwargs)
Jack Neus761e1842020-12-01 18:20:11 +0000458 return controller.RETURN_CODE_SUCCESS
459 except Exception:
Jack Neuse3150a42021-01-29 17:16:36 +0000460 logging.error('PushImage failed: ', exc_info=True)
Jack Neus761e1842020-12-01 18:20:11 +0000461 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY