blob: 4dd5c3b96280e11cf6acd9d1f67a6ff2b37eb42c [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
Mike Frysingeref94e4c2020-02-10 23:59:54 -050011import sys
Alex Klein19c4cc42019-02-27 14:47:57 -070012
Alex Klein231d2da2019-07-22 16:44:45 -060013from chromite.api import controller
Alex Klein076841b2019-08-29 15:19:39 -060014from chromite.api import faux
Alex Klein231d2da2019-07-22 16:44:45 -060015from chromite.api import validate
Alex Klein71a9a1d2019-10-28 15:45:10 -060016from chromite.api.controller import controller_util
Alex Klein19c4cc42019-02-27 14:47:57 -070017from chromite.lib import cros_build_lib
18from chromite.service import sdk
19
20
Mike Frysingeref94e4c2020-02-10 23:59:54 -050021assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
22
23
Alex Klein076841b2019-08-29 15:19:39 -060024def _ChrootVersionResponse(_input_proto, output_proto, _config):
25 """Add a fake chroot version to a successful response."""
26 output_proto.version.version = 168
27
28
29@faux.success(_ChrootVersionResponse)
30@faux.empty_error
Alex Klein231d2da2019-07-22 16:44:45 -060031def Create(input_proto, output_proto, config):
Alex Klein19c4cc42019-02-27 14:47:57 -070032 """Chroot creation, includes support for replacing an existing chroot.
33
34 Args:
35 input_proto (CreateRequest): The input proto.
36 output_proto (CreateResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -060037 config (api_config.ApiConfig): The API call config.
Alex Klein19c4cc42019-02-27 14:47:57 -070038 """
39 replace = not input_proto.flags.no_replace
40 bootstrap = input_proto.flags.bootstrap
41 use_image = not input_proto.flags.no_use_image
42
Alex Klein00aa8072019-04-15 16:36:00 -060043 chroot_path = input_proto.chroot.path
44 cache_dir = input_proto.chroot.cache_dir
Alex Klein19c4cc42019-02-27 14:47:57 -070045
46 if chroot_path and not os.path.isabs(chroot_path):
47 cros_build_lib.Die('The chroot path must be absolute.')
48
Alex Klein231d2da2019-07-22 16:44:45 -060049 if config.validate_only:
50 return controller.RETURN_CODE_VALID_INPUT
51
Chris McDonald5dcdb892020-02-07 15:10:46 -070052 args = sdk.CreateArguments(
53 replace=replace,
54 bootstrap=bootstrap,
55 use_image=use_image,
56 cache_dir=cache_dir,
57 chroot_path=chroot_path)
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 Klein231d2da2019-07-22 16:44:45 -060071@validate.validation_complete
72def Update(input_proto, output_proto, _config):
Alex Kleinaa5c4172019-02-27 17:12:20 -070073 """Update the chroot.
74
75 Args:
76 input_proto (UpdateRequest): The input proto.
77 output_proto (UpdateResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -060078 _config (api_config.ApiConfig): The API call config.
Alex Kleinaa5c4172019-02-27 17:12:20 -070079 """
80 build_source = input_proto.flags.build_source
81 targets = [target.name for target in input_proto.toolchain_targets]
Chris McDonald68faa2a2020-01-13 12:23:05 -070082 toolchain_changed = input_proto.flags.toolchain_changed
Alex Kleinaa5c4172019-02-27 17:12:20 -070083
Chris McDonald68faa2a2020-01-13 12:23:05 -070084 args = sdk.UpdateArguments(
85 build_source=build_source,
86 toolchain_targets=targets,
87 toolchain_changed=toolchain_changed)
88
Alex Kleinaa5c4172019-02-27 17:12:20 -070089 version = sdk.Update(args)
90
91 if version:
92 output_proto.version.version = version
93 else:
94 # This should be very rare, if ever used, but worth noting.
95 cros_build_lib.Die('No chroot version could be found. There was likely an'
96 'error creating the chroot that was not detected.')
Alex Klein730cf552019-10-16 11:28:22 -060097
98
99@faux.all_empty
100@validate.validation_complete
101def Delete(input_proto, _output_proto, _config):
102 """Delete a chroot."""
Alex Klein71a9a1d2019-10-28 15:45:10 -0600103 chroot = controller_util.ParseChroot(input_proto.chroot)
104 sdk.Delete(chroot)
Chris McDonald53ad5442020-01-17 14:11:55 -0700105
106
107@faux.all_empty
108@validate.validation_complete
109def Unmount(input_proto, _output_proto, _config):
110 """Unmount a chroot"""
111 chroot = controller_util.ParseChroot(input_proto.chroot)
112 sdk.Unmount(chroot)
Chris McDonaldf48ea202020-01-29 13:19:23 -0700113
114
115@faux.all_empty
116@validate.validation_complete
117def Clean(input_proto, _output_proto, _config):
118 """Clean unneeded files from a chroot."""
119 chroot = controller_util.ParseChroot(input_proto.chroot)
Alex Kleincbc70d22020-02-10 11:05:47 -0700120 sdk.Clean(chroot, sysroots=True, tmp=True)
Chris McDonald9d486802020-01-29 15:57:22 -0700121
122
123@faux.all_empty
124@validate.validation_complete
125def CreateSnapshot(input_proto, output_proto, _config):
126 """Create a chroot snapshot and return a corresponding opaque snapshot key."""
127 chroot = controller_util.ParseChroot(input_proto.chroot)
128 token = sdk.CreateSnapshot(chroot, replace_if_needed=True)
129 output_proto.snapshot_token.value = token
130
131
132@faux.all_empty
133@validate.validation_complete
134def RestoreSnapshot(input_proto, _output_proto, _config):
135 """Restore a chroot snapshot from a snapshot key."""
136 chroot = controller_util.ParseChroot(input_proto.chroot)
137 token = input_proto.snapshot_token.value
138 sdk.RestoreSnapshot(token, chroot)