blob: d434379a62781736aff26e3f9fe57abeaeb81a4c [file] [log] [blame]
Evan Hernandezd437b4e2019-03-25 13:48:30 -06001# -*- 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
8from __future__ import print_function
9
10import urlparse
11
12from chromite.api.gen.chromite.api import binhost_pb2
Alex Kleina471f682019-05-31 11:23:45 -060013from chromite.lib import build_target_util
LaMont Jonesc64ae212019-04-15 15:41:28 -060014from chromite.lib import constants
15from chromite.lib import cros_build_lib
Evan Hernandezd437b4e2019-03-25 13:48:30 -060016from chromite.lib import gs
17from chromite.service import binhost
18
19
Alex Kleina471f682019-05-31 11:23:45 -060020def GetPrivatePrebuiltAclArgs(input_proto, output_proto):
21 """Get the ACL args from the files in the private overlays."""
22 build_target_name = input_proto.build_target.name
23 if not build_target_name:
24 cros_build_lib.Die('Build target name is required.')
25
26 build_target = build_target_util.BuildTarget(build_target_name)
27
28 try:
29 args = binhost.GetPrebuiltAclArgs(build_target)
30 except binhost.Error as e:
31 cros_build_lib.Die(e.message)
32
33 for arg, value in args:
34 new_arg = output_proto.args.add()
35 new_arg.arg = arg
36 new_arg.value = value
37
38
Evan Hernandezd437b4e2019-03-25 13:48:30 -060039def PrepareBinhostUploads(input_proto, output_proto):
40 """Return a list of files to uplooad to the binhost.
41
42 See BinhostService documentation in api/proto/binhost.proto.
43
44 Args:
45 input_proto (PrepareBinhostUploadsRequest): The input proto.
46 output_proto (PrepareBinhostUploadsResponse): The output proto.
47 """
48 target = input_proto.build_target.name
49 uri = input_proto.uri
50
51 # For now, we enforce that all input URIs are Google Storage buckets.
52 if not gs.PathIsGs(uri):
53 raise ValueError('Upload URI %s must be Google Storage.' % uri)
54
55 parsed_uri = urlparse.urlparse(uri)
56 upload_uri = gs.GetGsURL(parsed_uri.netloc)
Alex Klein3e728442019-05-15 11:46:57 -060057 upload_path = parsed_uri.path.lstrip('/')
Evan Hernandezd437b4e2019-03-25 13:48:30 -060058
59 # Read all packages and update the index. The index must be uploaded to the
60 # binhost for Portage to use it, so include it in upload_targets.
61 uploads_dir = binhost.GetPrebuiltsRoot(target)
62 upload_targets = binhost.GetPrebuiltsFiles(uploads_dir)
Alex Klein3e728442019-05-15 11:46:57 -060063 index_path = binhost.UpdatePackageIndex(uploads_dir, upload_uri, upload_path,
64 sudo=True)
Evan Hernandezd437b4e2019-03-25 13:48:30 -060065 assert index_path.startswith(uploads_dir), (
66 'expected index_path to start with uploads_dir')
67 upload_targets.append(index_path[len(uploads_dir):])
68
69 output_proto.uploads_dir = uploads_dir
70 for upload_target in upload_targets:
71 output_proto.upload_targets.add().path = upload_target.strip('/')
72
73
74def SetBinhost(input_proto, output_proto):
75 """Set the URI for a given binhost key and build target.
76
77 See BinhostService documentation in api/proto/binhost.proto.
78
79 Args:
80 input_proto (SetBinhostRequest): The input proto.
81 output_proto (SetBinhostResponse): The output proto.
82 """
83 target = input_proto.build_target.name
84 key = binhost_pb2.BinhostKey.Name(input_proto.key)
85 uri = input_proto.uri
86 private = input_proto.private
Alex Klein6fb0eb82019-05-20 16:16:14 -060087
88 # Temporary measure to force the new parallel cq post submit builders to write
89 # to a different file than the old ones. Writing to the same file was causing
90 # them to fight over the new one's value and the old logic of clearing out
91 # the values for files it didn't update. Once we've done a full switch over,
92 # we can dump this logic and delete all of the PARALLEL_POSTSUBMIT_BINHOST
93 # configs.
94 # TODO(crbug.com/965244) remove this.
95 if key == 'POSTSUBMIT_BINHOST':
96 key = 'PARALLEL_POSTSUBMIT_BINHOST'
97
Evan Hernandezd437b4e2019-03-25 13:48:30 -060098 output_proto.output_file = binhost.SetBinhost(target, key, uri,
99 private=private)
LaMont Jonesc64ae212019-04-15 15:41:28 -0600100
Evan Hernandez763f58b2019-05-15 10:57:49 -0600101_OVERLAY_TYPE_TO_NAME = {
LaMont Jonesc64ae212019-04-15 15:41:28 -0600102 binhost_pb2.OVERLAYTYPE_PUBLIC: constants.PUBLIC_OVERLAYS,
103 binhost_pb2.OVERLAYTYPE_PRIVATE: constants.PRIVATE_OVERLAYS,
104 binhost_pb2.OVERLAYTYPE_BOTH: constants.BOTH_OVERLAYS,
105 binhost_pb2.OVERLAYTYPE_NONE: None
106}
107
108def RegenBuildCache(input_proto):
109 """Regenerate the Build Cache for a build target.
110
111 See BinhostService documentation in api/proto/binhost.proto.
112
113 Args:
114 input_proto (RegenBuildCacheRequest): The input proto.
115 output_proto (RegenBuildCacheResponse): The output proto.
116 """
117 overlay_type = input_proto.overlay_type
118 sysroot = input_proto.sysroot
Evan Hernandez763f58b2019-05-15 10:57:49 -0600119 if overlay_type in _OVERLAY_TYPE_TO_NAME:
120 binhost.RegenBuildCache(_OVERLAY_TYPE_TO_NAME[overlay_type], sysroot.path)
LaMont Jonesc64ae212019-04-15 15:41:28 -0600121 else:
122 cros_build_lib.Die('Overlay_type must be specified.')