Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | # Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """SDK chroot operations.""" |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | |
| 10 | import os |
Mike Frysinger | ef94e4c | 2020-02-10 23:59:54 -0500 | [diff] [blame] | 11 | import sys |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 12 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 13 | from chromite.api import controller |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 14 | from chromite.api import faux |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 15 | from chromite.api import validate |
Alex Klein | 71a9a1d | 2019-10-28 15:45:10 -0600 | [diff] [blame] | 16 | from chromite.api.controller import controller_util |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 17 | from chromite.lib import cros_build_lib |
| 18 | from chromite.service import sdk |
| 19 | |
| 20 | |
Mike Frysinger | ef94e4c | 2020-02-10 23:59:54 -0500 | [diff] [blame] | 21 | assert sys.version_info >= (3, 6), 'This module requires Python 3.6+' |
| 22 | |
| 23 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 24 | def _ChrootVersionResponse(_input_proto, output_proto, _config): |
| 25 | """Add a fake chroot version to a successful response.""" |
| 26 | output_proto.version.version = 168 |
| 27 | |
| 28 | |
| 29 | @faux.success(_ChrootVersionResponse) |
| 30 | @faux.empty_error |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 31 | def Create(input_proto, output_proto, config): |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 32 | """Chroot creation, includes support for replacing an existing chroot. |
| 33 | |
| 34 | Args: |
| 35 | input_proto (CreateRequest): The input proto. |
| 36 | output_proto (CreateResponse): The output proto. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 37 | config (api_config.ApiConfig): The API call config. |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 38 | """ |
| 39 | replace = not input_proto.flags.no_replace |
| 40 | bootstrap = input_proto.flags.bootstrap |
| 41 | use_image = not input_proto.flags.no_use_image |
| 42 | |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 43 | chroot_path = input_proto.chroot.path |
| 44 | cache_dir = input_proto.chroot.cache_dir |
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, |
| 57 | chroot_path=chroot_path) |
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 | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 71 | @validate.validation_complete |
| 72 | def Update(input_proto, output_proto, _config): |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 73 | """Update the chroot. |
| 74 | |
| 75 | Args: |
| 76 | input_proto (UpdateRequest): The input proto. |
| 77 | output_proto (UpdateResponse): The output proto. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 78 | _config (api_config.ApiConfig): The API call config. |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 79 | """ |
| 80 | build_source = input_proto.flags.build_source |
| 81 | targets = [target.name for target in input_proto.toolchain_targets] |
Chris McDonald | 68faa2a | 2020-01-13 12:23:05 -0700 | [diff] [blame] | 82 | toolchain_changed = input_proto.flags.toolchain_changed |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 83 | |
Chris McDonald | 68faa2a | 2020-01-13 12:23:05 -0700 | [diff] [blame] | 84 | args = sdk.UpdateArguments( |
| 85 | build_source=build_source, |
| 86 | toolchain_targets=targets, |
| 87 | toolchain_changed=toolchain_changed) |
| 88 | |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 89 | version = sdk.Update(args) |
| 90 | |
| 91 | if version: |
| 92 | output_proto.version.version = version |
| 93 | else: |
| 94 | # This should be very rare, if ever used, but worth noting. |
| 95 | cros_build_lib.Die('No chroot version could be found. There was likely an' |
| 96 | 'error creating the chroot that was not detected.') |
Alex Klein | 730cf55 | 2019-10-16 11:28:22 -0600 | [diff] [blame] | 97 | |
| 98 | |
| 99 | @faux.all_empty |
| 100 | @validate.validation_complete |
| 101 | def Delete(input_proto, _output_proto, _config): |
| 102 | """Delete a chroot.""" |
Alex Klein | 71a9a1d | 2019-10-28 15:45:10 -0600 | [diff] [blame] | 103 | chroot = controller_util.ParseChroot(input_proto.chroot) |
Michael Mortensen | 42a700e | 2020-07-01 12:50:45 -0600 | [diff] [blame] | 104 | sdk.Delete(chroot, force=True) |
Chris McDonald | 53ad544 | 2020-01-17 14:11:55 -0700 | [diff] [blame] | 105 | |
| 106 | |
| 107 | @faux.all_empty |
| 108 | @validate.validation_complete |
| 109 | def Unmount(input_proto, _output_proto, _config): |
| 110 | """Unmount a chroot""" |
| 111 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 112 | sdk.Unmount(chroot) |
Chris McDonald | f48ea20 | 2020-01-29 13:19:23 -0700 | [diff] [blame] | 113 | |
| 114 | |
| 115 | @faux.all_empty |
Michael Mortensen | 52a98ac | 2020-07-28 16:00:18 -0600 | [diff] [blame] | 116 | @validate.require('path.path') |
| 117 | @validate.validation_complete |
| 118 | def UnmountPath(input_proto, _output_proto, _config): |
| 119 | """Unmount a path""" |
| 120 | sdk.UnmountPath(input_proto.path) |
| 121 | |
| 122 | |
| 123 | @faux.all_empty |
Chris McDonald | f48ea20 | 2020-01-29 13:19:23 -0700 | [diff] [blame] | 124 | @validate.validation_complete |
| 125 | def Clean(input_proto, _output_proto, _config): |
| 126 | """Clean unneeded files from a chroot.""" |
| 127 | chroot = controller_util.ParseChroot(input_proto.chroot) |
Alex Klein | ba4b8d2 | 2020-05-07 11:08:29 -0600 | [diff] [blame] | 128 | sdk.Clean(chroot, images=True, sysroots=True, tmp=True) |
Chris McDonald | 9d48680 | 2020-01-29 15:57:22 -0700 | [diff] [blame] | 129 | |
| 130 | |
| 131 | @faux.all_empty |
| 132 | @validate.validation_complete |
| 133 | def CreateSnapshot(input_proto, output_proto, _config): |
| 134 | """Create a chroot snapshot and return a corresponding opaque snapshot key.""" |
| 135 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 136 | token = sdk.CreateSnapshot(chroot, replace_if_needed=True) |
| 137 | output_proto.snapshot_token.value = token |
| 138 | |
| 139 | |
| 140 | @faux.all_empty |
| 141 | @validate.validation_complete |
| 142 | def RestoreSnapshot(input_proto, _output_proto, _config): |
| 143 | """Restore a chroot snapshot from a snapshot key.""" |
| 144 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 145 | token = input_proto.snapshot_token.value |
| 146 | sdk.RestoreSnapshot(token, chroot) |