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 | |
| 12 | from chromite.api.gen.chromite.api import binhost_pb2 |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 13 | from chromite.lib import constants |
| 14 | from chromite.lib import cros_build_lib |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 15 | from chromite.lib import gs |
| 16 | from chromite.service import binhost |
| 17 | |
| 18 | |
| 19 | def PrepareBinhostUploads(input_proto, output_proto): |
| 20 | """Return a list of files to uplooad to the binhost. |
| 21 | |
| 22 | See BinhostService documentation in api/proto/binhost.proto. |
| 23 | |
| 24 | Args: |
| 25 | input_proto (PrepareBinhostUploadsRequest): The input proto. |
| 26 | output_proto (PrepareBinhostUploadsResponse): The output proto. |
| 27 | """ |
| 28 | target = input_proto.build_target.name |
| 29 | uri = input_proto.uri |
| 30 | |
| 31 | # For now, we enforce that all input URIs are Google Storage buckets. |
| 32 | if not gs.PathIsGs(uri): |
| 33 | raise ValueError('Upload URI %s must be Google Storage.' % uri) |
| 34 | |
| 35 | parsed_uri = urlparse.urlparse(uri) |
| 36 | upload_uri = gs.GetGsURL(parsed_uri.netloc) |
Alex Klein | 3e72844 | 2019-05-15 11:46:57 -0600 | [diff] [blame] | 37 | upload_path = parsed_uri.path.lstrip('/') |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 38 | |
| 39 | # Read all packages and update the index. The index must be uploaded to the |
| 40 | # binhost for Portage to use it, so include it in upload_targets. |
| 41 | uploads_dir = binhost.GetPrebuiltsRoot(target) |
| 42 | upload_targets = binhost.GetPrebuiltsFiles(uploads_dir) |
Alex Klein | 3e72844 | 2019-05-15 11:46:57 -0600 | [diff] [blame] | 43 | index_path = binhost.UpdatePackageIndex(uploads_dir, upload_uri, upload_path, |
| 44 | sudo=True) |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 45 | assert index_path.startswith(uploads_dir), ( |
| 46 | 'expected index_path to start with uploads_dir') |
| 47 | upload_targets.append(index_path[len(uploads_dir):]) |
| 48 | |
| 49 | output_proto.uploads_dir = uploads_dir |
| 50 | for upload_target in upload_targets: |
| 51 | output_proto.upload_targets.add().path = upload_target.strip('/') |
| 52 | |
| 53 | |
| 54 | def SetBinhost(input_proto, output_proto): |
| 55 | """Set the URI for a given binhost key and build target. |
| 56 | |
| 57 | See BinhostService documentation in api/proto/binhost.proto. |
| 58 | |
| 59 | Args: |
| 60 | input_proto (SetBinhostRequest): The input proto. |
| 61 | output_proto (SetBinhostResponse): The output proto. |
| 62 | """ |
| 63 | target = input_proto.build_target.name |
| 64 | key = binhost_pb2.BinhostKey.Name(input_proto.key) |
| 65 | uri = input_proto.uri |
| 66 | private = input_proto.private |
Alex Klein | 6fb0eb8 | 2019-05-20 16:16:14 -0600 | [diff] [blame] | 67 | |
| 68 | # Temporary measure to force the new parallel cq post submit builders to write |
| 69 | # to a different file than the old ones. Writing to the same file was causing |
| 70 | # them to fight over the new one's value and the old logic of clearing out |
| 71 | # the values for files it didn't update. Once we've done a full switch over, |
| 72 | # we can dump this logic and delete all of the PARALLEL_POSTSUBMIT_BINHOST |
| 73 | # configs. |
| 74 | # TODO(crbug.com/965244) remove this. |
| 75 | if key == 'POSTSUBMIT_BINHOST': |
| 76 | key = 'PARALLEL_POSTSUBMIT_BINHOST' |
| 77 | |
Evan Hernandez | d437b4e | 2019-03-25 13:48:30 -0600 | [diff] [blame] | 78 | output_proto.output_file = binhost.SetBinhost(target, key, uri, |
| 79 | private=private) |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 80 | |
Evan Hernandez | 763f58b | 2019-05-15 10:57:49 -0600 | [diff] [blame] | 81 | _OVERLAY_TYPE_TO_NAME = { |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 82 | binhost_pb2.OVERLAYTYPE_PUBLIC: constants.PUBLIC_OVERLAYS, |
| 83 | binhost_pb2.OVERLAYTYPE_PRIVATE: constants.PRIVATE_OVERLAYS, |
| 84 | binhost_pb2.OVERLAYTYPE_BOTH: constants.BOTH_OVERLAYS, |
| 85 | binhost_pb2.OVERLAYTYPE_NONE: None |
| 86 | } |
| 87 | |
| 88 | def RegenBuildCache(input_proto): |
| 89 | """Regenerate the Build Cache for a build target. |
| 90 | |
| 91 | See BinhostService documentation in api/proto/binhost.proto. |
| 92 | |
| 93 | Args: |
| 94 | input_proto (RegenBuildCacheRequest): The input proto. |
| 95 | output_proto (RegenBuildCacheResponse): The output proto. |
| 96 | """ |
| 97 | overlay_type = input_proto.overlay_type |
| 98 | sysroot = input_proto.sysroot |
Evan Hernandez | 763f58b | 2019-05-15 10:57:49 -0600 | [diff] [blame] | 99 | if overlay_type in _OVERLAY_TYPE_TO_NAME: |
| 100 | binhost.RegenBuildCache(_OVERLAY_TYPE_TO_NAME[overlay_type], sysroot.path) |
LaMont Jones | c64ae21 | 2019-04-15 15:41:28 -0600 | [diff] [blame] | 101 | else: |
| 102 | cros_build_lib.Die('Overlay_type must be specified.') |