blob: db575920a5bf3074e9bcd8f64c39c3109a8f12b0 [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2019 The ChromiumOS Authors
Alex Klein19c4cc42019-02-27 14:47:57 -07002# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""SDK chroot operations."""
6
Alex Klein19c4cc42019-02-27 14:47:57 -07007import os
Greg Edelston9dcdc8a2023-01-11 17:07:10 -07008from typing import Dict, Union
Alex Klein19c4cc42019-02-27 14:47:57 -07009
Alex Klein231d2da2019-07-22 16:44:45 -060010from chromite.api import controller
Alex Klein076841b2019-08-29 15:19:39 -060011from chromite.api import faux
Alex Klein231d2da2019-07-22 16:44:45 -060012from chromite.api import validate
Alex Klein71a9a1d2019-10-28 15:45:10 -060013from chromite.api.controller import controller_util
Bob Haarman0853ce92022-12-13 18:11:53 +000014from chromite.api.gen.chromiumos import common_pb2
Alex Klein19c4cc42019-02-27 14:47:57 -070015from chromite.lib import cros_build_lib
16from chromite.service import sdk
17
18
Alex Klein076841b2019-08-29 15:19:39 -060019def _ChrootVersionResponse(_input_proto, output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -060020 """Add a fake chroot version to a successful response."""
21 output_proto.version.version = 168
Alex Klein076841b2019-08-29 15:19:39 -060022
23
Bob Haarman171d4092023-01-10 16:44:19 +000024def _BinhostCLs(_input_proto, output_proto, _config):
25 """Add fake CL identifiers to a successful response."""
Bob Haarmanc0082602022-09-20 16:12:43 -070026 output_proto.cls = [
Bob Haarman171d4092023-01-10 16:44:19 +000027 "fakecl:1",
28 "fakecl:2",
Bob Haarmanc0082602022-09-20 16:12:43 -070029 ]
30
31
Bob Haarman0853ce92022-12-13 18:11:53 +000032def _BuildSdkTarballResponse(_input_proto, output_proto, _config):
33 """Populate a fake BuildSdkTarballResponse."""
34 output_proto.sdk_tarball_path.path = "/fake/sdk/tarball.tar.gz"
35 output_proto.sdk_tarball_path.location = common_pb2.Path.OUTSIDE
36
37
38@faux.success(_BuildSdkTarballResponse)
39@validate.require("chroot")
40@validate.validation_complete
41def BuildSdkTarball(
42 input_proto: "BuildSdkTarballRequest",
43 output_proto: "BuildSdkTarballResponse",
44 _config: "api_config.ApiConfig",
45) -> None:
46 chroot = controller_util.ParseChroot(input_proto.chroot)
47 output_proto.sdk_tarball_path.path = str(sdk.BuildSdkTarball(chroot))
48 output_proto.sdk_tarball_path.location = common_pb2.Path.OUTSIDE
49
50
Greg Edelston01ae5942023-01-30 16:26:54 -070051def _CreateManifestFromSdkResponse(_input_proto, output_proto, _config):
52 """Populate a fake CreateManifestFromSdkResponse."""
53 output_proto.manifest_path.path = "/fake/sdk/tarball.tar.gz.Manifest"
54 output_proto.manifest_path.location = common_pb2.Path.Location.INSIDE
55
56
57@faux.success(_CreateManifestFromSdkResponse)
58@validate.require("chroot")
59@validate.require("sdk_path")
60@validate.require("dest_dir")
61@validate.validation_complete
62def CreateManifestFromSdk(
63 input_proto: "CreateManifestFromSdkRequest",
64 output_proto: "CreateManifestFromSdkResponse",
65 _config: "api_config.ApiConfig",
66) -> None:
67 """Create a manifest file showing the ebuilds in an SDK."""
68
69 def _assert_path_is_absolute(path: str, name: str):
70 """Raise an exception if the given path is not absolute."""
71 if not os.path.isabs(path):
72 cros_build_lib.Die(f"The {name} must be absolute; got {path}")
73
74 _assert_path_is_absolute(input_proto.chroot.path, "chroot path")
75 _assert_path_is_absolute(input_proto.sdk_path.path, "SDK path")
76 _assert_path_is_absolute(input_proto.dest_dir.path, "destination directory")
77
Greg Edelston1f5deb62023-03-31 14:22:08 -060078 sdk_path = controller_util.pb2_path_to_pathlib_path(
Greg Edelston01ae5942023-01-30 16:26:54 -070079 input_proto.sdk_path, input_proto.chroot
80 )
Greg Edelston1f5deb62023-03-31 14:22:08 -060081 dest_dir = controller_util.pb2_path_to_pathlib_path(
Greg Edelston01ae5942023-01-30 16:26:54 -070082 input_proto.dest_dir, input_proto.chroot
83 )
84
85 manifest_path = sdk.CreateManifestFromSdk(sdk_path, dest_dir)
86 output_proto.manifest_path.path = str(manifest_path)
87 output_proto.manifest_path.location = common_pb2.Path.Location.OUTSIDE
88
89
Alex Klein076841b2019-08-29 15:19:39 -060090@faux.success(_ChrootVersionResponse)
91@faux.empty_error
Alex Klein1699fab2022-09-08 08:46:06 -060092def Create(
93 input_proto: "CreateRequest",
94 output_proto: "CreateResponse",
95 config: "api_config.ApiConfig",
96) -> Union[int, None]:
97 """Chroot creation, includes support for replacing an existing chroot.
Alex Klein19c4cc42019-02-27 14:47:57 -070098
Alex Klein1699fab2022-09-08 08:46:06 -060099 Args:
Alex Klein611dddd2022-10-11 17:02:01 -0600100 input_proto: The input proto.
101 output_proto: The output proto.
102 config: The API call config.
Kevin Shelton50dabff2022-04-09 11:29:53 -0700103
Alex Klein1699fab2022-09-08 08:46:06 -0600104 Returns:
Alex Klein611dddd2022-10-11 17:02:01 -0600105 An error code, None otherwise.
Alex Klein1699fab2022-09-08 08:46:06 -0600106 """
107 replace = not input_proto.flags.no_replace
108 bootstrap = input_proto.flags.bootstrap
Brian Norris67374d82023-05-01 12:31:12 -0700109 chroot = controller_util.ParseChroot(input_proto.chroot)
Alex Klein19c4cc42019-02-27 14:47:57 -0700110
Alex Klein1699fab2022-09-08 08:46:06 -0600111 sdk_version = input_proto.sdk_version
112 skip_chroot_upgrade = input_proto.skip_chroot_upgrade
Alex Klein19c4cc42019-02-27 14:47:57 -0700113
Alex Klein1699fab2022-09-08 08:46:06 -0600114 if config.validate_only:
115 return controller.RETURN_CODE_VALID_INPUT
Alex Klein231d2da2019-07-22 16:44:45 -0600116
Alex Klein1699fab2022-09-08 08:46:06 -0600117 args = sdk.CreateArguments(
118 replace=replace,
119 bootstrap=bootstrap,
Brian Norris67374d82023-05-01 12:31:12 -0700120 chroot=chroot,
Alex Klein1699fab2022-09-08 08:46:06 -0600121 sdk_version=sdk_version,
122 skip_chroot_upgrade=skip_chroot_upgrade,
123 )
Alex Klein19c4cc42019-02-27 14:47:57 -0700124
Alex Klein1699fab2022-09-08 08:46:06 -0600125 version = sdk.Create(args)
Alex Klein19c4cc42019-02-27 14:47:57 -0700126
Alex Klein1699fab2022-09-08 08:46:06 -0600127 if version:
128 output_proto.version.version = version
129 else:
130 # This should be very rare, if ever used, but worth noting.
131 cros_build_lib.Die(
132 "No chroot version could be found. There was likely an"
133 "error creating the chroot that was not detected."
134 )
Alex Kleinaa5c4172019-02-27 17:12:20 -0700135
136
Alex Klein076841b2019-08-29 15:19:39 -0600137@faux.success(_ChrootVersionResponse)
138@faux.empty_error
Alex Klein1699fab2022-09-08 08:46:06 -0600139@validate.require_each("toolchain_targets", ["name"])
Alex Klein231d2da2019-07-22 16:44:45 -0600140@validate.validation_complete
Alex Klein1699fab2022-09-08 08:46:06 -0600141def Update(
142 input_proto: "UpdateRequest",
143 output_proto: "UpdateResponse",
144 _config: "api_config.ApiConfig",
145):
146 """Update the chroot.
Alex Kleinaa5c4172019-02-27 17:12:20 -0700147
Alex Klein1699fab2022-09-08 08:46:06 -0600148 Args:
Alex Klein611dddd2022-10-11 17:02:01 -0600149 input_proto: The input proto.
150 output_proto: The output proto.
151 _config: The API call config.
Alex Klein1699fab2022-09-08 08:46:06 -0600152 """
153 build_source = input_proto.flags.build_source
154 targets = [target.name for target in input_proto.toolchain_targets]
155 toolchain_changed = input_proto.flags.toolchain_changed
Alex Kleinaa5c4172019-02-27 17:12:20 -0700156
Alex Klein1699fab2022-09-08 08:46:06 -0600157 args = sdk.UpdateArguments(
158 build_source=build_source,
159 toolchain_targets=targets,
160 toolchain_changed=toolchain_changed,
161 )
Chris McDonald68faa2a2020-01-13 12:23:05 -0700162
Alex Klein1699fab2022-09-08 08:46:06 -0600163 version = sdk.Update(args)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700164
Alex Klein1699fab2022-09-08 08:46:06 -0600165 if version:
166 output_proto.version.version = version
167 else:
168 # This should be very rare, if ever used, but worth noting.
169 cros_build_lib.Die(
170 "No chroot version could be found. There was likely an"
171 "error creating the chroot that was not detected."
172 )
Alex Klein730cf552019-10-16 11:28:22 -0600173
174
175@faux.all_empty
Greg Edelston6733dc52023-02-15 15:20:07 -0700176@validate.require("binhost_gs_bucket")
177@validate.validation_complete
178def Uprev(input_proto, output_proto, _config):
Greg Edelston40aea812023-03-27 16:34:35 -0600179 """Update SDK version file and prebuilt files to point to the latest SDK.
180
181 Files will be changed locally, but not committed.
182 """
183 # If the UprevRequest did not specify a target version,
184 # check the remote SDK version file on Google Cloud Storage for the latest
185 # uprev target.
Greg Edelstond401e5a2023-04-28 15:29:11 -0600186 target_version = (
187 input_proto.version or sdk.get_latest_uprev_target_version()
188 )
Greg Edelston40aea812023-03-27 16:34:35 -0600189
190 # The main uprev logic occurs in service/sdk.py.
Greg Edelstond401e5a2023-04-28 15:29:11 -0600191 modified_files = sdk.uprev_sdk_and_prebuilts(
Greg Edelston6733dc52023-02-15 15:20:07 -0700192 binhost_gs_bucket=input_proto.binhost_gs_bucket,
193 version=target_version,
194 )
Greg Edelston40aea812023-03-27 16:34:35 -0600195
196 # Populate the UprevResponse object with the modified files.
Greg Edelston6733dc52023-02-15 15:20:07 -0700197 for modified_file in modified_files:
Greg Edelston40aea812023-03-27 16:34:35 -0600198 proto_path = output_proto.modified_files.add()
199 proto_path.path = str(modified_file)
200 proto_path.location = common_pb2.Path.OUTSIDE
Greg Edelston6733dc52023-02-15 15:20:07 -0700201 output_proto.version = target_version
202
203
204@faux.all_empty
Alex Klein730cf552019-10-16 11:28:22 -0600205@validate.validation_complete
206def Delete(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600207 """Delete a chroot."""
208 chroot = controller_util.ParseChroot(input_proto.chroot)
209 sdk.Delete(chroot, force=True)
Chris McDonald53ad5442020-01-17 14:11:55 -0700210
211
212@faux.all_empty
213@validate.validation_complete
Brian Norrisf624bf42023-03-02 12:57:49 -0800214def Unmount(_input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600215 """Unmount a chroot"""
Brian Norrisf624bf42023-03-02 12:57:49 -0800216 # Deprecated. Do nothing.
Chris McDonaldf48ea202020-01-29 13:19:23 -0700217
218
219@faux.all_empty
Alex Klein1699fab2022-09-08 08:46:06 -0600220@validate.require("path.path")
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600221@validate.validation_complete
222def UnmountPath(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600223 """Unmount a path"""
224 sdk.UnmountPath(input_proto.path.path)
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600225
226
227@faux.all_empty
Chris McDonaldf48ea202020-01-29 13:19:23 -0700228@validate.validation_complete
229def Clean(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600230 """Clean unneeded files from a chroot."""
231 chroot = controller_util.ParseChroot(input_proto.chroot)
George Engelbrecht86cfae62023-04-05 10:57:41 -0600232
233 # Default (flagless) call sets 'safe' and 'sysroots'.
234 if not (
235 input_proto.safe
236 or input_proto.images
237 or input_proto.sysroots
238 or input_proto.tmp
239 or input_proto.cache
240 or input_proto.logs
241 or input_proto.workdirs
242 or input_proto.incrementals
243 ):
244 sdk.Clean(chroot, safe=True, sysroots=True)
245 else:
246 sdk.Clean(
247 chroot,
248 safe=input_proto.safe,
249 images=input_proto.images,
250 sysroots=input_proto.sysroots,
251 tmp=input_proto.tmp,
252 cache=input_proto.cache,
253 logs=input_proto.logs,
254 workdirs=input_proto.workdirs,
255 incrementals=input_proto.incrementals,
256 )
Chris McDonald9d486802020-01-29 15:57:22 -0700257
258
259@faux.all_empty
260@validate.validation_complete
Bob Haarmand1225ea2022-01-19 22:01:42 +0000261def BuildPrebuilts(input_proto, _output_proto, _config):
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700262 """Build the binary packages that comprise the Chromium OS SDK."""
Alex Klein1699fab2022-09-08 08:46:06 -0600263 chroot = controller_util.ParseChroot(input_proto.chroot)
Greg Edelston199c5362023-01-04 12:29:07 -0700264 sdk.BuildPrebuilts(chroot, board=input_proto.build_target.name)
Bob Haarmand1225ea2022-01-19 22:01:42 +0000265
266
Bob Haarman171d4092023-01-10 16:44:19 +0000267@faux.success(_BinhostCLs)
Bob Haarmanc0082602022-09-20 16:12:43 -0700268@faux.empty_error
Bob Haarman0853ce92022-12-13 18:11:53 +0000269@validate.require(
270 "prepend_version", "version", "upload_location", "sdk_tarball_template"
271)
Bob Haarmand1225ea2022-01-19 22:01:42 +0000272@validate.validation_complete
Bob Haarmanc0082602022-09-20 16:12:43 -0700273def CreateBinhostCLs(
274 input_proto: "CreateBinhostCLsRequest",
275 output_proto: "CreateBinhostCLsResponse",
276 _config: "api_config.ApiConfig",
277) -> None:
Alex Klein1699fab2022-09-08 08:46:06 -0600278 """Create CLs to update the binhost to point at uploaded prebuilts."""
Bob Haarman171d4092023-01-10 16:44:19 +0000279 output_proto.cls.extend(
280 sdk.CreateBinhostCLs(
281 input_proto.prepend_version,
282 input_proto.version,
283 input_proto.upload_location,
284 input_proto.sdk_tarball_template,
285 )
Alex Klein1699fab2022-09-08 08:46:06 -0600286 )
Bob Haarman338f8b72022-02-04 01:02:46 +0000287
288
289@faux.all_empty
Alex Klein1699fab2022-09-08 08:46:06 -0600290@validate.require("prepend_version", "version", "upload_location")
Bob Haarman338f8b72022-02-04 01:02:46 +0000291@validate.validation_complete
Bob Haarmand1225ea2022-01-19 22:01:42 +0000292def UploadPrebuiltPackages(input_proto, _output_proto, _config):
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700293 """Upload prebuilt packages."""
Alex Klein1699fab2022-09-08 08:46:06 -0600294 sdk.UploadPrebuiltPackages(
295 controller_util.ParseChroot(input_proto.chroot),
296 input_proto.prepend_version,
297 input_proto.version,
298 input_proto.upload_location,
299 )
Greg Edelston9dcdc8a2023-01-11 17:07:10 -0700300
301
302@faux.all_empty
303@validate.require("chroot")
304@validate.validation_complete
305def BuildSdkToolchain(input_proto, output_proto, _config):
306 """Build cross-compiler packages for the SDK."""
307 chroot = controller_util.ParseChroot(input_proto.chroot)
308 extra_env: Dict[str, str] = {}
309 if input_proto.use_flags:
310 extra_env["USE"] = " ".join(use.flag for use in input_proto.use_flags)
311 generated_files = sdk.BuildSdkToolchain(chroot, extra_env=extra_env)
312 output_proto.generated_files.extend(generated_files)