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 | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 16 | from chromite.api import validate |
Alex Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 17 | from chromite.api.controller import controller_util |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 18 | from chromite.api.gen.chromite.api import binhost_pb2 |
Alex Klein | a471f68 | 2019-05-31 11:23:45 -0600 | [diff] [blame] | 19 | from chromite.lib import build_target_util |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 20 | from chromite.lib import constants |
| 21 | from chromite.lib import cros_build_lib |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 22 | from chromite.lib import gs |
Alex Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 23 | from chromite.lib import sysroot_lib |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 24 | from chromite.service import binhost |
| 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 | |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 34 | @validate.require('build_target.name') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 35 | @validate.validation_complete |
| 36 | def GetBinhosts(input_proto, output_proto, _config): |
Alex Klein | 7e40d25 | 2019-06-10 09:01:32 -0600 | [diff] [blame] | 37 | """Get a list of binhosts.""" |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 38 | build_target = build_target_util.BuildTarget(input_proto.build_target.name) |
Alex Klein | 7e40d25 | 2019-06-10 09:01:32 -0600 | [diff] [blame] | 39 | |
| 40 | binhosts = binhost.GetBinhosts(build_target) |
| 41 | |
| 42 | for current in binhosts: |
| 43 | new_binhost = output_proto.binhosts.add() |
| 44 | new_binhost.uri = current |
Alex Klein | d3ac0bd | 2019-06-10 15:17:51 -0600 | [diff] [blame] | 45 | new_binhost.package_index = 'Packages' |
Alex Klein | 7e40d25 | 2019-06-10 09:01:32 -0600 | [diff] [blame] | 46 | |
| 47 | |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 48 | @validate.require('build_target.name') |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 49 | @validate.validation_complete |
| 50 | def GetPrivatePrebuiltAclArgs(input_proto, output_proto, _config): |
Alex Klein | a471f68 | 2019-05-31 11:23:45 -0600 | [diff] [blame] | 51 | """Get the ACL args from the files in the private overlays.""" |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 52 | build_target = build_target_util.BuildTarget(input_proto.build_target.name) |
Alex Klein | a471f68 | 2019-05-31 11:23:45 -0600 | [diff] [blame] | 53 | |
| 54 | try: |
| 55 | args = binhost.GetPrebuiltAclArgs(build_target) |
| 56 | except binhost.Error as e: |
| 57 | cros_build_lib.Die(e.message) |
| 58 | |
| 59 | for arg, value in args: |
| 60 | new_arg = output_proto.args.add() |
| 61 | new_arg.arg = arg |
| 62 | new_arg.value = value |
| 63 | |
| 64 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 65 | @validate.require('uri') |
| 66 | def PrepareBinhostUploads(input_proto, output_proto, config): |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 67 | """Return a list of files to uplooad to the binhost. |
| 68 | |
| 69 | See BinhostService documentation in api/proto/binhost.proto. |
| 70 | |
| 71 | Args: |
| 72 | input_proto (PrepareBinhostUploadsRequest): The input proto. |
| 73 | output_proto (PrepareBinhostUploadsResponse): The output proto. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 74 | config (api_config.ApiConfig): The API call config. |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 75 | """ |
Alex Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 76 | target_name = (input_proto.sysroot.build_target.name |
| 77 | or input_proto.build_target.name) |
| 78 | sysroot_path = input_proto.sysroot.path |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 79 | |
Alex Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 80 | if not sysroot_path and not target_name: |
| 81 | cros_build_lib.Die('Sysroot.path is required.') |
| 82 | |
| 83 | build_target = build_target_util.BuildTarget(target_name) |
| 84 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 85 | |
| 86 | if not sysroot_path: |
| 87 | # Very temporary, so not worried about this not calling the lib function. |
| 88 | sysroot_path = build_target.root |
| 89 | sysroot = sysroot_lib.Sysroot(sysroot_path) |
| 90 | |
| 91 | uri = input_proto.uri |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 92 | # For now, we enforce that all input URIs are Google Storage buckets. |
| 93 | if not gs.PathIsGs(uri): |
| 94 | raise ValueError('Upload URI %s must be Google Storage.' % uri) |
| 95 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 96 | if config.validate_only: |
| 97 | return controller.RETURN_CODE_VALID_INPUT |
| 98 | |
Mike Frysinger | 3dcacee | 2019-08-23 17:09:11 -0400 | [diff] [blame] | 99 | parsed_uri = urllib.parse.urlparse(uri) |
Alex Klein | 8dffea4 | 2019-06-14 14:01:36 -0600 | [diff] [blame] | 100 | upload_uri = gs.GetGsURL(parsed_uri.netloc, for_gsutil=True).rstrip('/') |
Alex Klein | 3e72844 | 2019-05-15 11:46:57 -0600 | [diff] [blame] | 101 | upload_path = parsed_uri.path.lstrip('/') |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 102 | |
| 103 | # Read all packages and update the index. The index must be uploaded to the |
| 104 | # binhost for Portage to use it, so include it in upload_targets. |
Alex Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 105 | uploads_dir = binhost.GetPrebuiltsRoot(chroot, sysroot, build_target) |
Alex Klein | 3e72844 | 2019-05-15 11:46:57 -0600 | [diff] [blame] | 106 | index_path = binhost.UpdatePackageIndex(uploads_dir, upload_uri, upload_path, |
| 107 | sudo=True) |
Alex Klein | b5d57a6 | 2019-06-20 12:04:53 -0600 | [diff] [blame] | 108 | upload_targets = binhost.GetPrebuiltsFiles(uploads_dir) |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 109 | assert index_path.startswith(uploads_dir), ( |
| 110 | 'expected index_path to start with uploads_dir') |
| 111 | upload_targets.append(index_path[len(uploads_dir):]) |
| 112 | |
| 113 | output_proto.uploads_dir = uploads_dir |
| 114 | for upload_target in upload_targets: |
| 115 | output_proto.upload_targets.add().path = upload_target.strip('/') |
| 116 | |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 117 | @validate.require('uri', 'sysroot.path') |
| 118 | @validate.exists('uploads_dir') |
| 119 | def PrepareDevInstallBinhostUploads(input_proto, output_proto, config): |
| 120 | """Return a list of files to upload to the binhost" |
| 121 | |
| 122 | The files will also be copied to the uploads_dir. |
| 123 | See BinhostService documentation in api/proto/binhost.proto. |
| 124 | |
| 125 | Args: |
| 126 | input_proto (PrepareDevInstallBinhostUploadsRequest): The input proto. |
| 127 | output_proto (PrepareDevInstallBinhostUploadsResponse): The output proto. |
| 128 | config (api_config.ApiConfig): The API call config. |
| 129 | """ |
| 130 | sysroot_path = input_proto.sysroot.path |
| 131 | |
| 132 | # build_target = build_target_util.BuildTarget(target_name) |
| 133 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 134 | sysroot = sysroot_lib.Sysroot(sysroot_path) |
| 135 | |
| 136 | uri = input_proto.uri |
| 137 | # For now, we enforce that all input URIs are Google Storage buckets. |
| 138 | if not gs.PathIsGs(uri): |
| 139 | raise ValueError('Upload URI %s must be Google Storage.' % uri) |
| 140 | |
| 141 | if config.validate_only: |
| 142 | return controller.RETURN_CODE_VALID_INPUT |
| 143 | |
Mike Frysinger | 3dcacee | 2019-08-23 17:09:11 -0400 | [diff] [blame] | 144 | parsed_uri = urllib.parse.urlparse(uri) |
Michael Mortensen | fc82388 | 2019-08-27 14:38:07 -0600 | [diff] [blame] | 145 | upload_uri = gs.GetGsURL(parsed_uri.netloc, for_gsutil=True).rstrip('/') |
| 146 | upload_path = parsed_uri.path.lstrip('/') |
| 147 | |
| 148 | # Calculate the filename for the to-be-created Packages file, which will |
| 149 | # contain only devinstall packages. |
| 150 | devinstall_package_index_path = os.path.join(input_proto.uploads_dir, |
| 151 | 'Packages') |
| 152 | upload_targets_list = binhost.ReadDevInstallFilesToCreatePackageIndex( |
| 153 | chroot, sysroot, devinstall_package_index_path, upload_uri, upload_path) |
| 154 | |
| 155 | package_dir = chroot.full_path(sysroot.path, 'packages') |
| 156 | for upload_target in upload_targets_list: |
| 157 | # Copy each package to target/category/package |
| 158 | upload_target = upload_target.strip('/') |
| 159 | category = upload_target.split(os.sep)[0] |
| 160 | target_dir = os.path.join(input_proto.uploads_dir, category) |
| 161 | if not os.path.exists(target_dir): |
| 162 | os.makedirs(target_dir) |
| 163 | full_src_pkg_path = os.path.join(package_dir, upload_target) |
| 164 | full_target_src_path = os.path.join(input_proto.uploads_dir, upload_target) |
| 165 | shutil.copyfile(full_src_pkg_path, full_target_src_path) |
| 166 | output_proto.upload_targets.add().path = upload_target |
| 167 | output_proto.upload_targets.add().path = 'Packages' |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 168 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 169 | @validate.require('build_target.name', 'key', 'uri') |
| 170 | @validate.validation_complete |
| 171 | def SetBinhost(input_proto, output_proto, _config): |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 172 | """Set the URI for a given binhost key and build target. |
| 173 | |
| 174 | See BinhostService documentation in api/proto/binhost.proto. |
| 175 | |
| 176 | Args: |
| 177 | input_proto (SetBinhostRequest): The input proto. |
| 178 | output_proto (SetBinhostResponse): The output proto. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 179 | _config (api_config.ApiConfig): The API call config. |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 180 | """ |
| 181 | target = input_proto.build_target.name |
| 182 | key = binhost_pb2.BinhostKey.Name(input_proto.key) |
| 183 | uri = input_proto.uri |
| 184 | private = input_proto.private |
Alex Klein | 6fb0eb8 | 2019-05-20 16:16:14 -0600 | [diff] [blame] | 185 | |
| 186 | # Temporary measure to force the new parallel cq post submit builders to write |
| 187 | # to a different file than the old ones. Writing to the same file was causing |
| 188 | # them to fight over the new one's value and the old logic of clearing out |
| 189 | # the values for files it didn't update. Once we've done a full switch over, |
| 190 | # we can dump this logic and delete all of the PARALLEL_POSTSUBMIT_BINHOST |
| 191 | # configs. |
| 192 | # TODO(crbug.com/965244) remove this. |
| 193 | if key == 'POSTSUBMIT_BINHOST': |
| 194 | key = 'PARALLEL_POSTSUBMIT_BINHOST' |
| 195 | |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 196 | output_proto.output_file = binhost.SetBinhost(target, key, uri, |
| 197 | private=private) |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 198 | |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 199 | |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 200 | @validate.require('overlay_type') |
| 201 | @validate.is_in('overlay_type', _OVERLAY_TYPE_TO_NAME) |
| 202 | @validate.validation_complete |
Alex Klein | 82c85d4 | 2019-08-14 15:47:51 -0600 | [diff] [blame] | 203 | def RegenBuildCache(input_proto, output_proto, _config): |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 204 | """Regenerate the Build Cache for a build target. |
| 205 | |
| 206 | See BinhostService documentation in api/proto/binhost.proto. |
| 207 | |
| 208 | Args: |
| 209 | input_proto (RegenBuildCacheRequest): The input proto. |
Alex Klein | 82c85d4 | 2019-08-14 15:47:51 -0600 | [diff] [blame] | 210 | output_proto (RegenBuildCacheResponse): The output proto. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 211 | _config (api_config.ApiConfig): The API call config. |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 212 | """ |
Alex Klein | 82c85d4 | 2019-08-14 15:47:51 -0600 | [diff] [blame] | 213 | chroot = controller_util.ParseChroot(input_proto.chroot) |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 214 | overlay_type = input_proto.overlay_type |
Alex Klein | 82c85d4 | 2019-08-14 15:47:51 -0600 | [diff] [blame] | 215 | overlays = binhost.RegenBuildCache(chroot, |
| 216 | _OVERLAY_TYPE_TO_NAME[overlay_type]) |
| 217 | |
| 218 | for overlay in overlays: |
| 219 | output_proto.modified_overlays.add().path = overlay |