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 | 7107bdd | 2019-03-14 17:14:31 -0600 | [diff] [blame] | 15 | from chromite.api.gen.chromite.api import image_pb2 |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame^] | 16 | from chromite.api.controller import controller_util |
| 17 | from chromite.lib import cros_build_lib |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 18 | from chromite.lib import constants |
| 19 | from chromite.lib import image_lib |
Alex Klein | b7cdbe6 | 2019-02-22 11:41:32 -0700 | [diff] [blame] | 20 | from chromite.service import image |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 21 | |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 22 | # The image.proto ImageType enum ids. |
| 23 | _BASE_ID = image_pb2.Image.BASE |
| 24 | _DEV_ID = image_pb2.Image.DEV |
| 25 | _TEST_ID = image_pb2.Image.TEST |
| 26 | |
| 27 | # Dict to allow easily translating names to enum ids and vice versa. |
| 28 | _IMAGE_MAPPING = { |
| 29 | _BASE_ID: constants.IMAGE_TYPE_BASE, |
| 30 | constants.IMAGE_TYPE_BASE: _BASE_ID, |
| 31 | _DEV_ID: constants.IMAGE_TYPE_DEV, |
| 32 | constants.IMAGE_TYPE_DEV: _DEV_ID, |
| 33 | _TEST_ID: constants.IMAGE_TYPE_TEST, |
| 34 | constants.IMAGE_TYPE_TEST: _TEST_ID, |
| 35 | } |
| 36 | |
| 37 | |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 38 | def Create(input_proto, output_proto): |
| 39 | """Build an image. |
| 40 | |
| 41 | Args: |
| 42 | input_proto (image_pb2.CreateImageRequest): The input message. |
| 43 | output_proto (image_pb2.CreateImageResult): The output message. |
| 44 | """ |
| 45 | board = input_proto.build_target.name |
| 46 | if not board: |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame^] | 47 | cros_build_lib.Die('build_target.name is required.') |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 48 | |
| 49 | image_types = set() |
| 50 | # Build the base image if no images provided. |
| 51 | to_build = input_proto.image_types or [_BASE_ID] |
| 52 | for current in to_build: |
| 53 | if current not in _IMAGE_MAPPING: |
| 54 | # Not expected, but at least it will be obvious if this comes up. |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame^] | 55 | cros_build_lib.Die( |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 56 | "The service's known image types do not match those in image.proto. " |
| 57 | 'Unknown Enum ID: %s' % current) |
| 58 | |
| 59 | image_types.add(_IMAGE_MAPPING[current]) |
| 60 | |
| 61 | enable_rootfs_verification = not input_proto.disable_rootfs_verification |
| 62 | version = input_proto.version or None |
| 63 | disk_layout = input_proto.disk_layout or None |
| 64 | builder_path = input_proto.builder_path or None |
| 65 | build_config = image.BuildConfig( |
| 66 | enable_rootfs_verification=enable_rootfs_verification, replace=True, |
| 67 | version=version, disk_layout=disk_layout, builder_path=builder_path, |
| 68 | ) |
| 69 | |
| 70 | # 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] | 71 | result = image.Build(board=board, images=sorted(list(image_types)), |
| 72 | config=build_config) |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 73 | |
Alex Klein | 1bcd988 | 2019-03-19 13:25:24 -0600 | [diff] [blame] | 74 | output_proto.success = result.success |
| 75 | if result.success: |
| 76 | # Success -- we need to list out the images we built in the output. |
| 77 | _PopulateBuiltImages(board, image_types, output_proto) |
| 78 | else: |
| 79 | # Failure -- include all of the failed packages in the output. |
| 80 | for package in result.failed_packages: |
| 81 | current = output_proto.failed_packages.add() |
| 82 | current.category = package.category |
| 83 | current.package_name = package.package |
| 84 | if package.version: |
| 85 | current.version = package.version |
| 86 | |
| 87 | return 1 |
| 88 | |
| 89 | |
| 90 | def _PopulateBuiltImages(board, image_types, output_proto): |
| 91 | """Helper to list out built images for Create.""" |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 92 | # Build out the ImageType->ImagePath mapping in the output. |
| 93 | # We're using the default path, so just fetch that, but read the symlink so |
| 94 | # the path we're returning is somewhat more permanent. |
| 95 | latest_link = image_lib.GetLatestImageLink(board) |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame^] | 96 | base_path = os.path.realpath(latest_link) |
Alex Klein | 5635568 | 2019-02-07 10:36:54 -0700 | [diff] [blame] | 97 | |
| 98 | for current in image_types: |
| 99 | type_id = _IMAGE_MAPPING[current] |
| 100 | path = os.path.join(base_path, constants.IMAGE_TYPE_TO_NAME[current]) |
| 101 | |
| 102 | new_image = output_proto.images.add() |
| 103 | new_image.path = path |
| 104 | new_image.type = type_id |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame^] | 105 | new_image.build_target.name = board |
| 106 | |
| 107 | |
| 108 | def CreateVm(input_proto, output_proto): |
| 109 | """Create a VM from an Image. |
| 110 | |
| 111 | Args: |
| 112 | input_proto (image_pb2.CreateVmRequest): The input message. |
| 113 | output_proto (image_pb2.CreateVmResponse): The output message. |
| 114 | """ |
| 115 | # TODO(saklein) This currently relies on the build target, but using the image |
| 116 | # path directly would be better. Change this to do that when create image |
| 117 | # returns an absolute image path rather than chroot relative path. |
| 118 | build_target_name = input_proto.image.build_target.name |
| 119 | proto_image_type = input_proto.image.type |
| 120 | |
| 121 | if not build_target_name: |
| 122 | cros_build_lib.Die('image.build_target.name is required.') |
| 123 | if proto_image_type not in _IMAGE_MAPPING: |
| 124 | cros_build_lib.Die('Unknown image.type value: %s', proto_image_type) |
| 125 | |
| 126 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 127 | is_test_image = proto_image_type == _TEST_ID |
| 128 | |
| 129 | try: |
| 130 | output_proto.vm_image.path = image.CreateVm(build_target_name, |
| 131 | chroot=chroot, |
| 132 | is_test=is_test_image) |
| 133 | except image.Error as e: |
| 134 | cros_build_lib.Die(e.message) |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 135 | |
| 136 | |
| 137 | def Test(input_proto, output_proto): |
| 138 | """Run image tests. |
| 139 | |
| 140 | Args: |
| 141 | input_proto (image_pb2.ImageTestRequest): The input message. |
| 142 | output_proto (image_pb2.ImageTestResult): The output message. |
| 143 | """ |
| 144 | image_path = input_proto.image.path |
| 145 | board = input_proto.build_target.name |
| 146 | result_directory = input_proto.result.directory |
| 147 | |
| 148 | if not board: |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame^] | 149 | cros_build_lib.Die('The build_target.name is required.') |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 150 | if not result_directory: |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame^] | 151 | cros_build_lib.Die('The result.directory is required.') |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 152 | if not image_path: |
Alex Klein | 4f0eb43 | 2019-05-02 13:56:04 -0600 | [diff] [blame^] | 153 | cros_build_lib.Die('The image.path is required.') |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 154 | |
| 155 | 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^] | 156 | cros_build_lib.Die( |
Alex Klein | 2966e30 | 2019-01-17 13:29:38 -0700 | [diff] [blame] | 157 | 'The image.path must be an existing image file with a .bin extension.') |
| 158 | |
| 159 | output_proto.success = image.Test(board, result_directory, |
| 160 | image_dir=image_path) |