Auto conf for project (#2) (#238)

* Auto conf for project (#2)

* allow using the current project for autoconfig

* provide mount_project attr to enable users to mount a local path

* fixing tests by passing explicitly empty list of run flags

* fix some of the issues with this PR

* still need to fix:
- Send upstream PR to use docker toolchain in base images docker
- verify why debian-jessie-bajul-head-autoconfig_test is failing

* fix failed merge

* addressing review comments

* addressing more review comments

* update base images docker pin

* fixing merge issue
diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml
index 8bb615f..38ec123 100644
--- a/.bazelci/presubmit.yml
+++ b/.bazelci/presubmit.yml
@@ -11,6 +11,7 @@
     - "//tests/config:ubuntu-xenial-custom-bazel-rc-version-autoconfig_test"
     - "//tests/config:debian8_clang_autoconfig_test"
     - "//tests/config:external-repo-autoconfig_test"
+    - "//tests/config:this-project-repo-autoconfig"
     - "//configs/debian8_clang:default-debian8-clang-0.4.0-bazel_0.18.0-autoconfig_test"
     - "//configs/debian8_clang:msan-debian8-clang-0.4.0-bazel_0.18.0-autoconfig_test"
     - "//configs/ubuntu16_04_clang:default-ubuntu16_04-clang-1.1-bazel_0.18.0-autoconfig_test"
diff --git a/container/debian8/layers/bazel/BUILD b/container/debian8/layers/bazel/BUILD
index db0da42..67478ff 100644
--- a/container/debian8/layers/bazel/BUILD
+++ b/container/debian8/layers/bazel/BUILD
@@ -65,5 +65,5 @@
 [language_tool_layer(
     name = "bazel_%s-ltl" % bazel_version,
     base = "@debian8//image",
-    tars = [":bazel_%s_tar" % bazel_version],
+    tars = [":bazel_%s_tar/bazel.tar" % bazel_version],
 ) for bazel_version, _ in BAZEL_VERSION_SHA256S.items()]
diff --git a/container/debian8/layers/python/BUILD b/container/debian8/layers/python/BUILD
index 2b6f0c3..9b72f44 100644
--- a/container/debian8/layers/python/BUILD
+++ b/container/debian8/layers/python/BUILD
@@ -44,7 +44,7 @@
         "/opt/python3.6/bin/pip3": "/opt/python3.6/bin/pip3.6",
         "/opt/python3.6/bin/easy_install3": "/opt/python3.6/bin/easy_install-3.6",
     },
-    tars = ["//third_party/python:debian8_tar"],
+    tars = ["//third_party/python:debian8_tar/python.tar"],
 )
 
 language_tool_layer(
diff --git a/container/ubuntu14_04/layers/bazel/BUILD b/container/ubuntu14_04/layers/bazel/BUILD
index e5a6af0..36bdac7 100644
--- a/container/ubuntu14_04/layers/bazel/BUILD
+++ b/container/ubuntu14_04/layers/bazel/BUILD
@@ -65,5 +65,5 @@
 [language_tool_layer(
     name = "bazel_%s-ltl" % bazel_version,
     base = "@trusty//image",
-    tars = [":bazel_%s_tar" % bazel_version],
+    tars = [":bazel_%s_tar/bazel.tar" % bazel_version],
 ) for bazel_version, _ in BAZEL_VERSION_SHA256S.items()]
diff --git a/container/ubuntu16_04/layers/bazel/BUILD b/container/ubuntu16_04/layers/bazel/BUILD
index 81bc1f5..600a792 100644
--- a/container/ubuntu16_04/layers/bazel/BUILD
+++ b/container/ubuntu16_04/layers/bazel/BUILD
@@ -64,5 +64,5 @@
 [language_tool_layer(
     name = "bazel_%s-ltl" % bazel_version,
     base = "@ubuntu16_04//image",
-    tars = [":bazel_%s_tar" % bazel_version],
+    tars = [":bazel_%s_tar/bazel.tar" % bazel_version],
 ) for bazel_version, _ in BAZEL_VERSION_SHA256S.items()]
diff --git a/container/ubuntu16_04/layers/python/BUILD b/container/ubuntu16_04/layers/python/BUILD
index 1afba8b..a015af9 100644
--- a/container/ubuntu16_04/layers/python/BUILD
+++ b/container/ubuntu16_04/layers/python/BUILD
@@ -44,7 +44,7 @@
         "/opt/python3.6/bin/pip3": "/opt/python3.6/bin/pip3.6",
         "/opt/python3.6/bin/easy_install3": "/opt/python3.6/bin/easy_install-3.6",
     },
-    tars = ["//third_party/python:ubuntu16_04_tar"],
+    tars = ["//third_party/python:ubuntu16_04_tar/python.tar"],
 )
 
 language_tool_layer(
diff --git a/repositories/repositories.bzl b/repositories/repositories.bzl
index 27b62e7..0647915 100644
--- a/repositories/repositories.bzl
+++ b/repositories/repositories.bzl
@@ -83,9 +83,9 @@
     if "base_images_docker" not in excludes:
         http_archive(
             name = "base_images_docker",
-            sha256 = "1bbc88e842c36d91074949b5f0eb49b55f7f71d81f2991e806f82342bfa01096",
-            strip_prefix = "base-images-docker-e0210a7d9dcff81f8c87019105bd6eb66e3df390",
-            urls = ["https://github.com/GoogleCloudPlatform/base-images-docker/archive/e0210a7d9dcff81f8c87019105bd6eb66e3df390.tar.gz"],
+            sha256 = "a97768efdba8ecfa4c2e71499b1ef8fbe95a22044cb26ed5ca062f91a2afcfb1",
+            strip_prefix = "base-images-docker-4f13bab1c063aa7d7388f62a6807317e252bcae0",
+            urls = ["https://github.com/GoogleContainerTools/base-images-docker/archive/4f13bab1c063aa7d7388f62a6807317e252bcae0.tar.gz"],
         )
 
     if "distroless" not in excludes:
diff --git a/rules/docker_config.bzl b/rules/docker_config.bzl
index 3784a9b..9ab45f5 100644
--- a/rules/docker_config.bzl
+++ b/rules/docker_config.bzl
@@ -159,12 +159,21 @@
     if ctx.attr.git_repo:
         clone_repo_cmd = ("cd " + bazel_config_dir + " && git clone " +
                           ctx.attr.git_repo + " " + project_repo_dir)
+
+    repo_dir = bazel_config_dir + "/" + project_repo_dir
     if ctx.attr.repo_pkg_tar:
         # if package tar was used then the command should expand it
-        repo_dir = bazel_config_dir + "/" + project_repo_dir
         clone_repo_cmd = ("mkdir %s && tar -xf /%s -C %s " %
                           (repo_dir, ctx.file.repo_pkg_tar.basename, repo_dir))
 
+    # if mount_project was selected, we'll mount it using docker_run_flags
+    docker_run_flags = [""]
+    if ctx.attr.mount_project:
+        mount_project = ctx.attr.mount_project
+        mount_project = ctx.expand_make_variables("mount_project", mount_project, {})
+        target = mount_project + ":" + repo_dir
+        docker_run_flags = ["-v", target]
+
     # Command to install custom Bazel version (if requested)
     install_bazel_cmd = "cd ."
     if ctx.attr.use_bazel_head:
@@ -249,7 +258,7 @@
 
     image_tar = ctx.new_file(name + ".tar")
 
-    # TODO(nlopezgi): fix upsream issue that output_executable is required
+    # TODO(nlopezgi): fix upstream issue that output_executable is required
     load_image_sh_file = ctx.new_file(name + "load.sh")
     _container.image.implementation(
         ctx,
@@ -268,20 +277,28 @@
         ("if [ -f /" + outputs_tar + " ]; " +
          "then tar -rf /extract.tar /" + outputs_tar + "; fi"),
     ]
-    print("\n== Docker autoconfig will run. ==\n" +
-          "To debug any errors run:\n" +
-          "> docker run -d <image_id> bash\n" +
-          "Where <image_id> is the image id printed out by the " +
-          ctx.attr.name + "_extract" + ".tar rule.\n" +
-          "Then run:\n>/" + install_sh.basename +
-          "\nfrom inside the container.")
+
+    print(("\n== Docker autoconfig will run. ==\n" +
+           "To debug any errors run:\n" +
+           "> docker run -it {mount_flags} <image_id> bash\n" +
+           "Where <image_id> is the image id printed out by the " +
+           "{name}_extract.tar rule.\n" +
+           "Then run:\n>/ {run_cmd}\n" +
+           "from inside the container.").format(
+        mount_flags = " ".join(docker_run_flags),
+        name = ctx.attr.name,
+        run_cmd = install_sh.basename,
+    ))
+
     extract_tar_file = ctx.new_file(name + "_extract.tar")
     _extract.implementation(
         ctx,
         name = ctx.attr.name + "_extract",
         image = image_tar,
+        docker_run_flags = docker_run_flags,
         commands = commands,
         extract_file = "/extract.tar",
+        script_file = ctx.actions.declare_file(ctx.attr.name + ".build"),
         output_file = extract_tar_file,
     )
 
@@ -296,6 +313,7 @@
 docker_toolchain_autoconfig_ = rule(
     attrs = _container.image.attrs + {
         "config_repos": attr.string_list(default = ["local_config_cc"]),
+        "mount_project": attr.string(),
         "use_default_project": attr.bool(default = False),
         "git_repo": attr.string(),
         "repo_pkg_tar": attr.label(allow_files = tar_filetype, single_file = True),
@@ -409,6 +427,14 @@
       git_repo: A git repo with the sources for the project to be used for
           autoconfigure. If no git_repo is passed, autoconfig will run with a
           sample c++ project.
+      mount_project: mounts a directory passed in an absolute path as the project
+          to use for autoconfig. Cannot be used if git_repo is passed.
+          Make variable substitution is enabled, so use:
+            mount_project = "$(mount_project)",
+          and then run:
+            bazel build <autoconf target> --define mount_project=$(realpath .)
+          from the root of the project to mount it as the project to use for
+          autoconfig.
       bazel_version: a specific version of Bazel used to generate toolchain
           configs. Format: x.x.x
       bazel_rc_version: a specific version of Bazel release candidate used to
@@ -450,8 +476,12 @@
     if packages_is_empty and "keys" in kwargs:
         fail("'keys' can only be specified when 'packages' is not empty.")
 
-    # If the git_repo was not provided, use the default autoconfig project
-    if "git_repo" not in kwargs:
+    if "git_repo" in kwargs and "mount_project" in kwargs:
+        fail("'git_repo' cannot be used with 'mount_project'.")
+
+    # If a git_repo or mount_project was not provided
+    # use the default autoconfig project
+    if "git_repo" not in kwargs and "mount_project" not in kwargs:
         kwargs["repo_pkg_tar"] = _DEFAULT_AUTOCONFIG_PROJECT_PKG_TAR
         kwargs["use_default_project"] = True
     kwargs["files"] = [
@@ -459,7 +489,7 @@
         _WORKSPACE_PREFIX + "rules:install_bazel_version.sh",
     ]
 
-    # Do not install packags if 'packages' is not specified or is an ampty list.
+    # Do not install packags if 'packages' is not specified or is an empty list.
     if not packages_is_empty:
         # "additional_repos" and "keys" are optional for docker_toolchain_autoconfig,
         # but required for toolchain_container". Use empty lists as placeholder.
diff --git a/tests/config/BUILD b/tests/config/BUILD
index 3317cb5..57f5f5f 100644
--- a/tests/config/BUILD
+++ b/tests/config/BUILD
@@ -116,6 +116,24 @@
     test = True,
 )
 
+# To build this target run:
+# bazel build //tests/config:this-project-repo-autoconfig --define mount_project=$(realpath .)
+# To test it run:
+# bazel test //tests/config:this-project-repo-autoconfig_test --define mount_project=$(realpath .)
+docker_toolchain_autoconfig(
+    name = "this-project-repo-autoconfig",
+    base = ":rbe-test-xenial-with-pkgs.tar",
+    config_repos = [
+        "local_config_sh",
+        "local_config_cc",
+    ],
+    env = gcc_env(),
+    incompatible_changes_off = True,
+    mount_project = "$(mount_project)",
+    tags = ["manual"],
+    test = True,
+)
+
 docker_toolchain_autoconfig(
     name = "ubuntu-xenial-bazel-head-autoconfig",
     base = ":rbe-test-xenial-with-pkgs.tar",