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 | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 15 | from chromite.lib import cros_build_lib |
| 16 | from chromite.service import sdk |
| 17 | |
| 18 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame^] | 19 | def _ChrootVersionResponse(_input_proto, output_proto, _config): |
| 20 | """Add a fake chroot version to a successful response.""" |
| 21 | output_proto.version.version = 168 |
| 22 | |
| 23 | |
| 24 | @faux.success(_ChrootVersionResponse) |
| 25 | @faux.empty_error |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 26 | def Create(input_proto, output_proto, config): |
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: |
| 30 | input_proto (CreateRequest): The input proto. |
| 31 | output_proto (CreateResponse): The output proto. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 32 | config (api_config.ApiConfig): The API call config. |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 33 | """ |
| 34 | replace = not input_proto.flags.no_replace |
| 35 | bootstrap = input_proto.flags.bootstrap |
| 36 | use_image = not input_proto.flags.no_use_image |
| 37 | |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 38 | chroot_path = input_proto.chroot.path |
| 39 | cache_dir = input_proto.chroot.cache_dir |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 40 | |
| 41 | if chroot_path and not os.path.isabs(chroot_path): |
| 42 | cros_build_lib.Die('The chroot path must be absolute.') |
| 43 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 44 | if config.validate_only: |
| 45 | return controller.RETURN_CODE_VALID_INPUT |
| 46 | |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 47 | paths = sdk.ChrootPaths(cache_dir=cache_dir, chroot_path=chroot_path) |
Alex Klein | 19c4cc4 | 2019-02-27 14:47:57 -0700 | [diff] [blame] | 48 | args = sdk.CreateArguments(replace=replace, bootstrap=bootstrap, |
| 49 | use_image=use_image, paths=paths) |
| 50 | |
| 51 | version = sdk.Create(args) |
| 52 | |
| 53 | if version: |
| 54 | output_proto.version.version = version |
| 55 | else: |
| 56 | # This should be very rare, if ever used, but worth noting. |
| 57 | cros_build_lib.Die('No chroot version could be found. There was likely an' |
| 58 | 'error creating the chroot that was not detected.') |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 59 | |
| 60 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame^] | 61 | @faux.success(_ChrootVersionResponse) |
| 62 | @faux.empty_error |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 63 | @validate.validation_complete |
| 64 | def Update(input_proto, output_proto, _config): |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 65 | """Update the chroot. |
| 66 | |
| 67 | Args: |
| 68 | input_proto (UpdateRequest): The input proto. |
| 69 | output_proto (UpdateResponse): The output proto. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 70 | _config (api_config.ApiConfig): The API call config. |
Alex Klein | aa5c417 | 2019-02-27 17:12:20 -0700 | [diff] [blame] | 71 | """ |
| 72 | build_source = input_proto.flags.build_source |
| 73 | targets = [target.name for target in input_proto.toolchain_targets] |
| 74 | |
| 75 | args = sdk.UpdateArguments(build_source=build_source, |
| 76 | toolchain_targets=targets) |
| 77 | version = sdk.Update(args) |
| 78 | |
| 79 | if version: |
| 80 | output_proto.version.version = version |
| 81 | else: |
| 82 | # This should be very rare, if ever used, but worth noting. |
| 83 | cros_build_lib.Die('No chroot version could be found. There was likely an' |
| 84 | 'error creating the chroot that was not detected.') |