Add a docker-compose update script which iterates through a directory where all subfolders contain a docker-compose.yml file
This commit is contained in:
parent
fd3710cb5c
commit
c614ea122f
2 changed files with 125 additions and 0 deletions
124
usr/local/bin/docker_compose_update
Executable file
124
usr/local/bin/docker_compose_update
Executable file
|
@ -0,0 +1,124 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
# usage
|
||||
usage() {
|
||||
USAGE=$(cat <<EOF
|
||||
Usage: docker_compose_update [CONFIG_FILE (absolute path)]
|
||||
|
||||
If no CONFIG_FILE is given, HOME/.docker_compose_update.conf is used. This fallback option
|
||||
has to exist or the script will exit.
|
||||
|
||||
Configuration can be done in any file and any pre-defined variable can be overwritten.
|
||||
|
||||
The following are at least required for the script to work:
|
||||
- DOCKER_COMPOSE_UPDATE_BASEDIR="/root/deployments" // where all docker stacks are located in their own sub-folders
|
||||
- DOCKER_COMPOSE_UPDATE_IGNORES=("mailcow") // folder names which will be ignored and not be updated, at least () is required
|
||||
- DOCKER_COMPOSE_UPDATE_COMMANDS[aDirectoryName]="./callFancyScriptInDirectory" // use another command to update, multiple possible.
|
||||
Example: accessor has to match the folder, e.g. <baseDir>/aDirectoryName will use command "./callFancyScriptInDirectory" when changing directory into it.
|
||||
- DOCKER_COMPOSE_UPDATE_COMMAND="sudo docker-compose down; sudo docker-compose pull; sudo docker-compose up -d;" // the default update command for deployments
|
||||
- DOCKER_COMPOSE_UPDATE_FAIL_ON_UPDATE_ERROR="false" // if set to true, script fails if at least one update procedure failed
|
||||
|
||||
You can copy this script to '/usr/local/bin' and use create a custom CONFIG_FILE as user. Examples can be found in '/usr/share/doc/<scriptname>'.
|
||||
EOF
|
||||
)
|
||||
echo "$USAGE";
|
||||
}
|
||||
|
||||
set -e;
|
||||
|
||||
declare -A DOCKER_COMPOSE_UPDATE_COMMANDS;
|
||||
|
||||
# check for config file
|
||||
source_config() {
|
||||
local config=$1;
|
||||
local configFallback=$2;
|
||||
|
||||
if [[ ! -f "$config" ]]; then
|
||||
if [[ ! -f "$configFallback" ]]; then
|
||||
echo "No config file specified and could not find default in '$configFallback'!";
|
||||
echo "";
|
||||
usage;
|
||||
exit 1;
|
||||
else
|
||||
config=$configFallback;
|
||||
fi
|
||||
fi
|
||||
|
||||
set -a;
|
||||
source "$config";
|
||||
set +a;
|
||||
}
|
||||
apply_defaults() {
|
||||
[[ -z ${DOCKER_COMPOSE_UPDATE_IGNORES} ]] && DOCKER_COMPOSE_UPDATE_IGNORES=();
|
||||
[[ -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";
|
||||
source_config "${CONFIG_FILE}" "${FALLBACK_CONFIG_FILE}";
|
||||
apply_defaults;
|
||||
check_required;
|
||||
|
||||
# DO NOT TOUCH BELOW
|
||||
currentDir=$(pwd)
|
||||
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
|
||||
|
||||
res=$(eval "${updateCommand}")
|
||||
|
||||
if [[ "${DOCKER_COMPOSE_UPDATE_FAIL_ON_UPDATE_ERROR}" == "false" ]]; then
|
||||
set -e;
|
||||
fi
|
||||
cd "../";
|
||||
echo "";
|
||||
fi
|
||||
done
|
|
@ -0,0 +1 @@
|
|||
DOCKER_COMPOSE_UPDATE_BASEDIR="/root/deployments"
|
Loading…
Reference in a new issue