blob: 3e2b4ff5ae6bdb43f868764f7d8e0cfdaf42669c [file] [log] [blame]
Evan Hernandezd437b4e2019-03-25 13:48:30 -06001# 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"""Portage Binhost operations."""
6
Michael Mortensenfc823882019-08-27 14:38:07 -06007import os
8import shutil
Kevin Shelton703b6882022-01-24 16:31:31 -08009from typing import TYPE_CHECKING
Mike Frysinger1cc8f1f2022-04-28 22:40:40 -040010import urllib.parse
Evan Hernandezd437b4e2019-03-25 13:48:30 -060011
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 Klein2b236722019-06-19 15:44:26 -060014from chromite.api import validate
Alex Kleinaf0e0452019-06-03 18:10:01 -060015from chromite.api.controller import controller_util
Evan Hernandezd437b4e2019-03-25 13:48:30 -060016from chromite.api.gen.chromite.api import binhost_pb2
LaMont Jonesc64ae212019-04-15 15:41:28 -060017from chromite.lib import constants
18from chromite.lib import cros_build_lib
Evan Hernandezd437b4e2019-03-25 13:48:30 -060019from chromite.lib import gs
Alex Kleinaf0e0452019-06-03 18:10:01 -060020from chromite.lib import sysroot_lib
Evan Hernandezd437b4e2019-03-25 13:48:30 -060021from chromite.service import binhost
22
Mike Frysinger1cc8f1f2022-04-28 22:40:40 -040023
Kevin Shelton703b6882022-01-24 16:31:31 -080024if TYPE_CHECKING:
25 from chromite.api import api_config
Mike Frysingeref94e4c2020-02-10 23:59:54 -050026
Alex Kleinaf0e0452019-06-03 18:10:01 -060027_OVERLAY_TYPE_TO_NAME = {
28 binhost_pb2.OVERLAYTYPE_PUBLIC: constants.PUBLIC_OVERLAYS,
29 binhost_pb2.OVERLAYTYPE_PRIVATE: constants.PRIVATE_OVERLAYS,
30 binhost_pb2.OVERLAYTYPE_BOTH: constants.BOTH_OVERLAYS,
31 binhost_pb2.OVERLAYTYPE_NONE: None
32}
33
Evan Hernandezd437b4e2019-03-25 13:48:30 -060034
Michael Mortensena0af77b2019-11-13 11:15:15 -070035def _GetBinhostsResponse(_input_proto, output_proto, _config):
36 """Add fake binhosts to a successful response."""
37 new_binhost = output_proto.binhosts.add()
38 new_binhost.uri = ('gs://cr-prebuilt/board/amd64-generic/'
39 'paladin-R66-17.0.0-rc2/packages/')
40 new_binhost.package_index = 'Packages'
41
42
43@faux.success(_GetBinhostsResponse)
44@faux.empty_error
Alex Klein2b236722019-06-19 15:44:26 -060045@validate.require('build_target.name')
Alex Klein231d2da2019-07-22 16:44:45 -060046@validate.validation_complete
47def GetBinhosts(input_proto, output_proto, _config):
Alex Klein7e40d252019-06-10 09:01:32 -060048 """Get a list of binhosts."""
Alex Klein2960c752020-03-09 13:43:38 -060049 build_target = controller_util.ParseBuildTarget(input_proto.build_target)
Alex Klein7e40d252019-06-10 09:01:32 -060050
51 binhosts = binhost.GetBinhosts(build_target)
52
53 for current in binhosts:
54 new_binhost = output_proto.binhosts.add()
55 new_binhost.uri = current
Alex Kleind3ac0bd2019-06-10 15:17:51 -060056 new_binhost.package_index = 'Packages'
Alex Klein7e40d252019-06-10 09:01:32 -060057
58
Michael Mortensen42251f92019-11-14 11:01:43 -070059def _GetPrivatePrebuiltAclArgsResponse(_input_proto, output_proto, _config):
60 """Add fake acls to a successful response."""
61 new_arg = output_proto.args.add()
62 new_arg.arg = '-g'
63 new_arg.value = 'group1:READ'
64
65
66@faux.success(_GetPrivatePrebuiltAclArgsResponse)
67@faux.empty_error
Alex Klein2b236722019-06-19 15:44:26 -060068@validate.require('build_target.name')
Alex Klein231d2da2019-07-22 16:44:45 -060069@validate.validation_complete
70def GetPrivatePrebuiltAclArgs(input_proto, output_proto, _config):
Alex Kleina471f682019-05-31 11:23:45 -060071 """Get the ACL args from the files in the private overlays."""
Alex Klein2960c752020-03-09 13:43:38 -060072 build_target = controller_util.ParseBuildTarget(input_proto.build_target)
Alex Kleina471f682019-05-31 11:23:45 -060073
74 try:
75 args = binhost.GetPrebuiltAclArgs(build_target)
76 except binhost.Error as e:
Mike Frysinger6b5c3cd2019-08-27 16:51:00 -040077 cros_build_lib.Die(e)
Alex Kleina471f682019-05-31 11:23:45 -060078
79 for arg, value in args:
80 new_arg = output_proto.args.add()
81 new_arg.arg = arg
82 new_arg.value = value
83
84
Michael Mortensen42251f92019-11-14 11:01:43 -070085def _PrepareBinhostUploadsResponse(_input_proto, output_proto, _config):
86 """Add fake binhost upload targets to a successful response."""
87 output_proto.uploads_dir = '/upload/directory'
88 output_proto.upload_targets.add().path = 'upload_target'
89
90
91@faux.success(_PrepareBinhostUploadsResponse)
92@faux.empty_error
Alex Klein231d2da2019-07-22 16:44:45 -060093@validate.require('uri')
Kevin Shelton703b6882022-01-24 16:31:31 -080094def PrepareBinhostUploads(
95 input_proto: binhost_pb2.PrepareBinhostUploadsRequest,
96 output_proto: binhost_pb2.PrepareBinhostUploadsResponse,
97 config: 'api_config.ApiConfig'):
Michael Mortensen42251f92019-11-14 11:01:43 -070098 """Return a list of files to upload to the binhost.
Evan Hernandezd437b4e2019-03-25 13:48:30 -060099
100 See BinhostService documentation in api/proto/binhost.proto.
101
102 Args:
Kevin Shelton703b6882022-01-24 16:31:31 -0800103 input_proto: The input proto.
104 output_proto: The output proto.
105 config: The API call config.
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600106 """
Alex Klein2960c752020-03-09 13:43:38 -0600107 if input_proto.sysroot.build_target.name:
108 build_target_msg = input_proto.sysroot.build_target
109 else:
110 build_target_msg = input_proto.build_target
Alex Kleinaf0e0452019-06-03 18:10:01 -0600111 sysroot_path = input_proto.sysroot.path
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600112
Alex Klein2960c752020-03-09 13:43:38 -0600113 if not sysroot_path and not build_target_msg.name:
Alex Kleinaf0e0452019-06-03 18:10:01 -0600114 cros_build_lib.Die('Sysroot.path is required.')
115
Alex Klein2960c752020-03-09 13:43:38 -0600116 build_target = controller_util.ParseBuildTarget(build_target_msg)
Alex Kleinaf0e0452019-06-03 18:10:01 -0600117 chroot = controller_util.ParseChroot(input_proto.chroot)
118
119 if not sysroot_path:
Alex Kleinaf0e0452019-06-03 18:10:01 -0600120 sysroot_path = build_target.root
121 sysroot = sysroot_lib.Sysroot(sysroot_path)
122
123 uri = input_proto.uri
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600124 # For now, we enforce that all input URIs are Google Storage buckets.
125 if not gs.PathIsGs(uri):
126 raise ValueError('Upload URI %s must be Google Storage.' % uri)
127
Alex Klein17154892022-08-10 00:26:37 -0600128 package_index_paths = [f.path.path for f in input_proto.package_index_files]
129
Alex Klein231d2da2019-07-22 16:44:45 -0600130 if config.validate_only:
131 return controller.RETURN_CODE_VALID_INPUT
132
Mike Frysinger3dcacee2019-08-23 17:09:11 -0400133 parsed_uri = urllib.parse.urlparse(uri)
Alex Klein8dffea42019-06-14 14:01:36 -0600134 upload_uri = gs.GetGsURL(parsed_uri.netloc, for_gsutil=True).rstrip('/')
Alex Klein3e728442019-05-15 11:46:57 -0600135 upload_path = parsed_uri.path.lstrip('/')
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600136
137 # Read all packages and update the index. The index must be uploaded to the
138 # binhost for Portage to use it, so include it in upload_targets.
Alex Kleinaf0e0452019-06-03 18:10:01 -0600139 uploads_dir = binhost.GetPrebuiltsRoot(chroot, sysroot, build_target)
Alex Kleine3a915f2022-08-09 23:04:48 -0600140 index_path = binhost.UpdatePackageIndex(
141 uploads_dir, upload_uri, upload_path, sudo=True)
Alex Klein17154892022-08-10 00:26:37 -0600142 upload_targets = binhost.GetPrebuiltsFiles(uploads_dir, package_index_paths)
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600143 assert index_path.startswith(uploads_dir), (
144 'expected index_path to start with uploads_dir')
145 upload_targets.append(index_path[len(uploads_dir):])
146
147 output_proto.uploads_dir = uploads_dir
148 for upload_target in upload_targets:
149 output_proto.upload_targets.add().path = upload_target.strip('/')
150
Alex Klein076841b2019-08-29 15:19:39 -0600151
Michael Mortensen42251f92019-11-14 11:01:43 -0700152def _PrepareDevInstallBinhostUploadsResponse(_input_proto, output_proto,
153 _config):
154 """Add fake binhost files to a successful response."""
155 output_proto.upload_targets.add().path = 'app-arch/zip-3.0-r3.tbz2'
156 output_proto.upload_targets.add().path = 'virtual/python-enum34-1.tbz2'
157 output_proto.upload_targets.add().path = 'Packages'
158
159
160@faux.success(_PrepareDevInstallBinhostUploadsResponse)
161@faux.empty_error
Michael Mortensenfc823882019-08-27 14:38:07 -0600162@validate.require('uri', 'sysroot.path')
163@validate.exists('uploads_dir')
Kevin Shelton703b6882022-01-24 16:31:31 -0800164def PrepareDevInstallBinhostUploads(
165 input_proto: binhost_pb2.PrepareDevInstallBinhostUploadsRequest,
166 output_proto: binhost_pb2.PrepareDevInstallBinhostUploadsResponse,
167 config: 'api_config.ApiConfig'):
Michael Mortensenfc823882019-08-27 14:38:07 -0600168 """Return a list of files to upload to the binhost"
169
170 The files will also be copied to the uploads_dir.
171 See BinhostService documentation in api/proto/binhost.proto.
172
173 Args:
Kevin Shelton703b6882022-01-24 16:31:31 -0800174 input_proto: The input proto.
175 output_proto: The output proto.
176 config: The API call config.
Michael Mortensenfc823882019-08-27 14:38:07 -0600177 """
178 sysroot_path = input_proto.sysroot.path
179
Michael Mortensenfc823882019-08-27 14:38:07 -0600180 chroot = controller_util.ParseChroot(input_proto.chroot)
181 sysroot = sysroot_lib.Sysroot(sysroot_path)
182
183 uri = input_proto.uri
184 # For now, we enforce that all input URIs are Google Storage buckets.
185 if not gs.PathIsGs(uri):
186 raise ValueError('Upload URI %s must be Google Storage.' % uri)
187
188 if config.validate_only:
189 return controller.RETURN_CODE_VALID_INPUT
190
Mike Frysinger3dcacee2019-08-23 17:09:11 -0400191 parsed_uri = urllib.parse.urlparse(uri)
Michael Mortensenfc823882019-08-27 14:38:07 -0600192 upload_uri = gs.GetGsURL(parsed_uri.netloc, for_gsutil=True).rstrip('/')
193 upload_path = parsed_uri.path.lstrip('/')
194
195 # Calculate the filename for the to-be-created Packages file, which will
196 # contain only devinstall packages.
197 devinstall_package_index_path = os.path.join(input_proto.uploads_dir,
198 'Packages')
199 upload_targets_list = binhost.ReadDevInstallFilesToCreatePackageIndex(
200 chroot, sysroot, devinstall_package_index_path, upload_uri, upload_path)
201
202 package_dir = chroot.full_path(sysroot.path, 'packages')
203 for upload_target in upload_targets_list:
204 # Copy each package to target/category/package
205 upload_target = upload_target.strip('/')
206 category = upload_target.split(os.sep)[0]
207 target_dir = os.path.join(input_proto.uploads_dir, category)
208 if not os.path.exists(target_dir):
209 os.makedirs(target_dir)
210 full_src_pkg_path = os.path.join(package_dir, upload_target)
211 full_target_src_path = os.path.join(input_proto.uploads_dir, upload_target)
212 shutil.copyfile(full_src_pkg_path, full_target_src_path)
213 output_proto.upload_targets.add().path = upload_target
214 output_proto.upload_targets.add().path = 'Packages'
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600215
Alex Klein076841b2019-08-29 15:19:39 -0600216
Michael Mortensen42251f92019-11-14 11:01:43 -0700217def _SetBinhostResponse(_input_proto, output_proto, _config):
218 """Add fake binhost file to a successful response."""
219 output_proto.output_file = '/path/to/BINHOST.conf'
220
221
222@faux.success(_SetBinhostResponse)
223@faux.empty_error
Alex Klein231d2da2019-07-22 16:44:45 -0600224@validate.require('build_target.name', 'key', 'uri')
225@validate.validation_complete
Kevin Shelton703b6882022-01-24 16:31:31 -0800226def SetBinhost(input_proto: binhost_pb2.SetBinhostRequest,
227 output_proto: binhost_pb2.SetBinhostResponse,
228 _config: 'api_config.ApiConfig'):
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600229 """Set the URI for a given binhost key and build target.
230
231 See BinhostService documentation in api/proto/binhost.proto.
232
233 Args:
Kevin Shelton703b6882022-01-24 16:31:31 -0800234 input_proto: The input proto.
235 output_proto: The output proto.
236 _config: The API call config.
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600237 """
238 target = input_proto.build_target.name
239 key = binhost_pb2.BinhostKey.Name(input_proto.key)
240 uri = input_proto.uri
241 private = input_proto.private
Alex Klein6fb0eb82019-05-20 16:16:14 -0600242
Alex Kleine3a915f2022-08-09 23:04:48 -0600243 output_proto.output_file = binhost.SetBinhost(
244 target, key, uri, private=private)
LaMont Jonesc64ae212019-04-15 15:41:28 -0600245
LaMont Jonesc64ae212019-04-15 15:41:28 -0600246
Michael Mortensen42251f92019-11-14 11:01:43 -0700247def _RegenBuildCacheResponse(_input_proto, output_proto, _config):
248 """Add fake binhosts cache path to a successful response."""
249 output_proto.modified_overlays.add().path = '/path/to/BuildCache'
250
251
252@faux.success(_RegenBuildCacheResponse)
253@faux.empty_error
Alex Klein231d2da2019-07-22 16:44:45 -0600254@validate.require('overlay_type')
255@validate.is_in('overlay_type', _OVERLAY_TYPE_TO_NAME)
256@validate.validation_complete
Kevin Shelton703b6882022-01-24 16:31:31 -0800257def RegenBuildCache(input_proto: binhost_pb2.RegenBuildCacheRequest,
258 output_proto: binhost_pb2.RegenBuildCacheResponse,
259 _config: 'api_config.ApiConfig'):
LaMont Jonesc64ae212019-04-15 15:41:28 -0600260 """Regenerate the Build Cache for a build target.
261
262 See BinhostService documentation in api/proto/binhost.proto.
263
264 Args:
Kevin Shelton703b6882022-01-24 16:31:31 -0800265 input_proto: The input proto.
266 output_proto: The output proto.
267 _config: The API call config.
LaMont Jonesc64ae212019-04-15 15:41:28 -0600268 """
Alex Klein82c85d42019-08-14 15:47:51 -0600269 chroot = controller_util.ParseChroot(input_proto.chroot)
LaMont Jonesc64ae212019-04-15 15:41:28 -0600270 overlay_type = input_proto.overlay_type
Alex Klein82c85d42019-08-14 15:47:51 -0600271 overlays = binhost.RegenBuildCache(chroot,
272 _OVERLAY_TYPE_TO_NAME[overlay_type])
273
274 for overlay in overlays:
275 output_proto.modified_overlays.add().path = overlay