system-helpers/usr/local/bin/docker_compose_update

132 lines
3.1 KiB
Text
Raw Normal View History

#!/usr/bin/env bash
# usage
usage() {
USAGE=$(cat <<EOF
Usage: docker_compose_update [CONFIG_FILE (absolute path)]
An utility to batch update *docker-compose* deployments residing in sub folders. Please see man docker_compose_update.
EOF
)
echo "$USAGE";
}
set -e;
declare -A DOCKER_COMPOSE_UPDATE_COMMANDS;
# check for config file
2022-07-10 16:29:44 +00:00
apply_config() {
local config=$1;
if [[ ! -f "$config" ]]; then
echo "No config file specified";
echo "";
usage;
exit 1;
fi
set -a;
# shellcheck disable=SC1090
2022-07-10 16:29:44 +00:00
source "$config";
set +a;
}
source_config() {
2022-07-10 16:29:44 +00:00
local config=$1;
local configFallback=$2;
local configGlobalFallback=$3;
if [[ -f "$config" ]]; then
apply_config "$config";
2022-07-10 16:33:00 +00:00
return;
2022-07-10 16:29:44 +00:00
fi
if [[ -f "$configFallback" ]]; then
apply_config "$configFallback";
2022-07-10 16:33:00 +00:00
return;
2022-07-10 16:29:44 +00:00
fi
if [[ -f "$configGlobalFallback" ]]; then
apply_config "$configGlobalFallback";
2022-07-10 16:33:00 +00:00
return;
2022-07-10 16:29:44 +00:00
fi
}
2022-07-10 16:29:44 +00:00
apply_defaults() {
[[ -z ${DOCKER_COMPOSE_UPDATE_IGNORES} ]] && DOCKER_COMPOSE_UPDATE_IGNORES=();
2021-12-11 12:34:01 +00:00
[[ -z ${DOCKER_COMPOSE_UPDATE_COMMAND} ]] && DOCKER_COMPOSE_UPDATE_COMMAND="docker-compose down; docker-compose pull; docker-compose up -d;"
[[ -z ${DOCKER_COMPOSE_UPDATE_FAIL_ON_UPDATE_ERROR} ]] && DOCKER_COMPOSE_UPDATE_FAIL_ON_UPDATE_ERROR="false";
}
check_required() {
if [[ -z ${DOCKER_COMPOSE_UPDATE_BASEDIR} ]]; then
echo "DOCKER_COMPOSE_UPDATE_BASEDIR is required"
exit 1;
fi
if [[ -z ${DOCKER_COMPOSE_UPDATE_COMMAND} ]]; then
echo "DOCKER_COMPOSE_UPDATE_COMMAND is required"
exit 1;
fi
}
# check requirements
CONFIG_FILE="$1";
FALLBACK_CONFIG_FILE="${HOME}/.docker_compose_update.conf";
2022-07-10 16:29:44 +00:00
FALLBACK_CONFIG_GLOBAL_FILE="/etc/docker_compose_update.conf";
source_config "${CONFIG_FILE}" "${FALLBACK_CONFIG_FILE}" "${FALLBACK_CONFIG_GLOBAL_FILE}";
apply_defaults;
check_required;
# DO NOT TOUCH BELOW
currentDir=$(pwd)
echo "Current directory is '${currentDir}'"
cd "${DOCKER_COMPOSE_UPDATE_BASEDIR}";
for dir in *; do
if [ -d "${dir}" ]; then
basename=$(basename "${dir}")
ignore=false;
for ignorePattern in "${DOCKER_COMPOSE_UPDATE_IGNORES[@]}"; do
if [[ "${basename}" == *"${ignorePattern}"* ]]; then
ignore=true;
fi
done
if [[ "${ignore}" == "true" ]]; then
echo "Ignoring '${basename}'";
continue;
fi
echo "Handling '${basename}'";
updateCommand=""
for cmd in "${!DOCKER_COMPOSE_UPDATE_COMMANDS[@]}"; do
if [[ "${cmd}" == "${basename}" ]]; then
updateCommand="${DOCKER_COMPOSE_UPDATE_COMMANDS[$cmd]}";
fi
done
if [[ -n "${updateCommand}" ]]; then
echo "Using custom command '${updateCommand}'";
else
updateCommand="${DOCKER_COMPOSE_UPDATE_COMMAND}";
echo "Using command '${DOCKER_COMPOSE_UPDATE_COMMAND}'";
fi
cd "${basename}";
if [[ "${DOCKER_COMPOSE_UPDATE_FAIL_ON_UPDATE_ERROR}" == "false" ]]; then
set +e;
fi
# shellcheck disable=SC2034
res=$(eval "${updateCommand}")
if [[ "${DOCKER_COMPOSE_UPDATE_FAIL_ON_UPDATE_ERROR}" == "false" ]]; then
set -e;
fi
cd "../";
echo "";
fi
done