blob: 67e7e3ad543856990e9b346ca57d8dcc14bd42e4 [file] [log] [blame]
Alex Klein19c4cc42019-02-27 14:47:57 -07001# Copyright 2019 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""SDK chroot operations."""
6
Alex Klein19c4cc42019-02-27 14:47:57 -07007import os
8
Alex Klein231d2da2019-07-22 16:44:45 -06009from chromite.api import controller
Alex Klein076841b2019-08-29 15:19:39 -060010from chromite.api import faux
Alex Klein231d2da2019-07-22 16:44:45 -060011from chromite.api import validate
Alex Klein71a9a1d2019-10-28 15:45:10 -060012from chromite.api.controller import controller_util
Alex Klein19c4cc42019-02-27 14:47:57 -070013from chromite.lib import cros_build_lib
14from chromite.service import sdk
15
16
Alex Klein076841b2019-08-29 15:19:39 -060017def _ChrootVersionResponse(_input_proto, output_proto, _config):
18 """Add a fake chroot version to a successful response."""
19 output_proto.version.version = 168
20
21
22@faux.success(_ChrootVersionResponse)
23@faux.empty_error
Alex Klein231d2da2019-07-22 16:44:45 -060024def Create(input_proto, output_proto, config):
Alex Klein19c4cc42019-02-27 14:47:57 -070025 """Chroot creation, includes support for replacing an existing chroot.
26
27 Args:
28 input_proto (CreateRequest): The input proto.
29 output_proto (CreateResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -060030 config (api_config.ApiConfig): The API call config.
Alex Klein19c4cc42019-02-27 14:47:57 -070031 """
32 replace = not input_proto.flags.no_replace
33 bootstrap = input_proto.flags.bootstrap
34 use_image = not input_proto.flags.no_use_image
35
Alex Klein00aa8072019-04-15 16:36:00 -060036 chroot_path = input_proto.chroot.path
37 cache_dir = input_proto.chroot.cache_dir
Alex Klein19c4cc42019-02-27 14:47:57 -070038
39 if chroot_path and not os.path.isabs(chroot_path):
40 cros_build_lib.Die('The chroot path must be absolute.')
41
Alex Klein231d2da2019-07-22 16:44:45 -060042 if config.validate_only:
43 return controller.RETURN_CODE_VALID_INPUT
44
Chris McDonald5dcdb892020-02-07 15:10:46 -070045 args = sdk.CreateArguments(
46 replace=replace,
47 bootstrap=bootstrap,
48 use_image=use_image,
49 cache_dir=cache_dir,
50 chroot_path=chroot_path)
Alex Klein19c4cc42019-02-27 14:47:57 -070051
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 Kleinaa5c4172019-02-27 17:12:20 -070060
61
Alex Klein076841b2019-08-29 15:19:39 -060062@faux.success(_ChrootVersionResponse)
63@faux.empty_error
Alex Klein45b73432020-09-23 13:51:20 -060064@validate.require_each('toolchain_targets', ['name'])
Alex Klein231d2da2019-07-22 16:44:45 -060065@validate.validation_complete
66def Update(input_proto, output_proto, _config):
Alex Kleinaa5c4172019-02-27 17:12:20 -070067 """Update the chroot.
68
69 Args:
70 input_proto (UpdateRequest): The input proto.
71 output_proto (UpdateResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -060072 _config (api_config.ApiConfig): The API call config.
Alex Kleinaa5c4172019-02-27 17:12:20 -070073 """
74 build_source = input_proto.flags.build_source
75 targets = [target.name for target in input_proto.toolchain_targets]
Chris McDonald68faa2a2020-01-13 12:23:05 -070076 toolchain_changed = input_proto.flags.toolchain_changed
Alex Kleinaa5c4172019-02-27 17:12:20 -070077
Chris McDonald68faa2a2020-01-13 12:23:05 -070078 args = sdk.UpdateArguments(
79 build_source=build_source,
80 toolchain_targets=targets,
81 toolchain_changed=toolchain_changed)
82
Alex Kleinaa5c4172019-02-27 17:12:20 -070083 version = sdk.Update(args)
84
85 if version:
86 output_proto.version.version = version
87 else:
88 # This should be very rare, if ever used, but worth noting.
89 cros_build_lib.Die('No chroot version could be found. There was likely an'
90 'error creating the chroot that was not detected.')
Alex Klein730cf552019-10-16 11:28:22 -060091
92
93@faux.all_empty
94@validate.validation_complete
95def Delete(input_proto, _output_proto, _config):
96 """Delete a chroot."""
Alex Klein71a9a1d2019-10-28 15:45:10 -060097 chroot = controller_util.ParseChroot(input_proto.chroot)
Michael Mortensen42a700e2020-07-01 12:50:45 -060098 sdk.Delete(chroot, force=True)
Chris McDonald53ad5442020-01-17 14:11:55 -070099
100
101@faux.all_empty
102@validate.validation_complete
103def Unmount(input_proto, _output_proto, _config):
104 """Unmount a chroot"""
105 chroot = controller_util.ParseChroot(input_proto.chroot)
106 sdk.Unmount(chroot)
Chris McDonaldf48ea202020-01-29 13:19:23 -0700107
108
109@faux.all_empty
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600110@validate.require('path.path')
111@validate.validation_complete
112def UnmountPath(input_proto, _output_proto, _config):
113 """Unmount a path"""
Alex Kleinb44a6af2020-08-05 15:57:12 -0600114 sdk.UnmountPath(input_proto.path.path)
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600115
116
117@faux.all_empty
Chris McDonaldf48ea202020-01-29 13:19:23 -0700118@validate.validation_complete
119def Clean(input_proto, _output_proto, _config):
120 """Clean unneeded files from a chroot."""
121 chroot = controller_util.ParseChroot(input_proto.chroot)
Alex Klein76c44382022-01-18 17:27:28 -0700122 sdk.Clean(
123 chroot,
124 images=True,
125 sysroots=True,
126 tmp=True,
127 cache=True,
128 logs=True,
129 workdirs=True)
Chris McDonald9d486802020-01-29 15:57:22 -0700130
131
132@faux.all_empty
133@validate.validation_complete
134def CreateSnapshot(input_proto, output_proto, _config):
135 """Create a chroot snapshot and return a corresponding opaque snapshot key."""
136 chroot = controller_util.ParseChroot(input_proto.chroot)
137 token = sdk.CreateSnapshot(chroot, replace_if_needed=True)
138 output_proto.snapshot_token.value = token
139
140
141@faux.all_empty
142@validate.validation_complete
143def RestoreSnapshot(input_proto, _output_proto, _config):
144 """Restore a chroot snapshot from a snapshot key."""
145 chroot = controller_util.ParseChroot(input_proto.chroot)
146 token = input_proto.snapshot_token.value
147 sdk.RestoreSnapshot(token, chroot)