Enable using custom java home for rbe_autoconfig (#267)

* allow overriding java home with attr

* enable setting java_home from env var

* fix build issue

* fix docs

* fail if JAVA_HOME is empty

* refactor java_home resolution and fix for env var case

* disable versbose

* modify sample .bazelrc file

* buildify

* addressing review comments

* addressing review comments
diff --git a/examples/remotebuildexecution/rbe_system_check/.bazelrc.sample b/examples/remotebuildexecution/rbe_system_check/.bazelrc.sample
index 02e10ed..bea5525 100644
--- a/examples/remotebuildexecution/rbe_system_check/.bazelrc.sample
+++ b/examples/remotebuildexecution/rbe_system_check/.bazelrc.sample
@@ -31,8 +31,8 @@
 # Toolchain/platform flags that work with rbe_autoconfig rule.
 # Note target name must be set to "rbe_default" to use these flags as is.
 build:remote --crosstool_top=@rbe_default//rbe_config_cc:toolchain
-build:remote --host_javabase=@rbe_default//config:jdk8
-build:remote --javabase=@rbe_default//config:jdk8
+build:remote --host_javabase=@rbe_default//config:jdk
+build:remote --javabase=@rbe_default//config:jdk
 build:remote --host_java_toolchain=@bazel_tools//tools/jdk:toolchain_hostjdk8
 build:remote --java_toolchain=@bazel_tools//tools/jdk:toolchain_hostjdk8
 build:remote --extra_execution_platforms=@rbe_default//config:platform
diff --git a/rules/BUILD.platform.tpl b/rules/BUILD.platform.tpl
index 7daa34c..aa12bb0 100644
--- a/rules/BUILD.platform.tpl
+++ b/rules/BUILD.platform.tpl
@@ -24,15 +24,9 @@
 )
 
 java_runtime(
-    name = "jdk8",
+    name = "jdk",
     srcs = [],
-    java_home = "/usr/lib/jvm/java-8-openjdk-amd64",
-)
-
-java_runtime(
-    name = "jdk10",
-    srcs = [],
-    java_home = "/usr/lib/jvm/zulu" + JDK_VERSION + "-linux_x64-allmodules",
+    java_home = "%{java_home}",
 )
 
 toolchain(
diff --git a/rules/get_java_home.sh.tpl b/rules/get_java_home.sh.tpl
new file mode 100644
index 0000000..7cd2593
--- /dev/null
+++ b/rules/get_java_home.sh.tpl
@@ -0,0 +1,23 @@
+#!/bin/bash
+#
+# Copyright 2017 The Bazel Authors. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+set -ex
+
+# This is a generated file that gets the value of the JAVA_HOME env
+# var in a docker image.
+
+echo $(docker inspect -f '{{range $i, $v := .Config.Env}}{{println $v}}{{end}}' %{image_name} | grep JAVA_HOME | cut -d'=' -f2)
+
diff --git a/rules/rbe_repo.bzl b/rules/rbe_repo.bzl
index 750b128..cbfb8a8 100644
--- a/rules/rbe_repo.bzl
+++ b/rules/rbe_repo.bzl
@@ -97,8 +97,8 @@
 
       bazel build ... \
                 --crosstool_top=//rbe-configs/bazel_{bazel_version}:toolchain \
-                --host_javabase=//rbe-configs/bazel_{bazel_version}/config:jdk8 \
-                --javabase=//rbe-configs/bazel_{bazel_version}/config:jdk8 \
+                --host_javabase=//rbe-configs/bazel_{bazel_version}/config:jdk \
+                --javabase=//rbe-configs/bazel_{bazel_version}/config:jdk \
                 --host_java_toolchain=@bazel_tools//tools/jdk:toolchain_hostjdk8 \
                 --java_toolchain=@bazel_tools//tools/jdk:toolchain_hostjdk8 \
                 --extra_execution_platforms=/rbe-configs/bazel_{bazel_version}/config:platform \
@@ -120,8 +120,8 @@
 
       bazel build ... \
                 --crosstool_top=@rbe_default//rbe_config_cc:toolchain \
-                --host_javabase=@rbe_default//config:jdk8 \
-                --javabase=@rbe_default//config:jdk8 \
+                --host_javabase=@rbe_default//config:jdk \
+                --javabase=@rbe_default//config:jdk \
                 --host_java_toolchain=@bazel_tools//tools/jdk:toolchain_hostjdk8 \
                 --java_toolchain=@bazel_tools//tools/jdk:toolchain_hostjdk8 \
                 --extra_execution_platforms=@rbe_default//config:platform \
@@ -198,7 +198,7 @@
     "@bazel_tools//platforms:linux",
     "@bazel_tools//platforms:x86_64",
 ]
-_VERBOSE = False
+_VERBOSE = True
 
 def _impl(ctx):
     """Core implementation of _rbe_autoconfig repository rule."""
@@ -241,6 +241,10 @@
         bazel_version = ctx.attr.bazel_version
         bazel_rc_version = ctx.attr.bazel_rc_version
 
+    # Get the value of JAVA_HOME to set in the produced
+    # java_runtime
+    java_home = _get_java_home(ctx, image_name)
+
     # run the container and extract the autoconf directory
     _run_and_extract(
         ctx,
@@ -258,6 +262,7 @@
         ctx,
         bazel_version = bazel_version,
         image_name = image_name,
+        java_home = java_home,
         name = name,
     )
 
@@ -299,6 +304,32 @@
     _print_exec_results("pull image", result, fail_on_error = True)
     print("Image pulled.")
 
+# Gets the value of java_home either from attr or
+# by running docker run image_name printenv JAVA_HOME.
+def _get_java_home(ctx, image_name):
+    if ctx.attr.java_home:
+        return ctx.attr.java_home
+
+    # Create the template to run
+    template = ctx.path(Label("@bazel_toolchains//rules:get_java_home.sh.tpl"))
+    ctx.template(
+        "get_java_home.sh",
+        template,
+        {
+            "%{image_name}": image_name,
+        },
+        True,
+    )
+
+    # run get_java_home.sh
+    result = ctx.execute(["./get_java_home.sh"])
+    _print_exec_results("get java_home", result, fail_on_error = True)
+    java_home = result.stdout.splitlines()[0]
+    if java_home == "":
+        fail("Could not find JAVA_HOME in the container and one was not " +
+             "passed to rbe_autoconfig rule.")
+    return java_home
+
 # Creates file "container/run_in_container.sh" which can be mounted onto container
 # to run the commands to install bazel, run it and create the output tar
 def _create_docker_cmd(
@@ -450,6 +481,7 @@
         ctx,
         bazel_version,
         image_name,
+        java_home,
         name):
     toolchain_target = "@" + name + "//" + _RBE_CONFIG_DIR
     if ctx.attr.output_base:
@@ -469,6 +501,7 @@
         {
             "%{exec_compatible_with}": exec_compatible_with,
             "%{image_name}": image_name,
+            "%{java_home}": java_home,
             "%{target_compatible_with}": target_compatible_with,
             "%{toolchain}": toolchain_target,
         },
@@ -537,6 +570,13 @@
             doc = ("If set to False the flag --all_incompatible_changes will " +
                    "be used when generating the toolchain configs."),
         ),
+        "java_home": attr.string(
+            doc = ("Optional. The location of java_home in the container. " +
+                   "For example, '/usr/lib/jvm/java-8-openjdk-amd64'. If " +
+                   "not set, the rule will attempt to read the JAVA_HOME env " +
+                   "var from the container. If that is not set the rule will " +
+                   "fail."),
+        ),
         "output_base": attr.string(
             doc = ("Optional. The directory (under the project root) where the " +
                    "produced toolchain configs will be copied to."),
@@ -599,8 +639,9 @@
         bazel_rc = None,
         config_dir = None,
         digest = None,
-        env = clang_env(),
+        env = None,
         exec_compatible_with = None,
+        java_home = None,
         output_base = None,
         revision = None,
         registry = None,
@@ -626,6 +667,10 @@
       digest: Optional. The digest of the image to pull. Should only be set if
           a custom container is required.
           Must be set together with registry and repository.
+      java_home: Optional. The location of java_home in the container. For
+          example , '/usr/lib/jvm/java-8-openjdk-amd64'. If not set, the rule
+          will attempt to read the JAVA_HOME env var from the container.
+          If that is not set the rule will fail.
       output_base: Optional. The directory (under the project root) where the
           produced toolchain configs will be copied to.
       config_dir: Optional. Subdirectory where configs will be copied to.
@@ -665,6 +710,8 @@
         if not revision or revision == "latest":
             revision = RBE_UBUNTU16_04_LATEST
         digest = public_rbe_ubuntu16_04_sha256s().get(revision, None)
+        if not env:
+            env = clang_env()
     if not digest:
         fail(("Could not find a valid digest for revision %s, " +
               "please make sure it is declared in " +
@@ -677,6 +724,7 @@
         digest = digest,
         env = env,
         exec_compatible_with = exec_compatible_with,
+        java_home = java_home,
         output_base = output_base,
         registry = registry,
         repository = repository,