blob: 584eea02f6a004dee31d633fd0d6bf591903a0ce [file] [log] [blame]
Trent Aptedbd3cb412023-08-18 11:37:02 +10001# Copyright 2023 The ChromiumOS Authors
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 Subtools builder Controller.
6
7Build API endpoint for converting protos to/from chromite.service.sdk_subtools.
8"""
9
Trent Aptedc94115a2023-09-20 16:39:18 +100010from pathlib import Path
Trent Aptedbd3cb412023-08-18 11:37:02 +100011from typing import Optional
12
13from chromite.api import api_config
14from chromite.api import controller
15from chromite.api import faux
16from chromite.api import validate
17from chromite.api.controller import controller_util
18from chromite.api.gen.chromite.api import sdk_subtools_pb2
Trent Aptedc94115a2023-09-20 16:39:18 +100019from chromite.api.gen.chromiumos import common_pb2
Trent Aptedbd3cb412023-08-18 11:37:02 +100020from chromite.lib import build_target_lib
Trent Aptedc94115a2023-09-20 16:39:18 +100021from chromite.lib import cros_build_lib
Trent Aptedbd3cb412023-08-18 11:37:02 +100022from chromite.lib import sysroot_lib
23from chromite.service import sdk_subtools
24
25
26@faux.empty_success
27@validate.validation_complete
28def BuildSdkSubtools(
29 _input_proto: sdk_subtools_pb2.BuildSdkSubtoolsRequest,
30 output_proto: sdk_subtools_pb2.BuildSdkSubtoolsResponse,
31 config: api_config.ApiConfig,
32) -> Optional[int]:
Trent Aptedc94115a2023-09-20 16:39:18 +100033 """Setup, and update packages in an SDK, then bundle subtools for upload."""
Trent Aptedbd3cb412023-08-18 11:37:02 +100034 build_target = build_target_lib.BuildTarget(
35 # Note `input_proto.chroot`` is not passed to `build_root` here:
36 # api.router.py clears the `chroot` field when entering the chroot, so
37 # it should always be empty when this endpoint is invoked.
38 name="amd64-subtools-host",
39 build_root="/",
40 )
41 if config.validate_only:
42 return controller.RETURN_CODE_VALID_INPUT
43
44 sdk_subtools.setup_base_sdk(build_target, setup_chroot=True, sudo=True)
45
46 try:
47 # Use shellcheck as a placeholder for testing. Eventually this will be
48 # a virtual package target.
49 sdk_subtools.update_packages(["dev-util/shellcheck"])
50 except sysroot_lib.PackageInstallError as e:
51 if not e.failed_packages:
52 # No packages to report, so just exit with an error code.
53 return controller.RETURN_CODE_COMPLETED_UNSUCCESSFULLY
54
55 host_sysroot = sysroot_lib.Sysroot("/")
56 controller_util.retrieve_package_log_paths(
57 e.failed_packages, output_proto, host_sysroot
58 )
59
60 return controller.RETURN_CODE_UNSUCCESSFUL_RESPONSE_AVAILABLE
61
Trent Aptedc94115a2023-09-20 16:39:18 +100062 (bundles, _) = sdk_subtools.bundle_and_prepare_upload()
63 output_proto.bundle_paths.extend(
64 common_pb2.Path(path=str(b), location=common_pb2.Path.INSIDE)
65 for b in bundles
66 )
67 return None
68
69
70@faux.empty_success
71@validate.validation_complete
72def UploadSdkSubtools(
73 input_proto: sdk_subtools_pb2.UploadSdkSubtoolsRequest,
74 _output_proto: sdk_subtools_pb2.UploadSdkSubtoolsResponse,
75 config: api_config.ApiConfig,
76) -> Optional[int]:
77 """Uploads a list of bundled subtools."""
78 if any(
79 p.location != common_pb2.Path.OUTSIDE for p in input_proto.bundle_paths
80 ):
81 cros_build_lib.Die(
82 "UploadSdkSubtools requires outside-chroot bundle paths."
83 )
84
85 bundles = [Path(path.path) for path in input_proto.bundle_paths]
86 if config.validate_only:
87 return controller.RETURN_CODE_VALID_INPUT
88
89 sdk_subtools.upload_prepared_bundles(input_proto.use_production, bundles)
Trent Aptedbd3cb412023-08-18 11:37:02 +100090 return None