blob: 77b9f197bfad94367a8181d670a193f082ff7915 [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
13from chromite.api import validate
Alex Klein19c4cc42019-02-27 14:47:57 -070014from chromite.lib import cros_build_lib
15from chromite.service import sdk
16
17
Alex Klein231d2da2019-07-22 16:44:45 -060018def Create(input_proto, output_proto, config):
Alex Klein19c4cc42019-02-27 14:47:57 -070019 """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 Klein231d2da2019-07-22 16:44:45 -060024 config (api_config.ApiConfig): The API call config.
Alex Klein19c4cc42019-02-27 14:47:57 -070025 """
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 Klein00aa8072019-04-15 16:36:00 -060030 chroot_path = input_proto.chroot.path
31 cache_dir = input_proto.chroot.cache_dir
Alex Klein19c4cc42019-02-27 14:47:57 -070032
33 if chroot_path and not os.path.isabs(chroot_path):
34 cros_build_lib.Die('The chroot path must be absolute.')
35
Alex Klein231d2da2019-07-22 16:44:45 -060036 if config.validate_only:
37 return controller.RETURN_CODE_VALID_INPUT
38
Alex Klein00aa8072019-04-15 16:36:00 -060039 paths = sdk.ChrootPaths(cache_dir=cache_dir, chroot_path=chroot_path)
Alex Klein19c4cc42019-02-27 14:47:57 -070040 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 Kleinaa5c4172019-02-27 17:12:20 -070051
52
Alex Klein231d2da2019-07-22 16:44:45 -060053@validate.validation_complete
54def Update(input_proto, output_proto, _config):
Alex Kleinaa5c4172019-02-27 17:12:20 -070055 """Update the chroot.
56
57 Args:
58 input_proto (UpdateRequest): The input proto.
59 output_proto (UpdateResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -060060 _config (api_config.ApiConfig): The API call config.
Alex Kleinaa5c4172019-02-27 17:12:20 -070061 """
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.')