Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 1 | # 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 Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 7 | import os |
| 8 | import shutil |
Kevin Shelton | 703b688 | 2022-01-24 16:31:31 -0800 | [diff] [blame] | 9 | from typing import TYPE_CHECKING |
Mike Frysinger | 1cc8f1f | 2022-04-28 22:40:40 -0400 | [diff] [blame] | 10 | import urllib.parse |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 11 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 12 | from chromite.api import controller |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 13 | from chromite.api import faux |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 14 | from chromite.api import validate |
Alex Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 15 | from chromite.api.controller import controller_util |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 16 | from chromite.api.gen.chromite.api import binhost_pb2 |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 17 | from chromite.lib import constants |
| 18 | from chromite.lib import cros_build_lib |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 19 | from chromite.lib import gs |
Alex Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 20 | from chromite.lib import sysroot_lib |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 21 | from chromite.service import binhost |
| 22 | |
Mike Frysinger | 1cc8f1f | 2022-04-28 22:40:40 -0400 | [diff] [blame] | 23 | |
Kevin Shelton | 703b688 | 2022-01-24 16:31:31 -0800 | [diff] [blame] | 24 | if TYPE_CHECKING: |
| 25 | from chromite.api import api_config |
Mike Frysinger | ef94e4c | 2020-02-10 23:59:54 -0500 | [diff] [blame] | 26 | |
Alex Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 27 | _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 Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 34 | |
Michael Mortensen | a0af77b | 2019-11-13 11:15:15 -0700 | [diff] [blame] | 35 | def _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 Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 45 | @validate.require('build_target.name') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 46 | @validate.validation_complete |
| 47 | def GetBinhosts(input_proto, output_proto, _config): |
Alex Klein | 7e40d25 | 2019-06-10 09:01:32 -0600 | [diff] [blame] | 48 | """Get a list of binhosts.""" |
Alex Klein | 2960c75 | 2020-03-09 13:43:38 -0600 | [diff] [blame] | 49 | build_target = controller_util.ParseBuildTarget(input_proto.build_target) |
Alex Klein | 7e40d25 | 2019-06-10 09:01:32 -0600 | [diff] [blame] | 50 | |
| 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 Klein | d3ac0bd | 2019-06-10 15:17:51 -0600 | [diff] [blame] | 56 | new_binhost.package_index = 'Packages' |
Alex Klein | 7e40d25 | 2019-06-10 09:01:32 -0600 | [diff] [blame] | 57 | |
| 58 | |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 59 | def _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 Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 68 | @validate.require('build_target.name') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 69 | @validate.validation_complete |
| 70 | def GetPrivatePrebuiltAclArgs(input_proto, output_proto, _config): |
Alex Klein | a471f68 | 2019-05-31 11:23:45 -0600 | [diff] [blame] | 71 | """Get the ACL args from the files in the private overlays.""" |
Alex Klein | 2960c75 | 2020-03-09 13:43:38 -0600 | [diff] [blame] | 72 | build_target = controller_util.ParseBuildTarget(input_proto.build_target) |
Alex Klein | a471f68 | 2019-05-31 11:23:45 -0600 | [diff] [blame] | 73 | |
| 74 | try: |
| 75 | args = binhost.GetPrebuiltAclArgs(build_target) |
| 76 | except binhost.Error as e: |
Mike Frysinger | 6b5c3cd | 2019-08-27 16:51:00 -0400 | [diff] [blame] | 77 | cros_build_lib.Die(e) |
Alex Klein | a471f68 | 2019-05-31 11:23:45 -0600 | [diff] [blame] | 78 | |
| 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 Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 85 | def _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 Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 93 | @validate.require('uri') |
Kevin Shelton | 703b688 | 2022-01-24 16:31:31 -0800 | [diff] [blame] | 94 | def PrepareBinhostUploads( |
| 95 | input_proto: binhost_pb2.PrepareBinhostUploadsRequest, |
| 96 | output_proto: binhost_pb2.PrepareBinhostUploadsResponse, |
| 97 | config: 'api_config.ApiConfig'): |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 98 | """Return a list of files to upload to the binhost. |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 99 | |
| 100 | See BinhostService documentation in api/proto/binhost.proto. |
| 101 | |
| 102 | Args: |
Kevin Shelton | 703b688 | 2022-01-24 16:31:31 -0800 | [diff] [blame] | 103 | input_proto: The input proto. |
| 104 | output_proto: The output proto. |
| 105 | config: The API call config. |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 106 | """ |
Alex Klein | 2960c75 | 2020-03-09 13:43:38 -0600 | [diff] [blame] | 107 | 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 Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 111 | sysroot_path = input_proto.sysroot.path |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 112 | |
Alex Klein | 2960c75 | 2020-03-09 13:43:38 -0600 | [diff] [blame] | 113 | if not sysroot_path and not build_target_msg.name: |
Alex Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 114 | cros_build_lib.Die('Sysroot.path is required.') |
| 115 | |
Alex Klein | 2960c75 | 2020-03-09 13:43:38 -0600 | [diff] [blame] | 116 | build_target = controller_util.ParseBuildTarget(build_target_msg) |
Alex Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 117 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 118 | |
| 119 | if not sysroot_path: |
Alex Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 120 | sysroot_path = build_target.root |
| 121 | sysroot = sysroot_lib.Sysroot(sysroot_path) |
| 122 | |
| 123 | uri = input_proto.uri |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 124 | # 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 Klein | 1715489 | 2022-08-10 00:26:37 -0600 | [diff] [blame^] | 128 | package_index_paths = [f.path.path for f in input_proto.package_index_files] |
| 129 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 130 | if config.validate_only: |
| 131 | return controller.RETURN_CODE_VALID_INPUT |
| 132 | |
Mike Frysinger | 3dcacee | 2019-08-23 17:09:11 -0400 | [diff] [blame] | 133 | parsed_uri = urllib.parse.urlparse(uri) |
Alex Klein | 8dffea4 | 2019-06-14 14:01:36 -0600 | [diff] [blame] | 134 | upload_uri = gs.GetGsURL(parsed_uri.netloc, for_gsutil=True).rstrip('/') |
Alex Klein | 3e72844 | 2019-05-15 11:46:57 -0600 | [diff] [blame] | 135 | upload_path = parsed_uri.path.lstrip('/') |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 136 | |
| 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 Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 139 | uploads_dir = binhost.GetPrebuiltsRoot(chroot, sysroot, build_target) |
Alex Klein | e3a915f | 2022-08-09 23:04:48 -0600 | [diff] [blame] | 140 | index_path = binhost.UpdatePackageIndex( |
| 141 | uploads_dir, upload_uri, upload_path, sudo=True) |
Alex Klein | 1715489 | 2022-08-10 00:26:37 -0600 | [diff] [blame^] | 142 | upload_targets = binhost.GetPrebuiltsFiles(uploads_dir, package_index_paths) |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 143 | 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 Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 151 | |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 152 | def _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 Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 162 | @validate.require('uri', 'sysroot.path') |
| 163 | @validate.exists('uploads_dir') |
Kevin Shelton | 703b688 | 2022-01-24 16:31:31 -0800 | [diff] [blame] | 164 | def PrepareDevInstallBinhostUploads( |
| 165 | input_proto: binhost_pb2.PrepareDevInstallBinhostUploadsRequest, |
| 166 | output_proto: binhost_pb2.PrepareDevInstallBinhostUploadsResponse, |
| 167 | config: 'api_config.ApiConfig'): |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 168 | """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 Shelton | 703b688 | 2022-01-24 16:31:31 -0800 | [diff] [blame] | 174 | input_proto: The input proto. |
| 175 | output_proto: The output proto. |
| 176 | config: The API call config. |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 177 | """ |
| 178 | sysroot_path = input_proto.sysroot.path |
| 179 | |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 180 | 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 Frysinger | 3dcacee | 2019-08-23 17:09:11 -0400 | [diff] [blame] | 191 | parsed_uri = urllib.parse.urlparse(uri) |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 192 | 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 Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 215 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 216 | |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 217 | def _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 Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 224 | @validate.require('build_target.name', 'key', 'uri') |
| 225 | @validate.validation_complete |
Kevin Shelton | 703b688 | 2022-01-24 16:31:31 -0800 | [diff] [blame] | 226 | def SetBinhost(input_proto: binhost_pb2.SetBinhostRequest, |
| 227 | output_proto: binhost_pb2.SetBinhostResponse, |
| 228 | _config: 'api_config.ApiConfig'): |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 229 | """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 Shelton | 703b688 | 2022-01-24 16:31:31 -0800 | [diff] [blame] | 234 | input_proto: The input proto. |
| 235 | output_proto: The output proto. |
| 236 | _config: The API call config. |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 237 | """ |
| 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 Klein | 6fb0eb8 | 2019-05-20 16:16:14 -0600 | [diff] [blame] | 242 | |
Alex Klein | e3a915f | 2022-08-09 23:04:48 -0600 | [diff] [blame] | 243 | output_proto.output_file = binhost.SetBinhost( |
| 244 | target, key, uri, private=private) |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 245 | |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 246 | |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 247 | def _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 Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 254 | @validate.require('overlay_type') |
| 255 | @validate.is_in('overlay_type', _OVERLAY_TYPE_TO_NAME) |
| 256 | @validate.validation_complete |
Kevin Shelton | 703b688 | 2022-01-24 16:31:31 -0800 | [diff] [blame] | 257 | def RegenBuildCache(input_proto: binhost_pb2.RegenBuildCacheRequest, |
| 258 | output_proto: binhost_pb2.RegenBuildCacheResponse, |
| 259 | _config: 'api_config.ApiConfig'): |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 260 | """Regenerate the Build Cache for a build target. |
| 261 | |
| 262 | See BinhostService documentation in api/proto/binhost.proto. |
| 263 | |
| 264 | Args: |
Kevin Shelton | 703b688 | 2022-01-24 16:31:31 -0800 | [diff] [blame] | 265 | input_proto: The input proto. |
| 266 | output_proto: The output proto. |
| 267 | _config: The API call config. |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 268 | """ |
Alex Klein | 82c85d4 | 2019-08-14 15:47:51 -0600 | [diff] [blame] | 269 | chroot = controller_util.ParseChroot(input_proto.chroot) |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 270 | overlay_type = input_proto.overlay_type |
Alex Klein | 82c85d4 | 2019-08-14 15:47:51 -0600 | [diff] [blame] | 271 | 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 |