blob: 06e41828e6281e1928c56f122defcb95132cab50 [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
Kevin Shelton50dabff2022-04-09 11:29:53 -07008from typing import Union
Alex Klein19c4cc42019-02-27 14:47:57 -07009
Alex Klein231d2da2019-07-22 16:44:45 -060010from chromite.api import controller
Alex Klein076841b2019-08-29 15:19:39 -060011from chromite.api import faux
Alex Klein231d2da2019-07-22 16:44:45 -060012from chromite.api import validate
Alex Klein71a9a1d2019-10-28 15:45:10 -060013from chromite.api.controller import controller_util
Alex Klein19c4cc42019-02-27 14:47:57 -070014from chromite.lib import cros_build_lib
15from chromite.service import sdk
16
17
Alex Klein076841b2019-08-29 15:19:39 -060018def _ChrootVersionResponse(_input_proto, output_proto, _config):
19 """Add a fake chroot version to a successful response."""
20 output_proto.version.version = 168
21
22
23@faux.success(_ChrootVersionResponse)
24@faux.empty_error
Kevin Shelton50dabff2022-04-09 11:29:53 -070025def Create(input_proto: 'CreateRequest', output_proto: 'CreateResponse',
26 config: 'api_config.ApiConfig') -> Union[int, None]:
Alex Klein19c4cc42019-02-27 14:47:57 -070027 """Chroot creation, includes support for replacing an existing chroot.
28
29 Args:
Kevin Shelton50dabff2022-04-09 11:29:53 -070030 input_proto: The input proto.
31 output_proto: The output proto.
32 config: The API call config.
33
34 Returns:
35 An error code, None otherwise.
Alex Klein19c4cc42019-02-27 14:47:57 -070036 """
37 replace = not input_proto.flags.no_replace
38 bootstrap = input_proto.flags.bootstrap
39 use_image = not input_proto.flags.no_use_image
40
Alex Klein00aa8072019-04-15 16:36:00 -060041 chroot_path = input_proto.chroot.path
42 cache_dir = input_proto.chroot.cache_dir
Jack Neus99be8bf2022-02-01 19:56:58 +000043 sdk_version = input_proto.sdk_version
Alex Klein19c4cc42019-02-27 14:47:57 -070044
45 if chroot_path and not os.path.isabs(chroot_path):
46 cros_build_lib.Die('The chroot path must be absolute.')
47
Alex Klein231d2da2019-07-22 16:44:45 -060048 if config.validate_only:
49 return controller.RETURN_CODE_VALID_INPUT
50
Chris McDonald5dcdb892020-02-07 15:10:46 -070051 args = sdk.CreateArguments(
52 replace=replace,
53 bootstrap=bootstrap,
54 use_image=use_image,
55 cache_dir=cache_dir,
Jack Neus99be8bf2022-02-01 19:56:58 +000056 chroot_path=chroot_path,
57 sdk_version=sdk_version)
Alex Klein19c4cc42019-02-27 14:47:57 -070058
59 version = sdk.Create(args)
60
61 if version:
62 output_proto.version.version = version
63 else:
64 # This should be very rare, if ever used, but worth noting.
65 cros_build_lib.Die('No chroot version could be found. There was likely an'
66 'error creating the chroot that was not detected.')
Alex Kleinaa5c4172019-02-27 17:12:20 -070067
68
Alex Klein076841b2019-08-29 15:19:39 -060069@faux.success(_ChrootVersionResponse)
70@faux.empty_error
Alex Klein45b73432020-09-23 13:51:20 -060071@validate.require_each('toolchain_targets', ['name'])
Alex Klein231d2da2019-07-22 16:44:45 -060072@validate.validation_complete
Kevin Shelton50dabff2022-04-09 11:29:53 -070073def Update(input_proto: 'UpdateRequest', output_proto: 'UpdateResponse',
74 _config: 'api_config.ApiConfig'):
Alex Kleinaa5c4172019-02-27 17:12:20 -070075 """Update the chroot.
76
77 Args:
Kevin Shelton50dabff2022-04-09 11:29:53 -070078 input_proto: The input proto.
79 output_proto: The output proto.
80 _config: The API call config.
Alex Kleinaa5c4172019-02-27 17:12:20 -070081 """
82 build_source = input_proto.flags.build_source
83 targets = [target.name for target in input_proto.toolchain_targets]
Chris McDonald68faa2a2020-01-13 12:23:05 -070084 toolchain_changed = input_proto.flags.toolchain_changed
Alex Kleinaa5c4172019-02-27 17:12:20 -070085
Chris McDonald68faa2a2020-01-13 12:23:05 -070086 args = sdk.UpdateArguments(
87 build_source=build_source,
88 toolchain_targets=targets,
89 toolchain_changed=toolchain_changed)
90
Alex Kleinaa5c4172019-02-27 17:12:20 -070091 version = sdk.Update(args)
92
93 if version:
94 output_proto.version.version = version
95 else:
96 # This should be very rare, if ever used, but worth noting.
97 cros_build_lib.Die('No chroot version could be found. There was likely an'
98 'error creating the chroot that was not detected.')
Alex Klein730cf552019-10-16 11:28:22 -060099
100
101@faux.all_empty
102@validate.validation_complete
103def Delete(input_proto, _output_proto, _config):
104 """Delete a chroot."""
Alex Klein71a9a1d2019-10-28 15:45:10 -0600105 chroot = controller_util.ParseChroot(input_proto.chroot)
Michael Mortensen42a700e2020-07-01 12:50:45 -0600106 sdk.Delete(chroot, force=True)
Chris McDonald53ad5442020-01-17 14:11:55 -0700107
108
109@faux.all_empty
110@validate.validation_complete
111def Unmount(input_proto, _output_proto, _config):
112 """Unmount a chroot"""
113 chroot = controller_util.ParseChroot(input_proto.chroot)
114 sdk.Unmount(chroot)
Chris McDonaldf48ea202020-01-29 13:19:23 -0700115
116
117@faux.all_empty
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600118@validate.require('path.path')
119@validate.validation_complete
120def UnmountPath(input_proto, _output_proto, _config):
121 """Unmount a path"""
Alex Kleinb44a6af2020-08-05 15:57:12 -0600122 sdk.UnmountPath(input_proto.path.path)
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600123
124
125@faux.all_empty
Chris McDonaldf48ea202020-01-29 13:19:23 -0700126@validate.validation_complete
127def Clean(input_proto, _output_proto, _config):
128 """Clean unneeded files from a chroot."""
129 chroot = controller_util.ParseChroot(input_proto.chroot)
Alex Kleinb2919e82022-02-23 10:55:36 -0700130 sdk.Clean(chroot, safe=True, sysroots=True)
Chris McDonald9d486802020-01-29 15:57:22 -0700131
132
133@faux.all_empty
134@validate.validation_complete
135def CreateSnapshot(input_proto, output_proto, _config):
136 """Create a chroot snapshot and return a corresponding opaque snapshot key."""
137 chroot = controller_util.ParseChroot(input_proto.chroot)
138 token = sdk.CreateSnapshot(chroot, replace_if_needed=True)
139 output_proto.snapshot_token.value = token
140
141
142@faux.all_empty
143@validate.validation_complete
144def RestoreSnapshot(input_proto, _output_proto, _config):
145 """Restore a chroot snapshot from a snapshot key."""
146 chroot = controller_util.ParseChroot(input_proto.chroot)
147 token = input_proto.snapshot_token.value
148 sdk.RestoreSnapshot(token, chroot)
Bob Haarmand1225ea2022-01-19 22:01:42 +0000149
150
151@faux.all_empty
152@validate.validation_complete
153def BuildPrebuilts(input_proto, _output_proto, _config):
154 """Builds the binary packages that comprise the Chromium OS SDK."""
155 chroot = controller_util.ParseChroot(input_proto.chroot)
156 sdk.BuildPrebuilts(chroot)
157
158
159@faux.all_empty
160@validate.require('prepend_version', 'version', 'upload_location')
161@validate.validation_complete
162def UploadPrebuiltPackages(input_proto, _output_proto, _config):
163 """Uploads prebuilt packages."""
164 sdk.UploadPrebuiltPackages(controller_util.ParseChroot(input_proto.chroot),
165 input_proto.prepend_version,
166 input_proto.version,
167 input_proto.upload_location)