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 |
Mike Frysinger | e852b07 | 2021-05-21 12:39:03 -0400 | [diff] [blame] | 9 | import urllib.parse |
Kevin Shelton | 703b688 | 2022-01-24 16:31:31 -0800 | [diff] [blame^] | 10 | from typing import TYPE_CHECKING |
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 | |
Kevin Shelton | 703b688 | 2022-01-24 16:31:31 -0800 | [diff] [blame^] | 23 | if TYPE_CHECKING: |
| 24 | from chromite.api import api_config |
Mike Frysinger | ef94e4c | 2020-02-10 23:59:54 -0500 | [diff] [blame] | 25 | |
Alex Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 26 | _OVERLAY_TYPE_TO_NAME = { |
| 27 | binhost_pb2.OVERLAYTYPE_PUBLIC: constants.PUBLIC_OVERLAYS, |
| 28 | binhost_pb2.OVERLAYTYPE_PRIVATE: constants.PRIVATE_OVERLAYS, |
| 29 | binhost_pb2.OVERLAYTYPE_BOTH: constants.BOTH_OVERLAYS, |
| 30 | binhost_pb2.OVERLAYTYPE_NONE: None |
| 31 | } |
| 32 | |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 33 | |
Michael Mortensen | a0af77b | 2019-11-13 11:15:15 -0700 | [diff] [blame] | 34 | def _GetBinhostsResponse(_input_proto, output_proto, _config): |
| 35 | """Add fake binhosts to a successful response.""" |
| 36 | new_binhost = output_proto.binhosts.add() |
| 37 | new_binhost.uri = ('gs://cr-prebuilt/board/amd64-generic/' |
| 38 | 'paladin-R66-17.0.0-rc2/packages/') |
| 39 | new_binhost.package_index = 'Packages' |
| 40 | |
| 41 | |
| 42 | @faux.success(_GetBinhostsResponse) |
| 43 | @faux.empty_error |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 44 | @validate.require('build_target.name') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 45 | @validate.validation_complete |
| 46 | def GetBinhosts(input_proto, output_proto, _config): |
Alex Klein | 7e40d25 | 2019-06-10 09:01:32 -0600 | [diff] [blame] | 47 | """Get a list of binhosts.""" |
Alex Klein | 2960c75 | 2020-03-09 13:43:38 -0600 | [diff] [blame] | 48 | build_target = controller_util.ParseBuildTarget(input_proto.build_target) |
Alex Klein | 7e40d25 | 2019-06-10 09:01:32 -0600 | [diff] [blame] | 49 | |
| 50 | binhosts = binhost.GetBinhosts(build_target) |
| 51 | |
| 52 | for current in binhosts: |
| 53 | new_binhost = output_proto.binhosts.add() |
| 54 | new_binhost.uri = current |
Alex Klein | d3ac0bd | 2019-06-10 15:17:51 -0600 | [diff] [blame] | 55 | new_binhost.package_index = 'Packages' |
Alex Klein | 7e40d25 | 2019-06-10 09:01:32 -0600 | [diff] [blame] | 56 | |
| 57 | |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 58 | def _GetPrivatePrebuiltAclArgsResponse(_input_proto, output_proto, _config): |
| 59 | """Add fake acls to a successful response.""" |
| 60 | new_arg = output_proto.args.add() |
| 61 | new_arg.arg = '-g' |
| 62 | new_arg.value = 'group1:READ' |
| 63 | |
| 64 | |
| 65 | @faux.success(_GetPrivatePrebuiltAclArgsResponse) |
| 66 | @faux.empty_error |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 67 | @validate.require('build_target.name') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 68 | @validate.validation_complete |
| 69 | def GetPrivatePrebuiltAclArgs(input_proto, output_proto, _config): |
Alex Klein | a471f68 | 2019-05-31 11:23:45 -0600 | [diff] [blame] | 70 | """Get the ACL args from the files in the private overlays.""" |
Alex Klein | 2960c75 | 2020-03-09 13:43:38 -0600 | [diff] [blame] | 71 | build_target = controller_util.ParseBuildTarget(input_proto.build_target) |
Alex Klein | a471f68 | 2019-05-31 11:23:45 -0600 | [diff] [blame] | 72 | |
| 73 | try: |
| 74 | args = binhost.GetPrebuiltAclArgs(build_target) |
| 75 | except binhost.Error as e: |
Mike Frysinger | 6b5c3cd | 2019-08-27 16:51:00 -0400 | [diff] [blame] | 76 | cros_build_lib.Die(e) |
Alex Klein | a471f68 | 2019-05-31 11:23:45 -0600 | [diff] [blame] | 77 | |
| 78 | for arg, value in args: |
| 79 | new_arg = output_proto.args.add() |
| 80 | new_arg.arg = arg |
| 81 | new_arg.value = value |
| 82 | |
| 83 | |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 84 | def _PrepareBinhostUploadsResponse(_input_proto, output_proto, _config): |
| 85 | """Add fake binhost upload targets to a successful response.""" |
| 86 | output_proto.uploads_dir = '/upload/directory' |
| 87 | output_proto.upload_targets.add().path = 'upload_target' |
| 88 | |
| 89 | |
| 90 | @faux.success(_PrepareBinhostUploadsResponse) |
| 91 | @faux.empty_error |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 92 | @validate.require('uri') |
Kevin Shelton | 703b688 | 2022-01-24 16:31:31 -0800 | [diff] [blame^] | 93 | def PrepareBinhostUploads( |
| 94 | input_proto: binhost_pb2.PrepareBinhostUploadsRequest, |
| 95 | output_proto: binhost_pb2.PrepareBinhostUploadsResponse, |
| 96 | config: 'api_config.ApiConfig'): |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 97 | """Return a list of files to upload to the binhost. |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 98 | |
| 99 | See BinhostService documentation in api/proto/binhost.proto. |
| 100 | |
| 101 | Args: |
Kevin Shelton | 703b688 | 2022-01-24 16:31:31 -0800 | [diff] [blame^] | 102 | input_proto: The input proto. |
| 103 | output_proto: The output proto. |
| 104 | config: The API call config. |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 105 | """ |
Alex Klein | 2960c75 | 2020-03-09 13:43:38 -0600 | [diff] [blame] | 106 | if input_proto.sysroot.build_target.name: |
| 107 | build_target_msg = input_proto.sysroot.build_target |
| 108 | else: |
| 109 | build_target_msg = input_proto.build_target |
Alex Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 110 | sysroot_path = input_proto.sysroot.path |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 111 | |
Alex Klein | 2960c75 | 2020-03-09 13:43:38 -0600 | [diff] [blame] | 112 | if not sysroot_path and not build_target_msg.name: |
Alex Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 113 | cros_build_lib.Die('Sysroot.path is required.') |
| 114 | |
Alex Klein | 2960c75 | 2020-03-09 13:43:38 -0600 | [diff] [blame] | 115 | build_target = controller_util.ParseBuildTarget(build_target_msg) |
Alex Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 116 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 117 | |
| 118 | if not sysroot_path: |
Alex Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 119 | sysroot_path = build_target.root |
| 120 | sysroot = sysroot_lib.Sysroot(sysroot_path) |
| 121 | |
| 122 | uri = input_proto.uri |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 123 | # For now, we enforce that all input URIs are Google Storage buckets. |
| 124 | if not gs.PathIsGs(uri): |
| 125 | raise ValueError('Upload URI %s must be Google Storage.' % uri) |
| 126 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 127 | if config.validate_only: |
| 128 | return controller.RETURN_CODE_VALID_INPUT |
| 129 | |
Mike Frysinger | 3dcacee | 2019-08-23 17:09:11 -0400 | [diff] [blame] | 130 | parsed_uri = urllib.parse.urlparse(uri) |
Alex Klein | 8dffea4 | 2019-06-14 14:01:36 -0600 | [diff] [blame] | 131 | upload_uri = gs.GetGsURL(parsed_uri.netloc, for_gsutil=True).rstrip('/') |
Alex Klein | 3e72844 | 2019-05-15 11:46:57 -0600 | [diff] [blame] | 132 | upload_path = parsed_uri.path.lstrip('/') |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 133 | |
| 134 | # Read all packages and update the index. The index must be uploaded to the |
| 135 | # binhost for Portage to use it, so include it in upload_targets. |
Alex Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 136 | uploads_dir = binhost.GetPrebuiltsRoot(chroot, sysroot, build_target) |
Alex Klein | 3e72844 | 2019-05-15 11:46:57 -0600 | [diff] [blame] | 137 | index_path = binhost.UpdatePackageIndex(uploads_dir, upload_uri, upload_path, |
| 138 | sudo=True) |
Alex Klein | b5d57a6 | 2019-06-20 12:04:53 -0600 | [diff] [blame] | 139 | upload_targets = binhost.GetPrebuiltsFiles(uploads_dir) |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 140 | assert index_path.startswith(uploads_dir), ( |
| 141 | 'expected index_path to start with uploads_dir') |
| 142 | upload_targets.append(index_path[len(uploads_dir):]) |
| 143 | |
| 144 | output_proto.uploads_dir = uploads_dir |
| 145 | for upload_target in upload_targets: |
| 146 | output_proto.upload_targets.add().path = upload_target.strip('/') |
| 147 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 148 | |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 149 | def _PrepareDevInstallBinhostUploadsResponse(_input_proto, output_proto, |
| 150 | _config): |
| 151 | """Add fake binhost files to a successful response.""" |
| 152 | output_proto.upload_targets.add().path = 'app-arch/zip-3.0-r3.tbz2' |
| 153 | output_proto.upload_targets.add().path = 'virtual/python-enum34-1.tbz2' |
| 154 | output_proto.upload_targets.add().path = 'Packages' |
| 155 | |
| 156 | |
| 157 | @faux.success(_PrepareDevInstallBinhostUploadsResponse) |
| 158 | @faux.empty_error |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 159 | @validate.require('uri', 'sysroot.path') |
| 160 | @validate.exists('uploads_dir') |
Kevin Shelton | 703b688 | 2022-01-24 16:31:31 -0800 | [diff] [blame^] | 161 | def PrepareDevInstallBinhostUploads( |
| 162 | input_proto: binhost_pb2.PrepareDevInstallBinhostUploadsRequest, |
| 163 | output_proto: binhost_pb2.PrepareDevInstallBinhostUploadsResponse, |
| 164 | config: 'api_config.ApiConfig'): |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 165 | """Return a list of files to upload to the binhost" |
| 166 | |
| 167 | The files will also be copied to the uploads_dir. |
| 168 | See BinhostService documentation in api/proto/binhost.proto. |
| 169 | |
| 170 | Args: |
Kevin Shelton | 703b688 | 2022-01-24 16:31:31 -0800 | [diff] [blame^] | 171 | input_proto: The input proto. |
| 172 | output_proto: The output proto. |
| 173 | config: The API call config. |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 174 | """ |
| 175 | sysroot_path = input_proto.sysroot.path |
| 176 | |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 177 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 178 | sysroot = sysroot_lib.Sysroot(sysroot_path) |
| 179 | |
| 180 | uri = input_proto.uri |
| 181 | # For now, we enforce that all input URIs are Google Storage buckets. |
| 182 | if not gs.PathIsGs(uri): |
| 183 | raise ValueError('Upload URI %s must be Google Storage.' % uri) |
| 184 | |
| 185 | if config.validate_only: |
| 186 | return controller.RETURN_CODE_VALID_INPUT |
| 187 | |
Mike Frysinger | 3dcacee | 2019-08-23 17:09:11 -0400 | [diff] [blame] | 188 | parsed_uri = urllib.parse.urlparse(uri) |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 189 | upload_uri = gs.GetGsURL(parsed_uri.netloc, for_gsutil=True).rstrip('/') |
| 190 | upload_path = parsed_uri.path.lstrip('/') |
| 191 | |
| 192 | # Calculate the filename for the to-be-created Packages file, which will |
| 193 | # contain only devinstall packages. |
| 194 | devinstall_package_index_path = os.path.join(input_proto.uploads_dir, |
| 195 | 'Packages') |
| 196 | upload_targets_list = binhost.ReadDevInstallFilesToCreatePackageIndex( |
| 197 | chroot, sysroot, devinstall_package_index_path, upload_uri, upload_path) |
| 198 | |
| 199 | package_dir = chroot.full_path(sysroot.path, 'packages') |
| 200 | for upload_target in upload_targets_list: |
| 201 | # Copy each package to target/category/package |
| 202 | upload_target = upload_target.strip('/') |
| 203 | category = upload_target.split(os.sep)[0] |
| 204 | target_dir = os.path.join(input_proto.uploads_dir, category) |
| 205 | if not os.path.exists(target_dir): |
| 206 | os.makedirs(target_dir) |
| 207 | full_src_pkg_path = os.path.join(package_dir, upload_target) |
| 208 | full_target_src_path = os.path.join(input_proto.uploads_dir, upload_target) |
| 209 | shutil.copyfile(full_src_pkg_path, full_target_src_path) |
| 210 | output_proto.upload_targets.add().path = upload_target |
| 211 | output_proto.upload_targets.add().path = 'Packages' |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 212 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 213 | |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 214 | def _SetBinhostResponse(_input_proto, output_proto, _config): |
| 215 | """Add fake binhost file to a successful response.""" |
| 216 | output_proto.output_file = '/path/to/BINHOST.conf' |
| 217 | |
| 218 | |
| 219 | @faux.success(_SetBinhostResponse) |
| 220 | @faux.empty_error |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 221 | @validate.require('build_target.name', 'key', 'uri') |
| 222 | @validate.validation_complete |
Kevin Shelton | 703b688 | 2022-01-24 16:31:31 -0800 | [diff] [blame^] | 223 | def SetBinhost(input_proto: binhost_pb2.SetBinhostRequest, |
| 224 | output_proto: binhost_pb2.SetBinhostResponse, |
| 225 | _config: 'api_config.ApiConfig'): |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 226 | """Set the URI for a given binhost key and build target. |
| 227 | |
| 228 | See BinhostService documentation in api/proto/binhost.proto. |
| 229 | |
| 230 | Args: |
Kevin Shelton | 703b688 | 2022-01-24 16:31:31 -0800 | [diff] [blame^] | 231 | input_proto: The input proto. |
| 232 | output_proto: The output proto. |
| 233 | _config: The API call config. |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 234 | """ |
| 235 | target = input_proto.build_target.name |
| 236 | key = binhost_pb2.BinhostKey.Name(input_proto.key) |
| 237 | uri = input_proto.uri |
| 238 | private = input_proto.private |
Alex Klein | 6fb0eb8 | 2019-05-20 16:16:14 -0600 | [diff] [blame] | 239 | |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 240 | output_proto.output_file = binhost.SetBinhost(target, key, uri, |
| 241 | private=private) |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 242 | |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 243 | |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 244 | def _RegenBuildCacheResponse(_input_proto, output_proto, _config): |
| 245 | """Add fake binhosts cache path to a successful response.""" |
| 246 | output_proto.modified_overlays.add().path = '/path/to/BuildCache' |
| 247 | |
| 248 | |
| 249 | @faux.success(_RegenBuildCacheResponse) |
| 250 | @faux.empty_error |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 251 | @validate.require('overlay_type') |
| 252 | @validate.is_in('overlay_type', _OVERLAY_TYPE_TO_NAME) |
| 253 | @validate.validation_complete |
Kevin Shelton | 703b688 | 2022-01-24 16:31:31 -0800 | [diff] [blame^] | 254 | def RegenBuildCache(input_proto: binhost_pb2.RegenBuildCacheRequest, |
| 255 | output_proto: binhost_pb2.RegenBuildCacheResponse, |
| 256 | _config: 'api_config.ApiConfig'): |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 257 | """Regenerate the Build Cache for a build target. |
| 258 | |
| 259 | See BinhostService documentation in api/proto/binhost.proto. |
| 260 | |
| 261 | Args: |
Kevin Shelton | 703b688 | 2022-01-24 16:31:31 -0800 | [diff] [blame^] | 262 | input_proto: The input proto. |
| 263 | output_proto: The output proto. |
| 264 | _config: The API call config. |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 265 | """ |
Alex Klein | 82c85d4 | 2019-08-14 15:47:51 -0600 | [diff] [blame] | 266 | chroot = controller_util.ParseChroot(input_proto.chroot) |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 267 | overlay_type = input_proto.overlay_type |
Alex Klein | 82c85d4 | 2019-08-14 15:47:51 -0600 | [diff] [blame] | 268 | overlays = binhost.RegenBuildCache(chroot, |
| 269 | _OVERLAY_TYPE_TO_NAME[overlay_type]) |
| 270 | |
| 271 | for overlay in overlays: |
| 272 | output_proto.modified_overlays.add().path = overlay |