blob: 9b6880b446fc6e6ecd2a3cc9cf31ae5fe0f5de2c [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
David Burgerb171d652019-05-13 16:07:00 -060016from chromite.api.gen.chromiumos import common_pb2
Alex Klein4f0eb432019-05-02 13:56:04 -060017from chromite.lib import cros_build_lib
Alex Klein56355682019-02-07 10:36:54 -070018from chromite.lib import constants
19from chromite.lib import image_lib
Alex Kleinb7cdbe62019-02-22 11:41:32 -070020from chromite.service import image
Alex Klein2966e302019-01-17 13:29:38 -070021
Alex Klein56355682019-02-07 10:36:54 -070022# The image.proto ImageType enum ids.
David Burgerb171d652019-05-13 16:07:00 -060023_BASE_ID = common_pb2.BASE
24_DEV_ID = common_pb2.DEV
25_TEST_ID = common_pb2.TEST
Alex Klein21b95022019-05-09 14:14:46 -060026_BASE_VM_ID = common_pb2.BASE_VM
27_TEST_VM_ID = common_pb2.TEST_VM
Alex Klein56355682019-02-07 10:36:54 -070028
29# Dict to allow easily translating names to enum ids and vice versa.
30_IMAGE_MAPPING = {
31 _BASE_ID: constants.IMAGE_TYPE_BASE,
32 constants.IMAGE_TYPE_BASE: _BASE_ID,
33 _DEV_ID: constants.IMAGE_TYPE_DEV,
34 constants.IMAGE_TYPE_DEV: _DEV_ID,
35 _TEST_ID: constants.IMAGE_TYPE_TEST,
36 constants.IMAGE_TYPE_TEST: _TEST_ID,
37}
38
Alex Klein21b95022019-05-09 14:14:46 -060039_VM_IMAGE_MAPPING = {
40 _BASE_VM_ID: _IMAGE_MAPPING[_BASE_ID],
41 _TEST_VM_ID: _IMAGE_MAPPING[_TEST_ID],
42}
43
Alex Klein56355682019-02-07 10:36:54 -070044
Alex Klein56355682019-02-07 10:36:54 -070045def Create(input_proto, output_proto):
46 """Build an image.
47
48 Args:
49 input_proto (image_pb2.CreateImageRequest): The input message.
50 output_proto (image_pb2.CreateImageResult): The output message.
51 """
52 board = input_proto.build_target.name
53 if not board:
Alex Klein4f0eb432019-05-02 13:56:04 -060054 cros_build_lib.Die('build_target.name is required.')
Alex Klein56355682019-02-07 10:36:54 -070055
Alex Klein56355682019-02-07 10:36:54 -070056 # Build the base image if no images provided.
57 to_build = input_proto.image_types or [_BASE_ID]
Alex Klein56355682019-02-07 10:36:54 -070058
Alex Klein21b95022019-05-09 14:14:46 -060059 image_types, vm_types = _ParseImagesToCreate(to_build)
60 build_config = _ParseCreateBuildConfig(input_proto)
Alex Klein56355682019-02-07 10:36:54 -070061
62 # Sorted isn't really necessary here, but it's much easier to test.
Alex Klein1bcd9882019-03-19 13:25:24 -060063 result = image.Build(board=board, images=sorted(list(image_types)),
64 config=build_config)
Alex Klein56355682019-02-07 10:36:54 -070065
Alex Klein1bcd9882019-03-19 13:25:24 -060066 output_proto.success = result.success
67 if result.success:
68 # Success -- we need to list out the images we built in the output.
69 _PopulateBuiltImages(board, image_types, output_proto)
70 else:
71 # Failure -- include all of the failed packages in the output.
72 for package in result.failed_packages:
73 current = output_proto.failed_packages.add()
74 current.category = package.category
75 current.package_name = package.package
76 if package.version:
77 current.version = package.version
78
Alex Klein8cb365a2019-05-15 16:24:53 -060079 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
Alex Klein1bcd9882019-03-19 13:25:24 -060080
Alex Klein21b95022019-05-09 14:14:46 -060081 if not vm_types:
82 # No VMs to build, we can exit now.
83 return 0
84
85 # There can be only one.
86 vm_type = vm_types.pop()
87 is_test = vm_type == _TEST_VM_ID
88 try:
89 vm_path = image.CreateVm(board, is_test=is_test)
90 except image.ImageToVmError as e:
91 cros_build_lib.Die(e.message)
92
93 new_image = output_proto.images.add()
94 new_image.path = vm_path
95 new_image.type = vm_type
96 new_image.build_target.name = board
97
98
99def _ParseImagesToCreate(to_build):
100 """Helper function to parse the image types to build.
101
102 This function exists just to clean up the Create function.
103
104 Args:
105 to_build (list[int]): The image type list.
106
107 Returns:
108 (set, set): The image and vm types, respectively, that need to be built.
109 """
110 image_types = set()
111 vm_types = set()
112 for current in to_build:
113 if current in _IMAGE_MAPPING:
114 image_types.add(_IMAGE_MAPPING[current])
115 elif current in _VM_IMAGE_MAPPING:
116 vm_types.add(current)
117 # Make sure we build the image required to build the VM.
118 image_types.add(_VM_IMAGE_MAPPING[current])
119 else:
120 # Not expected, but at least it will be obvious if this comes up.
121 cros_build_lib.Die(
122 "The service's known image types do not match those in image.proto. "
123 'Unknown Enum ID: %s' % current)
124
125 if len(vm_types) > 1:
126 cros_build_lib.Die('Cannot create more than one VM.')
127
128 return image_types, vm_types
129
130
131def _ParseCreateBuildConfig(input_proto):
132 """Helper to parse the image build config for Create."""
133 enable_rootfs_verification = not input_proto.disable_rootfs_verification
134 version = input_proto.version or None
135 disk_layout = input_proto.disk_layout or None
136 builder_path = input_proto.builder_path or None
137 return image.BuildConfig(
138 enable_rootfs_verification=enable_rootfs_verification, replace=True,
139 version=version, disk_layout=disk_layout, builder_path=builder_path,
140 )
141
Alex Klein1bcd9882019-03-19 13:25:24 -0600142
143def _PopulateBuiltImages(board, image_types, output_proto):
144 """Helper to list out built images for Create."""
Alex Klein56355682019-02-07 10:36:54 -0700145 # Build out the ImageType->ImagePath mapping in the output.
146 # We're using the default path, so just fetch that, but read the symlink so
147 # the path we're returning is somewhat more permanent.
148 latest_link = image_lib.GetLatestImageLink(board)
Alex Klein4f0eb432019-05-02 13:56:04 -0600149 base_path = os.path.realpath(latest_link)
Alex Klein56355682019-02-07 10:36:54 -0700150
151 for current in image_types:
152 type_id = _IMAGE_MAPPING[current]
153 path = os.path.join(base_path, constants.IMAGE_TYPE_TO_NAME[current])
154
155 new_image = output_proto.images.add()
156 new_image.path = path
157 new_image.type = type_id
Alex Klein4f0eb432019-05-02 13:56:04 -0600158 new_image.build_target.name = board
159
160
Alex Klein2966e302019-01-17 13:29:38 -0700161def Test(input_proto, output_proto):
162 """Run image tests.
163
164 Args:
165 input_proto (image_pb2.ImageTestRequest): The input message.
166 output_proto (image_pb2.ImageTestResult): The output message.
167 """
168 image_path = input_proto.image.path
169 board = input_proto.build_target.name
170 result_directory = input_proto.result.directory
171
172 if not board:
Alex Klein4f0eb432019-05-02 13:56:04 -0600173 cros_build_lib.Die('The build_target.name is required.')
Alex Klein2966e302019-01-17 13:29:38 -0700174 if not result_directory:
Alex Klein4f0eb432019-05-02 13:56:04 -0600175 cros_build_lib.Die('The result.directory is required.')
Alex Klein2966e302019-01-17 13:29:38 -0700176 if not image_path:
Alex Klein4f0eb432019-05-02 13:56:04 -0600177 cros_build_lib.Die('The image.path is required.')
Alex Klein2966e302019-01-17 13:29:38 -0700178
179 if not os.path.isfile(image_path) or not image_path.endswith('.bin'):
Alex Klein4f0eb432019-05-02 13:56:04 -0600180 cros_build_lib.Die(
Alex Klein2966e302019-01-17 13:29:38 -0700181 'The image.path must be an existing image file with a .bin extension.')
182
Alex Klein8cb365a2019-05-15 16:24:53 -0600183 success = image.Test(board, result_directory, image_dir=image_path)
184 output_proto.success = success
185
186 if success:
187 return controller.RETURN_CODE_SUCCESS
188 else:
189 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY