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 |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 14 | from chromite.lib import cros_build_lib |
| 15 | from chromite.service import sdk |
| 16 | |
| 17 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 18 | def _ChrootVersionResponse(_input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 19 | """Add a fake chroot version to a successful response.""" |
| 20 | output_proto.version.version = 168 |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 21 | |
| 22 | |
Bob Haarman | c008260 | 2022-09-20 16:12:43 -0700 | [diff] [blame] | 23 | def _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 Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 31 | @faux.success(_ChrootVersionResponse) |
| 32 | @faux.empty_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 33 | def 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 Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 39 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 40 | Args: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 41 | input_proto: The input proto. |
| 42 | output_proto: The output proto. |
| 43 | config: The API call config. |
Kevin Shelton | 50dabff | 2022-04-09 11:29:53 -0700 | [diff] [blame] | 44 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 45 | Returns: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 46 | An error code, None otherwise. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 47 | """ |
| 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 Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 51 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 52 | 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 Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 56 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 57 | if chroot_path and not os.path.isabs(chroot_path): |
| 58 | cros_build_lib.Die("The chroot path must be absolute.") |
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 | if config.validate_only: |
| 61 | return controller.RETURN_CODE_VALID_INPUT |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 62 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 63 | 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 Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 72 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 73 | version = sdk.Create(args) |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 74 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 75 | 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 Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 83 | |
| 84 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 85 | @faux.success(_ChrootVersionResponse) |
| 86 | @faux.empty_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 87 | @validate.require_each("toolchain_targets", ["name"]) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 88 | @validate.validation_complete |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 89 | def Update( |
| 90 | input_proto: "UpdateRequest", |
| 91 | output_proto: "UpdateResponse", |
| 92 | _config: "api_config.ApiConfig", |
| 93 | ): |
| 94 | """Update the chroot. |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 95 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 96 | Args: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 97 | input_proto: The input proto. |
| 98 | output_proto: The output proto. |
| 99 | _config: The API call config. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 100 | """ |
| 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 Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 104 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 105 | args = sdk.UpdateArguments( |
| 106 | build_source=build_source, |
| 107 | toolchain_targets=targets, |
| 108 | toolchain_changed=toolchain_changed, |
| 109 | ) |
Chris McDonald | 68faa2a | 2020-01-13 12:23:05 -0700 | [diff] [blame] | 110 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 111 | version = sdk.Update(args) |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 112 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 113 | 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 Klein | 730cf55 | 2019-10-16 11:28:22 -0600 | [diff] [blame] | 121 | |
| 122 | |
| 123 | @faux.all_empty |
| 124 | @validate.validation_complete |
| 125 | def Delete(input_proto, _output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 126 | """Delete a chroot.""" |
| 127 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 128 | sdk.Delete(chroot, force=True) |
Chris McDonald | 53ad544 | 2020-01-17 14:11:55 -0700 | [diff] [blame] | 129 | |
| 130 | |
| 131 | @faux.all_empty |
| 132 | @validate.validation_complete |
| 133 | def Unmount(input_proto, _output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 134 | """Unmount a chroot""" |
| 135 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 136 | sdk.Unmount(chroot) |
Chris McDonald | f48ea20 | 2020-01-29 13:19:23 -0700 | [diff] [blame] | 137 | |
| 138 | |
| 139 | @faux.all_empty |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 140 | @validate.require("path.path") |
Michael Mortensen | 52a98ac | 2020-07-28 16:00:18 -0600 | [diff] [blame] | 141 | @validate.validation_complete |
| 142 | def UnmountPath(input_proto, _output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 143 | """Unmount a path""" |
| 144 | sdk.UnmountPath(input_proto.path.path) |
Michael Mortensen | 52a98ac | 2020-07-28 16:00:18 -0600 | [diff] [blame] | 145 | |
| 146 | |
| 147 | @faux.all_empty |
Chris McDonald | f48ea20 | 2020-01-29 13:19:23 -0700 | [diff] [blame] | 148 | @validate.validation_complete |
| 149 | def Clean(input_proto, _output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 150 | """Clean unneeded files from a chroot.""" |
| 151 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 152 | sdk.Clean(chroot, safe=True, sysroots=True) |
Chris McDonald | 9d48680 | 2020-01-29 15:57:22 -0700 | [diff] [blame] | 153 | |
| 154 | |
| 155 | @faux.all_empty |
| 156 | @validate.validation_complete |
| 157 | def CreateSnapshot(input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 158 | """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 McDonald | 9d48680 | 2020-01-29 15:57:22 -0700 | [diff] [blame] | 162 | |
| 163 | |
| 164 | @faux.all_empty |
| 165 | @validate.validation_complete |
| 166 | def RestoreSnapshot(input_proto, _output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 167 | """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 Haarman | d1225ea | 2022-01-19 22:01:42 +0000 | [diff] [blame] | 171 | |
| 172 | |
| 173 | @faux.all_empty |
| 174 | @validate.validation_complete |
| 175 | def BuildPrebuilts(input_proto, _output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 176 | """Builds the binary packages that comprise the Chromium OS SDK.""" |
| 177 | chroot = controller_util.ParseChroot(input_proto.chroot) |
Greg Edelston | 199c536 | 2023-01-04 12:29:07 -0700 | [diff] [blame] | 178 | sdk.BuildPrebuilts(chroot, board=input_proto.build_target.name) |
Bob Haarman | d1225ea | 2022-01-19 22:01:42 +0000 | [diff] [blame] | 179 | |
| 180 | |
Bob Haarman | c008260 | 2022-09-20 16:12:43 -0700 | [diff] [blame] | 181 | @faux.success(_CLUris) |
| 182 | @faux.empty_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 183 | @validate.require("prepend_version", "version", "upload_location") |
Bob Haarman | d1225ea | 2022-01-19 22:01:42 +0000 | [diff] [blame] | 184 | @validate.validation_complete |
Bob Haarman | c008260 | 2022-09-20 16:12:43 -0700 | [diff] [blame] | 185 | def CreateBinhostCLs( |
| 186 | input_proto: "CreateBinhostCLsRequest", |
| 187 | output_proto: "CreateBinhostCLsResponse", |
| 188 | _config: "api_config.ApiConfig", |
| 189 | ) -> None: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 190 | """Create CLs to update the binhost to point at uploaded prebuilts.""" |
Bob Haarman | c008260 | 2022-09-20 16:12:43 -0700 | [diff] [blame] | 191 | uris = sdk.CreateBinhostCLs( |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 192 | input_proto.prepend_version, |
| 193 | input_proto.version, |
| 194 | input_proto.upload_location, |
| 195 | ) |
Bob Haarman | c008260 | 2022-09-20 16:12:43 -0700 | [diff] [blame] | 196 | output_proto.cls = uris |
Bob Haarman | 338f8b7 | 2022-02-04 01:02:46 +0000 | [diff] [blame] | 197 | |
| 198 | |
| 199 | @faux.all_empty |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 200 | @validate.require("prepend_version", "version", "upload_location") |
Bob Haarman | 338f8b7 | 2022-02-04 01:02:46 +0000 | [diff] [blame] | 201 | @validate.validation_complete |
Bob Haarman | d1225ea | 2022-01-19 22:01:42 +0000 | [diff] [blame] | 202 | def UploadPrebuiltPackages(input_proto, _output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 203 | """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 | ) |