Add experimental Debian8 based Bazel container. (#169)

* Factor out common components for Bazel layer.

* Add experimental Debian8 based Bazel container.
diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml
index 38ecfb8..394c51d 100644
--- a/.bazelci/presubmit.yml
+++ b/.bazelci/presubmit.yml
@@ -30,6 +30,7 @@
     - "//container/experimental/rbe-debian9:toolchain-test"
     - "//container/ubuntu16_04/builders/bazel:bazel_0.16.1-test"
     - "//container/ubuntu16_04/builders/bazel_docker_gcloud:bazel_0.16.1_docker_gcloud-test"
+    - "//container/debian8/builders/bazel:bazel_0.16.1-test"
     test_flags:
     - "--test_output=errors"
     - "--verbose_failures"
diff --git a/container/build.py b/container/build.py
index 21ca837..d03d3a7 100644
--- a/container/build.py
+++ b/container/build.py
@@ -84,7 +84,7 @@
 
 SUPPORTED_TYPES = [
     "rbe-debian8", "rbe-debian9", "rbe-ubuntu16_04", "ubuntu16_04-bazel",
-    "ubuntu16_04-bazel-docker-gcloud"
+    "ubuntu16_04-bazel-docker-gcloud", "debian8-bazel"
 ]
 
 # File passed in -m must include the following 3 maps
@@ -100,6 +100,7 @@
     "ubuntu16_04-bazel": "container/ubuntu16_04/builders/bazel",
     "ubuntu16_04-bazel-docker-gcloud":
         "container/ubuntu16_04/builders/bazel_docker_gcloud",
+    "debian8-bazel": "container/debian8/builders/bazel",
 }
 
 # Map to store all supported container type and the name of target to build it.
@@ -110,6 +111,7 @@
     "ubuntu16_04-bazel": "bazel_{}".format(LATEST_BAZEL_VERSION),
     "ubuntu16_04-bazel-docker-gcloud":
         "bazel_{}_docker_gcloud".format(LATEST_BAZEL_VERSION),
+    "debian8-bazel": "bazel_{}".format(LATEST_BAZEL_VERSION),
 }
 
 # Map to store all supported container type and the name of target to build it.
@@ -124,6 +126,8 @@
         "bazel_{}-packages.tar".format(LATEST_BAZEL_VERSION),
     "ubuntu16_04-bazel-docker-gcloud":
         "bazel_{}_docker_gcloud-packages.tar".format(LATEST_BAZEL_VERSION),
+    "debian8-bazel":
+         "bazel_{}-packages.tar".format(LATEST_BAZEL_VERSION),
 }
 
 # =========== ENDING HERE ===========
diff --git a/container/common/bazel/BUILD b/container/common/bazel/BUILD
new file mode 100644
index 0000000..263db40
--- /dev/null
+++ b/container/common/bazel/BUILD
@@ -0,0 +1,19 @@
+# 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.
+
+licenses(["notice"])  # Apache 2.0
+
+package(default_visibility = ["//visibility:public"])
+
+exports_files(glob(["**"]))
diff --git a/container/ubuntu16_04/layers/bazel/bazel.bazelrc.tpl b/container/common/bazel/bazel.bazelrc.tpl
similarity index 81%
rename from container/ubuntu16_04/layers/bazel/bazel.bazelrc.tpl
rename to container/common/bazel/bazel.bazelrc.tpl
index e84faed..76ba7f6 100644
--- a/container/ubuntu16_04/layers/bazel/bazel.bazelrc.tpl
+++ b/container/common/bazel/bazel.bazelrc.tpl
@@ -27,5 +27,5 @@
 build:msan --linkopt=-fsanitize=memory
 build:msan --cxxopt=--stdlib=libc++
 build:msan --copt=-fsanitize-memory-track-origins
-build:msan --host_crosstool_top=@bazel_toolchains//configs/ubuntu16_04_clang/1.0/bazel_{_BAZEL_CONFIG_VERSION}/default:toolchain
-build:msan --crosstool_top=@bazel_toolchains//configs/ubuntu16_04_clang/1.0/bazel_{_BAZEL_CONFIG_VERSION}/msan:toolchain
+build:msan --host_crosstool_top=@bazel_toolchains//configs/{_CONFIG_BASE}/bazel_{_BAZEL_CONFIG_VERSION}/default:toolchain
+build:msan --crosstool_top=@bazel_toolchains//configs/{_CONFIG_BASE}/bazel_{_BAZEL_CONFIG_VERSION}/msan:toolchain
diff --git a/container/ubuntu16_04/layers/bazel/bazel.yaml.tpl b/container/common/bazel/bazel.yaml.tpl
similarity index 81%
rename from container/ubuntu16_04/layers/bazel/bazel.yaml.tpl
rename to container/common/bazel/bazel.yaml.tpl
index 456d58f..c64ae0e 100644
--- a/container/ubuntu16_04/layers/bazel/bazel.yaml.tpl
+++ b/container/common/bazel/bazel.yaml.tpl
@@ -14,4 +14,4 @@
 fileContentTests:
 - name: 'bazelrc-content'
   path: '/etc/bazel.bazelrc'
-  expectedContents: ['.*--host_crosstool_top=@bazel_toolchains//configs/ubuntu16_04_clang/1.0/bazel_{_BAZEL_CONFIG_VERSION}/default:toolchain.*']
+  expectedContents: ['.*--host_crosstool_top=@bazel_toolchains//configs/{_CONFIG_BASE}/bazel_{_BAZEL_CONFIG_VERSION}/default:toolchain.*']
diff --git a/container/ubuntu16_04/layers/bazel/bazel_tools.yaml b/container/common/bazel/bazel_tools.yaml
similarity index 100%
rename from container/ubuntu16_04/layers/bazel/bazel_tools.yaml
rename to container/common/bazel/bazel_tools.yaml
diff --git a/container/ubuntu16_04/layers/bazel/version.bzl b/container/common/bazel/version.bzl
similarity index 100%
rename from container/ubuntu16_04/layers/bazel/version.bzl
rename to container/common/bazel/version.bzl
diff --git a/container/debian8/builders/bazel/BUILD b/container/debian8/builders/bazel/BUILD
new file mode 100644
index 0000000..462723a
--- /dev/null
+++ b/container/debian8/builders/bazel/BUILD
@@ -0,0 +1,65 @@
+# 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.
+
+licenses(["notice"])  # Apache 2.0
+
+package(default_visibility = ["//visibility:public"])
+
+load("//container/rules:docker_toolchains.bzl", "toolchain_container")
+load("@io_bazel_rules_docker//contrib:test.bzl", "container_test")
+load("//container/common/bazel:version.bzl", "BAZEL_VERSION_SHA256S")
+
+# Generate the Bazel container for all supported versions using the
+# corresponding Bazel language_tool_layer targets.
+[toolchain_container(
+    name = "bazel_%s" % bazel_version,
+    base = "@debian8//image",
+    cmd = [
+        "/bin/sh",
+        "-c",
+        "/bin/bash",
+    ],
+    # TODO(xingao) Fix this. We should not remove /etc/ssl/certs/java/cacerts
+    # file in the java-ltl, but instead, archive it for future container
+    # reproduction.
+    installation_cleanup_commands = "update-ca-certificates -f",
+    language_layers = [
+        "//container/debian8/layers/clang:clang-ltl",
+        "//container/debian8/layers/java:java-ltl",
+        "//container/debian8/layers/java:java10-ltl",
+        "//container/debian8/layers/python:python-ltl",
+        "//container/debian8/layers/bazel:bazel-tools",
+        "//container/debian8/layers/bazel:bazel_%s-ltl" % bazel_version,
+        "//container/debian8/layers/git:git-ltl",
+        "//container/debian8/layers/wget:wget-ltl",
+    ],
+) for bazel_version, _ in BAZEL_VERSION_SHA256S.items()]
+
+# container_test targets for all supported Bazel versions.
+[container_test(
+    name = "bazel_%s-test" % bazel_version,
+    configs = [
+        ":container.yaml",
+        "//container/common:clang.yaml",
+        "//container/common:git.yaml",
+        "//container/common:java.yaml",
+        "//container/common:python2.yaml",
+        "//container/common:wget.yaml",
+        "//container/common/bazel:bazel_tools.yaml",
+        "//container/debian8:debian8.yaml",
+        "//container/debian8/layers/bazel:bazel_%s.yaml" % bazel_version,
+    ],
+    image = ":bazel_%s" % bazel_version,
+    verbose = True,
+) for bazel_version, _ in BAZEL_VERSION_SHA256S.items()]
diff --git a/container/debian8/builders/bazel/container.yaml b/container/debian8/builders/bazel/container.yaml
new file mode 100644
index 0000000..c27a43a
--- /dev/null
+++ b/container/debian8/builders/bazel/container.yaml
@@ -0,0 +1,6 @@
+schemaVersion: "2.0.0"
+
+metadataTest:
+  env:
+    - key: 'PATH'
+      value: '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/python3.6/bin'
diff --git a/container/debian8/debian8.bzl b/container/debian8/debian8.bzl
new file mode 100644
index 0000000..54aef89
--- /dev/null
+++ b/container/debian8/debian8.bzl
@@ -0,0 +1,3 @@
+# <config_type>/<config_version>
+# Used in substitution in bazel.yaml.tpl and bazel.bazelrc.tpl
+CONFIG_BASE = "debian8_clang/0.3.0"
diff --git a/container/debian8/layers/bazel/BUILD b/container/debian8/layers/bazel/BUILD
new file mode 100644
index 0000000..1d9dc57
--- /dev/null
+++ b/container/debian8/layers/bazel/BUILD
@@ -0,0 +1,95 @@
+# 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.
+
+licenses(["notice"])  # Apache 2.0
+
+package(default_visibility = ["//visibility:public"])
+
+load("//container/rules:docker_toolchains.bzl", "language_tool_layer")
+load("//container/rules:docker_toolchains.bzl", "toolchain_container")
+load("@base_images_docker//util:run.bzl", "container_run_and_extract")
+load(
+    "//container/common/bazel:version.bzl",
+    "BAZEL_VERSION_SHA256S",
+    "BAZEL_VERSION_TO_CONFIG_VERSION",
+)
+load("//container/debian8:debian8.bzl", "CONFIG_BASE")
+
+exports_files(glob(["*.yaml"]))
+
+# Tools required by Bazel. Note that we do not add a CC complier here as we
+# will include that in the final container.
+language_tool_layer(
+    name = "bazel-tools",
+    base = "@debian8//image",
+    packages = [
+        "bash-completion",
+        "binutils",
+        "patch",
+        "unzip",
+        "zip",
+    ],
+)
+
+# Generate bazel.bazelrc files for all supported versions.
+[genrule(
+    name = "generate_bazel_%s_bazelrc" % bazel_version,
+    srcs = ["//container/common/bazel:bazel.bazelrc.tpl"],
+    outs = ["bazel-%s.bazelrc" % bazel_version],
+    cmd = "sed 's|{_BAZEL_CONFIG_VERSION}|" + BAZEL_VERSION_TO_CONFIG_VERSION.get(bazel_version) + "|; " +
+          "s|{_CONFIG_BASE}|" + CONFIG_BASE + "|' <$< >$@",
+) for bazel_version, _ in BAZEL_VERSION_SHA256S.items()]
+
+# Create image with Bazel installer for all supported versions.
+[toolchain_container(
+    name = "bazel_%s_installer_image" % bazel_version,
+    base = "@debian8//image",
+    files = [
+        "@bazel_%s_installer//file" % bazel_version.replace(".", ""),
+        ":bazel-%s.bazelrc" % bazel_version,
+    ],
+    packages = [
+        "unzip",
+    ],
+) for bazel_version, _ in BAZEL_VERSION_SHA256S.items()]
+
+# Extract Bazel binaries for all supported versions.
+[container_run_and_extract(
+    name = "bazel_%s_tar" % bazel_version,
+    commands = [
+        "mv /bazel-%s.bazelrc /etc/bazel.bazelrc" % bazel_version,
+        "chmod +x /bazel-%s-installer-linux-x86_64.sh" % bazel_version,
+        "/bazel-%s-installer-linux-x86_64.sh" % bazel_version,
+        "tar cvf /bazel.tar --mtime='1970-01-01' /usr/local/bin/bazel /usr/local/lib/bazel/* /etc/bazel.bazelrc",
+    ],
+    extract_file = "/bazel.tar",
+    image = ":bazel_%s_installer_image.tar" % bazel_version,
+) for bazel_version, _ in BAZEL_VERSION_SHA256S.items()]
+
+# Language_tool_layer targets for all supported Bazel versions.
+[language_tool_layer(
+    name = "bazel_%s-ltl" % bazel_version,
+    base = "@debian8//image",
+    tars = [":bazel_%s_tar" % bazel_version],
+) for bazel_version, _ in BAZEL_VERSION_SHA256S.items()]
+
+# Generate Bazel container testing yaml files with all supported Bazel versions.
+[genrule(
+    name = "generate_bazel_%s_yaml" % bazel_version,
+    srcs = ["//container/common/bazel:bazel.yaml.tpl"],
+    outs = ["bazel_%s.yaml" % bazel_version],
+    cmd = "sed 's|{_BAZEL_VERSION}|" + bazel_version + "|; " +
+          "s|{_BAZEL_CONFIG_VERSION}|" + BAZEL_VERSION_TO_CONFIG_VERSION.get(bazel_version) + "|; " +
+          "s|{_CONFIG_BASE}|" + CONFIG_BASE + "|' <$< >$@",
+) for bazel_version, _ in BAZEL_VERSION_SHA256S.items()]
diff --git a/container/debian8/layers/git/BUILD b/container/debian8/layers/git/BUILD
new file mode 100644
index 0000000..3cc805b
--- /dev/null
+++ b/container/debian8/layers/git/BUILD
@@ -0,0 +1,27 @@
+# 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.
+
+licenses(["notice"])  # Apache 2.0
+
+package(default_visibility = ["//visibility:public"])
+
+load("//container/rules:docker_toolchains.bzl", "language_tool_layer")
+
+language_tool_layer(
+    name = "git-ltl",
+    base = "@debian8//image",
+    packages = [
+        "git",
+    ],
+)
diff --git a/container/debian8/layers/wget/BUILD b/container/debian8/layers/wget/BUILD
new file mode 100644
index 0000000..7d99e8c
--- /dev/null
+++ b/container/debian8/layers/wget/BUILD
@@ -0,0 +1,28 @@
+# 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.
+
+licenses(["notice"])  # Apache 2.0
+
+package(default_visibility = ["//visibility:public"])
+
+load("//container/rules:docker_toolchains.bzl", "language_tool_layer")
+
+language_tool_layer(
+    name = "wget-ltl",
+    base = "@debian8//image",
+    packages = [
+        "ca-certificates",
+        "wget",
+    ],
+)
diff --git a/container/ubuntu16_04/builders/bazel/BUILD b/container/ubuntu16_04/builders/bazel/BUILD
index 63fd334..ced48b6 100644
--- a/container/ubuntu16_04/builders/bazel/BUILD
+++ b/container/ubuntu16_04/builders/bazel/BUILD
@@ -18,7 +18,7 @@
 
 load("//container/rules:docker_toolchains.bzl", "toolchain_container")
 load("@io_bazel_rules_docker//contrib:test.bzl", "container_test")
-load("//container/ubuntu16_04/layers/bazel:version.bzl", "BAZEL_VERSION_SHA256S")
+load("//container/common/bazel:version.bzl", "BAZEL_VERSION_SHA256S")
 
 # Generate the Bazel container for all supported versions using the
 # corresponding Bazel language_tool_layer targets.
@@ -56,9 +56,9 @@
         "//container/common:java.yaml",
         "//container/common:python2.yaml",
         "//container/common:wget.yaml",
+        "//container/common/bazel:bazel_tools.yaml",
         "//container/ubuntu16_04:ubuntu16_04.yaml",
         "//container/ubuntu16_04/layers/bazel:bazel_%s.yaml" % bazel_version,
-        "//container/ubuntu16_04/layers/bazel:bazel_tools.yaml",
     ],
     image = ":bazel_%s" % bazel_version,
     verbose = True,
diff --git a/container/ubuntu16_04/builders/bazel_docker_gcloud/BUILD b/container/ubuntu16_04/builders/bazel_docker_gcloud/BUILD
index db218e6..414ea07 100644
--- a/container/ubuntu16_04/builders/bazel_docker_gcloud/BUILD
+++ b/container/ubuntu16_04/builders/bazel_docker_gcloud/BUILD
@@ -18,7 +18,7 @@
 
 load("//container/rules:docker_toolchains.bzl", "toolchain_container")
 load("@io_bazel_rules_docker//contrib:test.bzl", "container_test")
-load("//container/ubuntu16_04/layers/bazel:version.bzl", "BAZEL_VERSION_SHA256S")
+load("//container/common/bazel:version.bzl", "BAZEL_VERSION_SHA256S")
 
 # Generate the container with Bazel and Docker for all supported versions of
 # Bazel.
@@ -58,9 +58,9 @@
         "//container/common:java.yaml",
         "//container/common:python2.yaml",
         "//container/common:wget.yaml",
+        "//container/common/bazel:bazel_tools.yaml",
         "//container/ubuntu16_04:ubuntu16_04.yaml",
         "//container/ubuntu16_04/layers/bazel:bazel_%s.yaml" % bazel_version,
-        "//container/ubuntu16_04/layers/bazel:bazel_tools.yaml",
         "//container/ubuntu16_04/layers/docker-17.12.0:docker-17.12.0.yaml",
         "//container/ubuntu16_04/layers/gcloud:gcloud.yaml",
     ],
diff --git a/container/ubuntu16_04/layers/bazel/BUILD b/container/ubuntu16_04/layers/bazel/BUILD
index 3880450..70c4a29 100644
--- a/container/ubuntu16_04/layers/bazel/BUILD
+++ b/container/ubuntu16_04/layers/bazel/BUILD
@@ -20,10 +20,11 @@
 load("//container/rules:docker_toolchains.bzl", "toolchain_container")
 load("@base_images_docker//util:run.bzl", "container_run_and_extract")
 load(
-    "//container/ubuntu16_04/layers/bazel:version.bzl",
+    "//container/common/bazel:version.bzl",
     "BAZEL_VERSION_SHA256S",
     "BAZEL_VERSION_TO_CONFIG_VERSION",
 )
+load("//container/ubuntu16_04:ubuntu16_04.bzl", "CONFIG_BASE")
 
 exports_files(glob(["*.yaml"]))
 
@@ -44,9 +45,10 @@
 # Generate bazel.bazelrc files for all supported versions.
 [genrule(
     name = "generate_bazel_%s_bazelrc" % bazel_version,
-    srcs = ["bazel.bazelrc.tpl"],
+    srcs = ["//container/common/bazel:bazel.bazelrc.tpl"],
     outs = ["bazel-%s.bazelrc" % bazel_version],
-    cmd = "sed 's|{_BAZEL_CONFIG_VERSION}|" + BAZEL_VERSION_TO_CONFIG_VERSION.get(bazel_version) + "|' <$< >$@",
+    cmd = "sed 's|{_BAZEL_CONFIG_VERSION}|" + BAZEL_VERSION_TO_CONFIG_VERSION.get(bazel_version) + "|; " +
+          "s|{_CONFIG_BASE}|" + CONFIG_BASE + "|' <$< >$@",
 ) for bazel_version, _ in BAZEL_VERSION_SHA256S.items()]
 
 # Create image with Bazel installer for all supported versions.
@@ -85,8 +87,9 @@
 # Generate Bazel container testing yaml files with all supported Bazel versions.
 [genrule(
     name = "generate_bazel_%s_yaml" % bazel_version,
-    srcs = ["bazel.yaml.tpl"],
+    srcs = ["//container/common/bazel:bazel.yaml.tpl"],
     outs = ["bazel_%s.yaml" % bazel_version],
     cmd = "sed 's|{_BAZEL_VERSION}|" + bazel_version + "|; " +
-          "s|{_BAZEL_CONFIG_VERSION}|" + BAZEL_VERSION_TO_CONFIG_VERSION.get(bazel_version) + "|' <$< >$@",
+          "s|{_BAZEL_CONFIG_VERSION}|" + BAZEL_VERSION_TO_CONFIG_VERSION.get(bazel_version) + "|; " +
+          "s|{_CONFIG_BASE}|" + CONFIG_BASE + "|' <$< >$@",
 ) for bazel_version, _ in BAZEL_VERSION_SHA256S.items()]
diff --git a/container/ubuntu16_04/ubuntu16_04.bzl b/container/ubuntu16_04/ubuntu16_04.bzl
new file mode 100644
index 0000000..f72864b
--- /dev/null
+++ b/container/ubuntu16_04/ubuntu16_04.bzl
@@ -0,0 +1,3 @@
+# <config_type>/<config_version>
+# Used in substitution in bazel.yaml.tpl and bazel.bazelrc.tpl
+CONFIG_BASE = "ubuntu16_04_clang/1.0"
diff --git a/repositories/repositories.bzl b/repositories/repositories.bzl
index 26d66f4..e689bc0 100644
--- a/repositories/repositories.bzl
+++ b/repositories/repositories.bzl
@@ -42,7 +42,7 @@
     "OPENJDK_SRC_SHA256",
 )
 load(
-    "//container/ubuntu16_04/layers/bazel:version.bzl",
+    "//container/common/bazel:version.bzl",
     "BAZEL_VERSION_SHA256S",
 )