Skip to content

Commit

Permalink
Add more docker_path error checks
Browse files Browse the repository at this point in the history
And also improve general error handling so that the user knows when the
error comes from docker or from this shim.
  • Loading branch information
felipecrs committed Jun 17, 2023
1 parent 6504853 commit 979b4cb
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 17 deletions.
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ COPY docker "${DOCKER_PATH}"
RUN mkdir -p /test && \
echo test | tee /test/only-inside-container

# Cleanup entrypoint to make execution faster
ENTRYPOINT []

# Set default stage
FROM felipecrs/fixdockergid:latest

Expand Down
46 changes: 38 additions & 8 deletions docker
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
#!/bin/bash
#!/usr/bin/env bash

set -euo pipefail
if [[ "${DOND_SHIM_DEBUG:-false}" == true ]]; then
set -o xtrace
fi

# Exit on any kind of errors
# https://unix.stackexchange.com/questions/23026
set -o errexit
set -o nounset
set -o pipefail
set -o errtrace
set -o functrace
shopt -s inherit_errexit

function echo_error() {
echo "ERROR(dond-shim):" "${@}" >&2
}

function error() {
echo_error "${@}"
exit 1
}

# Ensure the user knows that the error was originated from the shim
trap 'echo_error "Uncaught error at line ${LINENO}"' ERR

# Finds all docker options that take a value from the --help output and
# stores them in the docker_options_with_value array.
Expand Down Expand Up @@ -164,13 +187,16 @@ function fix_volume_arg() {
fi
}

if [[ "${DOND_SHIM_DEBUG:-false}" == true ]]; then
set -x
fi
script_path="$(
cd -- "$(dirname "$0")" >/dev/null 2>&1
pwd -P
)"
readonly script_path

# Parse supported environment variables
readonly mock_container_root_on_host="${DOND_SHIM_MOCK_CONTAINER_ROOT_ON_HOST:-}"
readonly print_command="${DOND_SHIM_PRINT_COMMAND:-false}"

if [[ -n "${DOND_SHIM_DOCKER_PATH:-}" ]]; then
# If DOND_SHIM_DOCKER_PATH is set, then use it to call the docker
readonly docker_path="${DOND_SHIM_DOCKER_PATH}"
Expand All @@ -184,9 +210,13 @@ else
fi

# Ensure docker_path is different from this script to avoid infinite loop
if [[ "${0}" == "${docker_path}" ]]; then
echo "Error: docker_path (${docker_path}) is pointing to dond-shim (${0})"
exit 1
if [[ "${script_path}" == "${docker_path}" ]]; then
error "docker_path (${docker_path}) points to this script (${script_path})"
fi

# Ensure docker_path actually exists
if ! command -v "${docker_path}" >/dev/null; then
error "docker_path (${docker_path}) points to a non-existing file or command"
fi

# Save original arguments
Expand Down
27 changes: 18 additions & 9 deletions test.sh
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
#!/bin/bash

set -euo pipefail
#!/usr/bin/env bash

if [[ "${DEBUG:-false}" == true ]]; then
set -x
export DOND_SHIM_DEBUG=true
set -o xtrace
fi

# Set docker versions from args or use defaults
# Exit on any kind of errors
# https://unix.stackexchange.com/questions/23026
set -o errexit
set -o nounset
set -o pipefail
set -o errtrace
set -o functrace
shopt -s inherit_errexit

# Ensure CTRL+C properly aborts the script
trap "exit 130" INT

# Set docker versions from args or use default
if [[ $# -eq 0 ]]; then
docker_versions=("18.09" "19.03" "20.10" "23" "24" latest)
else
docker_versions=("$@")
fi
readonly docker_versions

readonly docker_args=(docker run --rm --env DOND_SHIM_DEBUG --volume /var/run/docker.sock:/var/run/docker.sock)

for docker_version in "${docker_versions[@]}"; do
echo "Testing with docker version: ${docker_version}"

image_id="$(docker build --target test --build-arg "DOCKER_VERSION=${docker_version}" --quiet .)"

# shellcheck disable=SC2312
docker_args=(docker run --rm --env DOND_SHIM_DEBUG --entrypoint= --volume /var/run/docker.sock:/var/run/docker.sock)

echo "Do not change global options or after the image"
"${docker_args[@]}" --env DOND_SHIM_PRINT_COMMAND=true --volume "${PWD}:/wd" "${image_id}" \
docker --host test run --volume /wd:/wd alpine --volume /wd:/wd |
Expand Down

0 comments on commit 979b4cb

Please sign in to comment.