Skip to content
This repository has been archived by the owner on Oct 2, 2023. It is now read-only.

Commit

Permalink
Dont assume docker path in image util (#1262)
Browse files Browse the repository at this point in the history
* dont assume that docker is in PATH in image_util

* typo

* restore mysteriously lost line

* fix syntax error
  • Loading branch information
massemanet authored and nlopezgi committed Nov 8, 2019
1 parent 818cd1b commit 174d7c2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
22 changes: 16 additions & 6 deletions docker/package_managers/install_pkgs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
)
Expand All @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion docker/util/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ py_binary(
exports_files([
"commit.sh.tpl",
"extract.sh.tpl",
"image_util.sh",
"image_util.sh.tpl",
])

bzl_library(
Expand Down
7 changes: 5 additions & 2 deletions docker/util/image_util.sh → docker/util/image_util.sh.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ 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" == "[]" ];
then
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}"
}
19 changes: 15 additions & 4 deletions docker/util/run.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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],
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 174d7c2

Please sign in to comment.