Produce docker autoconfig logs (#1) (#223)

* Produce docker autoconfig logs (#1)

Docker autoconfig is not printing out anywhere the results of executing commands inside the container. This PR adds a new output to the rule that produces the log.
Example outputs produced by an autoconfig rule:
Target //tests/config:ubuntu-xenial-autoconfig up-to-date:
bazel-bin/tests/config/ubuntu-xenial-autoconfig-layer.tar
bazel-bin/tests/config/ubuntu-xenial-autoconfig.tar
bazel-bin/tests/config/ubuntu-xenial-autoconfig.digest
bazel-bin/tests/config/ubuntu-xenial-autoconfig_log.log
bazel-bin/tests/config/ubuntu-xenial-autoconfig_outputs.tar

* produce message to debug errors

* resolved review comments
* added print message in rule that shows instructions for reproducing
execution of docker container. Rule prints out (example):

== Docker autoconfig will run. ==
To debug any errors run:
> docker run -d <image_id> bash
Where <image_id> is the image id printed out by the ubuntu-xenial-autoconfig_extract.tar rule.
Then run:
>/ubuntu-xenial-autoconfig_install.sh
from inside the container.
diff --git a/rules/docker_config.bzl b/rules/docker_config.bzl
index 13bfd80..e9e1b7a 100644
--- a/rules/docker_config.bzl
+++ b/rules/docker_config.bzl
@@ -152,6 +152,7 @@
     bazel_config_dir = "/bazel-config"
     project_repo_dir = "project_src"
     name = ctx.attr.name
+    outputs_tar = ctx.outputs.output_tar.basename
 
     # Command to retrieve the project from github if requested.
     clone_repo_cmd = "cd ."
@@ -197,7 +198,7 @@
     for config_repo in ctx.attr.config_repos:
         src_dir = "$(bazel info output_base)/" + _EXTERNAL_FOLDER_PREFIX + config_repo
         copy_cmd.append("cp -dr " + src_dir + " " + "/")
-    copy_cmd.append("tar -cf /outputs.tar /" + " /".join(ctx.attr.config_repos))
+    copy_cmd.append("tar -cf /" + outputs_tar + " /" + " /".join(ctx.attr.config_repos))
     output_copy_cmd = " && ".join(copy_cmd)
 
     # Command to run autoconfigure targets.
@@ -226,6 +227,7 @@
         output = install_sh,
         content = "\n ".join([
             "set -ex",
+            "echo === Starting docker autoconfig ===",
             ctx.attr.setup_cmd,
             install_bazel_cmd,
             "echo === Cloning / expand project repo ===",
@@ -257,13 +259,38 @@
         workdir = bazel_config_dir,
     )
 
+    # Commands to run script to create autoconf results, output stderr to log file
+    # add the log file to a tar file and append the output.tar to that same tar file
+    commands = []
+    commands += ["/" + ctx.attr.name + "_install.sh 2> /" + ctx.attr.name + ".log"]
+    commands += ["tar -cf /extract.tar /" + ctx.attr.name + ".log"]
+    commands += [
+        ("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.")
+    extract_tar_file = ctx.new_file(name + "_extract.tar")
     _extract.implementation(
         ctx,
         name = ctx.attr.name + "_extract",
         image = image_tar,
-        commands = ["/" + ctx.attr.name + "_install.sh"],
-        extract_file = "/outputs.tar",
-        output_file = ctx.outputs.output_tar,
+        commands = commands,
+        extract_file = "/extract.tar",
+        output_file = extract_tar_file,
+    )
+
+    # Extracts the two outputs produced by this rule (outputs.tar + log file)
+    # from the tar file extracted from the container in the rule above
+    ctx.actions.run_shell(
+        inputs = [extract_tar_file],
+        outputs = [ctx.outputs.output_tar, ctx.outputs.log],
+        command = ("tar -C %s -xf %s" % (ctx.outputs.output_tar.dirname, extract_tar_file.path)),
     )
 
 docker_toolchain_autoconfig_ = rule(
@@ -295,6 +322,7 @@
         ),
     },
     outputs = _container.image.outputs + {
+        "log": "%{name}.log",
         "output_tar": "%{name}_outputs.tar",
     },
     implementation = _docker_toolchain_autoconfig_impl,