diff --git a/angel-docker-build.sh b/angel-docker-build.sh index 00235d537..de941516d 100755 --- a/angel-docker-build.sh +++ b/angel-docker-build.sh @@ -4,6 +4,10 @@ # set -e SCRIPT_DIR="$(cd "$(dirname "${0}")" && pwd)" + +# source common functionalities +. "${SCRIPT_DIR}/scripts/common.bash" + pushd "$SCRIPT_DIR" function usage() @@ -19,11 +23,6 @@ Options: " } -function log() -{ - >&2 echo "$@" -} - # Option parsing dc_forward_params=() while [[ $# -gt 0 ]] @@ -88,7 +87,9 @@ then log "Forwarding to docker-compose: ${dc_forward_params[@]}" fi -docker-compose \ +get_docker_compose_cmd DC_CMD + +"${DC_CMD[@]}" \ --env-file "$SCRIPT_DIR"/docker/.env \ -f "$SCRIPT_DIR"/docker/docker-compose.yml \ --profile build-only \ diff --git a/angel-docker-pull.sh b/angel-docker-pull.sh index f6c109b66..48597c644 100755 --- a/angel-docker-pull.sh +++ b/angel-docker-pull.sh @@ -4,6 +4,10 @@ # set -e SCRIPT_DIR="$(cd "$(dirname "${0}")" && pwd)" + +# source common functionalities +. "${SCRIPT_DIR}/scripts/common.bash" + pushd "$SCRIPT_DIR" function usage() @@ -36,10 +40,12 @@ done if [[ "${#dc_forward_params[@]}" -gt 0 ]] then # shellcheck disable=SC2145 - echo "Forwarding to docker-compose: ${dc_forward_params[@]}" + log "Forwarding to docker-compose: ${dc_forward_params[@]}" fi -docker-compose \ +get_docker_compose_cmd DC_CMD + +"${DC_CMD[@]}" \ --env-file "$SCRIPT_DIR"/docker/.env \ -f "$SCRIPT_DIR"/docker/docker-compose.yml \ --profile build-only \ diff --git a/angel-docker-push.sh b/angel-docker-push.sh index 602461ca3..7cc22d0ef 100755 --- a/angel-docker-push.sh +++ b/angel-docker-push.sh @@ -4,6 +4,10 @@ # set -e SCRIPT_DIR="$(cd "$(dirname "${0}")" && pwd)" + +# source common functionalities +. "${SCRIPT_DIR}/scripts/common.bash" + pushd "$SCRIPT_DIR" function usage() @@ -36,10 +40,12 @@ done if [[ "${#dc_forward_params[@]}" -gt 0 ]] then # shellcheck disable=SC2145 - echo "Forwarding to docker-compose: ${dc_forward_params[@]}" + log "Forwarding to docker-compose: ${dc_forward_params[@]}" fi -docker-compose \ +get_docker_compose_cmd DC_CMD + +"${DC_CMD[@]}" \ --env-file "$SCRIPT_DIR"/docker/.env \ -f "$SCRIPT_DIR"/docker/docker-compose.yml \ --profile build-only \ diff --git a/angel-workspace-shell.sh b/angel-workspace-shell.sh index 23064bdd1..0fa050d6d 100755 --- a/angel-workspace-shell.sh +++ b/angel-workspace-shell.sh @@ -5,6 +5,9 @@ set -e SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +# source common functionalities +. "${SCRIPT_DIR}/scripts/common.bash" + DEFAULT_SERVICE_NAME="workspace-shell-dev-gpu" function usage() @@ -29,11 +32,6 @@ Options: " } -function log() -{ - >&2 echo "$@" -} - # Option parsing passthrough_args=() while [[ $# -gt 0 ]] @@ -102,8 +100,10 @@ else " fi +get_docker_compose_cmd DC_CMD + set +e -docker-compose \ +"${DC_CMD[@]}" \ --env-file "$ENV_FILE" \ -f "$SCRIPT_DIR"/docker/docker-compose.yml \ run --rm \ diff --git a/scripts/common.bash b/scripts/common.bash new file mode 100644 index 000000000..96357eea7 --- /dev/null +++ b/scripts/common.bash @@ -0,0 +1,46 @@ +# Common functions and definitions for use in bash scripts. + + +# Logging function to output strings to STDERR. +function log() +{ + >&2 echo "$@" +} + + +# Define to a variable of the given name an array composed of the appropriate +# CLI arguments to invoke docker compose for the current system, if able at +# all. +# +# If a docker compose tool cannot be identified, this function returns code 1. +# +# This should generally be called like: +# get_docker_compose_cmd DC_CMD +# Which results in "DC_CMD" being defined as an array in the calling context, +# viewable like: +# echo "${DC_CMD[@]}" +# +function get_docker_compose_cmd() +{ + EXPORT_VAR_NAME="$1" + if [[ -z "$EXPORT_VAR_NAME" ]] + then + log "[ERROR] No export variable name provided as the first positional argument." + return 1 + fi + # Check for v1 docker-compose tool, otherwise try to make use of v2 + # docker-compose plugin + if ( command -v docker-compose >/dev/null 2>&1 ) + then + log "[INFO] Using v1 docker-compose python tool" + EVAL_STR="${EXPORT_VAR_NAME}=( docker-compose )" + elif ( docker compose >/dev/null 2>&1 ) + then + log "[INFO] Using v2 docker compose plugin" + EVAL_STR="${EXPORT_VAR_NAME}=( docker compose )" + else + log "[ERROR] No docker compose functionality found on the system." + return 1 + fi + eval "${EVAL_STR}" +}