blob: 056945902e05e4cab6dcef42df847e788b60613c [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 Kleina471f682019-05-31 11:23:45 -060029def GetPrivatePrebuiltAclArgs(input_proto, output_proto):
30 """Get the ACL args from the files in the private overlays."""
31 build_target_name = input_proto.build_target.name
32 if not build_target_name:
33 cros_build_lib.Die('Build target name is required.')
34
35 build_target = build_target_util.BuildTarget(build_target_name)
36
37 try:
38 args = binhost.GetPrebuiltAclArgs(build_target)
39 except binhost.Error as e:
40 cros_build_lib.Die(e.message)
41
42 for arg, value in args:
43 new_arg = output_proto.args.add()
44 new_arg.arg = arg
45 new_arg.value = value
46
47
Evan Hernandezd437b4e2019-03-25 13:48:30 -060048def PrepareBinhostUploads(input_proto, output_proto):
49 """Return a list of files to uplooad to the binhost.
50
51 See BinhostService documentation in api/proto/binhost.proto.
52
53 Args:
54 input_proto (PrepareBinhostUploadsRequest): The input proto.
55 output_proto (PrepareBinhostUploadsResponse): The output proto.
56 """
Alex Kleinaf0e0452019-06-03 18:10:01 -060057 target_name = (input_proto.sysroot.build_target.name
58 or input_proto.build_target.name)
59 sysroot_path = input_proto.sysroot.path
Evan Hernandezd437b4e2019-03-25 13:48:30 -060060
Alex Kleinaf0e0452019-06-03 18:10:01 -060061 if not sysroot_path and not target_name:
62 cros_build_lib.Die('Sysroot.path is required.')
63
64 build_target = build_target_util.BuildTarget(target_name)
65 chroot = controller_util.ParseChroot(input_proto.chroot)
66
67 if not sysroot_path:
68 # Very temporary, so not worried about this not calling the lib function.
69 sysroot_path = build_target.root
70 sysroot = sysroot_lib.Sysroot(sysroot_path)
71
72 uri = input_proto.uri
Evan Hernandezd437b4e2019-03-25 13:48:30 -060073 # For now, we enforce that all input URIs are Google Storage buckets.
74 if not gs.PathIsGs(uri):
75 raise ValueError('Upload URI %s must be Google Storage.' % uri)
76
77 parsed_uri = urlparse.urlparse(uri)
78 upload_uri = gs.GetGsURL(parsed_uri.netloc)
Alex Klein3e728442019-05-15 11:46:57 -060079 upload_path = parsed_uri.path.lstrip('/')
Evan Hernandezd437b4e2019-03-25 13:48:30 -060080
81 # Read all packages and update the index. The index must be uploaded to the
82 # binhost for Portage to use it, so include it in upload_targets.
Alex Kleinaf0e0452019-06-03 18:10:01 -060083 uploads_dir = binhost.GetPrebuiltsRoot(chroot, sysroot, build_target)
Evan Hernandezd437b4e2019-03-25 13:48:30 -060084 upload_targets = binhost.GetPrebuiltsFiles(uploads_dir)
Alex Klein3e728442019-05-15 11:46:57 -060085 index_path = binhost.UpdatePackageIndex(uploads_dir, upload_uri, upload_path,
86 sudo=True)
Evan Hernandezd437b4e2019-03-25 13:48:30 -060087 assert index_path.startswith(uploads_dir), (
88 'expected index_path to start with uploads_dir')
89 upload_targets.append(index_path[len(uploads_dir):])
90
91 output_proto.uploads_dir = uploads_dir
92 for upload_target in upload_targets:
93 output_proto.upload_targets.add().path = upload_target.strip('/')
94
95
96def SetBinhost(input_proto, output_proto):
97 """Set the URI for a given binhost key and build target.
98
99 See BinhostService documentation in api/proto/binhost.proto.
100
101 Args:
102 input_proto (SetBinhostRequest): The input proto.
103 output_proto (SetBinhostResponse): The output proto.
104 """
105 target = input_proto.build_target.name
106 key = binhost_pb2.BinhostKey.Name(input_proto.key)
107 uri = input_proto.uri
108 private = input_proto.private
Alex Klein6fb0eb82019-05-20 16:16:14 -0600109
110 # Temporary measure to force the new parallel cq post submit builders to write
111 # to a different file than the old ones. Writing to the same file was causing
112 # them to fight over the new one's value and the old logic of clearing out
113 # the values for files it didn't update. Once we've done a full switch over,
114 # we can dump this logic and delete all of the PARALLEL_POSTSUBMIT_BINHOST
115 # configs.
116 # TODO(crbug.com/965244) remove this.
117 if key == 'POSTSUBMIT_BINHOST':
118 key = 'PARALLEL_POSTSUBMIT_BINHOST'
119
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600120 output_proto.output_file = binhost.SetBinhost(target, key, uri,
121 private=private)
LaMont Jonesc64ae212019-04-15 15:41:28 -0600122
LaMont Jonesc64ae212019-04-15 15:41:28 -0600123
Alex Kleinaf0e0452019-06-03 18:10:01 -0600124def RegenBuildCache(input_proto, _output_proto):
LaMont Jonesc64ae212019-04-15 15:41:28 -0600125 """Regenerate the Build Cache for a build target.
126
127 See BinhostService documentation in api/proto/binhost.proto.
128
129 Args:
130 input_proto (RegenBuildCacheRequest): The input proto.
Alex Kleinaf0e0452019-06-03 18:10:01 -0600131 _output_proto (RegenBuildCacheResponse): The output proto.
LaMont Jonesc64ae212019-04-15 15:41:28 -0600132 """
133 overlay_type = input_proto.overlay_type
Evan Hernandez763f58b2019-05-15 10:57:49 -0600134 if overlay_type in _OVERLAY_TYPE_TO_NAME:
Alex Kleinaf0e0452019-06-03 18:10:01 -0600135 binhost.RegenBuildCache(_OVERLAY_TYPE_TO_NAME[overlay_type])
LaMont Jonesc64ae212019-04-15 15:41:28 -0600136 else:
137 cros_build_lib.Die('Overlay_type must be specified.')