From 174d7c2f5159c9144e999935e729c2faffc42495 Mon Sep 17 00:00:00 2001 From: mats cronqvist Date: Fri, 8 Nov 2019 17:05:39 +0100 Subject: [PATCH] Dont assume docker path in image util (#1262) * dont assume that docker is in PATH in image_util * typo * restore mysteriously lost line * fix syntax error --- docker/package_managers/install_pkgs.bzl | 22 ++++++++++++++----- docker/util/BUILD | 2 +- .../util/{image_util.sh => image_util.sh.tpl} | 7 ++++-- docker/util/run.bzl | 19 ++++++++++++---- 4 files changed, 37 insertions(+), 13 deletions(-) rename docker/util/{image_util.sh => image_util.sh.tpl} (64%) diff --git a/docker/package_managers/install_pkgs.bzl b/docker/package_managers/install_pkgs.bzl index dbe634fc1..bcc808e39 100644 --- a/docker/package_managers/install_pkgs.bzl +++ b/docker/package_managers/install_pkgs.bzl @@ -85,10 +85,20 @@ def _impl(ctx, image_tar = None, installables_tar = None, installation_cleanup_c ) unstripped_tar = ctx.actions.declare_file(output_tar.basename + ".unstripped") - script = ctx.actions.declare_file(ctx.label.name + ".build") - toolchain_info = ctx.toolchains["@io_bazel_rules_docker//toolchains/docker:toolchain_type"].info + # Generate a shell script to execute the reset cmd + image_util = ctx.actions.declare_file("image_util.sh") + ctx.actions.expand_template( + template = ctx.file._image_util_tpl, + output = image_util, + substitutions = { + "%{docker_tool_path}": toolchain_info.tool_path, + }, + is_executable = True, + ) + + script = ctx.actions.declare_file(ctx.label.name + ".build") ctx.actions.expand_template( template = ctx.file._run_install_tpl, output = script, @@ -101,7 +111,7 @@ def _impl(ctx, image_tar = None, installables_tar = None, installation_cleanup_c "%{output_file_name}": unstripped_tar.path, "%{output_image_name}": output_image_name, "%{to_json_tool}": ctx.executable._to_json_tool.path, - "%{util_script}": ctx.file._image_utils.path, + "%{util_script}": image_util.path, }, is_executable = True, ) @@ -112,7 +122,7 @@ def _impl(ctx, image_tar = None, installables_tar = None, installation_cleanup_c image_tar, install_script, installables_tar, - ctx.file._image_utils, + image_util, ], tools = [ctx.executable._extract_image_id, ctx.executable._to_json_tool], executable = script, @@ -163,8 +173,8 @@ _attrs = { executable = True, allow_files = True, ), - "_image_utils": attr.label( - default = "//docker/util:image_util.sh", + "_image_util_tpl": attr.label( + default = "//docker/util:image_util.sh.tpl", allow_single_file = True, ), "_installer_tpl": attr.label( diff --git a/docker/util/BUILD b/docker/util/BUILD index 5c74db74c..46f6dc6df 100644 --- a/docker/util/BUILD +++ b/docker/util/BUILD @@ -33,7 +33,7 @@ py_binary( exports_files([ "commit.sh.tpl", "extract.sh.tpl", - "image_util.sh", + "image_util.sh.tpl", ]) bzl_library( diff --git a/docker/util/image_util.sh b/docker/util/image_util.sh.tpl similarity index 64% rename from docker/util/image_util.sh rename to docker/util/image_util.sh.tpl index 484813bc3..82f4121af 100755 --- a/docker/util/image_util.sh +++ b/docker/util/image_util.sh.tpl @@ -5,9 +5,12 @@ reset_cmd() { local container_id=$2 local output_image_name=$3 + # Resolve the docker tool path + DOCKER="%{docker_tool_path}" + local old_cmd # docker inspect input cannot be piped into docker commit directly, we need to JSON format it. - old_cmd=$(docker inspect -f "{{range .Config.Cmd}}{{.}} {{end}}" "${original_image_name}") + old_cmd=$($DOCKER inspect -f "{{range .Config.Cmd}}{{.}} {{end}}" "${original_image_name}") fmt_cmd=$(echo "$old_cmd" | ${TO_JSON_TOOL}) # If CMD wasn't set, set it to a sane default. if [ "$fmt_cmd" == "" ] || [ "$fmt_cmd" == "[]" ]; @@ -15,5 +18,5 @@ reset_cmd() { fmt_cmd='["/bin/sh", "-c"]' fi - docker commit -c "CMD $fmt_cmd" "${container_id}" "${output_image_name}" + $DOCKER commit -c "CMD $fmt_cmd" "${container_id}" "${output_image_name}" } diff --git a/docker/util/run.bzl b/docker/util/run.bzl index 3960947a5..ef9a88c85 100644 --- a/docker/util/run.bzl +++ b/docker/util/run.bzl @@ -170,6 +170,17 @@ def _commit_impl( toolchain_info = ctx.toolchains["@io_bazel_rules_docker//toolchains/docker:toolchain_type"].info + # Generate a shell script to execute the reset cmd + image_utils = ctx.actions.declare_file("image_util.sh") + ctx.actions.expand_template( + template = ctx.file._image_utils_tpl, + output = image_utils, + substitutions = { + "%{docker_tool_path}": toolchain_info.tool_path, + }, + is_executable = True, + ) + # Generate a shell script to execute the run statement ctx.actions.expand_template( template = ctx.file._run_tpl, @@ -186,12 +197,12 @@ def _commit_impl( ), "%{output_tar}": output_image_tar.path, "%{to_json_tool}": ctx.executable._to_json_tool.path, - "%{util_script}": ctx.file._image_utils.path, + "%{util_script}": image_utils.path, }, is_executable = True, ) - runfiles = [image, ctx.file._image_utils] + runfiles = [image, image_utils] ctx.actions.run( outputs = [output_image_tar], @@ -225,8 +236,8 @@ _commit_attrs = { executable = True, allow_files = True, ), - "_image_utils": attr.label( - default = "//docker/util:image_util.sh", + "_image_utils_tpl": attr.label( + default = "//docker/util:image_util.sh.tpl", allow_single_file = True, ), "_run_tpl": attr.label(