blob: cde767f34cf78c94c350bc660ae4c84ae57c04d8 [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
Kevin Shelton50dabff2022-04-09 11:29:53 -07008from typing import 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
Alex Klein19c4cc42019-02-27 14:47:57 -070014from chromite.lib import cros_build_lib
15from chromite.service import sdk
16
17
Alex Klein076841b2019-08-29 15:19:39 -060018def _ChrootVersionResponse(_input_proto, output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -060019 """Add a fake chroot version to a successful response."""
20 output_proto.version.version = 168
Alex Klein076841b2019-08-29 15:19:39 -060021
22
Bob Haarmanc0082602022-09-20 16:12:43 -070023def _CLUris(_input_proto, output_proto, _config):
24 """Add fake CL URIs to a successful response."""
25 output_proto.cls = [
26 "https://crrev.com/fakecl/1",
27 "https://crrev.com/fakecl/2",
28 ]
29
30
Alex Klein076841b2019-08-29 15:19:39 -060031@faux.success(_ChrootVersionResponse)
32@faux.empty_error
Alex Klein1699fab2022-09-08 08:46:06 -060033def Create(
34 input_proto: "CreateRequest",
35 output_proto: "CreateResponse",
36 config: "api_config.ApiConfig",
37) -> Union[int, None]:
38 """Chroot creation, includes support for replacing an existing chroot.
Alex Klein19c4cc42019-02-27 14:47:57 -070039
Alex Klein1699fab2022-09-08 08:46:06 -060040 Args:
Alex Klein611dddd2022-10-11 17:02:01 -060041 input_proto: The input proto.
42 output_proto: The output proto.
43 config: The API call config.
Kevin Shelton50dabff2022-04-09 11:29:53 -070044
Alex Klein1699fab2022-09-08 08:46:06 -060045 Returns:
Alex Klein611dddd2022-10-11 17:02:01 -060046 An error code, None otherwise.
Alex Klein1699fab2022-09-08 08:46:06 -060047 """
48 replace = not input_proto.flags.no_replace
49 bootstrap = input_proto.flags.bootstrap
50 use_image = not input_proto.flags.no_use_image
Alex Klein19c4cc42019-02-27 14:47:57 -070051
Alex Klein1699fab2022-09-08 08:46:06 -060052 chroot_path = input_proto.chroot.path
53 cache_dir = input_proto.chroot.cache_dir
54 sdk_version = input_proto.sdk_version
55 skip_chroot_upgrade = input_proto.skip_chroot_upgrade
Alex Klein19c4cc42019-02-27 14:47:57 -070056
Alex Klein1699fab2022-09-08 08:46:06 -060057 if chroot_path and not os.path.isabs(chroot_path):
58 cros_build_lib.Die("The chroot path must be absolute.")
Alex Klein19c4cc42019-02-27 14:47:57 -070059
Alex Klein1699fab2022-09-08 08:46:06 -060060 if config.validate_only:
61 return controller.RETURN_CODE_VALID_INPUT
Alex Klein231d2da2019-07-22 16:44:45 -060062
Alex Klein1699fab2022-09-08 08:46:06 -060063 args = sdk.CreateArguments(
64 replace=replace,
65 bootstrap=bootstrap,
66 use_image=use_image,
67 cache_dir=cache_dir,
68 chroot_path=chroot_path,
69 sdk_version=sdk_version,
70 skip_chroot_upgrade=skip_chroot_upgrade,
71 )
Alex Klein19c4cc42019-02-27 14:47:57 -070072
Alex Klein1699fab2022-09-08 08:46:06 -060073 version = sdk.Create(args)
Alex Klein19c4cc42019-02-27 14:47:57 -070074
Alex Klein1699fab2022-09-08 08:46:06 -060075 if version:
76 output_proto.version.version = version
77 else:
78 # This should be very rare, if ever used, but worth noting.
79 cros_build_lib.Die(
80 "No chroot version could be found. There was likely an"
81 "error creating the chroot that was not detected."
82 )
Alex Kleinaa5c4172019-02-27 17:12:20 -070083
84
Alex Klein076841b2019-08-29 15:19:39 -060085@faux.success(_ChrootVersionResponse)
86@faux.empty_error
Alex Klein1699fab2022-09-08 08:46:06 -060087@validate.require_each("toolchain_targets", ["name"])
Alex Klein231d2da2019-07-22 16:44:45 -060088@validate.validation_complete
Alex Klein1699fab2022-09-08 08:46:06 -060089def Update(
90 input_proto: "UpdateRequest",
91 output_proto: "UpdateResponse",
92 _config: "api_config.ApiConfig",
93):
94 """Update the chroot.
Alex Kleinaa5c4172019-02-27 17:12:20 -070095
Alex Klein1699fab2022-09-08 08:46:06 -060096 Args:
Alex Klein611dddd2022-10-11 17:02:01 -060097 input_proto: The input proto.
98 output_proto: The output proto.
99 _config: The API call config.
Alex Klein1699fab2022-09-08 08:46:06 -0600100 """
101 build_source = input_proto.flags.build_source
102 targets = [target.name for target in input_proto.toolchain_targets]
103 toolchain_changed = input_proto.flags.toolchain_changed
Alex Kleinaa5c4172019-02-27 17:12:20 -0700104
Alex Klein1699fab2022-09-08 08:46:06 -0600105 args = sdk.UpdateArguments(
106 build_source=build_source,
107 toolchain_targets=targets,
108 toolchain_changed=toolchain_changed,
109 )
Chris McDonald68faa2a2020-01-13 12:23:05 -0700110
Alex Klein1699fab2022-09-08 08:46:06 -0600111 version = sdk.Update(args)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700112
Alex Klein1699fab2022-09-08 08:46:06 -0600113 if version:
114 output_proto.version.version = version
115 else:
116 # This should be very rare, if ever used, but worth noting.
117 cros_build_lib.Die(
118 "No chroot version could be found. There was likely an"
119 "error creating the chroot that was not detected."
120 )
Alex Klein730cf552019-10-16 11:28:22 -0600121
122
123@faux.all_empty
124@validate.validation_complete
125def Delete(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600126 """Delete a chroot."""
127 chroot = controller_util.ParseChroot(input_proto.chroot)
128 sdk.Delete(chroot, force=True)
Chris McDonald53ad5442020-01-17 14:11:55 -0700129
130
131@faux.all_empty
132@validate.validation_complete
133def Unmount(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600134 """Unmount a chroot"""
135 chroot = controller_util.ParseChroot(input_proto.chroot)
136 sdk.Unmount(chroot)
Chris McDonaldf48ea202020-01-29 13:19:23 -0700137
138
139@faux.all_empty
Alex Klein1699fab2022-09-08 08:46:06 -0600140@validate.require("path.path")
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600141@validate.validation_complete
142def UnmountPath(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600143 """Unmount a path"""
144 sdk.UnmountPath(input_proto.path.path)
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600145
146
147@faux.all_empty
Chris McDonaldf48ea202020-01-29 13:19:23 -0700148@validate.validation_complete
149def Clean(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600150 """Clean unneeded files from a chroot."""
151 chroot = controller_util.ParseChroot(input_proto.chroot)
152 sdk.Clean(chroot, safe=True, sysroots=True)
Chris McDonald9d486802020-01-29 15:57:22 -0700153
154
155@faux.all_empty
156@validate.validation_complete
157def CreateSnapshot(input_proto, output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600158 """Create a chroot snapshot and return a corresponding opaque snapshot key."""
159 chroot = controller_util.ParseChroot(input_proto.chroot)
160 token = sdk.CreateSnapshot(chroot, replace_if_needed=True)
161 output_proto.snapshot_token.value = token
Chris McDonald9d486802020-01-29 15:57:22 -0700162
163
164@faux.all_empty
165@validate.validation_complete
166def RestoreSnapshot(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600167 """Restore a chroot snapshot from a snapshot key."""
168 chroot = controller_util.ParseChroot(input_proto.chroot)
169 token = input_proto.snapshot_token.value
170 sdk.RestoreSnapshot(token, chroot)
Bob Haarmand1225ea2022-01-19 22:01:42 +0000171
172
173@faux.all_empty
174@validate.validation_complete
175def BuildPrebuilts(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600176 """Builds the binary packages that comprise the Chromium OS SDK."""
177 chroot = controller_util.ParseChroot(input_proto.chroot)
Greg Edelston199c5362023-01-04 12:29:07 -0700178 sdk.BuildPrebuilts(chroot, board=input_proto.build_target.name)
Bob Haarmand1225ea2022-01-19 22:01:42 +0000179
180
Bob Haarmanc0082602022-09-20 16:12:43 -0700181@faux.success(_CLUris)
182@faux.empty_error
Alex Klein1699fab2022-09-08 08:46:06 -0600183@validate.require("prepend_version", "version", "upload_location")
Bob Haarmand1225ea2022-01-19 22:01:42 +0000184@validate.validation_complete
Bob Haarmanc0082602022-09-20 16:12:43 -0700185def CreateBinhostCLs(
186 input_proto: "CreateBinhostCLsRequest",
187 output_proto: "CreateBinhostCLsResponse",
188 _config: "api_config.ApiConfig",
189) -> None:
Alex Klein1699fab2022-09-08 08:46:06 -0600190 """Create CLs to update the binhost to point at uploaded prebuilts."""
Bob Haarmanc0082602022-09-20 16:12:43 -0700191 uris = sdk.CreateBinhostCLs(
Alex Klein1699fab2022-09-08 08:46:06 -0600192 input_proto.prepend_version,
193 input_proto.version,
194 input_proto.upload_location,
195 )
Bob Haarmanc0082602022-09-20 16:12:43 -0700196 output_proto.cls = uris
Bob Haarman338f8b72022-02-04 01:02:46 +0000197
198
199@faux.all_empty
Alex Klein1699fab2022-09-08 08:46:06 -0600200@validate.require("prepend_version", "version", "upload_location")
Bob Haarman338f8b72022-02-04 01:02:46 +0000201@validate.validation_complete
Bob Haarmand1225ea2022-01-19 22:01:42 +0000202def UploadPrebuiltPackages(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600203 """Uploads prebuilt packages."""
204 sdk.UploadPrebuiltPackages(
205 controller_util.ParseChroot(input_proto.chroot),
206 input_proto.prepend_version,
207 input_proto.version,
208 input_proto.upload_location,
209 )