blob: 87522978e6dea4cb211400c52e2207bc6572d0fe [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
Michael Mortensenfc823882019-08-27 14:38:07 -060010import os
11import shutil
Mike Frysinger3dcacee2019-08-23 17:09:11 -040012
13from six.moves import urllib
Evan Hernandezd437b4e2019-03-25 13:48:30 -060014
Alex Klein231d2da2019-07-22 16:44:45 -060015from chromite.api import controller
Alex Klein076841b2019-08-29 15:19:39 -060016from chromite.api import faux
Alex Klein2b236722019-06-19 15:44:26 -060017from chromite.api import validate
Alex Kleinaf0e0452019-06-03 18:10:01 -060018from chromite.api.controller import controller_util
Evan Hernandezd437b4e2019-03-25 13:48:30 -060019from chromite.api.gen.chromite.api import binhost_pb2
Alex Kleina471f682019-05-31 11:23:45 -060020from chromite.lib import build_target_util
LaMont Jonesc64ae212019-04-15 15:41:28 -060021from chromite.lib import constants
22from chromite.lib import cros_build_lib
Evan Hernandezd437b4e2019-03-25 13:48:30 -060023from chromite.lib import gs
Alex Kleinaf0e0452019-06-03 18:10:01 -060024from chromite.lib import sysroot_lib
Evan Hernandezd437b4e2019-03-25 13:48:30 -060025from chromite.service import binhost
26
Alex Kleinaf0e0452019-06-03 18:10:01 -060027_OVERLAY_TYPE_TO_NAME = {
28 binhost_pb2.OVERLAYTYPE_PUBLIC: constants.PUBLIC_OVERLAYS,
29 binhost_pb2.OVERLAYTYPE_PRIVATE: constants.PRIVATE_OVERLAYS,
30 binhost_pb2.OVERLAYTYPE_BOTH: constants.BOTH_OVERLAYS,
31 binhost_pb2.OVERLAYTYPE_NONE: None
32}
33
Evan Hernandezd437b4e2019-03-25 13:48:30 -060034
Alex Klein076841b2019-08-29 15:19:39 -060035@faux.all_empty
Alex Klein2b236722019-06-19 15:44:26 -060036@validate.require('build_target.name')
Alex Klein231d2da2019-07-22 16:44:45 -060037@validate.validation_complete
38def GetBinhosts(input_proto, output_proto, _config):
Alex Klein7e40d252019-06-10 09:01:32 -060039 """Get a list of binhosts."""
Alex Klein2b236722019-06-19 15:44:26 -060040 build_target = build_target_util.BuildTarget(input_proto.build_target.name)
Alex Klein7e40d252019-06-10 09:01:32 -060041
42 binhosts = binhost.GetBinhosts(build_target)
43
44 for current in binhosts:
45 new_binhost = output_proto.binhosts.add()
46 new_binhost.uri = current
Alex Kleind3ac0bd2019-06-10 15:17:51 -060047 new_binhost.package_index = 'Packages'
Alex Klein7e40d252019-06-10 09:01:32 -060048
49
Alex Klein076841b2019-08-29 15:19:39 -060050@faux.all_empty
Alex Klein2b236722019-06-19 15:44:26 -060051@validate.require('build_target.name')
Alex Klein231d2da2019-07-22 16:44:45 -060052@validate.validation_complete
53def GetPrivatePrebuiltAclArgs(input_proto, output_proto, _config):
Alex Kleina471f682019-05-31 11:23:45 -060054 """Get the ACL args from the files in the private overlays."""
Alex Klein2b236722019-06-19 15:44:26 -060055 build_target = build_target_util.BuildTarget(input_proto.build_target.name)
Alex Kleina471f682019-05-31 11:23:45 -060056
57 try:
58 args = binhost.GetPrebuiltAclArgs(build_target)
59 except binhost.Error as e:
60 cros_build_lib.Die(e.message)
61
62 for arg, value in args:
63 new_arg = output_proto.args.add()
64 new_arg.arg = arg
65 new_arg.value = value
66
67
Alex Klein076841b2019-08-29 15:19:39 -060068@faux.all_empty
Alex Klein231d2da2019-07-22 16:44:45 -060069@validate.require('uri')
70def PrepareBinhostUploads(input_proto, output_proto, config):
Evan Hernandezd437b4e2019-03-25 13:48:30 -060071 """Return a list of files to uplooad to the binhost.
72
73 See BinhostService documentation in api/proto/binhost.proto.
74
75 Args:
76 input_proto (PrepareBinhostUploadsRequest): The input proto.
77 output_proto (PrepareBinhostUploadsResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -060078 config (api_config.ApiConfig): The API call config.
Evan Hernandezd437b4e2019-03-25 13:48:30 -060079 """
Alex Kleinaf0e0452019-06-03 18:10:01 -060080 target_name = (input_proto.sysroot.build_target.name
81 or input_proto.build_target.name)
82 sysroot_path = input_proto.sysroot.path
Evan Hernandezd437b4e2019-03-25 13:48:30 -060083
Alex Kleinaf0e0452019-06-03 18:10:01 -060084 if not sysroot_path and not target_name:
85 cros_build_lib.Die('Sysroot.path is required.')
86
87 build_target = build_target_util.BuildTarget(target_name)
88 chroot = controller_util.ParseChroot(input_proto.chroot)
89
90 if not sysroot_path:
91 # Very temporary, so not worried about this not calling the lib function.
92 sysroot_path = build_target.root
93 sysroot = sysroot_lib.Sysroot(sysroot_path)
94
95 uri = input_proto.uri
Evan Hernandezd437b4e2019-03-25 13:48:30 -060096 # For now, we enforce that all input URIs are Google Storage buckets.
97 if not gs.PathIsGs(uri):
98 raise ValueError('Upload URI %s must be Google Storage.' % uri)
99
Alex Klein231d2da2019-07-22 16:44:45 -0600100 if config.validate_only:
101 return controller.RETURN_CODE_VALID_INPUT
102
Mike Frysinger3dcacee2019-08-23 17:09:11 -0400103 parsed_uri = urllib.parse.urlparse(uri)
Alex Klein8dffea42019-06-14 14:01:36 -0600104 upload_uri = gs.GetGsURL(parsed_uri.netloc, for_gsutil=True).rstrip('/')
Alex Klein3e728442019-05-15 11:46:57 -0600105 upload_path = parsed_uri.path.lstrip('/')
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600106
107 # Read all packages and update the index. The index must be uploaded to the
108 # binhost for Portage to use it, so include it in upload_targets.
Alex Kleinaf0e0452019-06-03 18:10:01 -0600109 uploads_dir = binhost.GetPrebuiltsRoot(chroot, sysroot, build_target)
Alex Klein3e728442019-05-15 11:46:57 -0600110 index_path = binhost.UpdatePackageIndex(uploads_dir, upload_uri, upload_path,
111 sudo=True)
Alex Kleinb5d57a62019-06-20 12:04:53 -0600112 upload_targets = binhost.GetPrebuiltsFiles(uploads_dir)
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600113 assert index_path.startswith(uploads_dir), (
114 'expected index_path to start with uploads_dir')
115 upload_targets.append(index_path[len(uploads_dir):])
116
117 output_proto.uploads_dir = uploads_dir
118 for upload_target in upload_targets:
119 output_proto.upload_targets.add().path = upload_target.strip('/')
120
Alex Klein076841b2019-08-29 15:19:39 -0600121
122@faux.all_empty
Michael Mortensenfc823882019-08-27 14:38:07 -0600123@validate.require('uri', 'sysroot.path')
124@validate.exists('uploads_dir')
125def PrepareDevInstallBinhostUploads(input_proto, output_proto, config):
126 """Return a list of files to upload to the binhost"
127
128 The files will also be copied to the uploads_dir.
129 See BinhostService documentation in api/proto/binhost.proto.
130
131 Args:
132 input_proto (PrepareDevInstallBinhostUploadsRequest): The input proto.
133 output_proto (PrepareDevInstallBinhostUploadsResponse): The output proto.
134 config (api_config.ApiConfig): The API call config.
135 """
136 sysroot_path = input_proto.sysroot.path
137
138 # build_target = build_target_util.BuildTarget(target_name)
139 chroot = controller_util.ParseChroot(input_proto.chroot)
140 sysroot = sysroot_lib.Sysroot(sysroot_path)
141
142 uri = input_proto.uri
143 # For now, we enforce that all input URIs are Google Storage buckets.
144 if not gs.PathIsGs(uri):
145 raise ValueError('Upload URI %s must be Google Storage.' % uri)
146
147 if config.validate_only:
148 return controller.RETURN_CODE_VALID_INPUT
149
Mike Frysinger3dcacee2019-08-23 17:09:11 -0400150 parsed_uri = urllib.parse.urlparse(uri)
Michael Mortensenfc823882019-08-27 14:38:07 -0600151 upload_uri = gs.GetGsURL(parsed_uri.netloc, for_gsutil=True).rstrip('/')
152 upload_path = parsed_uri.path.lstrip('/')
153
154 # Calculate the filename for the to-be-created Packages file, which will
155 # contain only devinstall packages.
156 devinstall_package_index_path = os.path.join(input_proto.uploads_dir,
157 'Packages')
158 upload_targets_list = binhost.ReadDevInstallFilesToCreatePackageIndex(
159 chroot, sysroot, devinstall_package_index_path, upload_uri, upload_path)
160
161 package_dir = chroot.full_path(sysroot.path, 'packages')
162 for upload_target in upload_targets_list:
163 # Copy each package to target/category/package
164 upload_target = upload_target.strip('/')
165 category = upload_target.split(os.sep)[0]
166 target_dir = os.path.join(input_proto.uploads_dir, category)
167 if not os.path.exists(target_dir):
168 os.makedirs(target_dir)
169 full_src_pkg_path = os.path.join(package_dir, upload_target)
170 full_target_src_path = os.path.join(input_proto.uploads_dir, upload_target)
171 shutil.copyfile(full_src_pkg_path, full_target_src_path)
172 output_proto.upload_targets.add().path = upload_target
173 output_proto.upload_targets.add().path = 'Packages'
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600174
Alex Klein076841b2019-08-29 15:19:39 -0600175
176@faux.all_empty
Alex Klein231d2da2019-07-22 16:44:45 -0600177@validate.require('build_target.name', 'key', 'uri')
178@validate.validation_complete
179def SetBinhost(input_proto, output_proto, _config):
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600180 """Set the URI for a given binhost key and build target.
181
182 See BinhostService documentation in api/proto/binhost.proto.
183
184 Args:
185 input_proto (SetBinhostRequest): The input proto.
186 output_proto (SetBinhostResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -0600187 _config (api_config.ApiConfig): The API call config.
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600188 """
189 target = input_proto.build_target.name
190 key = binhost_pb2.BinhostKey.Name(input_proto.key)
191 uri = input_proto.uri
192 private = input_proto.private
Alex Klein6fb0eb82019-05-20 16:16:14 -0600193
194 # Temporary measure to force the new parallel cq post submit builders to write
195 # to a different file than the old ones. Writing to the same file was causing
196 # them to fight over the new one's value and the old logic of clearing out
197 # the values for files it didn't update. Once we've done a full switch over,
198 # we can dump this logic and delete all of the PARALLEL_POSTSUBMIT_BINHOST
199 # configs.
200 # TODO(crbug.com/965244) remove this.
201 if key == 'POSTSUBMIT_BINHOST':
202 key = 'PARALLEL_POSTSUBMIT_BINHOST'
203
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600204 output_proto.output_file = binhost.SetBinhost(target, key, uri,
205 private=private)
LaMont Jonesc64ae212019-04-15 15:41:28 -0600206
LaMont Jonesc64ae212019-04-15 15:41:28 -0600207
Alex Klein076841b2019-08-29 15:19:39 -0600208@faux.all_empty
Alex Klein231d2da2019-07-22 16:44:45 -0600209@validate.require('overlay_type')
210@validate.is_in('overlay_type', _OVERLAY_TYPE_TO_NAME)
211@validate.validation_complete
Alex Klein82c85d42019-08-14 15:47:51 -0600212def RegenBuildCache(input_proto, output_proto, _config):
LaMont Jonesc64ae212019-04-15 15:41:28 -0600213 """Regenerate the Build Cache for a build target.
214
215 See BinhostService documentation in api/proto/binhost.proto.
216
217 Args:
218 input_proto (RegenBuildCacheRequest): The input proto.
Alex Klein82c85d42019-08-14 15:47:51 -0600219 output_proto (RegenBuildCacheResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -0600220 _config (api_config.ApiConfig): The API call config.
LaMont Jonesc64ae212019-04-15 15:41:28 -0600221 """
Alex Klein82c85d42019-08-14 15:47:51 -0600222 chroot = controller_util.ParseChroot(input_proto.chroot)
LaMont Jonesc64ae212019-04-15 15:41:28 -0600223 overlay_type = input_proto.overlay_type
Alex Klein82c85d42019-08-14 15:47:51 -0600224 overlays = binhost.RegenBuildCache(chroot,
225 _OVERLAY_TYPE_TO_NAME[overlay_type])
226
227 for overlay in overlays:
228 output_proto.modified_overlays.add().path = overlay