diff --git a/.metadata b/.metadata index 3991bc0..b31cad5 100644 Binary files a/.metadata and b/.metadata differ diff --git a/tools/maintenance_reboot b/tools/maintenance_reboot index 248f825..dfce966 100755 --- a/tools/maintenance_reboot +++ b/tools/maintenance_reboot @@ -6,13 +6,27 @@ # Author: Kovács Zoltán # Kovács Zoltán # License: GNU/GPL v3+ (https://www.gnu.org/licenses/gpl-3.0.en.html) +# +# 2025-02-02 v1.1 +# new: On reboot (re)starts the per-service logger feature. # 2023-06-18 v1.0 # new: forked from the "Smartfront's DOCKER_skeleton" repository. -# 2021.08-30 v0.1 Initial release +# 2021-08-30 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. +# +START_LOGGER="tools/startup.d/110-startlogs.sh" +YMLFILE="docker-compose.yml" # Messages. # MSG_MISSINGDEP="Fatal: missing dependency" +MSG_MISSINGYML="Fatal: didn't find the docker-compose.yml file" # Checks the dependencies. # @@ -28,6 +42,7 @@ done # Where I'm? # https://gist.github.com/TheMengzor/968e5ea87e99d9c41782 +# SOURCE="${BASH_SOURCE[0]}" while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink @@ -39,4 +54,27 @@ while [ -h "$SOURCE" ]; do done; SCRPATH="$( cd -P "$( "$DIRNAME" "$SOURCE" )" && pwd )" #" SCRFILE="$("$BASENAME" "$(test -L "$0" && "$READLINK" "$0" || echo "$0")")" #" -# Actually this job does nothing. +# 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 + +# Starts per-service logging if it is not already running. +# +if [ -x "$BASE_DIR/$START_LOGGER" ]; then + "$BASE_DIR/$START_LOGGER" +fi + +# That's all, Folks :) diff --git a/tools/startup.d/100-docker-compose-withlogs.sh b/tools/startup.d/100-docker-compose-withlogs.sh index 1d06f65..c5cf9a6 100755 --- a/tools/startup.d/100-docker-compose-withlogs.sh +++ b/tools/startup.d/100-docker-compose-withlogs.sh @@ -3,14 +3,18 @@ # 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. +# command. Additionally may set up an aggregated logfile and rejects +# all multiple launch attempts. # -# Author: Kovács Zoltán +# Author: Kovács Zoltán +# Kovács Zoltán # License: GNU/GPL v3+ (https://www.gnu.org/licenses/gpl-3.0.en.html) # +# 2025-02-02 v1.3 +# mod: Only handles the aggregate log, per-service logging has been +# outsourced to a new code snippet. # 2025-02-02 v1.2 -# fix: unwanted error message in old docker-compose detection. +# fix: Unwanted error message in old docker-compose detection. # 2024-08-24 v1.1 # new: docker-compose v2 compatibility - tested with Ubuntu 24.04 LTS. # 2023-06-18 v1.0 @@ -91,20 +95,11 @@ fi # Starts the service. (cd "$BASE_DIR"; "$DOCKER_COMPOSE" $commandstring up -d) - -# Starts the logger - this/these process(es) will automatically terminate -# when the docker-compose stops. +# Starts the aggregated logger on request - this process will automatically terminate +# when the composition stops. if [ -n "$PAR_AGGLOGS" ]; then # Aggregated logs (cd "$BASE_DIR"; "$DOCKER_COMPOSE" $commandstring logs --no-color -t -f >> "$BASE_DIR/$LOGFILE" &) -else - # Separate logs, each for every running service. - for service in $(cd "$BASE_DIR"; "$DOCKER_COMPOSE" $commandstring ps --services) "" - do - if [ -n "$service" ]; then - (cd "$BASE_DIR"; "$DOCKER_COMPOSE" $commandstring logs --no-color -t -f $service >> "$BASE_DIR/$LOGDIR/$service.log" &) - fi - done fi # That's all, Folks! diff --git a/tools/startup.d/110-startlogs.sh b/tools/startup.d/110-startlogs.sh new file mode 100755 index 0000000..64a61a7 --- /dev/null +++ b/tools/startup.d/110-startlogs.sh @@ -0,0 +1,102 @@ +#!/bin/bash +# +# Configures the per-service log files for a running composed service. +# This script does not yet handle the aggregate logging functionality. +# +# Author: Kovács Zoltán +# License: GNU/GPL v3+ (https://www.gnu.org/licenses/gpl-3.0.en.html) +# +# 2025-02-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_DOESNOTRUN="This service doesn't run." +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 cut dirname docker grep ps 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 +# All dependencies are available via "$THECOMMAND" (upper case) call. +# +# Let's find which version of docker-compose is installed. +if [ $($DOCKER compose version >/dev/null 2>&1; echo $?) -eq 0 ]; then + # We'll use v2 if it is available. + DOCKER_COMPOSE="$DOCKER" + commandstring="compose" +else + # Otherwise falling back to v1. + DOCKER_COMPOSE="$(which docker-compose)" + commandstring="" +fi +# One of the two is mandatory. +if [ -z "$DOCKER_COMPOSE" ];then echo "$MSG_MISSINGDEP docker-compose" >&2; exit 1; fi +# Below docker-compose should be called as "$DOCKER_COMPOSE" $commandstring sequence. + +# 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 + +# Quits silently if the service is not running. +[[ -z "$(cd "$BASE_DIR"; "$DOCKER_COMPOSE" $commandstring ps --services --filter "status=running")" ]] && exit + +# Checks the logger(s), starts if it isn't running. +# These process(es) will automatically terminate when the composed service stops. +if [ -n "$PAR_AGGLOGS" ]; then + # Aggregated logs + # (cd "$BASE_DIR"; "$DOCKER_COMPOSE" $commandstring logs --no-color -t -f >> "$BASE_DIR/$LOGFILE" &) + : +else + # Separate logs, each for every running service. + for service in $(cd "$BASE_DIR"; "$DOCKER_COMPOSE" $commandstring ps --services) "" + do + if [ -n "$service" ]; then + # Converts the service's name to an actual running container's name. + container="$("$DOCKER" inspect -f '{{.Name}}' $(cd "$BASE_DIR"; "$DOCKER_COMPOSE" $commandtring ps -q "$service") | "$CUT" -c2-)" + # It will not start a new log if it already exists. + if [ -z "$("$PS" auxww | "$GREP" "$DOCKER" | "$GREP" 'logs' | "$GREP" "$container" )" ]; then + "$DOCKER" logs -t -f $container >> "$BASE_DIR/$LOGDIR/$service.log" 2>&1 & + fi + fi + done +fi + +# That's all, Folks!