BinhostService: Refactor PrepareBinhostUploads and RegenBuildCache

BUG=chromium:963215, chromium:963211, b:132732435, b:132732743
TEST=run_tests

Change-Id: I708f7f3dc13b6e3f3ec2e861490aad1400331421
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/chromite/+/1640829
Reviewed-by: Alex Klein <saklein@chromium.org>
Tested-by: Alex Klein <saklein@chromium.org>
diff --git a/api/controller/binhost.py b/api/controller/binhost.py
index d434379..0569459 100644
--- a/api/controller/binhost.py
+++ b/api/controller/binhost.py
@@ -9,13 +9,22 @@
 
 import urlparse
 
+from chromite.api.controller import controller_util
 from chromite.api.gen.chromite.api import binhost_pb2
 from chromite.lib import build_target_util
 from chromite.lib import constants
 from chromite.lib import cros_build_lib
 from chromite.lib import gs
+from chromite.lib import sysroot_lib
 from chromite.service import binhost
 
+_OVERLAY_TYPE_TO_NAME = {
+    binhost_pb2.OVERLAYTYPE_PUBLIC: constants.PUBLIC_OVERLAYS,
+    binhost_pb2.OVERLAYTYPE_PRIVATE: constants.PRIVATE_OVERLAYS,
+    binhost_pb2.OVERLAYTYPE_BOTH: constants.BOTH_OVERLAYS,
+    binhost_pb2.OVERLAYTYPE_NONE: None
+}
+
 
 def GetPrivatePrebuiltAclArgs(input_proto, output_proto):
   """Get the ACL args from the files in the private overlays."""
@@ -45,9 +54,22 @@
     input_proto (PrepareBinhostUploadsRequest): The input proto.
     output_proto (PrepareBinhostUploadsResponse): The output proto.
   """
-  target = input_proto.build_target.name
-  uri = input_proto.uri
+  target_name = (input_proto.sysroot.build_target.name
+                 or input_proto.build_target.name)
+  sysroot_path = input_proto.sysroot.path
 
+  if not sysroot_path and not target_name:
+    cros_build_lib.Die('Sysroot.path is required.')
+
+  build_target = build_target_util.BuildTarget(target_name)
+  chroot = controller_util.ParseChroot(input_proto.chroot)
+
+  if not sysroot_path:
+    # Very temporary, so not worried about this not calling the lib function.
+    sysroot_path = build_target.root
+  sysroot = sysroot_lib.Sysroot(sysroot_path)
+
+  uri = input_proto.uri
   # For now, we enforce that all input URIs are Google Storage buckets.
   if not gs.PathIsGs(uri):
     raise ValueError('Upload URI %s must be Google Storage.' % uri)
@@ -58,7 +80,7 @@
 
   # Read all packages and update the index. The index must be uploaded to the
   # binhost for Portage to use it, so include it in upload_targets.
-  uploads_dir = binhost.GetPrebuiltsRoot(target)
+  uploads_dir = binhost.GetPrebuiltsRoot(chroot, sysroot, build_target)
   upload_targets = binhost.GetPrebuiltsFiles(uploads_dir)
   index_path = binhost.UpdatePackageIndex(uploads_dir, upload_uri, upload_path,
                                           sudo=True)
@@ -98,25 +120,18 @@
   output_proto.output_file = binhost.SetBinhost(target, key, uri,
                                                 private=private)
 
-_OVERLAY_TYPE_TO_NAME = {
-    binhost_pb2.OVERLAYTYPE_PUBLIC: constants.PUBLIC_OVERLAYS,
-    binhost_pb2.OVERLAYTYPE_PRIVATE: constants.PRIVATE_OVERLAYS,
-    binhost_pb2.OVERLAYTYPE_BOTH: constants.BOTH_OVERLAYS,
-    binhost_pb2.OVERLAYTYPE_NONE: None
-}
 
-def RegenBuildCache(input_proto):
+def RegenBuildCache(input_proto, _output_proto):
   """Regenerate the Build Cache for a build target.
 
   See BinhostService documentation in api/proto/binhost.proto.
 
   Args:
     input_proto (RegenBuildCacheRequest): The input proto.
-    output_proto (RegenBuildCacheResponse): The output proto.
+    _output_proto (RegenBuildCacheResponse): The output proto.
   """
   overlay_type = input_proto.overlay_type
-  sysroot = input_proto.sysroot
   if overlay_type in _OVERLAY_TYPE_TO_NAME:
-    binhost.RegenBuildCache(_OVERLAY_TYPE_TO_NAME[overlay_type], sysroot.path)
+    binhost.RegenBuildCache(_OVERLAY_TYPE_TO_NAME[overlay_type])
   else:
     cros_build_lib.Die('Overlay_type must be specified.')
diff --git a/api/controller/binhost_unittest.py b/api/controller/binhost_unittest.py
index 1ae869d..1527856 100644
--- a/api/controller/binhost_unittest.py
+++ b/api/controller/binhost_unittest.py
@@ -78,11 +78,10 @@
 
     input_proto = binhost_pb2.RegenBuildCacheRequest()
     input_proto.overlay_type = binhost_pb2.OVERLAYTYPE_BOTH
-    input_proto.sysroot.path = '/path/to/chroot'
-    input_proto.sysroot.build_target.name = 'target'
+    output_proto = binhost_pb2.RegenBuildCacheResponse()
 
-    binhost.RegenBuildCache(input_proto)
-    regen_cache.assert_called_once_with('both', '/path/to/chroot')
+    binhost.RegenBuildCache(input_proto, output_proto)
+    regen_cache.assert_called_once_with('both')
 
   def testRequiresOverlayType(self):
     """RegenBuildCache dies if overlay_type not specified."""
@@ -91,9 +90,8 @@
 
     input_proto = binhost_pb2.RegenBuildCacheRequest()
     input_proto.overlay_type = binhost_pb2.OVERLAYTYPE_UNSPECIFIED
-    input_proto.sysroot.path = '/path/to/chroot'
-    input_proto.sysroot.build_target.name = 'target'
+    output_proto = binhost_pb2.RegenBuildCacheResponse()
 
-    binhost.RegenBuildCache(input_proto)
+    binhost.RegenBuildCache(input_proto, output_proto)
     die.assert_called_once()
     regen_cache.assert_not_called()