blob: 2249d60dd4b8ae1ca3cc8ecafdd2c993baf51535 [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
23@faux.success(_ChrootVersionResponse)
24@faux.empty_error
Alex Klein1699fab2022-09-08 08:46:06 -060025def Create(
26 input_proto: "CreateRequest",
27 output_proto: "CreateResponse",
28 config: "api_config.ApiConfig",
29) -> Union[int, None]:
30 """Chroot creation, includes support for replacing an existing chroot.
Alex Klein19c4cc42019-02-27 14:47:57 -070031
Alex Klein1699fab2022-09-08 08:46:06 -060032 Args:
33 input_proto: The input proto.
34 output_proto: The output proto.
35 config: The API call config.
Kevin Shelton50dabff2022-04-09 11:29:53 -070036
Alex Klein1699fab2022-09-08 08:46:06 -060037 Returns:
38 An error code, None otherwise.
39 """
40 replace = not input_proto.flags.no_replace
41 bootstrap = input_proto.flags.bootstrap
42 use_image = not input_proto.flags.no_use_image
Alex Klein19c4cc42019-02-27 14:47:57 -070043
Alex Klein1699fab2022-09-08 08:46:06 -060044 chroot_path = input_proto.chroot.path
45 cache_dir = input_proto.chroot.cache_dir
46 sdk_version = input_proto.sdk_version
47 skip_chroot_upgrade = input_proto.skip_chroot_upgrade
Alex Klein19c4cc42019-02-27 14:47:57 -070048
Alex Klein1699fab2022-09-08 08:46:06 -060049 if chroot_path and not os.path.isabs(chroot_path):
50 cros_build_lib.Die("The chroot path must be absolute.")
Alex Klein19c4cc42019-02-27 14:47:57 -070051
Alex Klein1699fab2022-09-08 08:46:06 -060052 if config.validate_only:
53 return controller.RETURN_CODE_VALID_INPUT
Alex Klein231d2da2019-07-22 16:44:45 -060054
Alex Klein1699fab2022-09-08 08:46:06 -060055 args = sdk.CreateArguments(
56 replace=replace,
57 bootstrap=bootstrap,
58 use_image=use_image,
59 cache_dir=cache_dir,
60 chroot_path=chroot_path,
61 sdk_version=sdk_version,
62 skip_chroot_upgrade=skip_chroot_upgrade,
63 )
Alex Klein19c4cc42019-02-27 14:47:57 -070064
Alex Klein1699fab2022-09-08 08:46:06 -060065 version = sdk.Create(args)
Alex Klein19c4cc42019-02-27 14:47:57 -070066
Alex Klein1699fab2022-09-08 08:46:06 -060067 if version:
68 output_proto.version.version = version
69 else:
70 # This should be very rare, if ever used, but worth noting.
71 cros_build_lib.Die(
72 "No chroot version could be found. There was likely an"
73 "error creating the chroot that was not detected."
74 )
Alex Kleinaa5c4172019-02-27 17:12:20 -070075
76
Alex Klein076841b2019-08-29 15:19:39 -060077@faux.success(_ChrootVersionResponse)
78@faux.empty_error
Alex Klein1699fab2022-09-08 08:46:06 -060079@validate.require_each("toolchain_targets", ["name"])
Alex Klein231d2da2019-07-22 16:44:45 -060080@validate.validation_complete
Alex Klein1699fab2022-09-08 08:46:06 -060081def Update(
82 input_proto: "UpdateRequest",
83 output_proto: "UpdateResponse",
84 _config: "api_config.ApiConfig",
85):
86 """Update the chroot.
Alex Kleinaa5c4172019-02-27 17:12:20 -070087
Alex Klein1699fab2022-09-08 08:46:06 -060088 Args:
89 input_proto: The input proto.
90 output_proto: The output proto.
91 _config: The API call config.
92 """
93 build_source = input_proto.flags.build_source
94 targets = [target.name for target in input_proto.toolchain_targets]
95 toolchain_changed = input_proto.flags.toolchain_changed
Alex Kleinaa5c4172019-02-27 17:12:20 -070096
Alex Klein1699fab2022-09-08 08:46:06 -060097 args = sdk.UpdateArguments(
98 build_source=build_source,
99 toolchain_targets=targets,
100 toolchain_changed=toolchain_changed,
101 )
Chris McDonald68faa2a2020-01-13 12:23:05 -0700102
Alex Klein1699fab2022-09-08 08:46:06 -0600103 version = sdk.Update(args)
Alex Kleinaa5c4172019-02-27 17:12:20 -0700104
Alex Klein1699fab2022-09-08 08:46:06 -0600105 if version:
106 output_proto.version.version = version
107 else:
108 # This should be very rare, if ever used, but worth noting.
109 cros_build_lib.Die(
110 "No chroot version could be found. There was likely an"
111 "error creating the chroot that was not detected."
112 )
Alex Klein730cf552019-10-16 11:28:22 -0600113
114
115@faux.all_empty
116@validate.validation_complete
117def Delete(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600118 """Delete a chroot."""
119 chroot = controller_util.ParseChroot(input_proto.chroot)
120 sdk.Delete(chroot, force=True)
Chris McDonald53ad5442020-01-17 14:11:55 -0700121
122
123@faux.all_empty
124@validate.validation_complete
125def Unmount(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600126 """Unmount a chroot"""
127 chroot = controller_util.ParseChroot(input_proto.chroot)
128 sdk.Unmount(chroot)
Chris McDonaldf48ea202020-01-29 13:19:23 -0700129
130
131@faux.all_empty
Alex Klein1699fab2022-09-08 08:46:06 -0600132@validate.require("path.path")
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600133@validate.validation_complete
134def UnmountPath(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600135 """Unmount a path"""
136 sdk.UnmountPath(input_proto.path.path)
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600137
138
139@faux.all_empty
Chris McDonaldf48ea202020-01-29 13:19:23 -0700140@validate.validation_complete
141def Clean(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600142 """Clean unneeded files from a chroot."""
143 chroot = controller_util.ParseChroot(input_proto.chroot)
144 sdk.Clean(chroot, safe=True, sysroots=True)
Chris McDonald9d486802020-01-29 15:57:22 -0700145
146
147@faux.all_empty
148@validate.validation_complete
149def CreateSnapshot(input_proto, output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600150 """Create a chroot snapshot and return a corresponding opaque snapshot key."""
151 chroot = controller_util.ParseChroot(input_proto.chroot)
152 token = sdk.CreateSnapshot(chroot, replace_if_needed=True)
153 output_proto.snapshot_token.value = token
Chris McDonald9d486802020-01-29 15:57:22 -0700154
155
156@faux.all_empty
157@validate.validation_complete
158def RestoreSnapshot(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600159 """Restore a chroot snapshot from a snapshot key."""
160 chroot = controller_util.ParseChroot(input_proto.chroot)
161 token = input_proto.snapshot_token.value
162 sdk.RestoreSnapshot(token, chroot)
Bob Haarmand1225ea2022-01-19 22:01:42 +0000163
164
165@faux.all_empty
166@validate.validation_complete
167def BuildPrebuilts(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600168 """Builds the binary packages that comprise the Chromium OS SDK."""
169 chroot = controller_util.ParseChroot(input_proto.chroot)
170 sdk.BuildPrebuilts(chroot)
Bob Haarmand1225ea2022-01-19 22:01:42 +0000171
172
173@faux.all_empty
Alex Klein1699fab2022-09-08 08:46:06 -0600174@validate.require("prepend_version", "version", "upload_location")
Bob Haarmand1225ea2022-01-19 22:01:42 +0000175@validate.validation_complete
Bob Haarman338f8b72022-02-04 01:02:46 +0000176def CreateBinhostCLs(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600177 """Create CLs to update the binhost to point at uploaded prebuilts."""
178 sdk.CreateBinhostCLs(
179 input_proto.prepend_version,
180 input_proto.version,
181 input_proto.upload_location,
182 )
Bob Haarman338f8b72022-02-04 01:02:46 +0000183
184
185@faux.all_empty
Alex Klein1699fab2022-09-08 08:46:06 -0600186@validate.require("prepend_version", "version", "upload_location")
Bob Haarman338f8b72022-02-04 01:02:46 +0000187@validate.validation_complete
Bob Haarmand1225ea2022-01-19 22:01:42 +0000188def UploadPrebuiltPackages(input_proto, _output_proto, _config):
Alex Klein1699fab2022-09-08 08:46:06 -0600189 """Uploads prebuilt packages."""
190 sdk.UploadPrebuiltPackages(
191 controller_util.ParseChroot(input_proto.chroot),
192 input_proto.prepend_version,
193 input_proto.version,
194 input_proto.upload_location,
195 )