Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 1 | # -*- 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 | |
| 8 | from __future__ import print_function |
| 9 | |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 10 | import os |
| 11 | import shutil |
Mike Frysinger | 3dcacee | 2019-08-23 17:09:11 -0400 | [diff] [blame] | 12 | |
| 13 | from six.moves import urllib |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 14 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 15 | from chromite.api import controller |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 16 | from chromite.api import faux |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 17 | from chromite.api import validate |
Alex Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 18 | from chromite.api.controller import controller_util |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 19 | from chromite.api.gen.chromite.api import binhost_pb2 |
Alex Klein | a471f68 | 2019-05-31 11:23:45 -0600 | [diff] [blame] | 20 | from chromite.lib import build_target_util |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 21 | from chromite.lib import constants |
| 22 | from chromite.lib import cros_build_lib |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 23 | from chromite.lib import gs |
Alex Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 24 | from chromite.lib import sysroot_lib |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 25 | from chromite.service import binhost |
| 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 | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 49 | build_target = build_target_util.BuildTarget(input_proto.build_target.name) |
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 | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 72 | build_target = build_target_util.BuildTarget(input_proto.build_target.name) |
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') |
| 94 | def PrepareBinhostUploads(input_proto, output_proto, config): |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 95 | """Return a list of files to upload to the binhost. |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 96 | |
| 97 | See BinhostService documentation in api/proto/binhost.proto. |
| 98 | |
| 99 | Args: |
| 100 | input_proto (PrepareBinhostUploadsRequest): The input proto. |
| 101 | output_proto (PrepareBinhostUploadsResponse): The output proto. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 102 | config (api_config.ApiConfig): The API call config. |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 103 | """ |
Alex Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 104 | target_name = (input_proto.sysroot.build_target.name |
| 105 | or input_proto.build_target.name) |
| 106 | sysroot_path = input_proto.sysroot.path |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 107 | |
Alex Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 108 | if not sysroot_path and not target_name: |
| 109 | cros_build_lib.Die('Sysroot.path is required.') |
| 110 | |
| 111 | build_target = build_target_util.BuildTarget(target_name) |
| 112 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 113 | |
| 114 | if not sysroot_path: |
| 115 | # Very temporary, so not worried about this not calling the lib function. |
| 116 | sysroot_path = build_target.root |
| 117 | sysroot = sysroot_lib.Sysroot(sysroot_path) |
| 118 | |
| 119 | uri = input_proto.uri |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 120 | # For now, we enforce that all input URIs are Google Storage buckets. |
| 121 | if not gs.PathIsGs(uri): |
| 122 | raise ValueError('Upload URI %s must be Google Storage.' % uri) |
| 123 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 124 | if config.validate_only: |
| 125 | return controller.RETURN_CODE_VALID_INPUT |
| 126 | |
Mike Frysinger | 3dcacee | 2019-08-23 17:09:11 -0400 | [diff] [blame] | 127 | parsed_uri = urllib.parse.urlparse(uri) |
Alex Klein | 8dffea4 | 2019-06-14 14:01:36 -0600 | [diff] [blame] | 128 | upload_uri = gs.GetGsURL(parsed_uri.netloc, for_gsutil=True).rstrip('/') |
Alex Klein | 3e72844 | 2019-05-15 11:46:57 -0600 | [diff] [blame] | 129 | upload_path = parsed_uri.path.lstrip('/') |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 130 | |
| 131 | # Read all packages and update the index. The index must be uploaded to the |
| 132 | # binhost for Portage to use it, so include it in upload_targets. |
Alex Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 133 | uploads_dir = binhost.GetPrebuiltsRoot(chroot, sysroot, build_target) |
Alex Klein | 3e72844 | 2019-05-15 11:46:57 -0600 | [diff] [blame] | 134 | index_path = binhost.UpdatePackageIndex(uploads_dir, upload_uri, upload_path, |
| 135 | sudo=True) |
Alex Klein | b5d57a6 | 2019-06-20 12:04:53 -0600 | [diff] [blame] | 136 | upload_targets = binhost.GetPrebuiltsFiles(uploads_dir) |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 137 | assert index_path.startswith(uploads_dir), ( |
| 138 | 'expected index_path to start with uploads_dir') |
| 139 | upload_targets.append(index_path[len(uploads_dir):]) |
| 140 | |
| 141 | output_proto.uploads_dir = uploads_dir |
| 142 | for upload_target in upload_targets: |
| 143 | output_proto.upload_targets.add().path = upload_target.strip('/') |
| 144 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 145 | |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 146 | def _PrepareDevInstallBinhostUploadsResponse(_input_proto, output_proto, |
| 147 | _config): |
| 148 | """Add fake binhost files to a successful response.""" |
| 149 | output_proto.upload_targets.add().path = 'app-arch/zip-3.0-r3.tbz2' |
| 150 | output_proto.upload_targets.add().path = 'virtual/python-enum34-1.tbz2' |
| 151 | output_proto.upload_targets.add().path = 'Packages' |
| 152 | |
| 153 | |
| 154 | @faux.success(_PrepareDevInstallBinhostUploadsResponse) |
| 155 | @faux.empty_error |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 156 | @validate.require('uri', 'sysroot.path') |
| 157 | @validate.exists('uploads_dir') |
| 158 | def PrepareDevInstallBinhostUploads(input_proto, output_proto, config): |
| 159 | """Return a list of files to upload to the binhost" |
| 160 | |
| 161 | The files will also be copied to the uploads_dir. |
| 162 | See BinhostService documentation in api/proto/binhost.proto. |
| 163 | |
| 164 | Args: |
| 165 | input_proto (PrepareDevInstallBinhostUploadsRequest): The input proto. |
| 166 | output_proto (PrepareDevInstallBinhostUploadsResponse): The output proto. |
| 167 | config (api_config.ApiConfig): The API call config. |
| 168 | """ |
| 169 | sysroot_path = input_proto.sysroot.path |
| 170 | |
| 171 | # build_target = build_target_util.BuildTarget(target_name) |
| 172 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 173 | sysroot = sysroot_lib.Sysroot(sysroot_path) |
| 174 | |
| 175 | uri = input_proto.uri |
| 176 | # For now, we enforce that all input URIs are Google Storage buckets. |
| 177 | if not gs.PathIsGs(uri): |
| 178 | raise ValueError('Upload URI %s must be Google Storage.' % uri) |
| 179 | |
| 180 | if config.validate_only: |
| 181 | return controller.RETURN_CODE_VALID_INPUT |
| 182 | |
Mike Frysinger | 3dcacee | 2019-08-23 17:09:11 -0400 | [diff] [blame] | 183 | parsed_uri = urllib.parse.urlparse(uri) |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 184 | upload_uri = gs.GetGsURL(parsed_uri.netloc, for_gsutil=True).rstrip('/') |
| 185 | upload_path = parsed_uri.path.lstrip('/') |
| 186 | |
| 187 | # Calculate the filename for the to-be-created Packages file, which will |
| 188 | # contain only devinstall packages. |
| 189 | devinstall_package_index_path = os.path.join(input_proto.uploads_dir, |
| 190 | 'Packages') |
| 191 | upload_targets_list = binhost.ReadDevInstallFilesToCreatePackageIndex( |
| 192 | chroot, sysroot, devinstall_package_index_path, upload_uri, upload_path) |
| 193 | |
| 194 | package_dir = chroot.full_path(sysroot.path, 'packages') |
| 195 | for upload_target in upload_targets_list: |
| 196 | # Copy each package to target/category/package |
| 197 | upload_target = upload_target.strip('/') |
| 198 | category = upload_target.split(os.sep)[0] |
| 199 | target_dir = os.path.join(input_proto.uploads_dir, category) |
| 200 | if not os.path.exists(target_dir): |
| 201 | os.makedirs(target_dir) |
| 202 | full_src_pkg_path = os.path.join(package_dir, upload_target) |
| 203 | full_target_src_path = os.path.join(input_proto.uploads_dir, upload_target) |
| 204 | shutil.copyfile(full_src_pkg_path, full_target_src_path) |
| 205 | output_proto.upload_targets.add().path = upload_target |
| 206 | output_proto.upload_targets.add().path = 'Packages' |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 207 | |
Alex Klein | 076841b | 2019-08-29 15:19:39 -0600 | [diff] [blame] | 208 | |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 209 | def _SetBinhostResponse(_input_proto, output_proto, _config): |
| 210 | """Add fake binhost file to a successful response.""" |
| 211 | output_proto.output_file = '/path/to/BINHOST.conf' |
| 212 | |
| 213 | |
| 214 | @faux.success(_SetBinhostResponse) |
| 215 | @faux.empty_error |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 216 | @validate.require('build_target.name', 'key', 'uri') |
| 217 | @validate.validation_complete |
| 218 | def SetBinhost(input_proto, output_proto, _config): |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 219 | """Set the URI for a given binhost key and build target. |
| 220 | |
| 221 | See BinhostService documentation in api/proto/binhost.proto. |
| 222 | |
| 223 | Args: |
| 224 | input_proto (SetBinhostRequest): The input proto. |
| 225 | output_proto (SetBinhostResponse): The output proto. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 226 | _config (api_config.ApiConfig): The API call config. |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 227 | """ |
| 228 | target = input_proto.build_target.name |
| 229 | key = binhost_pb2.BinhostKey.Name(input_proto.key) |
| 230 | uri = input_proto.uri |
| 231 | private = input_proto.private |
Alex Klein | 6fb0eb8 | 2019-05-20 16:16:14 -0600 | [diff] [blame] | 232 | |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 233 | output_proto.output_file = binhost.SetBinhost(target, key, uri, |
| 234 | private=private) |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 235 | |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 236 | |
Michael Mortensen | 42251f9 | 2019-11-14 11:01:43 -0700 | [diff] [blame] | 237 | def _RegenBuildCacheResponse(_input_proto, output_proto, _config): |
| 238 | """Add fake binhosts cache path to a successful response.""" |
| 239 | output_proto.modified_overlays.add().path = '/path/to/BuildCache' |
| 240 | |
| 241 | |
| 242 | @faux.success(_RegenBuildCacheResponse) |
| 243 | @faux.empty_error |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 244 | @validate.require('overlay_type') |
| 245 | @validate.is_in('overlay_type', _OVERLAY_TYPE_TO_NAME) |
| 246 | @validate.validation_complete |
Alex Klein | 82c85d4 | 2019-08-14 15:47:51 -0600 | [diff] [blame] | 247 | def RegenBuildCache(input_proto, output_proto, _config): |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 248 | """Regenerate the Build Cache for a build target. |
| 249 | |
| 250 | See BinhostService documentation in api/proto/binhost.proto. |
| 251 | |
| 252 | Args: |
| 253 | input_proto (RegenBuildCacheRequest): The input proto. |
Alex Klein | 82c85d4 | 2019-08-14 15:47:51 -0600 | [diff] [blame] | 254 | output_proto (RegenBuildCacheResponse): The output proto. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 255 | _config (api_config.ApiConfig): The API call config. |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 256 | """ |
Alex Klein | 82c85d4 | 2019-08-14 15:47:51 -0600 | [diff] [blame] | 257 | chroot = controller_util.ParseChroot(input_proto.chroot) |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 258 | overlay_type = input_proto.overlay_type |
Alex Klein | 82c85d4 | 2019-08-14 15:47:51 -0600 | [diff] [blame] | 259 | overlays = binhost.RegenBuildCache(chroot, |
| 260 | _OVERLAY_TYPE_TO_NAME[overlay_type]) |
| 261 | |
| 262 | for overlay in overlays: |
| 263 | output_proto.modified_overlays.add().path = overlay |