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