blob: e92c8fd5977d197c7ff2a78cf0796bd8e12cba4c [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 Klein2b236722019-06-19 15:44:26 -060016from chromite.api import validate
Alex Kleinaf0e0452019-06-03 18:10:01 -060017from chromite.api.controller import controller_util
Evan Hernandezd437b4e2019-03-25 13:48:30 -060018from chromite.api.gen.chromite.api import binhost_pb2
Alex Kleina471f682019-05-31 11:23:45 -060019from chromite.lib import build_target_util
LaMont Jonesc64ae212019-04-15 15:41:28 -060020from chromite.lib import constants
21from chromite.lib import cros_build_lib
Evan Hernandezd437b4e2019-03-25 13:48:30 -060022from chromite.lib import gs
Alex Kleinaf0e0452019-06-03 18:10:01 -060023from chromite.lib import sysroot_lib
Evan Hernandezd437b4e2019-03-25 13:48:30 -060024from chromite.service import binhost
25
Alex Kleinaf0e0452019-06-03 18:10:01 -060026_OVERLAY_TYPE_TO_NAME = {
27 binhost_pb2.OVERLAYTYPE_PUBLIC: constants.PUBLIC_OVERLAYS,
28 binhost_pb2.OVERLAYTYPE_PRIVATE: constants.PRIVATE_OVERLAYS,
29 binhost_pb2.OVERLAYTYPE_BOTH: constants.BOTH_OVERLAYS,
30 binhost_pb2.OVERLAYTYPE_NONE: None
31}
32
Evan Hernandezd437b4e2019-03-25 13:48:30 -060033
Alex Klein2b236722019-06-19 15:44:26 -060034@validate.require('build_target.name')
Alex Klein231d2da2019-07-22 16:44:45 -060035@validate.validation_complete
36def GetBinhosts(input_proto, output_proto, _config):
Alex Klein7e40d252019-06-10 09:01:32 -060037 """Get a list of binhosts."""
Alex Klein2b236722019-06-19 15:44:26 -060038 build_target = build_target_util.BuildTarget(input_proto.build_target.name)
Alex Klein7e40d252019-06-10 09:01:32 -060039
40 binhosts = binhost.GetBinhosts(build_target)
41
42 for current in binhosts:
43 new_binhost = output_proto.binhosts.add()
44 new_binhost.uri = current
Alex Kleind3ac0bd2019-06-10 15:17:51 -060045 new_binhost.package_index = 'Packages'
Alex Klein7e40d252019-06-10 09:01:32 -060046
47
Alex Klein2b236722019-06-19 15:44:26 -060048@validate.require('build_target.name')
Alex Klein231d2da2019-07-22 16:44:45 -060049@validate.validation_complete
50def GetPrivatePrebuiltAclArgs(input_proto, output_proto, _config):
Alex Kleina471f682019-05-31 11:23:45 -060051 """Get the ACL args from the files in the private overlays."""
Alex Klein2b236722019-06-19 15:44:26 -060052 build_target = build_target_util.BuildTarget(input_proto.build_target.name)
Alex Kleina471f682019-05-31 11:23:45 -060053
54 try:
55 args = binhost.GetPrebuiltAclArgs(build_target)
56 except binhost.Error as e:
57 cros_build_lib.Die(e.message)
58
59 for arg, value in args:
60 new_arg = output_proto.args.add()
61 new_arg.arg = arg
62 new_arg.value = value
63
64
Alex Klein231d2da2019-07-22 16:44:45 -060065@validate.require('uri')
66def PrepareBinhostUploads(input_proto, output_proto, config):
Evan Hernandezd437b4e2019-03-25 13:48:30 -060067 """Return a list of files to uplooad to the binhost.
68
69 See BinhostService documentation in api/proto/binhost.proto.
70
71 Args:
72 input_proto (PrepareBinhostUploadsRequest): The input proto.
73 output_proto (PrepareBinhostUploadsResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -060074 config (api_config.ApiConfig): The API call config.
Evan Hernandezd437b4e2019-03-25 13:48:30 -060075 """
Alex Kleinaf0e0452019-06-03 18:10:01 -060076 target_name = (input_proto.sysroot.build_target.name
77 or input_proto.build_target.name)
78 sysroot_path = input_proto.sysroot.path
Evan Hernandezd437b4e2019-03-25 13:48:30 -060079
Alex Kleinaf0e0452019-06-03 18:10:01 -060080 if not sysroot_path and not target_name:
81 cros_build_lib.Die('Sysroot.path is required.')
82
83 build_target = build_target_util.BuildTarget(target_name)
84 chroot = controller_util.ParseChroot(input_proto.chroot)
85
86 if not sysroot_path:
87 # Very temporary, so not worried about this not calling the lib function.
88 sysroot_path = build_target.root
89 sysroot = sysroot_lib.Sysroot(sysroot_path)
90
91 uri = input_proto.uri
Evan Hernandezd437b4e2019-03-25 13:48:30 -060092 # For now, we enforce that all input URIs are Google Storage buckets.
93 if not gs.PathIsGs(uri):
94 raise ValueError('Upload URI %s must be Google Storage.' % uri)
95
Alex Klein231d2da2019-07-22 16:44:45 -060096 if config.validate_only:
97 return controller.RETURN_CODE_VALID_INPUT
98
Mike Frysinger3dcacee2019-08-23 17:09:11 -040099 parsed_uri = urllib.parse.urlparse(uri)
Alex Klein8dffea42019-06-14 14:01:36 -0600100 upload_uri = gs.GetGsURL(parsed_uri.netloc, for_gsutil=True).rstrip('/')
Alex Klein3e728442019-05-15 11:46:57 -0600101 upload_path = parsed_uri.path.lstrip('/')
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600102
103 # Read all packages and update the index. The index must be uploaded to the
104 # binhost for Portage to use it, so include it in upload_targets.
Alex Kleinaf0e0452019-06-03 18:10:01 -0600105 uploads_dir = binhost.GetPrebuiltsRoot(chroot, sysroot, build_target)
Alex Klein3e728442019-05-15 11:46:57 -0600106 index_path = binhost.UpdatePackageIndex(uploads_dir, upload_uri, upload_path,
107 sudo=True)
Alex Kleinb5d57a62019-06-20 12:04:53 -0600108 upload_targets = binhost.GetPrebuiltsFiles(uploads_dir)
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600109 assert index_path.startswith(uploads_dir), (
110 'expected index_path to start with uploads_dir')
111 upload_targets.append(index_path[len(uploads_dir):])
112
113 output_proto.uploads_dir = uploads_dir
114 for upload_target in upload_targets:
115 output_proto.upload_targets.add().path = upload_target.strip('/')
116
Michael Mortensenfc823882019-08-27 14:38:07 -0600117@validate.require('uri', 'sysroot.path')
118@validate.exists('uploads_dir')
119def PrepareDevInstallBinhostUploads(input_proto, output_proto, config):
120 """Return a list of files to upload to the binhost"
121
122 The files will also be copied to the uploads_dir.
123 See BinhostService documentation in api/proto/binhost.proto.
124
125 Args:
126 input_proto (PrepareDevInstallBinhostUploadsRequest): The input proto.
127 output_proto (PrepareDevInstallBinhostUploadsResponse): The output proto.
128 config (api_config.ApiConfig): The API call config.
129 """
130 sysroot_path = input_proto.sysroot.path
131
132 # build_target = build_target_util.BuildTarget(target_name)
133 chroot = controller_util.ParseChroot(input_proto.chroot)
134 sysroot = sysroot_lib.Sysroot(sysroot_path)
135
136 uri = input_proto.uri
137 # For now, we enforce that all input URIs are Google Storage buckets.
138 if not gs.PathIsGs(uri):
139 raise ValueError('Upload URI %s must be Google Storage.' % uri)
140
141 if config.validate_only:
142 return controller.RETURN_CODE_VALID_INPUT
143
Mike Frysinger3dcacee2019-08-23 17:09:11 -0400144 parsed_uri = urllib.parse.urlparse(uri)
Michael Mortensenfc823882019-08-27 14:38:07 -0600145 upload_uri = gs.GetGsURL(parsed_uri.netloc, for_gsutil=True).rstrip('/')
146 upload_path = parsed_uri.path.lstrip('/')
147
148 # Calculate the filename for the to-be-created Packages file, which will
149 # contain only devinstall packages.
150 devinstall_package_index_path = os.path.join(input_proto.uploads_dir,
151 'Packages')
152 upload_targets_list = binhost.ReadDevInstallFilesToCreatePackageIndex(
153 chroot, sysroot, devinstall_package_index_path, upload_uri, upload_path)
154
155 package_dir = chroot.full_path(sysroot.path, 'packages')
156 for upload_target in upload_targets_list:
157 # Copy each package to target/category/package
158 upload_target = upload_target.strip('/')
159 category = upload_target.split(os.sep)[0]
160 target_dir = os.path.join(input_proto.uploads_dir, category)
161 if not os.path.exists(target_dir):
162 os.makedirs(target_dir)
163 full_src_pkg_path = os.path.join(package_dir, upload_target)
164 full_target_src_path = os.path.join(input_proto.uploads_dir, upload_target)
165 shutil.copyfile(full_src_pkg_path, full_target_src_path)
166 output_proto.upload_targets.add().path = upload_target
167 output_proto.upload_targets.add().path = 'Packages'
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600168
Alex Klein231d2da2019-07-22 16:44:45 -0600169@validate.require('build_target.name', 'key', 'uri')
170@validate.validation_complete
171def SetBinhost(input_proto, output_proto, _config):
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600172 """Set the URI for a given binhost key and build target.
173
174 See BinhostService documentation in api/proto/binhost.proto.
175
176 Args:
177 input_proto (SetBinhostRequest): The input proto.
178 output_proto (SetBinhostResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -0600179 _config (api_config.ApiConfig): The API call config.
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600180 """
181 target = input_proto.build_target.name
182 key = binhost_pb2.BinhostKey.Name(input_proto.key)
183 uri = input_proto.uri
184 private = input_proto.private
Alex Klein6fb0eb82019-05-20 16:16:14 -0600185
186 # Temporary measure to force the new parallel cq post submit builders to write
187 # to a different file than the old ones. Writing to the same file was causing
188 # them to fight over the new one's value and the old logic of clearing out
189 # the values for files it didn't update. Once we've done a full switch over,
190 # we can dump this logic and delete all of the PARALLEL_POSTSUBMIT_BINHOST
191 # configs.
192 # TODO(crbug.com/965244) remove this.
193 if key == 'POSTSUBMIT_BINHOST':
194 key = 'PARALLEL_POSTSUBMIT_BINHOST'
195
Evan Hernandezd437b4e2019-03-25 13:48:30 -0600196 output_proto.output_file = binhost.SetBinhost(target, key, uri,
197 private=private)
LaMont Jonesc64ae212019-04-15 15:41:28 -0600198
LaMont Jonesc64ae212019-04-15 15:41:28 -0600199
Alex Klein231d2da2019-07-22 16:44:45 -0600200@validate.require('overlay_type')
201@validate.is_in('overlay_type', _OVERLAY_TYPE_TO_NAME)
202@validate.validation_complete
Alex Klein82c85d42019-08-14 15:47:51 -0600203def RegenBuildCache(input_proto, output_proto, _config):
LaMont Jonesc64ae212019-04-15 15:41:28 -0600204 """Regenerate the Build Cache for a build target.
205
206 See BinhostService documentation in api/proto/binhost.proto.
207
208 Args:
209 input_proto (RegenBuildCacheRequest): The input proto.
Alex Klein82c85d42019-08-14 15:47:51 -0600210 output_proto (RegenBuildCacheResponse): The output proto.
Alex Klein231d2da2019-07-22 16:44:45 -0600211 _config (api_config.ApiConfig): The API call config.
LaMont Jonesc64ae212019-04-15 15:41:28 -0600212 """
Alex Klein82c85d42019-08-14 15:47:51 -0600213 chroot = controller_util.ParseChroot(input_proto.chroot)
LaMont Jonesc64ae212019-04-15 15:41:28 -0600214 overlay_type = input_proto.overlay_type
Alex Klein82c85d42019-08-14 15:47:51 -0600215 overlays = binhost.RegenBuildCache(chroot,
216 _OVERLAY_TYPE_TO_NAME[overlay_type])
217
218 for overlay in overlays:
219 output_proto.modified_overlays.add().path = overlay