blob: 0dda2c464b146116de558dd07145387f722ff505 [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 Klein7107bdd2019-03-14 17:14:31 -060015from chromite.api.gen.chromite.api import image_pb2
Alex Klein4f0eb432019-05-02 13:56:04 -060016from chromite.api.controller import controller_util
17from 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.
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 Klein56355682019-02-07 10:36:54 -070038def 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 Klein4f0eb432019-05-02 13:56:04 -060047 cros_build_lib.Die('build_target.name is required.')
Alex Klein56355682019-02-07 10:36:54 -070048
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 Klein4f0eb432019-05-02 13:56:04 -060055 cros_build_lib.Die(
Alex Klein56355682019-02-07 10:36:54 -070056 "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 Klein1bcd9882019-03-19 13:25:24 -060071 result = image.Build(board=board, images=sorted(list(image_types)),
72 config=build_config)
Alex Klein56355682019-02-07 10:36:54 -070073
Alex Klein1bcd9882019-03-19 13:25:24 -060074 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
90def _PopulateBuiltImages(board, image_types, output_proto):
91 """Helper to list out built images for Create."""
Alex Klein56355682019-02-07 10:36:54 -070092 # 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 Klein4f0eb432019-05-02 13:56:04 -060096 base_path = os.path.realpath(latest_link)
Alex Klein56355682019-02-07 10:36:54 -070097
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 Klein4f0eb432019-05-02 13:56:04 -0600105 new_image.build_target.name = board
106
107
108def 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 Klein2966e302019-01-17 13:29:38 -0700135
136
137def 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 Klein4f0eb432019-05-02 13:56:04 -0600149 cros_build_lib.Die('The build_target.name is required.')
Alex Klein2966e302019-01-17 13:29:38 -0700150 if not result_directory:
Alex Klein4f0eb432019-05-02 13:56:04 -0600151 cros_build_lib.Die('The result.directory is required.')
Alex Klein2966e302019-01-17 13:29:38 -0700152 if not image_path:
Alex Klein4f0eb432019-05-02 13:56:04 -0600153 cros_build_lib.Die('The image.path is required.')
Alex Klein2966e302019-01-17 13:29:38 -0700154
155 if not os.path.isfile(image_path) or not image_path.endswith('.bin'):
Alex Klein4f0eb432019-05-02 13:56:04 -0600156 cros_build_lib.Die(
Alex Klein2966e302019-01-17 13:29:38 -0700157 '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)