blob: 69747a2a63b55557583fe4e10735f6b56c837459 [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
12from chromite.api.gen.chromite.api import binhost_pb2
LaMont Jonesc64ae212019-04-15 15:41:28 -060013from chromite.lib import constants
14from chromite.lib import cros_build_lib
Evan Hernandezd437b4e2019-03-25 13:48:30 -060015from chromite.lib import gs
16from chromite.service import binhost
17
18
19def PrepareBinhostUploads(input_proto, output_proto):
20 """Return a list of files to uplooad to the binhost.
21
22 See BinhostService documentation in api/proto/binhost.proto.
23
24 Args:
25 input_proto (PrepareBinhostUploadsRequest): The input proto.
26 output_proto (PrepareBinhostUploadsResponse): The output proto.
27 """
28 target = input_proto.build_target.name
29 uri = input_proto.uri
30
31 # For now, we enforce that all input URIs are Google Storage buckets.
32 if not gs.PathIsGs(uri):
33 raise ValueError('Upload URI %s must be Google Storage.' % uri)
34
35 parsed_uri = urlparse.urlparse(uri)
36 upload_uri = gs.GetGsURL(parsed_uri.netloc)
37 upload_path = parsed_uri.path
38
39 # Read all packages and update the index. The index must be uploaded to the
40 # binhost for Portage to use it, so include it in upload_targets.
41 uploads_dir = binhost.GetPrebuiltsRoot(target)
42 upload_targets = binhost.GetPrebuiltsFiles(uploads_dir)
43 index_path = binhost.UpdatePackageIndex(uploads_dir, upload_uri, upload_path)
44 assert index_path.startswith(uploads_dir), (
45 'expected index_path to start with uploads_dir')
46 upload_targets.append(index_path[len(uploads_dir):])
47
48 output_proto.uploads_dir = uploads_dir
49 for upload_target in upload_targets:
50 output_proto.upload_targets.add().path = upload_target.strip('/')
51
52
53def SetBinhost(input_proto, output_proto):
54 """Set the URI for a given binhost key and build target.
55
56 See BinhostService documentation in api/proto/binhost.proto.
57
58 Args:
59 input_proto (SetBinhostRequest): The input proto.
60 output_proto (SetBinhostResponse): The output proto.
61 """
62 target = input_proto.build_target.name
63 key = binhost_pb2.BinhostKey.Name(input_proto.key)
64 uri = input_proto.uri
65 private = input_proto.private
66 output_proto.output_file = binhost.SetBinhost(target, key, uri,
67 private=private)
LaMont Jonesc64ae212019-04-15 15:41:28 -060068
69_overlay_type_to_name = {
70 binhost_pb2.OVERLAYTYPE_PUBLIC: constants.PUBLIC_OVERLAYS,
71 binhost_pb2.OVERLAYTYPE_PRIVATE: constants.PRIVATE_OVERLAYS,
72 binhost_pb2.OVERLAYTYPE_BOTH: constants.BOTH_OVERLAYS,
73 binhost_pb2.OVERLAYTYPE_NONE: None
74}
75
76def RegenBuildCache(input_proto):
77 """Regenerate the Build Cache for a build target.
78
79 See BinhostService documentation in api/proto/binhost.proto.
80
81 Args:
82 input_proto (RegenBuildCacheRequest): The input proto.
83 output_proto (RegenBuildCacheResponse): The output proto.
84 """
85 overlay_type = input_proto.overlay_type
86 sysroot = input_proto.sysroot
87 if overlay_type in _overlay_type_to_name:
88 binhost.RegenBuildCache(_overlay_type_to_name[overlay_type], sysroot.path)
89 else:
90 cros_build_lib.Die('Overlay_type must be specified.')