blob: 8180831d5948c476783300acc4f83139a86a9598 [file] [log] [blame]
Evan Hernandezd437b4e2019-03-25 13:48:30 -06001# -*- 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"""Portage Binhost operations."""
7
8from __future__ import print_function
9
Michael Mortensenfc823882019-08-27 14:38:07 -060010import os
11import shutil
Mike Frysingeref94e4c2020-02-10 23:59:54 -050012import sys
Mike Frysinger3dcacee2019-08-23 17:09:11 -040013
14from six.moves import urllib
Evan Hernandezd437b4e2019-03-25 13:48:30 -060015
Alex Klein231d2da2019-07-22 16:44:45 -060016from chromite.api import controller
Alex Klein076841b2019-08-29 15:19:39 -060017from chromite.api import faux
Alex Klein2b236722019-06-19 15:44:26 -060018from chromite.api import validate
Alex Kleinaf0e0452019-06-03 18:10:01 -060019from chromite.api.controller import controller_util
Evan Hernandezd437b4e2019-03-25 13:48:30 -060020from chromite.api.gen.chromite.api import binhost_pb2
LaMont Jonesc64ae212019-04-15 15:41:28 -060021from chromite.lib import constants
22from chromite.lib import cros_build_lib
Evan Hernandezd437b4e2019-03-25 13:48:30 -060023from chromite.lib import gs
Alex Kleinaf0e0452019-06-03 18:10:01 -060024from chromite.lib import sysroot_lib
Evan Hernandezd437b4e2019-03-25 13:48:30 -060025from chromite.service import binhost
26
Mike Frysingeref94e4c2020-02-10 23:59:54 -050027
28assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
29
30
Alex Kleinaf0e0452019-06-03 18:10:01 -060031_OVERLAY_TYPE_TO_NAME = {
32 binhost_pb2.OVERLAYTYPE_PUBLIC: constants.PUBLIC_OVERLAYS,
33 binhost_pb2.OVERLAYTYPE_PRIVATE: constants.PRIVATE_OVERLAYS,
34 binhost_pb2.OVERLAYTYPE_BOTH: constants.BOTH_OVERLAYS,
35 binhost_pb2.OVERLAYTYPE_NONE: None
36}
37
Evan Hernandezd437b4e2019-03-25 13:48:30 -060038
Michael Mortensena0af77b2019-11-13 11:15:15 -070039def _GetBinhostsResponse(_input_proto, output_proto, _config):
40 """Add fake binhosts to a successful response."""
41 new_binhost = output_proto.binhosts.add()
42 new_binhost.uri = ('gs://cr-prebuilt/board/amd64-generic/'
43 'paladin-R66-17.0.0-rc2/packages/')
44 new_binhost.package_index = 'Packages'
45
46
47@faux.success(_GetBinhostsResponse)
48@faux.empty_error
Alex Klein2b236722019-06-19 15:44:26 -060049@validate.require('build_target.name')
Alex Klein231d2da2019-07-22 16:44:45 -060050@validate.validation_complete
51def GetBinhosts(input_proto, output_proto, _config):
Alex Klein7e40d252019-06-10 09:01:32 -060052 """Get a list of binhosts."""
Alex Klein2960c752020-03-09 13:43:38 -060053 build_target = controller_util.ParseBuildTarget(input_proto.build_target)
Alex Klein7e40d252019-06-10 09:01:32 -060054
55 binhosts = binhost.GetBinhosts(build_target)
56
57 for current in binhosts:
58 new_binhost = output_proto.binhosts.add()
59 new_binhost.uri = current
Alex Kleind3ac0bd2019-06-10 15:17:51 -060060 new_binhost.package_index = 'Packages'
Alex Klein7e40d252019-06-10 09:01:32 -060061
62
Michael Mortensen42251f92019-11-14 11:01:43 -070063def _GetPrivatePrebuiltAclArgsResponse(_input_proto, output_proto, _config):
64 """Add fake acls to a successful response."""
65 new_arg = output_proto.args.add()
66 new_arg.arg = '-g'
67 new_arg.value = 'group1:READ'
68
69
70@faux.success(_GetPrivatePrebuiltAclArgsResponse)
71@faux.empty_error
Alex Klein2b236722019-06-19 15:44:26 -060072@validate.require('build_target.name')
Alex Klein231d2da2019-07-22 16:44:45 -060073@validate.validation_complete
74def GetPrivatePrebuiltAclArgs(input_proto, output_proto, _config):
Alex Kleina471f682019-05-31 11:23:45 -060075 """Get the ACL args from the files in the private overlays."""
Alex Klein2960c752020-03-09 13:43:38 -060076 build_target = controller_util.ParseBuildTarget(input_proto.build_target)
Alex Kleina471f682019-05-31 11:23:45 -060077
78 try:
79 args = binhost.GetPrebuiltAclArgs(build_target)
80 except binhost.Error as e:
Mike Frysinger6b5c3cd2019-08-27 16:51:00 -040081 cros_build_lib.Die(e)
Alex Kleina471f682019-05-31 11:23:45 -060082
83 for arg, value in args:
84 new_arg = output_proto.args.add()
85 new_arg.arg = arg
86 new_arg.value = value
87
88
Michael Mortensen42251f92019-11-14 11:01:43 -070089def _PrepareBinhostUploadsResponse(_input_proto, output_proto, _config):
90 """Add fake binhost upload targets to a successful response."""
91 output_proto.uploads_dir = '/upload/directory'
92 output_proto.upload_targets.add().path = 'upload_target'
93
94
95@faux.success(_PrepareBinhostUploadsResponse)
96@faux.empty_error
Alex Klein231d2da2019-07-22 16:44:45 -060097@validate.require('uri')
98def PrepareBinhostUploads(input_proto, output_proto, config):
Michael Mortensen42251f92019-11-14 11:01:43 -070099 """Return a list of files to upload to the binhost.
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600100
101 See BinhostService documentation in api/proto/binhost.proto.
102
103 Args:
104 input_proto (PrepareBinhostUploadsRequest): The input proto.
105 output_proto (PrepareBinhostUploadsResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -0600106 config (api_config.ApiConfig): The API call config.
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600107 """
Alex Klein2960c752020-03-09 13:43:38 -0600108 if input_proto.sysroot.build_target.name:
109 build_target_msg = input_proto.sysroot.build_target
110 else:
111 build_target_msg = input_proto.build_target
Alex Kleinaf0e0452019-06-03 18:10:01 -0600112 sysroot_path = input_proto.sysroot.path
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600113
Alex Klein2960c752020-03-09 13:43:38 -0600114 if not sysroot_path and not build_target_msg.name:
Alex Kleinaf0e0452019-06-03 18:10:01 -0600115 cros_build_lib.Die('Sysroot.path is required.')
116
Alex Klein2960c752020-03-09 13:43:38 -0600117 build_target = controller_util.ParseBuildTarget(build_target_msg)
Alex Kleinaf0e0452019-06-03 18:10:01 -0600118 chroot = controller_util.ParseChroot(input_proto.chroot)
119
120 if not sysroot_path:
Alex Kleinaf0e0452019-06-03 18:10:01 -0600121 sysroot_path = build_target.root
122 sysroot = sysroot_lib.Sysroot(sysroot_path)
123
124 uri = input_proto.uri
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600125 # For now, we enforce that all input URIs are Google Storage buckets.
126 if not gs.PathIsGs(uri):
127 raise ValueError('Upload URI %s must be Google Storage.' % uri)
128
Alex Klein231d2da2019-07-22 16:44:45 -0600129 if config.validate_only:
130 return controller.RETURN_CODE_VALID_INPUT
131
Mike Frysinger3dcacee2019-08-23 17:09:11 -0400132 parsed_uri = urllib.parse.urlparse(uri)
Alex Klein8dffea42019-06-14 14:01:36 -0600133 upload_uri = gs.GetGsURL(parsed_uri.netloc, for_gsutil=True).rstrip('/')
Alex Klein3e728442019-05-15 11:46:57 -0600134 upload_path = parsed_uri.path.lstrip('/')
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600135
136 # Read all packages and update the index. The index must be uploaded to the
137 # binhost for Portage to use it, so include it in upload_targets.
Alex Kleinaf0e0452019-06-03 18:10:01 -0600138 uploads_dir = binhost.GetPrebuiltsRoot(chroot, sysroot, build_target)
Alex Klein3e728442019-05-15 11:46:57 -0600139 index_path = binhost.UpdatePackageIndex(uploads_dir, upload_uri, upload_path,
140 sudo=True)
Alex Kleinb5d57a62019-06-20 12:04:53 -0600141 upload_targets = binhost.GetPrebuiltsFiles(uploads_dir)
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600142 assert index_path.startswith(uploads_dir), (
143 'expected index_path to start with uploads_dir')
144 upload_targets.append(index_path[len(uploads_dir):])
145
146 output_proto.uploads_dir = uploads_dir
147 for upload_target in upload_targets:
148 output_proto.upload_targets.add().path = upload_target.strip('/')
149
Alex Klein076841b2019-08-29 15:19:39 -0600150
Michael Mortensen42251f92019-11-14 11:01:43 -0700151def _PrepareDevInstallBinhostUploadsResponse(_input_proto, output_proto,
152 _config):
153 """Add fake binhost files to a successful response."""
154 output_proto.upload_targets.add().path = 'app-arch/zip-3.0-r3.tbz2'
155 output_proto.upload_targets.add().path = 'virtual/python-enum34-1.tbz2'
156 output_proto.upload_targets.add().path = 'Packages'
157
158
159@faux.success(_PrepareDevInstallBinhostUploadsResponse)
160@faux.empty_error
Michael Mortensenfc823882019-08-27 14:38:07 -0600161@validate.require('uri', 'sysroot.path')
162@validate.exists('uploads_dir')
163def PrepareDevInstallBinhostUploads(input_proto, output_proto, config):
164 """Return a list of files to upload to the binhost"
165
166 The files will also be copied to the uploads_dir.
167 See BinhostService documentation in api/proto/binhost.proto.
168
169 Args:
170 input_proto (PrepareDevInstallBinhostUploadsRequest): The input proto.
171 output_proto (PrepareDevInstallBinhostUploadsResponse): The output proto.
172 config (api_config.ApiConfig): The API call config.
173 """
174 sysroot_path = input_proto.sysroot.path
175
Michael Mortensenfc823882019-08-27 14:38:07 -0600176 chroot = controller_util.ParseChroot(input_proto.chroot)
177 sysroot = sysroot_lib.Sysroot(sysroot_path)
178
179 uri = input_proto.uri
180 # For now, we enforce that all input URIs are Google Storage buckets.
181 if not gs.PathIsGs(uri):
182 raise ValueError('Upload URI %s must be Google Storage.' % uri)
183
184 if config.validate_only:
185 return controller.RETURN_CODE_VALID_INPUT
186
Mike Frysinger3dcacee2019-08-23 17:09:11 -0400187 parsed_uri = urllib.parse.urlparse(uri)
Michael Mortensenfc823882019-08-27 14:38:07 -0600188 upload_uri = gs.GetGsURL(parsed_uri.netloc, for_gsutil=True).rstrip('/')
189 upload_path = parsed_uri.path.lstrip('/')
190
191 # Calculate the filename for the to-be-created Packages file, which will
192 # contain only devinstall packages.
193 devinstall_package_index_path = os.path.join(input_proto.uploads_dir,
194 'Packages')
195 upload_targets_list = binhost.ReadDevInstallFilesToCreatePackageIndex(
196 chroot, sysroot, devinstall_package_index_path, upload_uri, upload_path)
197
198 package_dir = chroot.full_path(sysroot.path, 'packages')
199 for upload_target in upload_targets_list:
200 # Copy each package to target/category/package
201 upload_target = upload_target.strip('/')
202 category = upload_target.split(os.sep)[0]
203 target_dir = os.path.join(input_proto.uploads_dir, category)
204 if not os.path.exists(target_dir):
205 os.makedirs(target_dir)
206 full_src_pkg_path = os.path.join(package_dir, upload_target)
207 full_target_src_path = os.path.join(input_proto.uploads_dir, upload_target)
208 shutil.copyfile(full_src_pkg_path, full_target_src_path)
209 output_proto.upload_targets.add().path = upload_target
210 output_proto.upload_targets.add().path = 'Packages'
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600211
Alex Klein076841b2019-08-29 15:19:39 -0600212
Michael Mortensen42251f92019-11-14 11:01:43 -0700213def _SetBinhostResponse(_input_proto, output_proto, _config):
214 """Add fake binhost file to a successful response."""
215 output_proto.output_file = '/path/to/BINHOST.conf'
216
217
218@faux.success(_SetBinhostResponse)
219@faux.empty_error
Alex Klein231d2da2019-07-22 16:44:45 -0600220@validate.require('build_target.name', 'key', 'uri')
221@validate.validation_complete
222def SetBinhost(input_proto, output_proto, _config):
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600223 """Set the URI for a given binhost key and build target.
224
225 See BinhostService documentation in api/proto/binhost.proto.
226
227 Args:
228 input_proto (SetBinhostRequest): The input proto.
229 output_proto (SetBinhostResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -0600230 _config (api_config.ApiConfig): The API call config.
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600231 """
232 target = input_proto.build_target.name
233 key = binhost_pb2.BinhostKey.Name(input_proto.key)
234 uri = input_proto.uri
235 private = input_proto.private
Alex Klein6fb0eb82019-05-20 16:16:14 -0600236
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600237 output_proto.output_file = binhost.SetBinhost(target, key, uri,
238 private=private)
LaMont Jonesc64ae212019-04-15 15:41:28 -0600239
LaMont Jonesc64ae212019-04-15 15:41:28 -0600240
Michael Mortensen42251f92019-11-14 11:01:43 -0700241def _RegenBuildCacheResponse(_input_proto, output_proto, _config):
242 """Add fake binhosts cache path to a successful response."""
243 output_proto.modified_overlays.add().path = '/path/to/BuildCache'
244
245
246@faux.success(_RegenBuildCacheResponse)
247@faux.empty_error
Alex Klein231d2da2019-07-22 16:44:45 -0600248@validate.require('overlay_type')
249@validate.is_in('overlay_type', _OVERLAY_TYPE_TO_NAME)
250@validate.validation_complete
Alex Klein82c85d42019-08-14 15:47:51 -0600251def RegenBuildCache(input_proto, output_proto, _config):
LaMont Jonesc64ae212019-04-15 15:41:28 -0600252 """Regenerate the Build Cache for a build target.
253
254 See BinhostService documentation in api/proto/binhost.proto.
255
256 Args:
257 input_proto (RegenBuildCacheRequest): The input proto.
Alex Klein82c85d42019-08-14 15:47:51 -0600258 output_proto (RegenBuildCacheResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -0600259 _config (api_config.ApiConfig): The API call config.
LaMont Jonesc64ae212019-04-15 15:41:28 -0600260 """
Alex Klein82c85d42019-08-14 15:47:51 -0600261 chroot = controller_util.ParseChroot(input_proto.chroot)
LaMont Jonesc64ae212019-04-15 15:41:28 -0600262 overlay_type = input_proto.overlay_type
Alex Klein82c85d42019-08-14 15:47:51 -0600263 overlays = binhost.RegenBuildCache(chroot,
264 _OVERLAY_TYPE_TO_NAME[overlay_type])
265
266 for overlay in overlays:
267 output_proto.modified_overlays.add().path = overlay