Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 1 | # Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 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 | |
| 23 | @faux.success(_ChrootVersionResponse) |
| 24 | @faux.empty_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 25 | def 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 Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 31 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 32 | Args: |
| 33 | input_proto: The input proto. |
| 34 | output_proto: The output proto. |
| 35 | config: The API call config. |
Kevin Shelton | 50dabff | 2022-04-09 11:29:53 -0700 | [diff] [blame] | 36 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 37 | 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 Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 43 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 44 | 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 Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 48 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 49 | if chroot_path and not os.path.isabs(chroot_path): |
| 50 | cros_build_lib.Die("The chroot path must be absolute.") |
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 | if config.validate_only: |
| 53 | return controller.RETURN_CODE_VALID_INPUT |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 54 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 55 | 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 Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 64 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 65 | version = sdk.Create(args) |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 66 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 67 | 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 Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 75 | |
| 76 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 77 | @faux.success(_ChrootVersionResponse) |
| 78 | @faux.empty_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 79 | @validate.require_each("toolchain_targets", ["name"]) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 80 | @validate.validation_complete |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 81 | def Update( |
| 82 | input_proto: "UpdateRequest", |
| 83 | output_proto: "UpdateResponse", |
| 84 | _config: "api_config.ApiConfig", |
| 85 | ): |
| 86 | """Update the chroot. |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 87 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 88 | 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 Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 96 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 97 | args = sdk.UpdateArguments( |
| 98 | build_source=build_source, |
| 99 | toolchain_targets=targets, |
| 100 | toolchain_changed=toolchain_changed, |
| 101 | ) |
Chris McDonald | 68faa2a | 2020-01-13 12:23:05 -0700 | [diff] [blame] | 102 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 103 | version = sdk.Update(args) |
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 | 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 Klein | 730cf55 | 2019-10-16 11:28:22 -0600 | [diff] [blame] | 113 | |
| 114 | |
| 115 | @faux.all_empty |
| 116 | @validate.validation_complete |
| 117 | def Delete(input_proto, _output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 118 | """Delete a chroot.""" |
| 119 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 120 | sdk.Delete(chroot, force=True) |
Chris McDonald | 53ad544 | 2020-01-17 14:11:55 -0700 | [diff] [blame] | 121 | |
| 122 | |
| 123 | @faux.all_empty |
| 124 | @validate.validation_complete |
| 125 | def Unmount(input_proto, _output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 126 | """Unmount a chroot""" |
| 127 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 128 | sdk.Unmount(chroot) |
Chris McDonald | f48ea20 | 2020-01-29 13:19:23 -0700 | [diff] [blame] | 129 | |
| 130 | |
| 131 | @faux.all_empty |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 132 | @validate.require("path.path") |
Michael Mortensen | 52a98ac | 2020-07-28 16:00:18 -0600 | [diff] [blame] | 133 | @validate.validation_complete |
| 134 | def UnmountPath(input_proto, _output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 135 | """Unmount a path""" |
| 136 | sdk.UnmountPath(input_proto.path.path) |
Michael Mortensen | 52a98ac | 2020-07-28 16:00:18 -0600 | [diff] [blame] | 137 | |
| 138 | |
| 139 | @faux.all_empty |
Chris McDonald | f48ea20 | 2020-01-29 13:19:23 -0700 | [diff] [blame] | 140 | @validate.validation_complete |
| 141 | def Clean(input_proto, _output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 142 | """Clean unneeded files from a chroot.""" |
| 143 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 144 | sdk.Clean(chroot, safe=True, sysroots=True) |
Chris McDonald | 9d48680 | 2020-01-29 15:57:22 -0700 | [diff] [blame] | 145 | |
| 146 | |
| 147 | @faux.all_empty |
| 148 | @validate.validation_complete |
| 149 | def CreateSnapshot(input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 150 | """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 McDonald | 9d48680 | 2020-01-29 15:57:22 -0700 | [diff] [blame] | 154 | |
| 155 | |
| 156 | @faux.all_empty |
| 157 | @validate.validation_complete |
| 158 | def RestoreSnapshot(input_proto, _output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 159 | """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 Haarman | d1225ea | 2022-01-19 22:01:42 +0000 | [diff] [blame] | 163 | |
| 164 | |
| 165 | @faux.all_empty |
| 166 | @validate.validation_complete |
| 167 | def BuildPrebuilts(input_proto, _output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 168 | """Builds the binary packages that comprise the Chromium OS SDK.""" |
| 169 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 170 | sdk.BuildPrebuilts(chroot) |
Bob Haarman | d1225ea | 2022-01-19 22:01:42 +0000 | [diff] [blame] | 171 | |
| 172 | |
| 173 | @faux.all_empty |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 174 | @validate.require("prepend_version", "version", "upload_location") |
Bob Haarman | d1225ea | 2022-01-19 22:01:42 +0000 | [diff] [blame] | 175 | @validate.validation_complete |
Bob Haarman | 338f8b7 | 2022-02-04 01:02:46 +0000 | [diff] [blame] | 176 | def CreateBinhostCLs(input_proto, _output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 177 | """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 Haarman | 338f8b7 | 2022-02-04 01:02:46 +0000 | [diff] [blame] | 183 | |
| 184 | |
| 185 | @faux.all_empty |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 186 | @validate.require("prepend_version", "version", "upload_location") |
Bob Haarman | 338f8b7 | 2022-02-04 01:02:46 +0000 | [diff] [blame] | 187 | @validate.validation_complete |
Bob Haarman | d1225ea | 2022-01-19 22:01:42 +0000 | [diff] [blame] | 188 | def UploadPrebuiltPackages(input_proto, _output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 189 | """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 | ) |