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