2
0
docker-skeleton/tools/startup.d/100-docker-compose-withlogs.sh

89 lines
3.2 KiB
Bash
Executable File

#!/bin/bash
#
# Launches a dockerized service defined by a docker-compose.yml file.
# This is a general purpose launcher, doesn't requires customization.
# Actually it is a humble wrapper script for a 'docker-compose up -d'
# command. Additionally sets up an aggregated logfile or per-service
# logfiles and prevents the multiple launch attempts.
#
# Author: Kovács Zoltán <kovacs.zoltan@smartfront.hu>
# License: GNU/GPL v3+ (https://www.gnu.org/licenses/gpl-3.0.en.html)
# 2021-09-02 v0.1 Initial release
# Accepted environment variables and their defaults.
PAR_BASEDIR=${PAR_BASEDIR:-""} # Service's base folder
PAR_AGGLOGS=${PAR_AGGLOGS:-""} # Not empty for aggregated logs
# Other initialisations.
LOGDIR="logs"
LOGFILE="logs/docker-compose.log" # For aggregated log
YMLFILE="docker-compose.yml"
# Messages.
MSG_ALREADYRUN="This service is already running."
MSG_MISSINGDEP="Fatal: missing dependency"
MSG_MISSINGYML="Fatal: didn't find the docker-compose.yml file"
# Checks the dependencies.
TR=$(which tr 2>/dev/null)
if [ -z "$TR" ]; then echo "$MSG_MISSINGDEP tr."; exit 1 ; fi
for item in dirname docker-compose readlink
do
if [ -n "$(which $item)" ]
then export $(echo $item | "$TR" '[:lower:]' '[:upper:]' | "$TR" '-' '_')=$(which $item)
else echo "$MSG_MISSINGDEP $item." >&2; exit 1; fi
done
# Where I'm?
# https://gist.github.com/TheMengzor/968e5ea87e99d9c41782
SOURCE="$0"
while [ -h "$SOURCE" ]; do
# resolve $SOURCE until the file is no longer a symlink
SCRPATH="$( cd -P "$("$DIRNAME" "$SOURCE" )" && pwd )" #"
SOURCE="$("$READLINK" "$SOURCE")"
# if $SOURCE was a relative symlink, we need to resolve it
# relative to the path where the symlink file was located
[[ $SOURCE != /* ]] && SOURCE="$SCRPATH/$SOURCE"
done; SCRPATH="$( cd -P "$("$DIRNAME" "$SOURCE" )" && pwd )" #"
# Searches the base folder, containing a docker-compose.yml file.
# Called from the base folder (./)?
BASE_DIR="$PAR_BASEDIR"
TEST_DIR="$SCRPATH"
[[ -z "$BASE_DIR" ]] && [[ -r "$TEST_DIR/$YMLFILE" ]] && BASE_DIR="$TEST_DIR"
# Called from ./tools?
TEST_DIR="$("$DIRNAME" "$TEST_DIR")"
[[ -z "$BASE_DIR" ]] && [[ -r "$TEST_DIR/$YMLFILE" ]] && BASE_DIR="$TEST_DIR"
# Called from ./tools/*.d?
TEST_DIR="$("$DIRNAME" "$TEST_DIR")"
[[ -z "$BASE_DIR" ]] && [[ -r "$TEST_DIR/$YMLFILE" ]] && BASE_DIR="$TEST_DIR"
# On failure gives it up here.
if [ -z "$BASE_DIR" -o ! -r "$BASE_DIR/$YMLFILE" ]; then
echo "$MSG_MISSINGYML" >&2; exit 1
fi
# Doesn't start if it is already running.
if [ -n "$(cd "$BASE_DIR"; "$DOCKER_COMPOSE" ps --services --filter "status=running")" ]; then
echo "$MSG_ALREADYRUN" >&2; exit 1
fi
# Starts the service.
(cd "$BASE_DIR"; "$DOCKER_COMPOSE" up -d)
# Starts the logger - this/these process(es) will automatically terminate
# when the docker-compose stops.
if [ -n "$PAR_AGGLOGS" ]; then
# Aggregated logs
(cd "$BASE_DIR"; "$DOCKER_COMPOSE" logs --no-color -t -f >> "$BASE_DIR/$LOGFILE" &)
else
# Separate logs, each for every running service.
for service in $(cd "$BASE_DIR"; "$DOCKER_COMPOSE" ps --services) ""
do
if [ -n "$service" ]; then
(cd "$BASE_DIR"; "$DOCKER_COMPOSE" logs --no-color -t -f $service >> "$BASE_DIR/$LOGDIR/$service.log" &)
fi
done
fi
# That's all, Folks!