blob: 729026eb032d2f66930346e54ee87ba7e458503a [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
Alex Kleinaf0e0452019-06-03 18:10:01 -060012from chromite.api.controller import controller_util
Evan Hernandezd437b4e2019-03-25 13:48:30 -060013from chromite.api.gen.chromite.api import binhost_pb2
Alex Kleina471f682019-05-31 11:23:45 -060014from chromite.lib import build_target_util
LaMont Jonesc64ae212019-04-15 15:41:28 -060015from chromite.lib import constants
16from chromite.lib import cros_build_lib
Evan Hernandezd437b4e2019-03-25 13:48:30 -060017from chromite.lib import gs
Alex Kleinaf0e0452019-06-03 18:10:01 -060018from chromite.lib import sysroot_lib
Evan Hernandezd437b4e2019-03-25 13:48:30 -060019from chromite.service import binhost
20
Alex Kleinaf0e0452019-06-03 18:10:01 -060021_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 Hernandezd437b4e2019-03-25 13:48:30 -060028
Alex Klein7e40d252019-06-10 09:01:32 -060029def 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
42 new_binhost.package_index = binhost.GetPackageIndex(current)
43
44
Alex Kleina471f682019-05-31 11:23:45 -060045def 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 Hernandezd437b4e2019-03-25 13:48:30 -060064def 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 Kleinaf0e0452019-06-03 18:10:01 -060073 target_name = (input_proto.sysroot.build_target.name
74 or input_proto.build_target.name)
75 sysroot_path = input_proto.sysroot.path
Evan Hernandezd437b4e2019-03-25 13:48:30 -060076
Alex Kleinaf0e0452019-06-03 18:10:01 -060077 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 Hernandezd437b4e2019-03-25 13:48:30 -060089 # 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 Klein7e40d252019-06-10 09:01:32 -060093 package_index_paths = [f.path for f in input_proto.package_index_files]
94
Evan Hernandezd437b4e2019-03-25 13:48:30 -060095 parsed_uri = urlparse.urlparse(uri)
96 upload_uri = gs.GetGsURL(parsed_uri.netloc)
Alex Klein3e728442019-05-15 11:46:57 -060097 upload_path = parsed_uri.path.lstrip('/')
Evan Hernandezd437b4e2019-03-25 13:48:30 -060098
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 Kleinaf0e0452019-06-03 18:10:01 -0600101 uploads_dir = binhost.GetPrebuiltsRoot(chroot, sysroot, build_target)
Alex Klein7e40d252019-06-10 09:01:32 -0600102 upload_targets = binhost.GetPrebuiltsFiles(uploads_dir, package_index_paths)
Alex Klein3e728442019-05-15 11:46:57 -0600103 index_path = binhost.UpdatePackageIndex(uploads_dir, upload_uri, upload_path,
104 sudo=True)
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600105 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
114def 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 Klein6fb0eb82019-05-20 16:16:14 -0600127
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 Hernandezd437b4e2019-03-25 13:48:30 -0600138 output_proto.output_file = binhost.SetBinhost(target, key, uri,
139 private=private)
LaMont Jonesc64ae212019-04-15 15:41:28 -0600140
LaMont Jonesc64ae212019-04-15 15:41:28 -0600141
Alex Kleinaf0e0452019-06-03 18:10:01 -0600142def RegenBuildCache(input_proto, _output_proto):
LaMont Jonesc64ae212019-04-15 15:41:28 -0600143 """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 Kleinaf0e0452019-06-03 18:10:01 -0600149 _output_proto (RegenBuildCacheResponse): The output proto.
LaMont Jonesc64ae212019-04-15 15:41:28 -0600150 """
151 overlay_type = input_proto.overlay_type
Evan Hernandez763f58b2019-05-15 10:57:49 -0600152 if overlay_type in _OVERLAY_TYPE_TO_NAME:
Alex Kleinaf0e0452019-06-03 18:10:01 -0600153 binhost.RegenBuildCache(_OVERLAY_TYPE_TO_NAME[overlay_type])
LaMont Jonesc64ae212019-04-15 15:41:28 -0600154 else:
155 cros_build_lib.Die('Overlay_type must be specified.')