blob: 88d2d52f12564281b6d74f9b649b59913e60545e [file] [log] [blame]
Alex Klein2966e302019-01-17 13:29:38 -07001# -*- 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
8The image related API endpoints should generally be found here.
9"""
10
11from __future__ import print_function
12
13import os
14
Alex Klein8cb365a2019-05-15 16:24:53 -060015from chromite.api import controller
Alex Klein2b236722019-06-19 15:44:26 -060016from chromite.api import validate
David Burgerb171d652019-05-13 16:07:00 -060017from chromite.api.gen.chromiumos import common_pb2
Alex Klein4f0eb432019-05-02 13:56:04 -060018from chromite.lib import cros_build_lib
Alex Klein56355682019-02-07 10:36:54 -070019from chromite.lib import constants
20from chromite.lib import image_lib
Alex Kleinb7cdbe62019-02-22 11:41:32 -070021from chromite.service import image
Alex Klein2966e302019-01-17 13:29:38 -070022
Alex Klein56355682019-02-07 10:36:54 -070023# The image.proto ImageType enum ids.
David Burgerb171d652019-05-13 16:07:00 -060024_BASE_ID = common_pb2.BASE
25_DEV_ID = common_pb2.DEV
26_TEST_ID = common_pb2.TEST
Alex Klein21b95022019-05-09 14:14:46 -060027_BASE_VM_ID = common_pb2.BASE_VM
28_TEST_VM_ID = common_pb2.TEST_VM
Michael Mortenseneefe8952019-08-12 15:37:15 -060029_RECOVERY_ID = common_pb2.RECOVERY
30_FACTORY_ID = common_pb2.FACTORY
31_FIRMWARE_ID = common_pb2.FIRMWARE
Alex Klein56355682019-02-07 10:36:54 -070032
33# Dict to allow easily translating names to enum ids and vice versa.
34_IMAGE_MAPPING = {
35 _BASE_ID: constants.IMAGE_TYPE_BASE,
36 constants.IMAGE_TYPE_BASE: _BASE_ID,
37 _DEV_ID: constants.IMAGE_TYPE_DEV,
38 constants.IMAGE_TYPE_DEV: _DEV_ID,
39 _TEST_ID: constants.IMAGE_TYPE_TEST,
40 constants.IMAGE_TYPE_TEST: _TEST_ID,
Michael Mortenseneefe8952019-08-12 15:37:15 -060041 _RECOVERY_ID: constants.IMAGE_TYPE_RECOVERY,
42 constants.IMAGE_TYPE_RECOVERY: _RECOVERY_ID,
43 _FACTORY_ID: constants.IMAGE_TYPE_FACTORY,
44 constants.IMAGE_TYPE_FACTORY: _FACTORY_ID,
45 _FIRMWARE_ID: constants.IMAGE_TYPE_FIRMWARE,
46 constants.IMAGE_TYPE_FIRMWARE: _FIRMWARE_ID,
Alex Klein56355682019-02-07 10:36:54 -070047}
48
Alex Klein21b95022019-05-09 14:14:46 -060049_VM_IMAGE_MAPPING = {
50 _BASE_VM_ID: _IMAGE_MAPPING[_BASE_ID],
51 _TEST_VM_ID: _IMAGE_MAPPING[_TEST_ID],
52}
53
Alex Klein56355682019-02-07 10:36:54 -070054
Alex Klein2b236722019-06-19 15:44:26 -060055@validate.require('build_target.name')
Alex Klein231d2da2019-07-22 16:44:45 -060056@validate.validation_complete
57def Create(input_proto, output_proto, _config):
Alex Klein56355682019-02-07 10:36:54 -070058 """Build an image.
59
60 Args:
61 input_proto (image_pb2.CreateImageRequest): The input message.
62 output_proto (image_pb2.CreateImageResult): The output message.
Alex Klein231d2da2019-07-22 16:44:45 -060063 _config (api_config.ApiConfig): The API call config.
Alex Klein56355682019-02-07 10:36:54 -070064 """
65 board = input_proto.build_target.name
Alex Klein56355682019-02-07 10:36:54 -070066
Alex Klein56355682019-02-07 10:36:54 -070067 # Build the base image if no images provided.
68 to_build = input_proto.image_types or [_BASE_ID]
Alex Klein56355682019-02-07 10:36:54 -070069
Alex Klein21b95022019-05-09 14:14:46 -060070 image_types, vm_types = _ParseImagesToCreate(to_build)
71 build_config = _ParseCreateBuildConfig(input_proto)
Alex Klein56355682019-02-07 10:36:54 -070072
73 # Sorted isn't really necessary here, but it's much easier to test.
Alex Klein1bcd9882019-03-19 13:25:24 -060074 result = image.Build(board=board, images=sorted(list(image_types)),
75 config=build_config)
Alex Klein56355682019-02-07 10:36:54 -070076
Alex Klein1bcd9882019-03-19 13:25:24 -060077 output_proto.success = result.success
78 if result.success:
79 # Success -- we need to list out the images we built in the output.
80 _PopulateBuiltImages(board, image_types, output_proto)
81 else:
Alex Klein2557b4f2019-07-11 14:34:00 -060082 # Failure, include all of the failed packages in the output when available.
83 if not result.failed_packages:
84 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
85
Alex Klein1bcd9882019-03-19 13:25:24 -060086 for package in result.failed_packages:
87 current = output_proto.failed_packages.add()
88 current.category = package.category
89 current.package_name = package.package
90 if package.version:
91 current.version = package.version
92
Alex Klein8cb365a2019-05-15 16:24:53 -060093 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
Alex Klein1bcd9882019-03-19 13:25:24 -060094
Alex Klein21b95022019-05-09 14:14:46 -060095 if not vm_types:
96 # No VMs to build, we can exit now.
Alex Klein231d2da2019-07-22 16:44:45 -060097 return controller.RETURN_CODE_SUCCESS
Alex Klein21b95022019-05-09 14:14:46 -060098
99 # There can be only one.
100 vm_type = vm_types.pop()
101 is_test = vm_type == _TEST_VM_ID
102 try:
103 vm_path = image.CreateVm(board, is_test=is_test)
104 except image.ImageToVmError as e:
105 cros_build_lib.Die(e.message)
106
107 new_image = output_proto.images.add()
108 new_image.path = vm_path
109 new_image.type = vm_type
110 new_image.build_target.name = board
111
112
113def _ParseImagesToCreate(to_build):
114 """Helper function to parse the image types to build.
115
116 This function exists just to clean up the Create function.
117
118 Args:
119 to_build (list[int]): The image type list.
120
121 Returns:
122 (set, set): The image and vm types, respectively, that need to be built.
123 """
124 image_types = set()
125 vm_types = set()
126 for current in to_build:
127 if current in _IMAGE_MAPPING:
128 image_types.add(_IMAGE_MAPPING[current])
129 elif current in _VM_IMAGE_MAPPING:
130 vm_types.add(current)
131 # Make sure we build the image required to build the VM.
132 image_types.add(_VM_IMAGE_MAPPING[current])
133 else:
134 # Not expected, but at least it will be obvious if this comes up.
135 cros_build_lib.Die(
136 "The service's known image types do not match those in image.proto. "
137 'Unknown Enum ID: %s' % current)
138
139 if len(vm_types) > 1:
140 cros_build_lib.Die('Cannot create more than one VM.')
141
142 return image_types, vm_types
143
144
145def _ParseCreateBuildConfig(input_proto):
146 """Helper to parse the image build config for Create."""
147 enable_rootfs_verification = not input_proto.disable_rootfs_verification
148 version = input_proto.version or None
149 disk_layout = input_proto.disk_layout or None
150 builder_path = input_proto.builder_path or None
151 return image.BuildConfig(
152 enable_rootfs_verification=enable_rootfs_verification, replace=True,
153 version=version, disk_layout=disk_layout, builder_path=builder_path,
154 )
155
Alex Klein1bcd9882019-03-19 13:25:24 -0600156
157def _PopulateBuiltImages(board, image_types, output_proto):
158 """Helper to list out built images for Create."""
Alex Klein56355682019-02-07 10:36:54 -0700159 # Build out the ImageType->ImagePath mapping in the output.
160 # We're using the default path, so just fetch that, but read the symlink so
161 # the path we're returning is somewhat more permanent.
162 latest_link = image_lib.GetLatestImageLink(board)
Alex Klein4f0eb432019-05-02 13:56:04 -0600163 base_path = os.path.realpath(latest_link)
Alex Klein56355682019-02-07 10:36:54 -0700164
165 for current in image_types:
166 type_id = _IMAGE_MAPPING[current]
167 path = os.path.join(base_path, constants.IMAGE_TYPE_TO_NAME[current])
168
169 new_image = output_proto.images.add()
170 new_image.path = path
171 new_image.type = type_id
Alex Klein4f0eb432019-05-02 13:56:04 -0600172 new_image.build_target.name = board
173
174
Michael Mortensenc83c9952019-08-05 12:15:12 -0600175@validate.exists('image.path')
Alex Klein231d2da2019-07-22 16:44:45 -0600176@validate.validation_complete
177def SignerTest(input_proto, output_proto, _config):
Michael Mortensenc83c9952019-08-05 12:15:12 -0600178 """Run image tests.
179
180 Args:
181 input_proto (image_pb2.ImageTestRequest): The input message.
182 output_proto (image_pb2.ImageTestResult): The output message.
Alex Klein231d2da2019-07-22 16:44:45 -0600183 _config (api_config.ApiConfig): The API call config.
Michael Mortensenc83c9952019-08-05 12:15:12 -0600184 """
Michael Mortensenc83c9952019-08-05 12:15:12 -0600185 image_path = input_proto.image.path
186
Alex Klein231d2da2019-07-22 16:44:45 -0600187 result = image_lib.SecurityTest(image=image_path)
Michael Mortensenc83c9952019-08-05 12:15:12 -0600188 output_proto.success = result
189 if result:
190 return controller.RETURN_CODE_SUCCESS
191 else:
192 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
193
Alex Klein2b236722019-06-19 15:44:26 -0600194@validate.require('build_target.name', 'result.directory')
195@validate.exists('image.path')
Alex Klein231d2da2019-07-22 16:44:45 -0600196def Test(input_proto, output_proto, config):
Alex Klein2966e302019-01-17 13:29:38 -0700197 """Run image tests.
198
199 Args:
200 input_proto (image_pb2.ImageTestRequest): The input message.
201 output_proto (image_pb2.ImageTestResult): The output message.
Alex Klein231d2da2019-07-22 16:44:45 -0600202 config (api_config.ApiConfig): The API call config.
Alex Klein2966e302019-01-17 13:29:38 -0700203 """
204 image_path = input_proto.image.path
205 board = input_proto.build_target.name
206 result_directory = input_proto.result.directory
207
Alex Klein2966e302019-01-17 13:29:38 -0700208 if not os.path.isfile(image_path) or not image_path.endswith('.bin'):
Alex Klein4f0eb432019-05-02 13:56:04 -0600209 cros_build_lib.Die(
Alex Klein2966e302019-01-17 13:29:38 -0700210 'The image.path must be an existing image file with a .bin extension.')
211
Alex Klein231d2da2019-07-22 16:44:45 -0600212 if config.validate_only:
213 return controller.RETURN_CODE_VALID_INPUT
214
Alex Klein8cb365a2019-05-15 16:24:53 -0600215 success = image.Test(board, result_directory, image_dir=image_path)
216 output_proto.success = success
217
218 if success:
219 return controller.RETURN_CODE_SUCCESS
220 else:
221 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY