Goma: Fix log dir handling.

Make goma parsing optional to allow the handlers to affect the
chroot-based dynamic configs. Fix the log dir handling to
pass through a valid path to the Goma instance.

BUG=chromium:1031259
TEST=run_tests

Change-Id: I3e4fa23b8ba72b2209053422da98bbf2ea70e5d6
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/chromite/+/1972711
Tested-by: Alex Klein <saklein@chromium.org>
Reviewed-by: David Burger <dburger@chromium.org>
Commit-Queue: Alex Klein <saklein@chromium.org>
diff --git a/api/controller/controller_util.py b/api/controller/controller_util.py
index 9c6abaf..4595e7a 100644
--- a/api/controller/controller_util.py
+++ b/api/controller/controller_util.py
@@ -7,8 +7,11 @@
 
 from __future__ import print_function
 
+import os
+
 from chromite.api.gen.chromiumos import common_pb2
 from chromite.cbuildbot import goma_util
+from chromite.lib import constants
 from chromite.lib import portage_util
 from chromite.lib.build_target_util import BuildTarget
 from chromite.lib.chroot_lib import Chroot
@@ -22,11 +25,12 @@
   """Invalid message."""
 
 
-def ParseChroot(chroot_message):
+def ParseChroot(chroot_message, parse_goma=True):
   """Create a chroot object from the chroot message.
 
   Args:
     chroot_message (common_pb2.Chroot): The chroot message.
+    parse_goma (bool): Whether to try to parse the goma configs.
 
   Returns:
     Chroot: The parsed chroot object.
@@ -36,7 +40,7 @@
   """
   assert isinstance(chroot_message, common_pb2.Chroot)
 
-  path = chroot_message.path
+  path = chroot_message.path or constants.DEFAULT_CHROOT_PATH
   cache_dir = chroot_message.cache_dir
   chrome_root = chroot_message.chrome_dir
 
@@ -56,34 +60,46 @@
   if features:
     env['FEATURES'] = ' '.join(features)
 
-  goma = None
-  if chroot_message.goma.goma_dir:
-    chromeos_goma_dir = chroot_message.goma.chromeos_goma_dir or None
-    goma_approach = None
-    if chroot_message.goma.goma_approach == common_pb2.GomaConfig.RBE_PROD:
-      goma_approach = goma_util.GomaApproach('?prod', 'goma.chromium.org', True)
-    elif chroot_message.goma.goma_approach == common_pb2.GomaConfig.RBE_STAGING:
-      goma_approach = goma_util.GomaApproach('?staging',
-                                             'staging-goma.chromium.org', True)
-
-    log_dir = chroot_message.goma.log_dir.dir or None
-    stats_filename = chroot_message.goma.stats_file or None
-    counterz_filename = chroot_message.goma.counterz_file or None
-
-    goma = goma_util.Goma(chroot_message.goma.goma_dir,
-                          chroot_message.goma.goma_client_json,
-                          stage_name='BuildAPI',
-                          chromeos_goma_dir=chromeos_goma_dir,
-                          chroot_dir=path,
-                          goma_approach=goma_approach,
-                          log_dir=log_dir,
-                          stats_filename=stats_filename,
-                          counterz_filename=counterz_filename)
+  goma = ParseGomaConfig(chroot_message.goma, path) if parse_goma else None
 
   return Chroot(path=path, cache_dir=cache_dir, chrome_root=chrome_root,
                 env=env, goma=goma)
 
 
+def ParseGomaConfig(goma_message, chroot_path):
+  """Parse a goma config message."""
+  assert isinstance(goma_message, common_pb2.GomaConfig)
+
+  if not goma_message.goma_dir:
+    return None
+
+  # Parse the goma config.
+  chromeos_goma_dir = goma_message.chromeos_goma_dir or None
+  goma_approach = None
+  if goma_message.goma_approach == common_pb2.GomaConfig.RBE_PROD:
+    goma_approach = goma_util.GomaApproach('?prod', 'goma.chromium.org', True)
+  elif goma_message.goma_approach == common_pb2.GomaConfig.RBE_STAGING:
+    goma_approach = goma_util.GomaApproach('?staging',
+                                           'staging-goma.chromium.org', True)
+
+  log_dir = goma_message.log_dir.dir or None
+  if log_dir:
+    log_dir = os.path.join(chroot_path, log_dir.lstrip(os.sep))
+
+  stats_filename = goma_message.stats_file or None
+  counterz_filename = goma_message.counterz_file or None
+
+  return goma_util.Goma(goma_message.goma_dir,
+                        goma_message.goma_client_json,
+                        stage_name='BuildAPI',
+                        chromeos_goma_dir=chromeos_goma_dir,
+                        chroot_dir=chroot_path,
+                        goma_approach=goma_approach,
+                        log_dir=log_dir,
+                        stats_filename=stats_filename,
+                        counterz_filename=counterz_filename)
+
+
 def ParseBuildTarget(build_target_message):
   """Create a BuildTarget object from a build_target message.