Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2019 The ChromiumOS Authors |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 2 | # 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 Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 7 | import os |
Greg Edelston | 9dcdc8a | 2023-01-11 17:07:10 -0700 | [diff] [blame] | 8 | from typing import Dict, Union |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 9 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 10 | from chromite.api import controller |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 11 | from chromite.api import faux |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 12 | from chromite.api import validate |
Alex Klein | 71a9a1d | 2019-10-28 15:45:10 -0600 | [diff] [blame] | 13 | from chromite.api.controller import controller_util |
Bob Haarman | 0853ce9 | 2022-12-13 18:11:53 +0000 | [diff] [blame] | 14 | from chromite.api.gen.chromiumos import common_pb2 |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 15 | from chromite.lib import cros_build_lib |
Greg Edelston | 01ae594 | 2023-01-30 16:26:54 -0700 | [diff] [blame] | 16 | from chromite.lib import path_util |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 17 | from chromite.service import sdk |
| 18 | |
| 19 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 20 | def _ChrootVersionResponse(_input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 21 | """Add a fake chroot version to a successful response.""" |
| 22 | output_proto.version.version = 168 |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 23 | |
| 24 | |
Bob Haarman | 171d409 | 2023-01-10 16:44:19 +0000 | [diff] [blame] | 25 | def _BinhostCLs(_input_proto, output_proto, _config): |
| 26 | """Add fake CL identifiers to a successful response.""" |
Bob Haarman | c008260 | 2022-09-20 16:12:43 -0700 | [diff] [blame] | 27 | output_proto.cls = [ |
Bob Haarman | 171d409 | 2023-01-10 16:44:19 +0000 | [diff] [blame] | 28 | "fakecl:1", |
| 29 | "fakecl:2", |
Bob Haarman | c008260 | 2022-09-20 16:12:43 -0700 | [diff] [blame] | 30 | ] |
| 31 | |
| 32 | |
Bob Haarman | 0853ce9 | 2022-12-13 18:11:53 +0000 | [diff] [blame] | 33 | def _BuildSdkTarballResponse(_input_proto, output_proto, _config): |
| 34 | """Populate a fake BuildSdkTarballResponse.""" |
| 35 | output_proto.sdk_tarball_path.path = "/fake/sdk/tarball.tar.gz" |
| 36 | output_proto.sdk_tarball_path.location = common_pb2.Path.OUTSIDE |
| 37 | |
| 38 | |
| 39 | @faux.success(_BuildSdkTarballResponse) |
| 40 | @validate.require("chroot") |
| 41 | @validate.validation_complete |
| 42 | def BuildSdkTarball( |
| 43 | input_proto: "BuildSdkTarballRequest", |
| 44 | output_proto: "BuildSdkTarballResponse", |
| 45 | _config: "api_config.ApiConfig", |
| 46 | ) -> None: |
| 47 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 48 | output_proto.sdk_tarball_path.path = str(sdk.BuildSdkTarball(chroot)) |
| 49 | output_proto.sdk_tarball_path.location = common_pb2.Path.OUTSIDE |
| 50 | |
| 51 | |
Greg Edelston | 01ae594 | 2023-01-30 16:26:54 -0700 | [diff] [blame] | 52 | def _CreateManifestFromSdkResponse(_input_proto, output_proto, _config): |
| 53 | """Populate a fake CreateManifestFromSdkResponse.""" |
| 54 | output_proto.manifest_path.path = "/fake/sdk/tarball.tar.gz.Manifest" |
| 55 | output_proto.manifest_path.location = common_pb2.Path.Location.INSIDE |
| 56 | |
| 57 | |
| 58 | @faux.success(_CreateManifestFromSdkResponse) |
| 59 | @validate.require("chroot") |
| 60 | @validate.require("sdk_path") |
| 61 | @validate.require("dest_dir") |
| 62 | @validate.validation_complete |
| 63 | def CreateManifestFromSdk( |
| 64 | input_proto: "CreateManifestFromSdkRequest", |
| 65 | output_proto: "CreateManifestFromSdkResponse", |
| 66 | _config: "api_config.ApiConfig", |
| 67 | ) -> None: |
| 68 | """Create a manifest file showing the ebuilds in an SDK.""" |
| 69 | |
| 70 | def _assert_path_is_absolute(path: str, name: str): |
| 71 | """Raise an exception if the given path is not absolute.""" |
| 72 | if not os.path.isabs(path): |
| 73 | cros_build_lib.Die(f"The {name} must be absolute; got {path}") |
| 74 | |
| 75 | _assert_path_is_absolute(input_proto.chroot.path, "chroot path") |
| 76 | _assert_path_is_absolute(input_proto.sdk_path.path, "SDK path") |
| 77 | _assert_path_is_absolute(input_proto.dest_dir.path, "destination directory") |
| 78 | |
| 79 | sdk_path = path_util.ProtoPathToPathlibPath( |
| 80 | input_proto.sdk_path, input_proto.chroot |
| 81 | ) |
| 82 | dest_dir = path_util.ProtoPathToPathlibPath( |
| 83 | input_proto.dest_dir, input_proto.chroot |
| 84 | ) |
| 85 | |
| 86 | manifest_path = sdk.CreateManifestFromSdk(sdk_path, dest_dir) |
| 87 | output_proto.manifest_path.path = str(manifest_path) |
| 88 | output_proto.manifest_path.location = common_pb2.Path.Location.OUTSIDE |
| 89 | |
| 90 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 91 | @faux.success(_ChrootVersionResponse) |
| 92 | @faux.empty_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 93 | def Create( |
| 94 | input_proto: "CreateRequest", |
| 95 | output_proto: "CreateResponse", |
| 96 | config: "api_config.ApiConfig", |
| 97 | ) -> Union[int, None]: |
| 98 | """Chroot creation, includes support for replacing an existing chroot. |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 99 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 100 | Args: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 101 | input_proto: The input proto. |
| 102 | output_proto: The output proto. |
| 103 | config: The API call config. |
Kevin Shelton | 50dabff | 2022-04-09 11:29:53 -0700 | [diff] [blame] | 104 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 105 | Returns: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 106 | An error code, None otherwise. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 107 | """ |
| 108 | replace = not input_proto.flags.no_replace |
| 109 | bootstrap = input_proto.flags.bootstrap |
| 110 | use_image = not input_proto.flags.no_use_image |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 111 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 112 | chroot_path = input_proto.chroot.path |
| 113 | cache_dir = input_proto.chroot.cache_dir |
| 114 | sdk_version = input_proto.sdk_version |
| 115 | skip_chroot_upgrade = input_proto.skip_chroot_upgrade |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 116 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 117 | if chroot_path and not os.path.isabs(chroot_path): |
| 118 | cros_build_lib.Die("The chroot path must be absolute.") |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 119 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 120 | if config.validate_only: |
| 121 | return controller.RETURN_CODE_VALID_INPUT |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 122 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 123 | args = sdk.CreateArguments( |
| 124 | replace=replace, |
| 125 | bootstrap=bootstrap, |
| 126 | use_image=use_image, |
| 127 | cache_dir=cache_dir, |
| 128 | chroot_path=chroot_path, |
| 129 | sdk_version=sdk_version, |
| 130 | skip_chroot_upgrade=skip_chroot_upgrade, |
| 131 | ) |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 132 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 133 | version = sdk.Create(args) |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 134 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 135 | if version: |
| 136 | output_proto.version.version = version |
| 137 | else: |
| 138 | # This should be very rare, if ever used, but worth noting. |
| 139 | cros_build_lib.Die( |
| 140 | "No chroot version could be found. There was likely an" |
| 141 | "error creating the chroot that was not detected." |
| 142 | ) |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 143 | |
| 144 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 145 | @faux.success(_ChrootVersionResponse) |
| 146 | @faux.empty_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 147 | @validate.require_each("toolchain_targets", ["name"]) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 148 | @validate.validation_complete |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 149 | def Update( |
| 150 | input_proto: "UpdateRequest", |
| 151 | output_proto: "UpdateResponse", |
| 152 | _config: "api_config.ApiConfig", |
| 153 | ): |
| 154 | """Update the chroot. |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 155 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 156 | Args: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 157 | input_proto: The input proto. |
| 158 | output_proto: The output proto. |
| 159 | _config: The API call config. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 160 | """ |
| 161 | build_source = input_proto.flags.build_source |
| 162 | targets = [target.name for target in input_proto.toolchain_targets] |
| 163 | toolchain_changed = input_proto.flags.toolchain_changed |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 164 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 165 | args = sdk.UpdateArguments( |
| 166 | build_source=build_source, |
| 167 | toolchain_targets=targets, |
| 168 | toolchain_changed=toolchain_changed, |
| 169 | ) |
Chris McDonald | 68faa2a | 2020-01-13 12:23:05 -0700 | [diff] [blame] | 170 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 171 | version = sdk.Update(args) |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 172 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 173 | if version: |
| 174 | output_proto.version.version = version |
| 175 | else: |
| 176 | # This should be very rare, if ever used, but worth noting. |
| 177 | cros_build_lib.Die( |
| 178 | "No chroot version could be found. There was likely an" |
| 179 | "error creating the chroot that was not detected." |
| 180 | ) |
Alex Klein | 730cf55 | 2019-10-16 11:28:22 -0600 | [diff] [blame] | 181 | |
| 182 | |
| 183 | @faux.all_empty |
| 184 | @validate.validation_complete |
| 185 | def Delete(input_proto, _output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 186 | """Delete a chroot.""" |
| 187 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 188 | sdk.Delete(chroot, force=True) |
Chris McDonald | 53ad544 | 2020-01-17 14:11:55 -0700 | [diff] [blame] | 189 | |
| 190 | |
| 191 | @faux.all_empty |
| 192 | @validate.validation_complete |
| 193 | def Unmount(input_proto, _output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 194 | """Unmount a chroot""" |
| 195 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 196 | sdk.Unmount(chroot) |
Chris McDonald | f48ea20 | 2020-01-29 13:19:23 -0700 | [diff] [blame] | 197 | |
| 198 | |
| 199 | @faux.all_empty |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 200 | @validate.require("path.path") |
Michael Mortensen | 52a98ac | 2020-07-28 16:00:18 -0600 | [diff] [blame] | 201 | @validate.validation_complete |
| 202 | def UnmountPath(input_proto, _output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 203 | """Unmount a path""" |
| 204 | sdk.UnmountPath(input_proto.path.path) |
Michael Mortensen | 52a98ac | 2020-07-28 16:00:18 -0600 | [diff] [blame] | 205 | |
| 206 | |
| 207 | @faux.all_empty |
Chris McDonald | f48ea20 | 2020-01-29 13:19:23 -0700 | [diff] [blame] | 208 | @validate.validation_complete |
| 209 | def Clean(input_proto, _output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 210 | """Clean unneeded files from a chroot.""" |
| 211 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 212 | sdk.Clean(chroot, safe=True, sysroots=True) |
Chris McDonald | 9d48680 | 2020-01-29 15:57:22 -0700 | [diff] [blame] | 213 | |
| 214 | |
| 215 | @faux.all_empty |
| 216 | @validate.validation_complete |
| 217 | def CreateSnapshot(input_proto, output_proto, _config): |
Alex Klein | ab87ceb | 2023-01-24 12:00:51 -0700 | [diff] [blame] | 218 | """Create a chroot snapshot and return a corresponding opaque key.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 219 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 220 | token = sdk.CreateSnapshot(chroot, replace_if_needed=True) |
| 221 | output_proto.snapshot_token.value = token |
Chris McDonald | 9d48680 | 2020-01-29 15:57:22 -0700 | [diff] [blame] | 222 | |
| 223 | |
| 224 | @faux.all_empty |
| 225 | @validate.validation_complete |
| 226 | def RestoreSnapshot(input_proto, _output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 227 | """Restore a chroot snapshot from a snapshot key.""" |
| 228 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 229 | token = input_proto.snapshot_token.value |
| 230 | sdk.RestoreSnapshot(token, chroot) |
Bob Haarman | d1225ea | 2022-01-19 22:01:42 +0000 | [diff] [blame] | 231 | |
| 232 | |
| 233 | @faux.all_empty |
| 234 | @validate.validation_complete |
| 235 | def BuildPrebuilts(input_proto, _output_proto, _config): |
Greg Edelston | 9dcdc8a | 2023-01-11 17:07:10 -0700 | [diff] [blame] | 236 | """Build the binary packages that comprise the Chromium OS SDK.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 237 | chroot = controller_util.ParseChroot(input_proto.chroot) |
Greg Edelston | 199c536 | 2023-01-04 12:29:07 -0700 | [diff] [blame] | 238 | sdk.BuildPrebuilts(chroot, board=input_proto.build_target.name) |
Bob Haarman | d1225ea | 2022-01-19 22:01:42 +0000 | [diff] [blame] | 239 | |
| 240 | |
Bob Haarman | 171d409 | 2023-01-10 16:44:19 +0000 | [diff] [blame] | 241 | @faux.success(_BinhostCLs) |
Bob Haarman | c008260 | 2022-09-20 16:12:43 -0700 | [diff] [blame] | 242 | @faux.empty_error |
Bob Haarman | 0853ce9 | 2022-12-13 18:11:53 +0000 | [diff] [blame] | 243 | @validate.require( |
| 244 | "prepend_version", "version", "upload_location", "sdk_tarball_template" |
| 245 | ) |
Bob Haarman | d1225ea | 2022-01-19 22:01:42 +0000 | [diff] [blame] | 246 | @validate.validation_complete |
Bob Haarman | c008260 | 2022-09-20 16:12:43 -0700 | [diff] [blame] | 247 | def CreateBinhostCLs( |
| 248 | input_proto: "CreateBinhostCLsRequest", |
| 249 | output_proto: "CreateBinhostCLsResponse", |
| 250 | _config: "api_config.ApiConfig", |
| 251 | ) -> None: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 252 | """Create CLs to update the binhost to point at uploaded prebuilts.""" |
Bob Haarman | 171d409 | 2023-01-10 16:44:19 +0000 | [diff] [blame] | 253 | output_proto.cls.extend( |
| 254 | sdk.CreateBinhostCLs( |
| 255 | input_proto.prepend_version, |
| 256 | input_proto.version, |
| 257 | input_proto.upload_location, |
| 258 | input_proto.sdk_tarball_template, |
| 259 | ) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 260 | ) |
Bob Haarman | 338f8b7 | 2022-02-04 01:02:46 +0000 | [diff] [blame] | 261 | |
| 262 | |
| 263 | @faux.all_empty |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 264 | @validate.require("prepend_version", "version", "upload_location") |
Bob Haarman | 338f8b7 | 2022-02-04 01:02:46 +0000 | [diff] [blame] | 265 | @validate.validation_complete |
Bob Haarman | d1225ea | 2022-01-19 22:01:42 +0000 | [diff] [blame] | 266 | def UploadPrebuiltPackages(input_proto, _output_proto, _config): |
Greg Edelston | 9dcdc8a | 2023-01-11 17:07:10 -0700 | [diff] [blame] | 267 | """Upload prebuilt packages.""" |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 268 | sdk.UploadPrebuiltPackages( |
| 269 | controller_util.ParseChroot(input_proto.chroot), |
| 270 | input_proto.prepend_version, |
| 271 | input_proto.version, |
| 272 | input_proto.upload_location, |
| 273 | ) |
Greg Edelston | 9dcdc8a | 2023-01-11 17:07:10 -0700 | [diff] [blame] | 274 | |
| 275 | |
| 276 | @faux.all_empty |
| 277 | @validate.require("chroot") |
| 278 | @validate.validation_complete |
| 279 | def BuildSdkToolchain(input_proto, output_proto, _config): |
| 280 | """Build cross-compiler packages for the SDK.""" |
| 281 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 282 | extra_env: Dict[str, str] = {} |
| 283 | if input_proto.use_flags: |
| 284 | extra_env["USE"] = " ".join(use.flag for use in input_proto.use_flags) |
| 285 | generated_files = sdk.BuildSdkToolchain(chroot, extra_env=extra_env) |
| 286 | output_proto.generated_files.extend(generated_files) |