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 | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 128 | if config.validate_only: |
| 129 | return controller.RETURN_CODE_VALID_INPUT |
| 130 | |
Mike Frysinger | 3dcacee | 2019-08-23 17:09:11 -0400 | [diff] [blame] | 131 | parsed_uri = urllib.parse.urlparse(uri) |
Alex Klein | 8dffea4 | 2019-06-14 14:01:36 -0600 | [diff] [blame] | 132 | upload_uri = gs.GetGsURL(parsed_uri.netloc, for_gsutil=True).rstrip('/') |
Alex Klein | 3e72844 | 2019-05-15 11:46:57 -0600 | [diff] [blame] | 133 | upload_path = parsed_uri.path.lstrip('/') |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 134 | |
| 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 Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 137 | uploads_dir = binhost.GetPrebuiltsRoot(chroot, sysroot, build_target) |
Alex Klein | e3a915f | 2022-08-09 23:04:48 -0600 | [diff] [blame] | 138 | index_path = binhost.UpdatePackageIndex( |
| 139 | uploads_dir, upload_uri, upload_path, sudo=True) |
Navil Perez | 4e8bb23 | 2022-08-16 18:11:13 +0000 | [diff] [blame^] | 140 | upload_targets = binhost.GetPrebuiltsFiles(uploads_dir) |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 141 | 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 Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 149 | |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 150 | def _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 Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 160 | @validate.require('uri', 'sysroot.path') |
| 161 | @validate.exists('uploads_dir') |
Kevin Shelton | 703b688 | 2022-01-24 16:31:31 -0800 | [diff] [blame] | 162 | def PrepareDevInstallBinhostUploads( |
| 163 | input_proto: binhost_pb2.PrepareDevInstallBinhostUploadsRequest, |
| 164 | output_proto: binhost_pb2.PrepareDevInstallBinhostUploadsResponse, |
| 165 | config: 'api_config.ApiConfig'): |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 166 | """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 Shelton | 703b688 | 2022-01-24 16:31:31 -0800 | [diff] [blame] | 172 | input_proto: The input proto. |
| 173 | output_proto: The output proto. |
| 174 | config: The API call config. |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 175 | """ |
| 176 | sysroot_path = input_proto.sysroot.path |
| 177 | |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 178 | 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 Frysinger | 3dcacee | 2019-08-23 17:09:11 -0400 | [diff] [blame] | 189 | parsed_uri = urllib.parse.urlparse(uri) |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 190 | 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 Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 213 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 214 | |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 215 | def _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 Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 222 | @validate.require('build_target.name', 'key', 'uri') |
| 223 | @validate.validation_complete |
Kevin Shelton | 703b688 | 2022-01-24 16:31:31 -0800 | [diff] [blame] | 224 | def SetBinhost(input_proto: binhost_pb2.SetBinhostRequest, |
| 225 | output_proto: binhost_pb2.SetBinhostResponse, |
| 226 | _config: 'api_config.ApiConfig'): |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 227 | """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 Shelton | 703b688 | 2022-01-24 16:31:31 -0800 | [diff] [blame] | 232 | input_proto: The input proto. |
| 233 | output_proto: The output proto. |
| 234 | _config: The API call config. |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 235 | """ |
| 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 Klein | 6fb0eb8 | 2019-05-20 16:16:14 -0600 | [diff] [blame] | 240 | |
Alex Klein | e3a915f | 2022-08-09 23:04:48 -0600 | [diff] [blame] | 241 | output_proto.output_file = binhost.SetBinhost( |
| 242 | target, key, uri, private=private) |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 243 | |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 244 | |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 245 | def _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 Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 252 | @validate.require('overlay_type') |
| 253 | @validate.is_in('overlay_type', _OVERLAY_TYPE_TO_NAME) |
| 254 | @validate.validation_complete |
Kevin Shelton | 703b688 | 2022-01-24 16:31:31 -0800 | [diff] [blame] | 255 | def RegenBuildCache(input_proto: binhost_pb2.RegenBuildCacheRequest, |
| 256 | output_proto: binhost_pb2.RegenBuildCacheResponse, |
| 257 | _config: 'api_config.ApiConfig'): |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 258 | """Regenerate the Build Cache for a build target. |
| 259 | |
| 260 | See BinhostService documentation in api/proto/binhost.proto. |
| 261 | |
| 262 | Args: |
Kevin Shelton | 703b688 | 2022-01-24 16:31:31 -0800 | [diff] [blame] | 263 | input_proto: The input proto. |
| 264 | output_proto: The output proto. |
| 265 | _config: The API call config. |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 266 | """ |
Alex Klein | 82c85d4 | 2019-08-14 15:47:51 -0600 | [diff] [blame] | 267 | chroot = controller_util.ParseChroot(input_proto.chroot) |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 268 | overlay_type = input_proto.overlay_type |
Alex Klein | 82c85d4 | 2019-08-14 15:47:51 -0600 | [diff] [blame] | 269 | 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 |