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 |
| 11 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 12 | from chromite.api import controller |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 13 | from chromite.api import faux |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 14 | from chromite.api import validate |
Alex Klein | 71a9a1d | 2019-10-28 15:45:10 -0600 | [diff] [blame] | 15 | from chromite.api.controller import controller_util |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 16 | from chromite.lib import cros_build_lib |
| 17 | from chromite.service import sdk |
| 18 | |
| 19 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 20 | def _ChrootVersionResponse(_input_proto, output_proto, _config): |
| 21 | """Add a fake chroot version to a successful response.""" |
| 22 | output_proto.version.version = 168 |
| 23 | |
| 24 | |
| 25 | @faux.success(_ChrootVersionResponse) |
| 26 | @faux.empty_error |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 27 | def Create(input_proto, output_proto, config): |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 28 | """Chroot creation, includes support for replacing an existing chroot. |
| 29 | |
| 30 | Args: |
| 31 | input_proto (CreateRequest): The input proto. |
| 32 | output_proto (CreateResponse): The output proto. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 33 | config (api_config.ApiConfig): The API call config. |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 34 | """ |
| 35 | replace = not input_proto.flags.no_replace |
| 36 | bootstrap = input_proto.flags.bootstrap |
| 37 | use_image = not input_proto.flags.no_use_image |
| 38 | |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 39 | chroot_path = input_proto.chroot.path |
| 40 | cache_dir = input_proto.chroot.cache_dir |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 41 | |
| 42 | if chroot_path and not os.path.isabs(chroot_path): |
| 43 | cros_build_lib.Die('The chroot path must be absolute.') |
| 44 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 45 | if config.validate_only: |
| 46 | return controller.RETURN_CODE_VALID_INPUT |
| 47 | |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 48 | paths = sdk.ChrootPaths(cache_dir=cache_dir, chroot_path=chroot_path) |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 49 | args = sdk.CreateArguments(replace=replace, bootstrap=bootstrap, |
| 50 | use_image=use_image, paths=paths) |
| 51 | |
| 52 | version = sdk.Create(args) |
| 53 | |
| 54 | if version: |
| 55 | output_proto.version.version = version |
| 56 | else: |
| 57 | # This should be very rare, if ever used, but worth noting. |
| 58 | cros_build_lib.Die('No chroot version could be found. There was likely an' |
| 59 | 'error creating the chroot that was not detected.') |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 60 | |
| 61 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 62 | @faux.success(_ChrootVersionResponse) |
| 63 | @faux.empty_error |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 64 | @validate.validation_complete |
| 65 | def Update(input_proto, output_proto, _config): |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 66 | """Update the chroot. |
| 67 | |
| 68 | Args: |
| 69 | input_proto (UpdateRequest): The input proto. |
| 70 | output_proto (UpdateResponse): The output proto. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 71 | _config (api_config.ApiConfig): The API call config. |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 72 | """ |
| 73 | build_source = input_proto.flags.build_source |
| 74 | targets = [target.name for target in input_proto.toolchain_targets] |
Chris McDonald | 68faa2a | 2020-01-13 12:23:05 -0700 | [diff] [blame] | 75 | toolchain_changed = input_proto.flags.toolchain_changed |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 76 | |
Chris McDonald | 68faa2a | 2020-01-13 12:23:05 -0700 | [diff] [blame] | 77 | args = sdk.UpdateArguments( |
| 78 | build_source=build_source, |
| 79 | toolchain_targets=targets, |
| 80 | toolchain_changed=toolchain_changed) |
| 81 | |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 82 | version = sdk.Update(args) |
| 83 | |
| 84 | if version: |
| 85 | output_proto.version.version = version |
| 86 | else: |
| 87 | # This should be very rare, if ever used, but worth noting. |
| 88 | cros_build_lib.Die('No chroot version could be found. There was likely an' |
| 89 | 'error creating the chroot that was not detected.') |
Alex Klein | 730cf55 | 2019-10-16 11:28:22 -0600 | [diff] [blame] | 90 | |
| 91 | |
| 92 | @faux.all_empty |
| 93 | @validate.validation_complete |
| 94 | def Delete(input_proto, _output_proto, _config): |
| 95 | """Delete a chroot.""" |
Alex Klein | 71a9a1d | 2019-10-28 15:45:10 -0600 | [diff] [blame] | 96 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 97 | sdk.Delete(chroot) |
Chris McDonald | 53ad544 | 2020-01-17 14:11:55 -0700 | [diff] [blame] | 98 | |
| 99 | |
| 100 | @faux.all_empty |
| 101 | @validate.validation_complete |
| 102 | def Unmount(input_proto, _output_proto, _config): |
| 103 | """Unmount a chroot""" |
| 104 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 105 | sdk.Unmount(chroot) |
Chris McDonald | f48ea20 | 2020-01-29 13:19:23 -0700 | [diff] [blame] | 106 | |
| 107 | |
| 108 | @faux.all_empty |
| 109 | @validate.validation_complete |
| 110 | def Clean(input_proto, _output_proto, _config): |
| 111 | """Clean unneeded files from a chroot.""" |
| 112 | chroot = controller_util.ParseChroot(input_proto.chroot) |
Alex Klein | cbc70d2 | 2020-02-10 11:05:47 -0700 | [diff] [blame^] | 113 | sdk.Clean(chroot, sysroots=True, tmp=True) |