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 |
Kevin Shelton | 50dabff | 2022-04-09 11:29:53 -0700 | [diff] [blame] | 8 | from typing import 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 |
| 16 | from chromite.service import sdk |
| 17 | |
| 18 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 19 | def _ChrootVersionResponse(_input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 20 | """Add a fake chroot version to a successful response.""" |
| 21 | output_proto.version.version = 168 |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 22 | |
| 23 | |
Bob Haarman | 171d409 | 2023-01-10 16:44:19 +0000 | [diff] [blame] | 24 | def _BinhostCLs(_input_proto, output_proto, _config): |
| 25 | """Add fake CL identifiers to a successful response.""" |
Bob Haarman | c008260 | 2022-09-20 16:12:43 -0700 | [diff] [blame] | 26 | output_proto.cls = [ |
Bob Haarman | 171d409 | 2023-01-10 16:44:19 +0000 | [diff] [blame] | 27 | "fakecl:1", |
| 28 | "fakecl:2", |
Bob Haarman | c008260 | 2022-09-20 16:12:43 -0700 | [diff] [blame] | 29 | ] |
| 30 | |
| 31 | |
Bob Haarman | 0853ce9 | 2022-12-13 18:11:53 +0000 | [diff] [blame] | 32 | def _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 |
| 41 | def 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 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 51 | @faux.success(_ChrootVersionResponse) |
| 52 | @faux.empty_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 53 | def Create( |
| 54 | input_proto: "CreateRequest", |
| 55 | output_proto: "CreateResponse", |
| 56 | config: "api_config.ApiConfig", |
| 57 | ) -> Union[int, None]: |
| 58 | """Chroot creation, includes support for replacing an existing chroot. |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 59 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 60 | Args: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 61 | input_proto: The input proto. |
| 62 | output_proto: The output proto. |
| 63 | config: The API call config. |
Kevin Shelton | 50dabff | 2022-04-09 11:29:53 -0700 | [diff] [blame] | 64 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 65 | Returns: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 66 | An error code, None otherwise. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 67 | """ |
| 68 | replace = not input_proto.flags.no_replace |
| 69 | bootstrap = input_proto.flags.bootstrap |
| 70 | use_image = not input_proto.flags.no_use_image |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 71 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 72 | chroot_path = input_proto.chroot.path |
| 73 | cache_dir = input_proto.chroot.cache_dir |
| 74 | sdk_version = input_proto.sdk_version |
| 75 | skip_chroot_upgrade = input_proto.skip_chroot_upgrade |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 76 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 77 | if chroot_path and not os.path.isabs(chroot_path): |
| 78 | cros_build_lib.Die("The chroot path must be absolute.") |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 79 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 80 | if config.validate_only: |
| 81 | return controller.RETURN_CODE_VALID_INPUT |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 82 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 83 | args = sdk.CreateArguments( |
| 84 | replace=replace, |
| 85 | bootstrap=bootstrap, |
| 86 | use_image=use_image, |
| 87 | cache_dir=cache_dir, |
| 88 | chroot_path=chroot_path, |
| 89 | sdk_version=sdk_version, |
| 90 | skip_chroot_upgrade=skip_chroot_upgrade, |
| 91 | ) |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 92 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 93 | version = sdk.Create(args) |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 94 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 95 | if version: |
| 96 | output_proto.version.version = version |
| 97 | else: |
| 98 | # This should be very rare, if ever used, but worth noting. |
| 99 | cros_build_lib.Die( |
| 100 | "No chroot version could be found. There was likely an" |
| 101 | "error creating the chroot that was not detected." |
| 102 | ) |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 103 | |
| 104 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 105 | @faux.success(_ChrootVersionResponse) |
| 106 | @faux.empty_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 107 | @validate.require_each("toolchain_targets", ["name"]) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 108 | @validate.validation_complete |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 109 | def Update( |
| 110 | input_proto: "UpdateRequest", |
| 111 | output_proto: "UpdateResponse", |
| 112 | _config: "api_config.ApiConfig", |
| 113 | ): |
| 114 | """Update the chroot. |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 115 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 116 | Args: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 117 | input_proto: The input proto. |
| 118 | output_proto: The output proto. |
| 119 | _config: The API call config. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 120 | """ |
| 121 | build_source = input_proto.flags.build_source |
| 122 | targets = [target.name for target in input_proto.toolchain_targets] |
| 123 | toolchain_changed = input_proto.flags.toolchain_changed |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 124 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 125 | args = sdk.UpdateArguments( |
| 126 | build_source=build_source, |
| 127 | toolchain_targets=targets, |
| 128 | toolchain_changed=toolchain_changed, |
| 129 | ) |
Chris McDonald | 68faa2a | 2020-01-13 12:23:05 -0700 | [diff] [blame] | 130 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 131 | version = sdk.Update(args) |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 132 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 133 | if version: |
| 134 | output_proto.version.version = version |
| 135 | else: |
| 136 | # This should be very rare, if ever used, but worth noting. |
| 137 | cros_build_lib.Die( |
| 138 | "No chroot version could be found. There was likely an" |
| 139 | "error creating the chroot that was not detected." |
| 140 | ) |
Alex Klein | 730cf55 | 2019-10-16 11:28:22 -0600 | [diff] [blame] | 141 | |
| 142 | |
| 143 | @faux.all_empty |
| 144 | @validate.validation_complete |
| 145 | def Delete(input_proto, _output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 146 | """Delete a chroot.""" |
| 147 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 148 | sdk.Delete(chroot, force=True) |
Chris McDonald | 53ad544 | 2020-01-17 14:11:55 -0700 | [diff] [blame] | 149 | |
| 150 | |
| 151 | @faux.all_empty |
| 152 | @validate.validation_complete |
| 153 | def Unmount(input_proto, _output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 154 | """Unmount a chroot""" |
| 155 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 156 | sdk.Unmount(chroot) |
Chris McDonald | f48ea20 | 2020-01-29 13:19:23 -0700 | [diff] [blame] | 157 | |
| 158 | |
| 159 | @faux.all_empty |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 160 | @validate.require("path.path") |
Michael Mortensen | 52a98ac | 2020-07-28 16:00:18 -0600 | [diff] [blame] | 161 | @validate.validation_complete |
| 162 | def UnmountPath(input_proto, _output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 163 | """Unmount a path""" |
| 164 | sdk.UnmountPath(input_proto.path.path) |
Michael Mortensen | 52a98ac | 2020-07-28 16:00:18 -0600 | [diff] [blame] | 165 | |
| 166 | |
| 167 | @faux.all_empty |
Chris McDonald | f48ea20 | 2020-01-29 13:19:23 -0700 | [diff] [blame] | 168 | @validate.validation_complete |
| 169 | def Clean(input_proto, _output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 170 | """Clean unneeded files from a chroot.""" |
| 171 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 172 | sdk.Clean(chroot, safe=True, sysroots=True) |
Chris McDonald | 9d48680 | 2020-01-29 15:57:22 -0700 | [diff] [blame] | 173 | |
| 174 | |
| 175 | @faux.all_empty |
| 176 | @validate.validation_complete |
| 177 | def CreateSnapshot(input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 178 | """Create a chroot snapshot and return a corresponding opaque snapshot key.""" |
| 179 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 180 | token = sdk.CreateSnapshot(chroot, replace_if_needed=True) |
| 181 | output_proto.snapshot_token.value = token |
Chris McDonald | 9d48680 | 2020-01-29 15:57:22 -0700 | [diff] [blame] | 182 | |
| 183 | |
| 184 | @faux.all_empty |
| 185 | @validate.validation_complete |
| 186 | def RestoreSnapshot(input_proto, _output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 187 | """Restore a chroot snapshot from a snapshot key.""" |
| 188 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 189 | token = input_proto.snapshot_token.value |
| 190 | sdk.RestoreSnapshot(token, chroot) |
Bob Haarman | d1225ea | 2022-01-19 22:01:42 +0000 | [diff] [blame] | 191 | |
| 192 | |
| 193 | @faux.all_empty |
| 194 | @validate.validation_complete |
| 195 | def BuildPrebuilts(input_proto, _output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 196 | """Builds the binary packages that comprise the Chromium OS SDK.""" |
| 197 | chroot = controller_util.ParseChroot(input_proto.chroot) |
Greg Edelston | 199c536 | 2023-01-04 12:29:07 -0700 | [diff] [blame] | 198 | sdk.BuildPrebuilts(chroot, board=input_proto.build_target.name) |
Bob Haarman | d1225ea | 2022-01-19 22:01:42 +0000 | [diff] [blame] | 199 | |
| 200 | |
Bob Haarman | 171d409 | 2023-01-10 16:44:19 +0000 | [diff] [blame] | 201 | @faux.success(_BinhostCLs) |
Bob Haarman | c008260 | 2022-09-20 16:12:43 -0700 | [diff] [blame] | 202 | @faux.empty_error |
Bob Haarman | 0853ce9 | 2022-12-13 18:11:53 +0000 | [diff] [blame] | 203 | @validate.require( |
| 204 | "prepend_version", "version", "upload_location", "sdk_tarball_template" |
| 205 | ) |
Bob Haarman | d1225ea | 2022-01-19 22:01:42 +0000 | [diff] [blame] | 206 | @validate.validation_complete |
Bob Haarman | c008260 | 2022-09-20 16:12:43 -0700 | [diff] [blame] | 207 | def CreateBinhostCLs( |
| 208 | input_proto: "CreateBinhostCLsRequest", |
| 209 | output_proto: "CreateBinhostCLsResponse", |
| 210 | _config: "api_config.ApiConfig", |
| 211 | ) -> None: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 212 | """Create CLs to update the binhost to point at uploaded prebuilts.""" |
Bob Haarman | 171d409 | 2023-01-10 16:44:19 +0000 | [diff] [blame] | 213 | output_proto.cls.extend( |
| 214 | sdk.CreateBinhostCLs( |
| 215 | input_proto.prepend_version, |
| 216 | input_proto.version, |
| 217 | input_proto.upload_location, |
| 218 | input_proto.sdk_tarball_template, |
| 219 | ) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 220 | ) |
Bob Haarman | 338f8b7 | 2022-02-04 01:02:46 +0000 | [diff] [blame] | 221 | |
| 222 | |
| 223 | @faux.all_empty |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 224 | @validate.require("prepend_version", "version", "upload_location") |
Bob Haarman | 338f8b7 | 2022-02-04 01:02:46 +0000 | [diff] [blame] | 225 | @validate.validation_complete |
Bob Haarman | d1225ea | 2022-01-19 22:01:42 +0000 | [diff] [blame] | 226 | def UploadPrebuiltPackages(input_proto, _output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 227 | """Uploads prebuilt packages.""" |
| 228 | sdk.UploadPrebuiltPackages( |
| 229 | controller_util.ParseChroot(input_proto.chroot), |
| 230 | input_proto.prepend_version, |
| 231 | input_proto.version, |
| 232 | input_proto.upload_location, |
| 233 | ) |