Created
May 3, 2020 07:56
-
-
Save jonathandreyer/8592e24454b230aaac9522853b89424a to your computer and use it in GitHub Desktop.
Script to manage multi-container Docker with more than one docker-compose file (up/ps/logs/pull/down/clean/restart)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#=================================================================================== | |
# FILE: docker-compose.sh | |
# | |
# USAGE: docker-compose [up] [ps] [logs {-f}] [pull] [down] [clean {-a}] [restart {-c}] | |
# | |
# DESCRIPTION: Launch instance of mainflux with docker-compose. | |
# | |
# OPTIONS: see function ’usage’ below | |
# AUTHOR: J.Dreyer | |
# VERSION: 1.0 | |
# CREATED: 03.05.2020 | |
#=================================================================================== | |
DOCKER_COMPOSE_DEFAULT=("docker/docker-compose.yml") | |
DOCKER_COMPOSE_ADDONS=("docker/addons/influxdb-writer/docker-compose.yml"\ | |
"docker/addons/influxdb-reader/docker-compose.yml"\ | |
"docker/addons/bootstrap/docker-compose.yml") | |
LOGS_SIZE_TAIL=25 | |
#=== FUNCTION ================================================================ | |
# NAME: display_usage | |
# DESCRIPTION: Display usage of script | |
#=============================================================================== | |
display_usage () { | |
echo "This script is useful to manage the docker-compose topologies." | |
echo -e "\nUsage:\n [up | ps | logs {-f} | pull | down | clean {-a} | restart {-c}] \n" | |
} | |
#=== FUNCTION ================================================================ | |
# DESCRIPTION: Check deamon docker engine | |
#=============================================================================== | |
check_daemon_docker () { | |
# Check if Docker deamon is running | |
if (! docker stats --no-stream > /dev/null 2>&1); | |
then | |
# Quit script when Docker is not available | |
echo "Docker not running!" | |
exit 1 | |
fi | |
} | |
#=== FUNCTION ================================================================ | |
# NAME: create_env_file_if_not_exist | |
# DESCRIPTION: Check if .env file is available and create a new one if it is not available. | |
#=============================================================================== | |
create_env_file_if_not_exist () { | |
if [ ! -f ".env" ]; then | |
cp default.env .env | |
echo -e "\nA new .env file created from template.\n" | |
fi | |
} | |
#=== FUNCTION ================================================================ | |
# NAME: docker_compose_up | |
# DESCRIPTION: Launch docker-compose topologie | |
#=============================================================================== | |
docker_compose_up () { | |
create_env_file_if_not_exist | |
# Launch base topologie | |
docker_compose_files="" | |
for default in ${DOCKER_COMPOSE_DEFAULT[*]} | |
do | |
docker_compose_files+="-f ${default} " | |
done | |
docker-compose ${docker_compose_files}up -d | |
# Launch addons | |
for addon in ${DOCKER_COMPOSE_ADDONS[*]} | |
do | |
docker-compose -f ${addon} up -d | |
done | |
} | |
#=== FUNCTION ================================================================ | |
# NAME: docker_compose_down | |
# DESCRIPTION: Stop docker-compose topologie | |
#=============================================================================== | |
docker_compose_down () { | |
# Stop addons | |
for (( idx=${#DOCKER_COMPOSE_ADDONS[@]}-1 ; idx>=0 ; idx-- )) | |
do | |
docker-compose -f ${DOCKER_COMPOSE_ADDONS[idx]} down | |
done | |
# Stop base topologie | |
docker_compose_files="" | |
for default in ${DOCKER_COMPOSE_DEFAULT[*]} | |
do | |
docker_compose_files+="-f ${default} " | |
done | |
docker-compose ${docker_compose_files}down | |
} | |
#=== FUNCTION ================================================================ | |
# NAME: docker_compose_ps | |
# DESCRIPTION: Show ps for docker-compose topologie | |
#=============================================================================== | |
docker_compose_ps () { | |
# Default docker-compose | |
docker_compose_base="" | |
for default in ${DOCKER_COMPOSE_DEFAULT[*]} | |
do | |
docker_compose_base+="-f $default " | |
done | |
docker_compose_files+=("$docker_compose_base") | |
# Addon docker-compose | |
for addon in ${DOCKER_COMPOSE_ADDONS[*]} | |
do | |
docker_compose_files+=("-f ${addon} ") | |
done | |
# Launch ps for all docker-compose files | |
for (( i = 0; i < ${#docker_compose_files[@]}; i++ )) | |
do | |
docker-compose ${docker_compose_files[$i]} ps | |
done | |
} | |
#=== FUNCTION ================================================================ | |
# NAME: docker_compose_logs | |
# DESCRIPTION: Show logs for docker-compose topologie (option "-f" to follow logs) | |
#=============================================================================== | |
docker_compose_logs () { | |
# Default docker-compose | |
docker_compose_base="" | |
for default in ${DOCKER_COMPOSE_DEFAULT[*]} | |
do | |
docker_compose_base+="-f $default " | |
done | |
docker_compose_files+=("$docker_compose_base") | |
# Addon docker-compose | |
for addon in ${DOCKER_COMPOSE_ADDONS[*]} | |
do | |
docker_compose_files+=("-f ${addon} ") | |
done | |
# Check size of logs | |
if [ -z "$LOGS_SIZE_TAIL" ] | |
then | |
TAIL="" | |
else | |
TAIL="--tail=$LOGS_SIZE_TAIL " | |
fi | |
# Check if -f is an option | |
if [ "$1" == "-f" ] | |
then | |
FOLLOW="--follow &" | |
else | |
FOLLOW="" | |
fi | |
# Launch logs for every components of docker-compose | |
for (( i = 0; i < ${#docker_compose_files[@]}; i++ )) | |
do | |
# eval to show container name in jobs list | |
eval "docker-compose ${docker_compose_files[$i]} logs $TAIL$FOLLOW" | |
done | |
# | |
if [ ! -z "$FOLLOW" ] | |
then | |
# Exit function (executed after EXIT signal) | |
function _exit { | |
echo | |
echo "Stopping logs" | |
echo "..." | |
# Using `sh -c` so that if some have exited, that error will | |
# not prevent further tails from being killed. | |
jobs -p | tr '\n' ' ' | xargs -I % sh -c "kill % || true" | |
echo "Done" | |
} | |
# On ctrl+c, kill all tails started by this script. | |
trap _exit EXIT | |
# Don't exit this script until ctrl+c or all tails exit. | |
wait | |
fi | |
} | |
#=== FUNCTION ================================================================ | |
# NAME: docker_compose_pull | |
# DESCRIPTION: Pull container for docker-compose topologie | |
#=============================================================================== | |
docker_compose_pull () { | |
# Default docker-compose | |
docker_compose_base="" | |
for default in ${DOCKER_COMPOSE_DEFAULT[*]} | |
do | |
docker_compose_base+="-f $default " | |
done | |
docker_compose_files+=("$docker_compose_base") | |
# Addon docker-compose | |
for addon in ${DOCKER_COMPOSE_ADDONS[*]} | |
do | |
docker_compose_files+=("-f ${addon} ") | |
done | |
# Launch ps for all docker-compose files | |
for (( i = 0; i < ${#docker_compose_files[@]}; i++ )) | |
do | |
docker-compose ${docker_compose_files[$i]} pull | |
done | |
} | |
#=== FUNCTION ================================================================ | |
# NAME: docker_compose_clean | |
# DESCRIPTION: Remove images container for docker-compose topologie (option "-a" to remove also images) | |
#=============================================================================== | |
docker_compose_clean () { | |
# Check if -a is an option | |
if [ "$1" == "-a" ] | |
then | |
# Remove stored images | |
docker system prune -a -f | |
fi | |
# Remove volume | |
docker volume prune -f | |
} | |
#=== FUNCTION ================================================================ | |
# NAME: docker_compose_restart | |
# DESCRIPTION: Restart docker-compose topologie (option "-c" to clean volumes) | |
#=============================================================================== | |
docker_compose_restart () { | |
docker_compose_down | |
# Check if -c is an option | |
if [ "$1" == "-c" ] | |
then | |
# Remove only volumes | |
docker_compose_clean | |
fi | |
docker_compose_up | |
} | |
#=== MAIN ================================================================ | |
# DESCRIPTION: Run core of script | |
#=========================================================================== | |
case "$1" in | |
"pull") | |
check_daemon_docker | |
docker_compose_pull | |
;; | |
"up") | |
check_daemon_docker | |
docker_compose_up | |
;; | |
"ps") | |
check_daemon_docker | |
docker_compose_ps | |
;; | |
"logs") | |
check_daemon_docker | |
docker_compose_logs $2 | |
;; | |
"down") | |
check_daemon_docker | |
docker_compose_down | |
;; | |
"clean") | |
check_daemon_docker | |
docker_compose_clean $2 | |
;; | |
"restart") | |
check_daemon_docker | |
docker_compose_restart $2 | |
;; | |
"-h" | "--help") | |
display_usage | |
;; | |
*) | |
display_usage | |
exit 1 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment