enter_chroot: Mount goma into chroot.

This CL adds an option to mount goma directory.
Also adds another option to mount service account json
file, which will be used on bots to run goma.

BUG=chromium:279618
TEST=Ran "cros_sdk --goma_dir $GOMA", then goma_ctl.py restart works.
     Ran "cros_sdk --goma_client_json $JSON_FILE".
     Note the options are proxied by another CL for chromite.

Change-Id: I6a024a5d6a427863a558093a67387e5c4d78f7ac
Reviewed-on: https://chromium-review.googlesource.com/448479
Commit-Ready: Hidehiko Abe <hidehiko@chromium.org>
Tested-by: Hidehiko Abe <hidehiko@chromium.org>
Reviewed-by: Don Garrett <dgarrett@chromium.org>
diff --git a/sdk_lib/enter_chroot.sh b/sdk_lib/enter_chroot.sh
index 65c2603..7fedfc1 100755
--- a/sdk_lib/enter_chroot.sh
+++ b/sdk_lib/enter_chroot.sh
@@ -28,6 +28,8 @@
 DEFINE_string workspace_root "" \
   "The root of your workspace."
 DEFINE_string cache_dir "" "Directory to use for caching."
+DEFINE_string goma_dir "" "Goma installed directory."
+DEFINE_string goma_client_json "" "Service account json file for goma."
 
 DEFINE_boolean ssh_agent $FLAGS_TRUE "Import ssh agent."
 DEFINE_boolean early_make_chroot $FLAGS_FALSE \
@@ -429,6 +431,26 @@
       setup_mount "${FLAGS_workspace_root}" --bind "${WORKSPACE_DIR}"
     fi
 
+    if [[ -n "${FLAGS_goma_dir}" ]]; then
+      debug "Mounting goma"
+      # $HOME/goma is the default directory for goma.
+      # It is used by goma if GOMA_DIR is not provide.
+      setup_mount "${FLAGS_goma_dir}" --bind "/home/${SUDO_USER}/goma"
+    fi
+
+    if [[ -n "${FLAGS_goma_client_json}" ]]; then
+      debug "Mounting service-account-goma-client.json"
+      local dest_path="/creds/service_accounts/service-account-goma-client.json"
+      # setup_mount assumes the target is a directory by default.
+      # So here, touch the file in advance.
+      local mounted_path="${MOUNTED_PATH}${dest_path}"
+      mkdir -p $(dirname "${mounted_path}")
+      touch "${mounted_path}"
+      # Note: Original UID:GUI and permission should be inherited even after
+      # mounting.
+      setup_mount "${FLAGS_goma_client_json}" --bind "${dest_path}"
+    fi
+
     # Mount additional directories as specified in .local_mounts file.
     local local_mounts="${FLAGS_trunk}/src/scripts/.local_mounts"
     if [[ -f ${local_mounts} ]]; then