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 | |
| 10 | import urlparse |
| 11 | |
Alex Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 12 | from chromite.api.controller import controller_util |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 13 | from chromite.api.gen.chromite.api import binhost_pb2 |
Alex Klein | a471f68 | 2019-05-31 11:23:45 -0600 | [diff] [blame] | 14 | from chromite.lib import build_target_util |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 15 | from chromite.lib import constants |
| 16 | from chromite.lib import cros_build_lib |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 17 | from chromite.lib import gs |
Alex Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 18 | from chromite.lib import sysroot_lib |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 19 | from chromite.service import binhost |
| 20 | |
Alex Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 21 | _OVERLAY_TYPE_TO_NAME = { |
| 22 | binhost_pb2.OVERLAYTYPE_PUBLIC: constants.PUBLIC_OVERLAYS, |
| 23 | binhost_pb2.OVERLAYTYPE_PRIVATE: constants.PRIVATE_OVERLAYS, |
| 24 | binhost_pb2.OVERLAYTYPE_BOTH: constants.BOTH_OVERLAYS, |
| 25 | binhost_pb2.OVERLAYTYPE_NONE: None |
| 26 | } |
| 27 | |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 28 | |
Alex Klein | 7e40d25 | 2019-06-10 09:01:32 -0600 | [diff] [blame] | 29 | def GetBinhosts(input_proto, output_proto): |
| 30 | """Get a list of binhosts.""" |
| 31 | target_name = input_proto.build_target.name |
| 32 | if not target_name: |
| 33 | cros_build_lib.Die('BuildTarget.name is required.') |
| 34 | |
| 35 | build_target = build_target_util.BuildTarget(target_name) |
| 36 | |
| 37 | binhosts = binhost.GetBinhosts(build_target) |
| 38 | |
| 39 | for current in binhosts: |
| 40 | new_binhost = output_proto.binhosts.add() |
| 41 | new_binhost.uri = current |
Alex Klein | d3ac0bd | 2019-06-10 15:17:51 -0600 | [diff] [blame^] | 42 | new_binhost.package_index = 'Packages' |
Alex Klein | 7e40d25 | 2019-06-10 09:01:32 -0600 | [diff] [blame] | 43 | |
| 44 | |
Alex Klein | a471f68 | 2019-05-31 11:23:45 -0600 | [diff] [blame] | 45 | def GetPrivatePrebuiltAclArgs(input_proto, output_proto): |
| 46 | """Get the ACL args from the files in the private overlays.""" |
| 47 | build_target_name = input_proto.build_target.name |
| 48 | if not build_target_name: |
| 49 | cros_build_lib.Die('Build target name is required.') |
| 50 | |
| 51 | build_target = build_target_util.BuildTarget(build_target_name) |
| 52 | |
| 53 | try: |
| 54 | args = binhost.GetPrebuiltAclArgs(build_target) |
| 55 | except binhost.Error as e: |
| 56 | cros_build_lib.Die(e.message) |
| 57 | |
| 58 | for arg, value in args: |
| 59 | new_arg = output_proto.args.add() |
| 60 | new_arg.arg = arg |
| 61 | new_arg.value = value |
| 62 | |
| 63 | |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 64 | def PrepareBinhostUploads(input_proto, output_proto): |
| 65 | """Return a list of files to uplooad to the binhost. |
| 66 | |
| 67 | See BinhostService documentation in api/proto/binhost.proto. |
| 68 | |
| 69 | Args: |
| 70 | input_proto (PrepareBinhostUploadsRequest): The input proto. |
| 71 | output_proto (PrepareBinhostUploadsResponse): The output proto. |
| 72 | """ |
Alex Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 73 | target_name = (input_proto.sysroot.build_target.name |
| 74 | or input_proto.build_target.name) |
| 75 | sysroot_path = input_proto.sysroot.path |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 76 | |
Alex Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 77 | if not sysroot_path and not target_name: |
| 78 | cros_build_lib.Die('Sysroot.path is required.') |
| 79 | |
| 80 | build_target = build_target_util.BuildTarget(target_name) |
| 81 | chroot = controller_util.ParseChroot(input_proto.chroot) |
| 82 | |
| 83 | if not sysroot_path: |
| 84 | # Very temporary, so not worried about this not calling the lib function. |
| 85 | sysroot_path = build_target.root |
| 86 | sysroot = sysroot_lib.Sysroot(sysroot_path) |
| 87 | |
| 88 | uri = input_proto.uri |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 89 | # For now, we enforce that all input URIs are Google Storage buckets. |
| 90 | if not gs.PathIsGs(uri): |
| 91 | raise ValueError('Upload URI %s must be Google Storage.' % uri) |
| 92 | |
Alex Klein | 7e40d25 | 2019-06-10 09:01:32 -0600 | [diff] [blame] | 93 | package_index_paths = [f.path for f in input_proto.package_index_files] |
| 94 | |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 95 | parsed_uri = urlparse.urlparse(uri) |
| 96 | upload_uri = gs.GetGsURL(parsed_uri.netloc) |
Alex Klein | 3e72844 | 2019-05-15 11:46:57 -0600 | [diff] [blame] | 97 | upload_path = parsed_uri.path.lstrip('/') |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 98 | |
| 99 | # Read all packages and update the index. The index must be uploaded to the |
| 100 | # binhost for Portage to use it, so include it in upload_targets. |
Alex Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 101 | uploads_dir = binhost.GetPrebuiltsRoot(chroot, sysroot, build_target) |
Alex Klein | 7e40d25 | 2019-06-10 09:01:32 -0600 | [diff] [blame] | 102 | upload_targets = binhost.GetPrebuiltsFiles(uploads_dir, package_index_paths) |
Alex Klein | 3e72844 | 2019-05-15 11:46:57 -0600 | [diff] [blame] | 103 | index_path = binhost.UpdatePackageIndex(uploads_dir, upload_uri, upload_path, |
| 104 | sudo=True) |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 105 | assert index_path.startswith(uploads_dir), ( |
| 106 | 'expected index_path to start with uploads_dir') |
| 107 | upload_targets.append(index_path[len(uploads_dir):]) |
| 108 | |
| 109 | output_proto.uploads_dir = uploads_dir |
| 110 | for upload_target in upload_targets: |
| 111 | output_proto.upload_targets.add().path = upload_target.strip('/') |
| 112 | |
| 113 | |
| 114 | def SetBinhost(input_proto, output_proto): |
| 115 | """Set the URI for a given binhost key and build target. |
| 116 | |
| 117 | See BinhostService documentation in api/proto/binhost.proto. |
| 118 | |
| 119 | Args: |
| 120 | input_proto (SetBinhostRequest): The input proto. |
| 121 | output_proto (SetBinhostResponse): The output proto. |
| 122 | """ |
| 123 | target = input_proto.build_target.name |
| 124 | key = binhost_pb2.BinhostKey.Name(input_proto.key) |
| 125 | uri = input_proto.uri |
| 126 | private = input_proto.private |
Alex Klein | 6fb0eb8 | 2019-05-20 16:16:14 -0600 | [diff] [blame] | 127 | |
| 128 | # Temporary measure to force the new parallel cq post submit builders to write |
| 129 | # to a different file than the old ones. Writing to the same file was causing |
| 130 | # them to fight over the new one's value and the old logic of clearing out |
| 131 | # the values for files it didn't update. Once we've done a full switch over, |
| 132 | # we can dump this logic and delete all of the PARALLEL_POSTSUBMIT_BINHOST |
| 133 | # configs. |
| 134 | # TODO(crbug.com/965244) remove this. |
| 135 | if key == 'POSTSUBMIT_BINHOST': |
| 136 | key = 'PARALLEL_POSTSUBMIT_BINHOST' |
| 137 | |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 138 | output_proto.output_file = binhost.SetBinhost(target, key, uri, |
| 139 | private=private) |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 140 | |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 141 | |
Alex Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 142 | def RegenBuildCache(input_proto, _output_proto): |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 143 | """Regenerate the Build Cache for a build target. |
| 144 | |
| 145 | See BinhostService documentation in api/proto/binhost.proto. |
| 146 | |
| 147 | Args: |
| 148 | input_proto (RegenBuildCacheRequest): The input proto. |
Alex Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 149 | _output_proto (RegenBuildCacheResponse): The output proto. |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 150 | """ |
| 151 | overlay_type = input_proto.overlay_type |
Evan Hernandez | 763f58b | 2019-05-15 10:57:49 -0600 | [diff] [blame] | 152 | if overlay_type in _OVERLAY_TYPE_TO_NAME: |
Alex Klein | af0e045 | 2019-06-03 18:10:01 -0600 | [diff] [blame] | 153 | binhost.RegenBuildCache(_OVERLAY_TYPE_TO_NAME[overlay_type]) |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 154 | else: |
| 155 | cros_build_lib.Die('Overlay_type must be specified.') |