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): |
| 19 | """Add a fake chroot version to a successful response.""" |
| 20 | output_proto.version.version = 168 |
| 21 | |
| 22 | |
| 23 | @faux.success(_ChrootVersionResponse) |
| 24 | @faux.empty_error |
Kevin Shelton | 50dabff | 2022-04-09 11:29:53 -0700 | [diff] [blame] | 25 | def Create(input_proto: 'CreateRequest', output_proto: 'CreateResponse', |
| 26 | config: 'api_config.ApiConfig') -> Union[int, None]: |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 27 | """Chroot creation, includes support for replacing an existing chroot. |
| 28 | |
| 29 | Args: |
Kevin Shelton | 50dabff | 2022-04-09 11:29:53 -0700 | [diff] [blame] | 30 | input_proto: The input proto. |
| 31 | output_proto: The output proto. |
| 32 | config: The API call config. |
| 33 | |
| 34 | Returns: |
| 35 | An error code, None otherwise. |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 36 | """ |
| 37 | replace = not input_proto.flags.no_replace |
| 38 | bootstrap = input_proto.flags.bootstrap |
| 39 | use_image = not input_proto.flags.no_use_image |
| 40 | |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 41 | chroot_path = input_proto.chroot.path |
| 42 | cache_dir = input_proto.chroot.cache_dir |
Jack Neus | 99be8bf | 2022-02-01 19:56:58 +0000 | [diff] [blame] | 43 | sdk_version = input_proto.sdk_version |
Jack Neus | 78f2555 | 2022-06-24 15:16:17 +0000 | [diff] [blame] | 44 | skip_chroot_upgrade = input_proto.skip_chroot_upgrade |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 45 | |
| 46 | if chroot_path and not os.path.isabs(chroot_path): |
| 47 | cros_build_lib.Die('The chroot path must be absolute.') |
| 48 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 49 | if config.validate_only: |
| 50 | return controller.RETURN_CODE_VALID_INPUT |
| 51 | |
Chris McDonald | 5dcdb89 | 2020-02-07 15:10:46 -0700 | [diff] [blame] | 52 | args = sdk.CreateArguments( |
| 53 | replace=replace, |
| 54 | bootstrap=bootstrap, |
| 55 | use_image=use_image, |
| 56 | cache_dir=cache_dir, |
Jack Neus | 99be8bf | 2022-02-01 19:56:58 +0000 | [diff] [blame] | 57 | chroot_path=chroot_path, |
Jack Neus | 78f2555 | 2022-06-24 15:16:17 +0000 | [diff] [blame] | 58 | sdk_version=sdk_version, |
| 59 | skip_chroot_upgrade=skip_chroot_upgrade) |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 60 | |
| 61 | version = sdk.Create(args) |
| 62 | |
| 63 | if version: |
| 64 | output_proto.version.version = version |
| 65 | else: |
| 66 | # This should be very rare, if ever used, but worth noting. |
| 67 | cros_build_lib.Die('No chroot version could be found. There was likely an' |
| 68 | 'error creating the chroot that was not detected.') |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 69 | |
| 70 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 71 | @faux.success(_ChrootVersionResponse) |
| 72 | @faux.empty_error |
Alex Klein | 45b7343 | 2020-09-23 13:51:20 -0600 | [diff] [blame] | 73 | @validate.require_each('toolchain_targets', ['name']) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 74 | @validate.validation_complete |
Kevin Shelton | 50dabff | 2022-04-09 11:29:53 -0700 | [diff] [blame] | 75 | def Update(input_proto: 'UpdateRequest', output_proto: 'UpdateResponse', |
| 76 | _config: 'api_config.ApiConfig'): |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 77 | """Update the chroot. |
| 78 | |
| 79 | Args: |
Kevin Shelton | 50dabff | 2022-04-09 11:29:53 -0700 | [diff] [blame] | 80 | input_proto: The input proto. |
| 81 | output_proto: The output proto. |
| 82 | _config: The API call config. |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 83 | """ |
| 84 | build_source = input_proto.flags.build_source |
| 85 | targets = [target.name for target in input_proto.toolchain_targets] |
Chris McDonald | 68faa2a | 2020-01-13 12:23:05 -0700 | [diff] [blame] | 86 | toolchain_changed = input_proto.flags.toolchain_changed |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 87 | |
Chris McDonald | 68faa2a | 2020-01-13 12:23:05 -0700 | [diff] [blame] | 88 | args = sdk.UpdateArguments( |
| 89 | build_source=build_source, |
| 90 | toolchain_targets=targets, |
| 91 | toolchain_changed=toolchain_changed) |
| 92 | |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 93 | version = sdk.Update(args) |
| 94 | |
| 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('No chroot version could be found. There was likely an' |
| 100 | 'error creating the chroot that was not detected.') |
Alex Klein | 730cf55 | 2019-10-16 11:28:22 -0600 | [diff] [blame] | 101 | |
| 102 | |
| 103 | @faux.all_empty |
| 104 | @validate.validation_complete |
| 105 | def Delete(input_proto, _output_proto, _config): |
| 106 | """Delete a chroot.""" |
Alex Klein | 71a9a1d | 2019-10-28 15:45:10 -0600 | [diff] [blame] | 107 | chroot = controller_util.ParseChroot(input_proto.chroot) |
Michael Mortensen | 42a700e | 2020-07-01 12:50:45 -0600 | [diff] [blame] | 108 | sdk.Delete(chroot, force=True) |
Chris McDonald | 53ad544 | 2020-01-17 14:11:55 -0700 | [diff] [blame] | 109 | |
| 110 | |
| 111 | @faux.all_empty |
| 112 | @validate.validation_complete |
| 113 | def Unmount(input_proto, _output_proto, _config): |
| 114 | """Unmount a chroot""" |
| 115 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 116 | sdk.Unmount(chroot) |
Chris McDonald | f48ea20 | 2020-01-29 13:19:23 -0700 | [diff] [blame] | 117 | |
| 118 | |
| 119 | @faux.all_empty |
Michael Mortensen | 52a98ac | 2020-07-28 16:00:18 -0600 | [diff] [blame] | 120 | @validate.require('path.path') |
| 121 | @validate.validation_complete |
| 122 | def UnmountPath(input_proto, _output_proto, _config): |
| 123 | """Unmount a path""" |
Alex Klein | b44a6af | 2020-08-05 15:57:12 -0600 | [diff] [blame] | 124 | sdk.UnmountPath(input_proto.path.path) |
Michael Mortensen | 52a98ac | 2020-07-28 16:00:18 -0600 | [diff] [blame] | 125 | |
| 126 | |
| 127 | @faux.all_empty |
Chris McDonald | f48ea20 | 2020-01-29 13:19:23 -0700 | [diff] [blame] | 128 | @validate.validation_complete |
| 129 | def Clean(input_proto, _output_proto, _config): |
| 130 | """Clean unneeded files from a chroot.""" |
| 131 | chroot = controller_util.ParseChroot(input_proto.chroot) |
Alex Klein | b2919e8 | 2022-02-23 10:55:36 -0700 | [diff] [blame] | 132 | sdk.Clean(chroot, safe=True, sysroots=True) |
Chris McDonald | 9d48680 | 2020-01-29 15:57:22 -0700 | [diff] [blame] | 133 | |
| 134 | |
| 135 | @faux.all_empty |
| 136 | @validate.validation_complete |
| 137 | def CreateSnapshot(input_proto, output_proto, _config): |
| 138 | """Create a chroot snapshot and return a corresponding opaque snapshot key.""" |
| 139 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 140 | token = sdk.CreateSnapshot(chroot, replace_if_needed=True) |
| 141 | output_proto.snapshot_token.value = token |
| 142 | |
| 143 | |
| 144 | @faux.all_empty |
| 145 | @validate.validation_complete |
| 146 | def RestoreSnapshot(input_proto, _output_proto, _config): |
| 147 | """Restore a chroot snapshot from a snapshot key.""" |
| 148 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 149 | token = input_proto.snapshot_token.value |
| 150 | sdk.RestoreSnapshot(token, chroot) |
Bob Haarman | d1225ea | 2022-01-19 22:01:42 +0000 | [diff] [blame] | 151 | |
| 152 | |
| 153 | @faux.all_empty |
| 154 | @validate.validation_complete |
| 155 | def BuildPrebuilts(input_proto, _output_proto, _config): |
| 156 | """Builds the binary packages that comprise the Chromium OS SDK.""" |
| 157 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 158 | sdk.BuildPrebuilts(chroot) |
| 159 | |
| 160 | |
| 161 | @faux.all_empty |
| 162 | @validate.require('prepend_version', 'version', 'upload_location') |
| 163 | @validate.validation_complete |
Bob Haarman | 338f8b7 | 2022-02-04 01:02:46 +0000 | [diff] [blame] | 164 | def CreateBinhostCLs(input_proto, _output_proto, _config): |
| 165 | """Create CLs to update the binhost to point at uploaded prebuilts.""" |
| 166 | sdk.CreateBinhostCLs(input_proto.prepend_version, |
| 167 | input_proto.version, |
| 168 | input_proto.upload_location) |
| 169 | |
| 170 | |
| 171 | @faux.all_empty |
| 172 | @validate.require('prepend_version', 'version', 'upload_location') |
| 173 | @validate.validation_complete |
Bob Haarman | d1225ea | 2022-01-19 22:01:42 +0000 | [diff] [blame] | 174 | def UploadPrebuiltPackages(input_proto, _output_proto, _config): |
| 175 | """Uploads prebuilt packages.""" |
| 176 | sdk.UploadPrebuiltPackages(controller_util.ParseChroot(input_proto.chroot), |
| 177 | input_proto.prepend_version, |
| 178 | input_proto.version, |
| 179 | input_proto.upload_location) |