blob: 1e8adfc7acca87ee8a4c530330b9a2b53d3bed46 [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 Klein71a9a1d2019-10-28 15:45:10 -060015from chromite.api.controller import controller_util
Alex Klein19c4cc42019-02-27 14:47:57 -070016from chromite.lib import cros_build_lib
17from chromite.service import sdk
18
19
Alex Klein076841b2019-08-29 15:19:39 -060020def _ChrootVersionResponse(_input_proto, output_proto, _config):
21 """Add a fake chroot version to a successful response."""
22 output_proto.version.version = 168
23
24
25@faux.success(_ChrootVersionResponse)
26@faux.empty_error
Alex Klein231d2da2019-07-22 16:44:45 -060027def Create(input_proto, output_proto, config):
Alex Klein19c4cc42019-02-27 14:47:57 -070028 """Chroot creation, includes support for replacing an existing chroot.
29
30 Args:
31 input_proto (CreateRequest): The input proto.
32 output_proto (CreateResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -060033 config (api_config.ApiConfig): The API call config.
Alex Klein19c4cc42019-02-27 14:47:57 -070034 """
35 replace = not input_proto.flags.no_replace
36 bootstrap = input_proto.flags.bootstrap
37 use_image = not input_proto.flags.no_use_image
38
Alex Klein00aa8072019-04-15 16:36:00 -060039 chroot_path = input_proto.chroot.path
40 cache_dir = input_proto.chroot.cache_dir
Alex Klein19c4cc42019-02-27 14:47:57 -070041
42 if chroot_path and not os.path.isabs(chroot_path):
43 cros_build_lib.Die('The chroot path must be absolute.')
44
Alex Klein231d2da2019-07-22 16:44:45 -060045 if config.validate_only:
46 return controller.RETURN_CODE_VALID_INPUT
47
Chris McDonald5dcdb892020-02-07 15:10:46 -070048 args = sdk.CreateArguments(
49 replace=replace,
50 bootstrap=bootstrap,
51 use_image=use_image,
52 cache_dir=cache_dir,
53 chroot_path=chroot_path)
Alex Klein19c4cc42019-02-27 14:47:57 -070054
55 version = sdk.Create(args)
56
57 if version:
58 output_proto.version.version = version
59 else:
60 # This should be very rare, if ever used, but worth noting.
61 cros_build_lib.Die('No chroot version could be found. There was likely an'
62 'error creating the chroot that was not detected.')
Alex Kleinaa5c4172019-02-27 17:12:20 -070063
64
Alex Klein076841b2019-08-29 15:19:39 -060065@faux.success(_ChrootVersionResponse)
66@faux.empty_error
Alex Klein45b73432020-09-23 13:51:20 -060067@validate.require_each('toolchain_targets', ['name'])
Alex Klein231d2da2019-07-22 16:44:45 -060068@validate.validation_complete
69def Update(input_proto, output_proto, _config):
Alex Kleinaa5c4172019-02-27 17:12:20 -070070 """Update the chroot.
71
72 Args:
73 input_proto (UpdateRequest): The input proto.
74 output_proto (UpdateResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -060075 _config (api_config.ApiConfig): The API call config.
Alex Kleinaa5c4172019-02-27 17:12:20 -070076 """
77 build_source = input_proto.flags.build_source
78 targets = [target.name for target in input_proto.toolchain_targets]
Chris McDonald68faa2a2020-01-13 12:23:05 -070079 toolchain_changed = input_proto.flags.toolchain_changed
Alex Kleinaa5c4172019-02-27 17:12:20 -070080
Chris McDonald68faa2a2020-01-13 12:23:05 -070081 args = sdk.UpdateArguments(
82 build_source=build_source,
83 toolchain_targets=targets,
84 toolchain_changed=toolchain_changed)
85
Alex Kleinaa5c4172019-02-27 17:12:20 -070086 version = sdk.Update(args)
87
88 if version:
89 output_proto.version.version = version
90 else:
91 # This should be very rare, if ever used, but worth noting.
92 cros_build_lib.Die('No chroot version could be found. There was likely an'
93 'error creating the chroot that was not detected.')
Alex Klein730cf552019-10-16 11:28:22 -060094
95
96@faux.all_empty
97@validate.validation_complete
98def Delete(input_proto, _output_proto, _config):
99 """Delete a chroot."""
Alex Klein71a9a1d2019-10-28 15:45:10 -0600100 chroot = controller_util.ParseChroot(input_proto.chroot)
Michael Mortensen42a700e2020-07-01 12:50:45 -0600101 sdk.Delete(chroot, force=True)
Chris McDonald53ad5442020-01-17 14:11:55 -0700102
103
104@faux.all_empty
105@validate.validation_complete
106def Unmount(input_proto, _output_proto, _config):
107 """Unmount a chroot"""
108 chroot = controller_util.ParseChroot(input_proto.chroot)
109 sdk.Unmount(chroot)
Chris McDonaldf48ea202020-01-29 13:19:23 -0700110
111
112@faux.all_empty
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600113@validate.require('path.path')
114@validate.validation_complete
115def UnmountPath(input_proto, _output_proto, _config):
116 """Unmount a path"""
Alex Kleinb44a6af2020-08-05 15:57:12 -0600117 sdk.UnmountPath(input_proto.path.path)
Michael Mortensen52a98ac2020-07-28 16:00:18 -0600118
119
120@faux.all_empty
Chris McDonaldf48ea202020-01-29 13:19:23 -0700121@validate.validation_complete
122def Clean(input_proto, _output_proto, _config):
123 """Clean unneeded files from a chroot."""
124 chroot = controller_util.ParseChroot(input_proto.chroot)
Alex Kleinba4b8d22020-05-07 11:08:29 -0600125 sdk.Clean(chroot, images=True, sysroots=True, tmp=True)
Chris McDonald9d486802020-01-29 15:57:22 -0700126
127
128@faux.all_empty
129@validate.validation_complete
130def CreateSnapshot(input_proto, output_proto, _config):
131 """Create a chroot snapshot and return a corresponding opaque snapshot key."""
132 chroot = controller_util.ParseChroot(input_proto.chroot)
133 token = sdk.CreateSnapshot(chroot, replace_if_needed=True)
134 output_proto.snapshot_token.value = token
135
136
137@faux.all_empty
138@validate.validation_complete
139def RestoreSnapshot(input_proto, _output_proto, _config):
140 """Restore a chroot snapshot from a snapshot key."""
141 chroot = controller_util.ParseChroot(input_proto.chroot)
142 token = input_proto.snapshot_token.value
143 sdk.RestoreSnapshot(token, chroot)