blob: e79917ab66fc439752e94bc6cf8eedb9e4b5d0b7 [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 Klein231d2da2019-07-22 16:44:45 -0600128 if config.validate_only:
129 return controller.RETURN_CODE_VALID_INPUT
130
Mike Frysinger3dcacee2019-08-23 17:09:11 -0400131 parsed_uri = urllib.parse.urlparse(uri)
Alex Klein8dffea42019-06-14 14:01:36 -0600132 upload_uri = gs.GetGsURL(parsed_uri.netloc, for_gsutil=True).rstrip('/')
Alex Klein3e728442019-05-15 11:46:57 -0600133 upload_path = parsed_uri.path.lstrip('/')
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600134
135 # Read all packages and update the index. The index must be uploaded to the
136 # binhost for Portage to use it, so include it in upload_targets.
Alex Kleinaf0e0452019-06-03 18:10:01 -0600137 uploads_dir = binhost.GetPrebuiltsRoot(chroot, sysroot, build_target)
Alex Kleine3a915f2022-08-09 23:04:48 -0600138 index_path = binhost.UpdatePackageIndex(
139 uploads_dir, upload_uri, upload_path, sudo=True)
Navil Perez4e8bb232022-08-16 18:11:13 +0000140 upload_targets = binhost.GetPrebuiltsFiles(uploads_dir)
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600141 assert index_path.startswith(uploads_dir), (
142 'expected index_path to start with uploads_dir')
143 upload_targets.append(index_path[len(uploads_dir):])
144
145 output_proto.uploads_dir = uploads_dir
146 for upload_target in upload_targets:
147 output_proto.upload_targets.add().path = upload_target.strip('/')
148
Alex Klein076841b2019-08-29 15:19:39 -0600149
Michael Mortensen42251f92019-11-14 11:01:43 -0700150def _PrepareDevInstallBinhostUploadsResponse(_input_proto, output_proto,
151 _config):
152 """Add fake binhost files to a successful response."""
153 output_proto.upload_targets.add().path = 'app-arch/zip-3.0-r3.tbz2'
154 output_proto.upload_targets.add().path = 'virtual/python-enum34-1.tbz2'
155 output_proto.upload_targets.add().path = 'Packages'
156
157
158@faux.success(_PrepareDevInstallBinhostUploadsResponse)
159@faux.empty_error
Michael Mortensenfc823882019-08-27 14:38:07 -0600160@validate.require('uri', 'sysroot.path')
161@validate.exists('uploads_dir')
Kevin Shelton703b6882022-01-24 16:31:31 -0800162def PrepareDevInstallBinhostUploads(
163 input_proto: binhost_pb2.PrepareDevInstallBinhostUploadsRequest,
164 output_proto: binhost_pb2.PrepareDevInstallBinhostUploadsResponse,
165 config: 'api_config.ApiConfig'):
Michael Mortensenfc823882019-08-27 14:38:07 -0600166 """Return a list of files to upload to the binhost"
167
168 The files will also be copied to the uploads_dir.
169 See BinhostService documentation in api/proto/binhost.proto.
170
171 Args:
Kevin Shelton703b6882022-01-24 16:31:31 -0800172 input_proto: The input proto.
173 output_proto: The output proto.
174 config: The API call config.
Michael Mortensenfc823882019-08-27 14:38:07 -0600175 """
176 sysroot_path = input_proto.sysroot.path
177
Michael Mortensenfc823882019-08-27 14:38:07 -0600178 chroot = controller_util.ParseChroot(input_proto.chroot)
179 sysroot = sysroot_lib.Sysroot(sysroot_path)
180
181 uri = input_proto.uri
182 # For now, we enforce that all input URIs are Google Storage buckets.
183 if not gs.PathIsGs(uri):
184 raise ValueError('Upload URI %s must be Google Storage.' % uri)
185
186 if config.validate_only:
187 return controller.RETURN_CODE_VALID_INPUT
188
Mike Frysinger3dcacee2019-08-23 17:09:11 -0400189 parsed_uri = urllib.parse.urlparse(uri)
Michael Mortensenfc823882019-08-27 14:38:07 -0600190 upload_uri = gs.GetGsURL(parsed_uri.netloc, for_gsutil=True).rstrip('/')
191 upload_path = parsed_uri.path.lstrip('/')
192
193 # Calculate the filename for the to-be-created Packages file, which will
194 # contain only devinstall packages.
195 devinstall_package_index_path = os.path.join(input_proto.uploads_dir,
196 'Packages')
197 upload_targets_list = binhost.ReadDevInstallFilesToCreatePackageIndex(
198 chroot, sysroot, devinstall_package_index_path, upload_uri, upload_path)
199
200 package_dir = chroot.full_path(sysroot.path, 'packages')
201 for upload_target in upload_targets_list:
202 # Copy each package to target/category/package
203 upload_target = upload_target.strip('/')
204 category = upload_target.split(os.sep)[0]
205 target_dir = os.path.join(input_proto.uploads_dir, category)
206 if not os.path.exists(target_dir):
207 os.makedirs(target_dir)
208 full_src_pkg_path = os.path.join(package_dir, upload_target)
209 full_target_src_path = os.path.join(input_proto.uploads_dir, upload_target)
210 shutil.copyfile(full_src_pkg_path, full_target_src_path)
211 output_proto.upload_targets.add().path = upload_target
212 output_proto.upload_targets.add().path = 'Packages'
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600213
Alex Klein076841b2019-08-29 15:19:39 -0600214
Michael Mortensen42251f92019-11-14 11:01:43 -0700215def _SetBinhostResponse(_input_proto, output_proto, _config):
216 """Add fake binhost file to a successful response."""
217 output_proto.output_file = '/path/to/BINHOST.conf'
218
219
220@faux.success(_SetBinhostResponse)
221@faux.empty_error
Alex Klein231d2da2019-07-22 16:44:45 -0600222@validate.require('build_target.name', 'key', 'uri')
223@validate.validation_complete
Kevin Shelton703b6882022-01-24 16:31:31 -0800224def SetBinhost(input_proto: binhost_pb2.SetBinhostRequest,
225 output_proto: binhost_pb2.SetBinhostResponse,
226 _config: 'api_config.ApiConfig'):
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600227 """Set the URI for a given binhost key and build target.
228
229 See BinhostService documentation in api/proto/binhost.proto.
230
231 Args:
Kevin Shelton703b6882022-01-24 16:31:31 -0800232 input_proto: The input proto.
233 output_proto: The output proto.
234 _config: The API call config.
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600235 """
236 target = input_proto.build_target.name
237 key = binhost_pb2.BinhostKey.Name(input_proto.key)
238 uri = input_proto.uri
239 private = input_proto.private
Alex Klein6fb0eb82019-05-20 16:16:14 -0600240
Alex Kleine3a915f2022-08-09 23:04:48 -0600241 output_proto.output_file = binhost.SetBinhost(
242 target, key, uri, private=private)
LaMont Jonesc64ae212019-04-15 15:41:28 -0600243
LaMont Jonesc64ae212019-04-15 15:41:28 -0600244
Michael Mortensen42251f92019-11-14 11:01:43 -0700245def _RegenBuildCacheResponse(_input_proto, output_proto, _config):
246 """Add fake binhosts cache path to a successful response."""
247 output_proto.modified_overlays.add().path = '/path/to/BuildCache'
248
249
250@faux.success(_RegenBuildCacheResponse)
251@faux.empty_error
Alex Klein231d2da2019-07-22 16:44:45 -0600252@validate.require('overlay_type')
253@validate.is_in('overlay_type', _OVERLAY_TYPE_TO_NAME)
254@validate.validation_complete
Kevin Shelton703b6882022-01-24 16:31:31 -0800255def RegenBuildCache(input_proto: binhost_pb2.RegenBuildCacheRequest,
256 output_proto: binhost_pb2.RegenBuildCacheResponse,
257 _config: 'api_config.ApiConfig'):
LaMont Jonesc64ae212019-04-15 15:41:28 -0600258 """Regenerate the Build Cache for a build target.
259
260 See BinhostService documentation in api/proto/binhost.proto.
261
262 Args:
Kevin Shelton703b6882022-01-24 16:31:31 -0800263 input_proto: The input proto.
264 output_proto: The output proto.
265 _config: The API call config.
LaMont Jonesc64ae212019-04-15 15:41:28 -0600266 """
Alex Klein82c85d42019-08-14 15:47:51 -0600267 chroot = controller_util.ParseChroot(input_proto.chroot)
LaMont Jonesc64ae212019-04-15 15:41:28 -0600268 overlay_type = input_proto.overlay_type
Alex Klein82c85d42019-08-14 15:47:51 -0600269 overlays = binhost.RegenBuildCache(chroot,
270 _OVERLAY_TYPE_TO_NAME[overlay_type])
271
272 for overlay in overlays:
273 output_proto.modified_overlays.add().path = overlay