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