blob: e48dc23634ab2f594a78bde80265e049caaf5b02 [file] [log] [blame]
Alex Klein19c4cc42019-02-27 14:47:57 -07001# -*- 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
8from __future__ import print_function
9
10import os
11
Alex Klein231d2da2019-07-22 16:44:45 -060012from chromite.api import controller
Alex Klein076841b2019-08-29 15:19:39 -060013from chromite.api import faux
Alex Klein231d2da2019-07-22 16:44:45 -060014from chromite.api import validate
Alex Klein19c4cc42019-02-27 14:47:57 -070015from chromite.lib import cros_build_lib
16from chromite.service import sdk
17
18
Alex Klein076841b2019-08-29 15:19:39 -060019def _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 Klein231d2da2019-07-22 16:44:45 -060026def Create(input_proto, output_proto, config):
Alex Klein19c4cc42019-02-27 14:47:57 -070027 """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 Klein231d2da2019-07-22 16:44:45 -060032 config (api_config.ApiConfig): The API call config.
Alex Klein19c4cc42019-02-27 14:47:57 -070033 """
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 Klein00aa8072019-04-15 16:36:00 -060038 chroot_path = input_proto.chroot.path
39 cache_dir = input_proto.chroot.cache_dir
Alex Klein19c4cc42019-02-27 14:47:57 -070040
41 if chroot_path and not os.path.isabs(chroot_path):
42 cros_build_lib.Die('The chroot path must be absolute.')
43
Alex Klein231d2da2019-07-22 16:44:45 -060044 if config.validate_only:
45 return controller.RETURN_CODE_VALID_INPUT
46
Alex Klein00aa8072019-04-15 16:36:00 -060047 paths = sdk.ChrootPaths(cache_dir=cache_dir, chroot_path=chroot_path)
Alex Klein19c4cc42019-02-27 14:47:57 -070048 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 Kleinaa5c4172019-02-27 17:12:20 -070059
60
Alex Klein076841b2019-08-29 15:19:39 -060061@faux.success(_ChrootVersionResponse)
62@faux.empty_error
Alex Klein231d2da2019-07-22 16:44:45 -060063@validate.validation_complete
64def Update(input_proto, output_proto, _config):
Alex Kleinaa5c4172019-02-27 17:12:20 -070065 """Update the chroot.
66
67 Args:
68 input_proto (UpdateRequest): The input proto.
69 output_proto (UpdateResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -060070 _config (api_config.ApiConfig): The API call config.
Alex Kleinaa5c4172019-02-27 17:12:20 -070071 """
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.')
Alex Klein730cf552019-10-16 11:28:22 -060085
86
87@faux.all_empty
88@validate.validation_complete
89def Delete(input_proto, _output_proto, _config):
90 """Delete a chroot."""
91 sdk.Delete(chroot_path=input_proto.chroot.path)