blob: e5b33794fab015003388ec78330ef21f47d7e206 [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 Klein231d2da2019-07-22 16:44:45 -060012from chromite.api import controller
Alex Klein2b236722019-06-19 15:44:26 -060013from chromite.api import validate
Alex Kleinaf0e0452019-06-03 18:10:01 -060014from chromite.api.controller import controller_util
Evan Hernandezd437b4e2019-03-25 13:48:30 -060015from chromite.api.gen.chromite.api import binhost_pb2
Alex Kleina471f682019-05-31 11:23:45 -060016from chromite.lib import build_target_util
LaMont Jonesc64ae212019-04-15 15:41:28 -060017from chromite.lib import constants
18from chromite.lib import cros_build_lib
Evan Hernandezd437b4e2019-03-25 13:48:30 -060019from chromite.lib import gs
Alex Kleinaf0e0452019-06-03 18:10:01 -060020from chromite.lib import sysroot_lib
Evan Hernandezd437b4e2019-03-25 13:48:30 -060021from chromite.service import binhost
22
Alex Kleinaf0e0452019-06-03 18:10:01 -060023_OVERLAY_TYPE_TO_NAME = {
24 binhost_pb2.OVERLAYTYPE_PUBLIC: constants.PUBLIC_OVERLAYS,
25 binhost_pb2.OVERLAYTYPE_PRIVATE: constants.PRIVATE_OVERLAYS,
26 binhost_pb2.OVERLAYTYPE_BOTH: constants.BOTH_OVERLAYS,
27 binhost_pb2.OVERLAYTYPE_NONE: None
28}
29
Evan Hernandezd437b4e2019-03-25 13:48:30 -060030
Alex Klein2b236722019-06-19 15:44:26 -060031@validate.require('build_target.name')
Alex Klein231d2da2019-07-22 16:44:45 -060032@validate.validation_complete
33def GetBinhosts(input_proto, output_proto, _config):
Alex Klein7e40d252019-06-10 09:01:32 -060034 """Get a list of binhosts."""
Alex Klein2b236722019-06-19 15:44:26 -060035 build_target = build_target_util.BuildTarget(input_proto.build_target.name)
Alex Klein7e40d252019-06-10 09:01:32 -060036
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 Kleind3ac0bd2019-06-10 15:17:51 -060042 new_binhost.package_index = 'Packages'
Alex Klein7e40d252019-06-10 09:01:32 -060043
44
Alex Klein2b236722019-06-19 15:44:26 -060045@validate.require('build_target.name')
Alex Klein231d2da2019-07-22 16:44:45 -060046@validate.validation_complete
47def GetPrivatePrebuiltAclArgs(input_proto, output_proto, _config):
Alex Kleina471f682019-05-31 11:23:45 -060048 """Get the ACL args from the files in the private overlays."""
Alex Klein2b236722019-06-19 15:44:26 -060049 build_target = build_target_util.BuildTarget(input_proto.build_target.name)
Alex Kleina471f682019-05-31 11:23:45 -060050
51 try:
52 args = binhost.GetPrebuiltAclArgs(build_target)
53 except binhost.Error as e:
54 cros_build_lib.Die(e.message)
55
56 for arg, value in args:
57 new_arg = output_proto.args.add()
58 new_arg.arg = arg
59 new_arg.value = value
60
61
Alex Klein231d2da2019-07-22 16:44:45 -060062@validate.require('uri')
63def PrepareBinhostUploads(input_proto, output_proto, config):
Evan Hernandezd437b4e2019-03-25 13:48:30 -060064 """Return a list of files to uplooad to the binhost.
65
66 See BinhostService documentation in api/proto/binhost.proto.
67
68 Args:
69 input_proto (PrepareBinhostUploadsRequest): The input proto.
70 output_proto (PrepareBinhostUploadsResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -060071 config (api_config.ApiConfig): The API call config.
Evan Hernandezd437b4e2019-03-25 13:48:30 -060072 """
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 Klein231d2da2019-07-22 16:44:45 -060093 if config.validate_only:
94 return controller.RETURN_CODE_VALID_INPUT
95
Evan Hernandezd437b4e2019-03-25 13:48:30 -060096 parsed_uri = urlparse.urlparse(uri)
Alex Klein8dffea42019-06-14 14:01:36 -060097 upload_uri = gs.GetGsURL(parsed_uri.netloc, for_gsutil=True).rstrip('/')
Alex Klein3e728442019-05-15 11:46:57 -060098 upload_path = parsed_uri.path.lstrip('/')
Evan Hernandezd437b4e2019-03-25 13:48:30 -060099
100 # Read all packages and update the index. The index must be uploaded to the
101 # binhost for Portage to use it, so include it in upload_targets.
Alex Kleinaf0e0452019-06-03 18:10:01 -0600102 uploads_dir = binhost.GetPrebuiltsRoot(chroot, sysroot, build_target)
Alex Klein3e728442019-05-15 11:46:57 -0600103 index_path = binhost.UpdatePackageIndex(uploads_dir, upload_uri, upload_path,
104 sudo=True)
Alex Kleinb5d57a62019-06-20 12:04:53 -0600105 upload_targets = binhost.GetPrebuiltsFiles(uploads_dir)
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600106 assert index_path.startswith(uploads_dir), (
107 'expected index_path to start with uploads_dir')
108 upload_targets.append(index_path[len(uploads_dir):])
109
110 output_proto.uploads_dir = uploads_dir
111 for upload_target in upload_targets:
112 output_proto.upload_targets.add().path = upload_target.strip('/')
113
114
Alex Klein231d2da2019-07-22 16:44:45 -0600115@validate.require('build_target.name', 'key', 'uri')
116@validate.validation_complete
117def SetBinhost(input_proto, output_proto, _config):
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600118 """Set the URI for a given binhost key and build target.
119
120 See BinhostService documentation in api/proto/binhost.proto.
121
122 Args:
123 input_proto (SetBinhostRequest): The input proto.
124 output_proto (SetBinhostResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -0600125 _config (api_config.ApiConfig): The API call config.
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600126 """
127 target = input_proto.build_target.name
128 key = binhost_pb2.BinhostKey.Name(input_proto.key)
129 uri = input_proto.uri
130 private = input_proto.private
Alex Klein6fb0eb82019-05-20 16:16:14 -0600131
132 # Temporary measure to force the new parallel cq post submit builders to write
133 # to a different file than the old ones. Writing to the same file was causing
134 # them to fight over the new one's value and the old logic of clearing out
135 # the values for files it didn't update. Once we've done a full switch over,
136 # we can dump this logic and delete all of the PARALLEL_POSTSUBMIT_BINHOST
137 # configs.
138 # TODO(crbug.com/965244) remove this.
139 if key == 'POSTSUBMIT_BINHOST':
140 key = 'PARALLEL_POSTSUBMIT_BINHOST'
141
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600142 output_proto.output_file = binhost.SetBinhost(target, key, uri,
143 private=private)
LaMont Jonesc64ae212019-04-15 15:41:28 -0600144
LaMont Jonesc64ae212019-04-15 15:41:28 -0600145
Alex Klein231d2da2019-07-22 16:44:45 -0600146@validate.require('overlay_type')
147@validate.is_in('overlay_type', _OVERLAY_TYPE_TO_NAME)
148@validate.validation_complete
Alex Klein82c85d42019-08-14 15:47:51 -0600149def RegenBuildCache(input_proto, output_proto, _config):
LaMont Jonesc64ae212019-04-15 15:41:28 -0600150 """Regenerate the Build Cache for a build target.
151
152 See BinhostService documentation in api/proto/binhost.proto.
153
154 Args:
155 input_proto (RegenBuildCacheRequest): The input proto.
Alex Klein82c85d42019-08-14 15:47:51 -0600156 output_proto (RegenBuildCacheResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -0600157 _config (api_config.ApiConfig): The API call config.
LaMont Jonesc64ae212019-04-15 15:41:28 -0600158 """
Alex Klein82c85d42019-08-14 15:47:51 -0600159 chroot = controller_util.ParseChroot(input_proto.chroot)
LaMont Jonesc64ae212019-04-15 15:41:28 -0600160 overlay_type = input_proto.overlay_type
Alex Klein82c85d42019-08-14 15:47:51 -0600161 overlays = binhost.RegenBuildCache(chroot,
162 _OVERLAY_TYPE_TO_NAME[overlay_type])
163
164 for overlay in overlays:
165 output_proto.modified_overlays.add().path = overlay