Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame] | 1 | # Copyright 2019 The ChromiumOS Authors |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 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 |
Greg Edelston | 724c13d | 2023-04-07 16:19:24 -0600 | [diff] [blame] | 17 | from chromite.lib import binpkg |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 18 | from chromite.lib import constants |
| 19 | from chromite.lib import cros_build_lib |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 20 | from chromite.lib import gs |
Cindy Lin | d4c122a | 2023-04-13 19:09:31 +0000 | [diff] [blame] | 21 | from chromite.lib import osutils |
Alex Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 22 | from chromite.lib import sysroot_lib |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 23 | from chromite.service import binhost |
| 24 | |
Mike Frysinger | 1cc8f1f | 2022-04-28 22:40:40 -0400 | [diff] [blame] | 25 | |
Kevin Shelton | 703b688 | 2022-01-24 16:31:31 -0800 | [diff] [blame] | 26 | if TYPE_CHECKING: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 27 | from chromite.api import api_config |
Mike Frysinger | ef94e4c | 2020-02-10 23:59:54 -0500 | [diff] [blame] | 28 | |
Alex Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 29 | _OVERLAY_TYPE_TO_NAME = { |
| 30 | binhost_pb2.OVERLAYTYPE_PUBLIC: constants.PUBLIC_OVERLAYS, |
| 31 | binhost_pb2.OVERLAYTYPE_PRIVATE: constants.PRIVATE_OVERLAYS, |
| 32 | binhost_pb2.OVERLAYTYPE_BOTH: constants.BOTH_OVERLAYS, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 33 | binhost_pb2.OVERLAYTYPE_NONE: None, |
Alex Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 34 | } |
| 35 | |
Arif Kasim | 6242cdd | 2022-10-19 17:51:00 +0000 | [diff] [blame] | 36 | # Default maximum number of URIs to be stored in Binhost conf file. |
| 37 | _DEFAULT_BINHOST_MAX_URIS = 1 |
| 38 | |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 39 | |
Michael Mortensen | a0af77b | 2019-11-13 11:15:15 -0700 | [diff] [blame] | 40 | def _GetBinhostsResponse(_input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 41 | """Add fake binhosts to a successful response.""" |
| 42 | new_binhost = output_proto.binhosts.add() |
| 43 | new_binhost.uri = ( |
| 44 | "gs://cr-prebuilt/board/amd64-generic/" |
| 45 | "paladin-R66-17.0.0-rc2/packages/" |
| 46 | ) |
| 47 | new_binhost.package_index = "Packages" |
Michael Mortensen | a0af77b | 2019-11-13 11:15:15 -0700 | [diff] [blame] | 48 | |
| 49 | |
| 50 | @faux.success(_GetBinhostsResponse) |
| 51 | @faux.empty_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 52 | @validate.require("build_target.name") |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 53 | @validate.validation_complete |
| 54 | def GetBinhosts(input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 55 | """Get a list of binhosts.""" |
| 56 | build_target = controller_util.ParseBuildTarget(input_proto.build_target) |
Alex Klein | 7e40d25 | 2019-06-10 09:01:32 -0600 | [diff] [blame] | 57 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 58 | binhosts = binhost.GetBinhosts(build_target) |
Alex Klein | 7e40d25 | 2019-06-10 09:01:32 -0600 | [diff] [blame] | 59 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 60 | for current in binhosts: |
| 61 | new_binhost = output_proto.binhosts.add() |
| 62 | new_binhost.uri = current |
| 63 | new_binhost.package_index = "Packages" |
Alex Klein | 7e40d25 | 2019-06-10 09:01:32 -0600 | [diff] [blame] | 64 | |
| 65 | |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 66 | def _GetPrivatePrebuiltAclArgsResponse(_input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 67 | """Add fake acls to a successful response.""" |
| 68 | new_arg = output_proto.args.add() |
| 69 | new_arg.arg = "-g" |
| 70 | new_arg.value = "group1:READ" |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 71 | |
| 72 | |
| 73 | @faux.success(_GetPrivatePrebuiltAclArgsResponse) |
| 74 | @faux.empty_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 75 | @validate.require("build_target.name") |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 76 | @validate.validation_complete |
| 77 | def GetPrivatePrebuiltAclArgs(input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 78 | """Get the ACL args from the files in the private overlays.""" |
| 79 | build_target = controller_util.ParseBuildTarget(input_proto.build_target) |
Alex Klein | a471f68 | 2019-05-31 11:23:45 -0600 | [diff] [blame] | 80 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 81 | try: |
| 82 | args = binhost.GetPrebuiltAclArgs(build_target) |
| 83 | except binhost.Error as e: |
| 84 | cros_build_lib.Die(e) |
Alex Klein | a471f68 | 2019-05-31 11:23:45 -0600 | [diff] [blame] | 85 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 86 | for arg, value in args: |
| 87 | new_arg = output_proto.args.add() |
| 88 | new_arg.arg = arg |
| 89 | new_arg.value = value |
Alex Klein | a471f68 | 2019-05-31 11:23:45 -0600 | [diff] [blame] | 90 | |
| 91 | |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 92 | def _PrepareBinhostUploadsResponse(_input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 93 | """Add fake binhost upload targets to a successful response.""" |
| 94 | output_proto.uploads_dir = "/upload/directory" |
| 95 | output_proto.upload_targets.add().path = "upload_target" |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 96 | |
| 97 | |
| 98 | @faux.success(_PrepareBinhostUploadsResponse) |
| 99 | @faux.empty_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 100 | @validate.require("uri") |
Kevin Shelton | 703b688 | 2022-01-24 16:31:31 -0800 | [diff] [blame] | 101 | def PrepareBinhostUploads( |
| 102 | input_proto: binhost_pb2.PrepareBinhostUploadsRequest, |
| 103 | output_proto: binhost_pb2.PrepareBinhostUploadsResponse, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 104 | config: "api_config.ApiConfig", |
| 105 | ): |
| 106 | """Return a list of files to upload to the binhost. |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 107 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 108 | See BinhostService documentation in api/proto/binhost.proto. |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 109 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 110 | Args: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 111 | input_proto: The input proto. |
| 112 | output_proto: The output proto. |
| 113 | config: The API call config. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 114 | """ |
| 115 | if input_proto.sysroot.build_target.name: |
| 116 | build_target_msg = input_proto.sysroot.build_target |
| 117 | else: |
| 118 | build_target_msg = input_proto.build_target |
| 119 | sysroot_path = input_proto.sysroot.path |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 120 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 121 | if not sysroot_path and not build_target_msg.name: |
| 122 | cros_build_lib.Die("Sysroot.path is required.") |
Alex Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 123 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 124 | build_target = controller_util.ParseBuildTarget(build_target_msg) |
| 125 | chroot = controller_util.ParseChroot(input_proto.chroot) |
Alex Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 126 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 127 | if not sysroot_path: |
| 128 | sysroot_path = build_target.root |
| 129 | sysroot = sysroot_lib.Sysroot(sysroot_path) |
Alex Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 130 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 131 | uri = input_proto.uri |
| 132 | # For now, we enforce that all input URIs are Google Storage buckets. |
| 133 | if not gs.PathIsGs(uri): |
| 134 | raise ValueError("Upload URI %s must be Google Storage." % uri) |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 135 | |
Alex Klein | a0472ed | 2022-11-02 12:07:04 -0600 | [diff] [blame] | 136 | package_index_paths = [f.path.path for f in input_proto.package_index_files] |
| 137 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 138 | if config.validate_only: |
| 139 | return controller.RETURN_CODE_VALID_INPUT |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 140 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 141 | parsed_uri = urllib.parse.urlparse(uri) |
| 142 | upload_uri = gs.GetGsURL(parsed_uri.netloc, for_gsutil=True).rstrip("/") |
| 143 | upload_path = parsed_uri.path.lstrip("/") |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 144 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 145 | # Read all packages and update the index. The index must be uploaded to the |
| 146 | # binhost for Portage to use it, so include it in upload_targets. |
| 147 | uploads_dir = binhost.GetPrebuiltsRoot(chroot, sysroot, build_target) |
| 148 | index_path = binhost.UpdatePackageIndex( |
| 149 | uploads_dir, upload_uri, upload_path, sudo=True |
| 150 | ) |
Alex Klein | a0472ed | 2022-11-02 12:07:04 -0600 | [diff] [blame] | 151 | upload_targets = binhost.GetPrebuiltsFiles( |
| 152 | uploads_dir, package_index_paths=package_index_paths, sudo=True |
| 153 | ) |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 154 | assert index_path.startswith( |
| 155 | uploads_dir |
| 156 | ), "expected index_path to start with uploads_dir" |
| 157 | upload_targets.append(index_path[len(uploads_dir) :]) |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 158 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 159 | output_proto.uploads_dir = uploads_dir |
| 160 | for upload_target in upload_targets: |
| 161 | output_proto.upload_targets.add().path = upload_target.strip("/") |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 162 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 163 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 164 | def _PrepareDevInstallBinhostUploadsResponse( |
| 165 | _input_proto, output_proto, _config |
| 166 | ): |
| 167 | """Add fake binhost files to a successful response.""" |
| 168 | output_proto.upload_targets.add().path = "app-arch/zip-3.0-r3.tbz2" |
| 169 | output_proto.upload_targets.add().path = "virtual/python-enum34-1.tbz2" |
| 170 | output_proto.upload_targets.add().path = "Packages" |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 171 | |
| 172 | |
| 173 | @faux.success(_PrepareDevInstallBinhostUploadsResponse) |
| 174 | @faux.empty_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 175 | @validate.require("uri", "sysroot.path") |
| 176 | @validate.exists("uploads_dir") |
Kevin Shelton | 703b688 | 2022-01-24 16:31:31 -0800 | [diff] [blame] | 177 | def PrepareDevInstallBinhostUploads( |
| 178 | input_proto: binhost_pb2.PrepareDevInstallBinhostUploadsRequest, |
| 179 | output_proto: binhost_pb2.PrepareDevInstallBinhostUploadsResponse, |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 180 | config: "api_config.ApiConfig", |
| 181 | ): |
| 182 | """Return a list of files to upload to the binhost" |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 183 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 184 | The files will also be copied to the uploads_dir. |
| 185 | See BinhostService documentation in api/proto/binhost.proto. |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 186 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 187 | Args: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 188 | input_proto: The input proto. |
| 189 | output_proto: The output proto. |
| 190 | config: The API call config. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 191 | """ |
| 192 | sysroot_path = input_proto.sysroot.path |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 193 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 194 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 195 | sysroot = sysroot_lib.Sysroot(sysroot_path) |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 196 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 197 | uri = input_proto.uri |
| 198 | # For now, we enforce that all input URIs are Google Storage buckets. |
| 199 | if not gs.PathIsGs(uri): |
| 200 | raise ValueError("Upload URI %s must be Google Storage." % uri) |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 201 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 202 | if config.validate_only: |
| 203 | return controller.RETURN_CODE_VALID_INPUT |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 204 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 205 | parsed_uri = urllib.parse.urlparse(uri) |
| 206 | upload_uri = gs.GetGsURL(parsed_uri.netloc, for_gsutil=True).rstrip("/") |
| 207 | upload_path = parsed_uri.path.lstrip("/") |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 208 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 209 | # Calculate the filename for the to-be-created Packages file, which will |
| 210 | # contain only devinstall packages. |
| 211 | devinstall_package_index_path = os.path.join( |
| 212 | input_proto.uploads_dir, "Packages" |
| 213 | ) |
| 214 | upload_targets_list = binhost.ReadDevInstallFilesToCreatePackageIndex( |
| 215 | chroot, sysroot, devinstall_package_index_path, upload_uri, upload_path |
| 216 | ) |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 217 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 218 | package_dir = chroot.full_path(sysroot.path, "packages") |
| 219 | for upload_target in upload_targets_list: |
| 220 | # Copy each package to target/category/package |
| 221 | upload_target = upload_target.strip("/") |
| 222 | category = upload_target.split(os.sep)[0] |
| 223 | target_dir = os.path.join(input_proto.uploads_dir, category) |
| 224 | if not os.path.exists(target_dir): |
| 225 | os.makedirs(target_dir) |
| 226 | full_src_pkg_path = os.path.join(package_dir, upload_target) |
| 227 | full_target_src_path = os.path.join( |
| 228 | input_proto.uploads_dir, upload_target |
| 229 | ) |
| 230 | shutil.copyfile(full_src_pkg_path, full_target_src_path) |
| 231 | output_proto.upload_targets.add().path = upload_target |
| 232 | output_proto.upload_targets.add().path = "Packages" |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 233 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 234 | |
Cindy Lin | d4c122a | 2023-04-13 19:09:31 +0000 | [diff] [blame] | 235 | def _PrepareChromeBinhostUploadsResponse(_input_proto, output_proto, _config): |
| 236 | """Add fake binhost files to a successful response.""" |
| 237 | output_proto.upload_targets.add().path = ( |
| 238 | "chromeos-base/chromeos-chrome-100-r1.tbz2" |
| 239 | ) |
| 240 | output_proto.upload_targets.add().path = ( |
| 241 | "chromeos-base/chrome-icu-100-r1.tbz2" |
| 242 | ) |
| 243 | output_proto.upload_targets.add().path = ( |
| 244 | "chromeos-base/chromeos-lacros-100-r1.tbz2" |
| 245 | ) |
| 246 | output_proto.upload_targets.add().path = "Packages" |
| 247 | |
| 248 | |
| 249 | @faux.success(_PrepareChromeBinhostUploadsResponse) |
| 250 | @faux.empty_error |
| 251 | @validate.require("uploads_dir", "uri", "sysroot.path") |
| 252 | @validate.validation_complete |
| 253 | def PrepareChromeBinhostUploads( |
| 254 | input_proto: binhost_pb2.PrepareChromeBinhostUploadsRequest, |
| 255 | output_proto: binhost_pb2.PrepareChromeBinhostUploadsResponse, |
| 256 | config: "api_config.ApiConfig", |
| 257 | ): |
| 258 | """Return a list of Chrome files to upload to the binhost. |
| 259 | |
| 260 | The files will also be copied to the uploads_dir. |
| 261 | See BinhostService documentation in api/proto/binhost.proto. |
| 262 | |
| 263 | Args: |
| 264 | input_proto: The input proto. |
| 265 | output_proto: The output proto. |
| 266 | config: The API call config. |
| 267 | """ |
| 268 | if config.validate_only: |
| 269 | return controller.RETURN_CODE_VALID_INPUT |
| 270 | |
| 271 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 272 | sysroot = sysroot_lib.Sysroot(input_proto.sysroot.path) |
| 273 | |
| 274 | uri = input_proto.uri |
| 275 | # For now, we enforce that all input URIs are Google Storage buckets. |
| 276 | if not gs.PathIsGs(uri): |
| 277 | raise ValueError("Upload URI %s must be Google Storage." % uri) |
| 278 | parsed_uri = urllib.parse.urlparse(uri) |
| 279 | gs_bucket = gs.GetGsURL(parsed_uri.netloc, for_gsutil=True).rstrip("/") |
| 280 | upload_path = parsed_uri.path.lstrip("/") |
| 281 | |
| 282 | # Determine the filename for the to-be-created Packages file, which will |
| 283 | # contain only Chrome packages. |
| 284 | chrome_package_index_path = os.path.join( |
| 285 | input_proto.uploads_dir, "Packages" |
| 286 | ) |
| 287 | upload_targets_list = binhost.CreateChromePackageIndex( |
| 288 | chroot, sysroot, chrome_package_index_path, gs_bucket, upload_path |
| 289 | ) |
| 290 | |
| 291 | package_dir = chroot.full_path(sysroot.path, "packages") |
| 292 | for upload_target in upload_targets_list: |
| 293 | # Copy each package to uploads_dir/category/package |
| 294 | upload_target = upload_target.strip("/") |
| 295 | category = upload_target.split("/")[0] |
| 296 | target_dir = os.path.join(input_proto.uploads_dir, category) |
| 297 | if not os.path.exists(target_dir): |
| 298 | osutils.SafeMakedirs(target_dir) |
| 299 | full_src_pkg_path = os.path.join(package_dir, upload_target) |
| 300 | full_target_src_path = os.path.join( |
| 301 | input_proto.uploads_dir, upload_target |
| 302 | ) |
| 303 | shutil.copyfile(full_src_pkg_path, full_target_src_path) |
| 304 | output_proto.upload_targets.add().path = upload_target |
| 305 | output_proto.upload_targets.add().path = "Packages" |
| 306 | |
| 307 | |
Greg Edelston | 724c13d | 2023-04-07 16:19:24 -0600 | [diff] [blame] | 308 | def _UpdatePackageIndexResponse(_input_proto, _output_proto, _config): |
| 309 | """Set up a fake successful response.""" |
| 310 | |
| 311 | |
| 312 | @faux.success(_UpdatePackageIndexResponse) |
| 313 | @faux.empty_error |
| 314 | @validate.require("package_index_file") |
| 315 | @validate.require_any("set_upload_location") |
| 316 | @validate.validation_complete |
| 317 | def UpdatePackageIndex( |
| 318 | input_proto: binhost_pb2.UpdatePackageIndexRequest, |
| 319 | _output_proto: binhost_pb2.UpdatePackageIndexResponse, |
| 320 | _config: "api_config.ApiConfig", |
| 321 | ): |
| 322 | """Implementation for the BinhostService/UpdatePackageIndex endpoint.""" |
| 323 | # Load the index file. |
| 324 | index_path = controller_util.pb2_path_to_pathlib_path( |
| 325 | input_proto.package_index_file, |
| 326 | chroot=input_proto.chroot, |
| 327 | ) |
| 328 | pkgindex = binpkg.PackageIndex() |
| 329 | pkgindex.ReadFilePath(index_path) |
| 330 | |
| 331 | # Set the upload location for all packages. |
| 332 | if input_proto.set_upload_location: |
| 333 | if not input_proto.uri: |
| 334 | raise ValueError("set_upload_location is True, but no uri provided") |
| 335 | parsed_uri = urllib.parse.urlparse(input_proto.uri) |
| 336 | pkgindex.SetUploadLocation( |
| 337 | gs.GetGsURL(parsed_uri.netloc, for_gsutil=True).rstrip("/"), |
| 338 | parsed_uri.path.lstrip("/"), |
| 339 | ) |
| 340 | |
| 341 | # Write the updated index file back to its original location. |
| 342 | pkgindex.WriteFile(index_path) |
| 343 | |
| 344 | |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 345 | def _SetBinhostResponse(_input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 346 | """Add fake binhost file to a successful response.""" |
| 347 | output_proto.output_file = "/path/to/BINHOST.conf" |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 348 | |
| 349 | |
| 350 | @faux.success(_SetBinhostResponse) |
| 351 | @faux.empty_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 352 | @validate.require("build_target.name", "key", "uri") |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 353 | @validate.validation_complete |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 354 | def SetBinhost( |
| 355 | input_proto: binhost_pb2.SetBinhostRequest, |
| 356 | output_proto: binhost_pb2.SetBinhostResponse, |
| 357 | _config: "api_config.ApiConfig", |
| 358 | ): |
| 359 | """Set the URI for a given binhost key and build target. |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 360 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 361 | See BinhostService documentation in api/proto/binhost.proto. |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 362 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 363 | Args: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 364 | input_proto: The input proto. |
| 365 | output_proto: The output proto. |
| 366 | _config: The API call config. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 367 | """ |
| 368 | target = input_proto.build_target.name |
| 369 | key = binhost_pb2.BinhostKey.Name(input_proto.key) |
| 370 | uri = input_proto.uri |
| 371 | private = input_proto.private |
Arif Kasim | 6242cdd | 2022-10-19 17:51:00 +0000 | [diff] [blame] | 372 | max_uris = input_proto.max_uris or _DEFAULT_BINHOST_MAX_URIS |
Alex Klein | 6fb0eb8 | 2019-05-20 16:16:14 -0600 | [diff] [blame] | 373 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 374 | output_proto.output_file = binhost.SetBinhost( |
Arif Kasim | 6242cdd | 2022-10-19 17:51:00 +0000 | [diff] [blame] | 375 | target, key, uri, private=private, max_uris=max_uris |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 376 | ) |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 377 | |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 378 | |
Arif Kasim | a046726 | 2022-11-11 17:08:14 +0000 | [diff] [blame] | 379 | def _GetBinhostConfPathResponse(_input_proto, output_proto, _config): |
| 380 | """Add fake binhost file to a successful response.""" |
| 381 | output_proto.conf_path = "/path/to/BINHOST.conf" |
| 382 | |
| 383 | |
| 384 | @faux.success(_GetBinhostConfPathResponse) |
| 385 | @faux.empty_error |
| 386 | @validate.require("build_target.name", "key") |
| 387 | @validate.validation_complete |
| 388 | def GetBinhostConfPath( |
| 389 | input_proto: binhost_pb2.GetBinhostConfPathRequest, |
| 390 | output_proto: binhost_pb2.GetBinhostConfPathResponse, |
| 391 | _config: "api_config.ApiConfig", |
| 392 | ): |
| 393 | target = input_proto.build_target.name |
| 394 | key = binhost_pb2.BinhostKey.Name(input_proto.key) |
| 395 | private = input_proto.private |
| 396 | output_proto.conf_path = str( |
| 397 | binhost.GetBinhostConfPath(target, key, private) |
| 398 | ) |
| 399 | |
| 400 | |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 401 | def _RegenBuildCacheResponse(_input_proto, output_proto, _config): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 402 | """Add fake binhosts cache path to a successful response.""" |
| 403 | output_proto.modified_overlays.add().path = "/path/to/BuildCache" |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 404 | |
| 405 | |
| 406 | @faux.success(_RegenBuildCacheResponse) |
| 407 | @faux.empty_error |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 408 | @validate.require("overlay_type") |
| 409 | @validate.is_in("overlay_type", _OVERLAY_TYPE_TO_NAME) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 410 | @validate.validation_complete |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 411 | def RegenBuildCache( |
| 412 | input_proto: binhost_pb2.RegenBuildCacheRequest, |
| 413 | output_proto: binhost_pb2.RegenBuildCacheResponse, |
| 414 | _config: "api_config.ApiConfig", |
| 415 | ): |
| 416 | """Regenerate the Build Cache for a build target. |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 417 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 418 | See BinhostService documentation in api/proto/binhost.proto. |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 419 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 420 | Args: |
Alex Klein | 611dddd | 2022-10-11 17:02:01 -0600 | [diff] [blame] | 421 | input_proto: The input proto. |
| 422 | output_proto: The output proto. |
| 423 | _config: The API call config. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 424 | """ |
| 425 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 426 | overlay_type = input_proto.overlay_type |
| 427 | overlays = binhost.RegenBuildCache( |
| 428 | chroot, _OVERLAY_TYPE_TO_NAME[overlay_type] |
| 429 | ) |
Alex Klein | 82c85d4 | 2019-08-14 15:47:51 -0600 | [diff] [blame] | 430 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 431 | for overlay in overlays: |
| 432 | output_proto.modified_overlays.add().path = overlay |