Initial commit - forked from the corresponding Smartfront repositories.
This commit is contained in:
58
tools/.launcher
Executable file
58
tools/.launcher
Executable file
@ -0,0 +1,58 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Simple launcher script to start some worker scripts within a folder
|
||||
# sequentially, in ABC order. The workers' folder is a subfolder of the
|
||||
# script's folder with same name as the script, followed by a .d extension.
|
||||
# All worker scripts must be executable and must have .sh extension.
|
||||
# Other files within the folder will be simply ignored.
|
||||
#
|
||||
# This script usually get called via symlink, therefore the workers' folder's
|
||||
# name comes from the symlink's name. This folder selection may be overridden
|
||||
# using an environment variable. Another environment variable may order to
|
||||
# waiting a time between workers' launch.
|
||||
#
|
||||
# Author: Kovács Zoltán <kovacs.zoltan@smartfront.hu>
|
||||
# Kovács Zoltán <kovacsz@marcusconsulting.hu>
|
||||
# License: GNU/GPL v3+ (https://www.gnu.org/licenses/gpl-3.0.en.html)
|
||||
# 2023-06-18 v1.0
|
||||
# new: forked from the "Smartfront's DOCKER_skeleton" repository.
|
||||
# 2021-08-30 v0.1 Initial release
|
||||
|
||||
# Accepted environment variables and their defaults.
|
||||
SLEEP_BETWEEN=${SLEEP_BETWEEN-"0"} # Secs between launches
|
||||
WORKER_DIR=${WORKER_DIR:-""} # Worker's folder
|
||||
|
||||
# Messages.
|
||||
MSG_MISSINGWRK="Fatal: missing worker's folder"
|
||||
|
||||
# Checks the components.
|
||||
[[ -z "$(which basename)" ]] && exit 1
|
||||
[[ -z "$(which dirname)" ]] && exit 1
|
||||
[[ -z "$(which printf)" ]] && exit 1
|
||||
[[ -z "$(which sleep)" ]] && exit 1
|
||||
# Where I'm?
|
||||
SCRPATH="$( cd -P "$(dirname "$0" )" && echo "$PWD" )" #"
|
||||
|
||||
# Checks the worker's folder.
|
||||
[[ -z "$WORKER_DIR" ]] && WORKER_DIR="$SCRPATH/$(basename "$0").d"
|
||||
if [ -z "$WORKER_DIR" -o ! -d "$WORKER_DIR" ]; then
|
||||
echo "$MSG_MISSINGWRK $WORKER_DIR" >&2; exit 1
|
||||
fi
|
||||
|
||||
# Converts the (optional) time parameter to float.
|
||||
SLEEP_BETWEEN=$(printf '%.2f' "$SLEEP_BETWEEN" 2>/dev/null)
|
||||
|
||||
# Enumerates the workers.
|
||||
WORKERS="$(cd "$WORKER_DIR"; ls -1 *.sh 2>/dev/null)"
|
||||
for worker in $WORKERS ""
|
||||
do
|
||||
# Safety first...
|
||||
if [ -n "$worker" -a -x "$WORKER_DIR/$worker" ]; then
|
||||
# Launches the worker then waits till it finishes.
|
||||
"$WORKER_DIR/$worker" "$@"
|
||||
# Optionally reduces the launch frequency.
|
||||
sleep ${SLEEP_BETWEEN//,/.} # decimal point need
|
||||
fi
|
||||
done
|
||||
|
||||
# That's all, Folks!
|
47
tools/acme
Executable file
47
tools/acme
Executable file
@ -0,0 +1,47 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# https://github.com/acmesh-official/acme.sh
|
||||
#
|
||||
# A humble wrapper script to the acme.sh tool which have to exist
|
||||
# somewhere in PATH. Sets the tool to use this service's config
|
||||
# and log files.
|
||||
#
|
||||
# Author: Kovács Zoltán <kovacs.zoltan@smartfront.hu>
|
||||
# Kovács Zoltán <kovacsz@marcusconsulting.hu>
|
||||
# License: GNU/GPL v3+ (https://www.gnu.org/licenses/gpl-3.0.en.html)
|
||||
# 2023-06-18 v1.0
|
||||
# new: forked from the "Smartfront's DOCKER_skeleton" repository.
|
||||
# 2021-09-14 v0.1 Initial release
|
||||
|
||||
# Messages.
|
||||
#
|
||||
MSG_MISSINGTOOL="Fatal: missing socket relay tool"
|
||||
MSG_MISSINGWORKER="Fatal: missing worker script"
|
||||
|
||||
# Where I'm?
|
||||
#
|
||||
SCRPATH="$( cd -P "$( "$(which dirname)" "$0" )" && echo "$PWD" )"
|
||||
[[ -z "$SCRPATH" ]] && exit 1
|
||||
|
||||
# Where is the service's base?
|
||||
#
|
||||
SERVICE="$( cd -P "$( "$(which dirname)" "$SCRPATH" )" && echo "$PWD" )"
|
||||
[[ -z "$SERVICE" ]] && exit 1
|
||||
|
||||
# Checks the worker components.
|
||||
#
|
||||
ACME="$(PATH="$SCRPATH:$PATH" which acme.sh)"
|
||||
if [ -z "$ACME" -o ! -x "$ACME" ]; then
|
||||
echo -e "$MSG_MISSINGWORKER acme.sh" >&2; exit 1
|
||||
fi
|
||||
SOCAT="$(PATH="$SCRPATH:$PATH" which socat)"
|
||||
if [ -z "$SOCAT" -o ! -x "$SOCAT" ]; then
|
||||
echo -e "$MSG_MISSINGTOOL socat" >&2; exit 1
|
||||
fi
|
||||
|
||||
# Finally launches the worker with the original command line parameters.
|
||||
#
|
||||
export LE_WORKING_DIR="$SERVICE/configs/acme"
|
||||
export LE_CONFIG_HOME="$SERVICE/configs/acme"
|
||||
export LOG_FILE="$SERVICE/logs/web/acme.log"
|
||||
"$ACME" "$@"
|
1
tools/backup
Symbolic link
1
tools/backup
Symbolic link
@ -0,0 +1 @@
|
||||
.launcher
|
78
tools/backup.d/configs_backup.sh
Executable file
78
tools/backup.d/configs_backup.sh
Executable file
@ -0,0 +1,78 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Backups the configuration files of this docker-composed service.
|
||||
# This is a general purpose worker script, doesn't requires customization.
|
||||
#
|
||||
# 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-03 v0.1 Initial release
|
||||
|
||||
# Accepted environment variables and their defaults.
|
||||
PAR_BASEDIR=${PAR_BASEDIR:-""} # Service's base folder
|
||||
PAR_BACKUPDIR=${PAR_BACKUPDIR:-""} # Folder to dump within
|
||||
|
||||
# Other initialisations.
|
||||
BACKUPDIR="storage/backups/tarballs" # Folder to dump within
|
||||
USER=${USER:-LOGNAME} # Fix for cron enviroment only
|
||||
YMLFILE="docker-compose.yml"
|
||||
|
||||
# Messages.
|
||||
MSG_MISSINGDEP="Fatal: missing dependency"
|
||||
MSG_MISSINGYML="Fatal: didn't find the docker-compose.yml file"
|
||||
MSG_NONWRITE="The target directory isn't writable"
|
||||
|
||||
# Checks the dependencies.
|
||||
TR=$(which tr 2>/dev/null)
|
||||
if [ -z "$TR" ]; then echo "$MSG_MISSINGDEP tr."; exit 1 ; fi
|
||||
for item in date dirname hostname readlink tar
|
||||
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
|
||||
# Sets the absolute paths.
|
||||
BACKUPDIR="${PAR_BACKUPDIR:-$BASE_DIR/$BACKUPDIR}"
|
||||
|
||||
# The dump target folder must be writable.
|
||||
[[ ! -w "$BACKUPDIR" ]] \
|
||||
&& echo "$MSG_NONWRITE: $BACKUPDIR" >&2 && exit 1
|
||||
|
||||
# Tries the FS backup.
|
||||
if [ -w "$BACKUPDIR" ]; then
|
||||
BACKUP_NAME="configs.$("$DATE" '+%Y%m%d_%H%M%S').$("$HOSTNAME")"
|
||||
( cd "$BASE_DIR"
|
||||
"$TAR" czf "$BACKUPDIR/$BACKUP_NAME.tgz" \
|
||||
"$YMLFILE" configs docker \
|
||||
2>>"$BACKUPDIR/$BACKUP_NAME.log"
|
||||
)
|
||||
fi
|
||||
|
||||
# That's all, Folks! :)
|
73
tools/build
Executable file
73
tools/build
Executable file
@ -0,0 +1,73 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Builds 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 build'
|
||||
# command. Always pulls the latest image(s) from repository.
|
||||
#
|
||||
# Author: Kovács Zoltán <kovacs.zoltan@smartfront.hu>
|
||||
# Kovács Zoltán <kovacsz@marcusconsulting.hu>
|
||||
# License: GNU/GPL v3+ (https://www.gnu.org/licenses/gpl-3.0.en.html)
|
||||
# 2023-06-18 v1.0
|
||||
# new: forked from the "Smartfront's DOCKER_skeleton" repository.
|
||||
# 2021-10-03 v0.2
|
||||
# fix: now pulls the images even if the service doesn't requires build.
|
||||
# 2021-08-30 v0.1 Initial release
|
||||
|
||||
# Accepted environment variables and their defaults.
|
||||
PAR_BASEDIR=${PAR_BASEDIR:-""} # Service's base folder.
|
||||
|
||||
# Other initialisations.
|
||||
YMLFILE="docker-compose.yml"
|
||||
|
||||
# Messages.
|
||||
MSG_ALREADYRUN="This service is running - shut down before build."
|
||||
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 attempts to build if the service is running.
|
||||
if [ -n "$(cd "$BASE_DIR"; "$DOCKER_COMPOSE" ps --services --filter "status=running")" ]; then
|
||||
echo "$MSG_ALREADYRUN" >&2; exit 1
|
||||
fi
|
||||
# Pulls the components and builds the service (if necessary).
|
||||
(cd "$BASE_DIR"; "$DOCKER_COMPOSE" rm -f; "$DOCKER_COMPOSE" pull; "$DOCKER_COMPOSE" build)
|
||||
|
||||
# That's all, Folks!
|
30
tools/customize_apache2.sh
Normal file
30
tools/customize_apache2.sh
Normal file
@ -0,0 +1,30 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Set the variables below then run this script to generate config/apache2.conf and
|
||||
# config/apache2_ssl.conf (the webserver configuration files for this service).
|
||||
# Take a revision then symlink them into the instances/.apache2 folder and reload
|
||||
# the webserver to activate.
|
||||
|
||||
PAR_ACMEHOST="localhost"
|
||||
PAR_ACMEPORT="8100"
|
||||
PAR_LOCATION=""
|
||||
PAR_SERVICE="$HOME/services/servicename"
|
||||
PAR_PROXYHOST="localhost"
|
||||
PAR_PROXYPORT="8201"
|
||||
PAR_SERVERNAME="www.example.com"
|
||||
PAR_LOCATION=""
|
||||
|
||||
# Do not change anything below.
|
||||
PARAMETERS='$PAR_ACMEHOST:$PAR_ACMEPORT:$PAR_SERVICE:$PAR_PROXYHOST:$PAR_PROXYPORT'
|
||||
PARAMETERS+=':$PAR_SERVERNAME:$PAR_LOCATION'
|
||||
for parameter in $(echo "$PARAMETERS" | tr ":" "\n")
|
||||
do export ${parameter:1}; done
|
||||
cat "$PAR_SERVICE/.templates/apache2/apache2.conf" | envsubst "$PARAMETERS" \
|
||||
> "$PAR_SERVICE/configs/apache2.conf"
|
||||
|
||||
|
||||
PARAMETERS+=""
|
||||
for parameter in $(echo "$PARAMETERS" | tr ":" "\n")
|
||||
do export ${parameter:1}; done
|
||||
cat "$PAR_SERVICE/.templates/apache2/apache2_ssl.conf" | envsubst "$PARAMETERS" \
|
||||
> "$PAR_SERVICE/configs/apache2_ssl.conf"
|
20
tools/customize_nginx.sh
Normal file
20
tools/customize_nginx.sh
Normal file
@ -0,0 +1,20 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Set the variables below then run this script to generate the config/nginx.conf
|
||||
# (the webserver configuration file for this service). Take a revision then
|
||||
# symlink it into the instances/.nginx folder and reload the webserver to activate.
|
||||
|
||||
PAR_ACMEHOST="localhost"
|
||||
PAR_ACMEPORT="8100"
|
||||
PAR_SERVICE="$HOME/services/servicename"
|
||||
PAR_PROXYHOST="localhost"
|
||||
PAR_PROXYPORT="8201"
|
||||
PAR_SERVERNAME="www.example.com"
|
||||
PAR_LOCATION=""
|
||||
|
||||
# Do not change anything below.
|
||||
PARAMETERS='$PAR_ACMEHOST:$PAR_ACMEPORT:$PAR_SERVICE:$PAR_PROXYHOST:$PAR_PROXYPORT:$PAR_SERVERNAME:$PAR_LOCATION'
|
||||
for parameter in $(echo "$PARAMETERS" | tr ":" "\n")
|
||||
do export ${parameter:1}; done
|
||||
cat "$PAR_SERVICE/.templates/nginx/nginx.conf" | envsubst "$PARAMETERS" \
|
||||
> "$PAR_SERVICE/configs/nginx.conf"
|
63
tools/maintenance_daily
Executable file
63
tools/maintenance_daily
Executable file
@ -0,0 +1,63 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Maintenance operations at once a day.
|
||||
# This script called usually by the cron (but indirectly).
|
||||
#
|
||||
# Uses the rotate_folder utility which must be available on path.
|
||||
# Uses the acme wrapper script which have to exist in same folder
|
||||
# as this script.
|
||||
#
|
||||
# Author: Kovács Zoltán <kovacs.zoltan@smartfront.hu>
|
||||
# Kovács Zoltán <kovacsz@marcusconsulting.hu>
|
||||
# License: GNU/GPL v3+ (https://www.gnu.org/licenses/gpl-3.0.en.html)
|
||||
# 2023-06-18 v1.0
|
||||
# new: forked from the "Smartfront's DOCKER_skeleton" repository.
|
||||
# 2021-09-14 v0.2
|
||||
# add: Checks the SSL web certificate (if any), renews it if necessary.
|
||||
# 2021-09-01 v0.1 Initial release
|
||||
|
||||
# Checks the components.
|
||||
[[ -z "$(which dirname)" ]] && exit 1
|
||||
[[ -z "$(which readlink)" ]] && exit 1
|
||||
[[ -z "$(which xargs)" ]] && exit 1
|
||||
|
||||
# Where I'm?
|
||||
SCRPATH="$( cd -P "$( "$(which dirname)" "$0" )" && echo "$PWD" )"
|
||||
|
||||
# Checks the SSL web certificate, renews it if necessery.
|
||||
#
|
||||
# Uses the acme wrapper script located in the same directory.
|
||||
ACME="$SCRPATH/acme"
|
||||
ACMELOG="$($(which dirname) "$SCRPATH")/logs/web/acme.log"
|
||||
if [ -n "$ACME" -a -x "$ACME" ]; then
|
||||
"$ACME" --cron >> "$ACMELOG" 2>&1
|
||||
fi
|
||||
# Done with the certificate.
|
||||
|
||||
# Daily backup operations.
|
||||
#
|
||||
# Launches the backup script.
|
||||
[[ -x "$SCRPATH/backup" ]] && "$SCRPATH/backup"
|
||||
# Done with backups.
|
||||
|
||||
# Rotates the backup folders.
|
||||
#
|
||||
# Enumerates the folders and tries to rotate they content.
|
||||
BACKUPSROOT="$("$(which dirname)" "$SCRPATH")/storage/backups" #"
|
||||
for folder in $(ls -1 "$BACKUPSROOT" 2>/dev/null | $(which xargs) -0 ) ""
|
||||
do
|
||||
if [ -n "$folder" ]; then
|
||||
# Dereferenced absolute path.
|
||||
folder="$("$(which readlink)" -e "$BACKUPSROOT/$folder")" #"
|
||||
# Does it a folder with a prepared configuration?
|
||||
if [ -d "$folder" -a -r "$folder/.rotate_folder.conf" ]; then
|
||||
# Does the rotate job.
|
||||
if [ -x "$SCRPATH/rotate_folder" ]; then
|
||||
"$SCRPATH/rotate_folder" -f "$folder" >/dev/null
|
||||
elif [ -x "$(which rotate_folder)" ]; then
|
||||
"$(which rotate_folder)" -f "$folder" >/dev/null
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
done
|
||||
# Done with rotating.
|
18
tools/maintenance_midnight
Executable file
18
tools/maintenance_midnight
Executable file
@ -0,0 +1,18 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Maintenance operations at midnight.
|
||||
# This script called usually by the cron (but indirectly).
|
||||
#
|
||||
# Author: Kovács Zoltán <kovacs.zoltan@smartfront.hu>
|
||||
# Kovács Zoltán <kovacsz@marcusconsulting.hu>
|
||||
# License: GNU/GPL v3+ (https://www.gnu.org/licenses/gpl-3.0.en.html)
|
||||
# 2023-06-18 v1.0
|
||||
# new: forked from the "Smartfront's DOCKER_skeleton" repository.
|
||||
# 2021-08-30 v0.1 Initial release
|
||||
|
||||
# Where I'm?
|
||||
SCRPATH="$( cd -P "$( "$(which dirname)" "$0" )" && echo "$PWD" )"
|
||||
|
||||
# Launches the logrotate for service logs.
|
||||
|
||||
[[ -x "$SCRPATH/rotate_logs" ]] && "$SCRPATH/rotate_logs" >/dev/null 2>&1
|
42
tools/maintenance_reboot
Executable file
42
tools/maintenance_reboot
Executable file
@ -0,0 +1,42 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Maintenence operations at reboot.
|
||||
# This script called usually by the cron.
|
||||
#
|
||||
# Author: Kovács Zoltán <kovacs.zoltan@smartfront.hu>
|
||||
# Kovács Zoltán <kovacsz@marcusconsulting.hu>
|
||||
# License: GNU/GPL v3+ (https://www.gnu.org/licenses/gpl-3.0.en.html)
|
||||
# 2023-06-18 v1.0
|
||||
# new: forked from the "Smartfront's DOCKER_skeleton" repository.
|
||||
# 2021.08-30 v0.1 Initial release
|
||||
|
||||
# Messages.
|
||||
#
|
||||
MSG_MISSINGDEP="Fatal: missing dependency"
|
||||
|
||||
# Checks the dependencies.
|
||||
#
|
||||
TR=$(which tr 2>/dev/null)
|
||||
if [ -z "$TR" ]; then echo "$MSG_MISSINGDEP tr."; exit 1 ; fi
|
||||
for item in basename cut dirname readlink
|
||||
do
|
||||
if [ -n "$(which $item)" ]
|
||||
then export $(echo $item | "$TR" '[:lower:]' '[:upper:]')=$(which $item)
|
||||
else echo "$MSG_MISSINGDEP $item." >&2; exit 1; fi
|
||||
done
|
||||
# All dependencies are available via "$THECOMMAND" (upper case) call.
|
||||
|
||||
# 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
|
||||
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 )" #"
|
||||
SCRFILE="$("$BASENAME" "$(test -L "$0" && "$READLINK" "$0" || echo "$0")")" #"
|
||||
|
||||
# Actually this job does nothing.
|
158
tools/rotate_logs
Executable file
158
tools/rotate_logs
Executable file
@ -0,0 +1,158 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# A simple wrapper script to rotate the service's logs. Creates a little bit
|
||||
# crafted logrotate configuration (if it doesn't exist yet) and calls the
|
||||
# standard logrotate.
|
||||
#
|
||||
# Uses the copytruncate utility which must be available on path.
|
||||
# Doesn't rotate logs for stopped services.
|
||||
#
|
||||
# Author: Kovács Zoltán <kovacs.zoltan@smartfront.hu>
|
||||
# Kovács Zoltán <kovacsz@marcusconsulting.hu>
|
||||
# License: GNU/GPL v3+ (https://www.gnu.org/licenses/gpl-3.0.en.html)
|
||||
# 2023-06-18 v1.0
|
||||
# new: forked from the "Smartfront's DOCKER_skeleton" repository.
|
||||
# 2021-09-14 v0.2
|
||||
# add: Rotates the web logs (if any) as well.
|
||||
# 2021-09-02 v0.1 Initial release
|
||||
|
||||
# Accepted environment variables and their defaults.
|
||||
PAR_BASEDIR=${PAR_BASEDIR:-""} # Service's base folder
|
||||
PAR_CONFDIR=${PAR_CONFDIR:-""} # Folder containing configs
|
||||
PAR_LOGDIR=${PAR_LOGDIR:-""} # Folder containing logs
|
||||
|
||||
# Other initialisations.
|
||||
CONFDIR="configs" # Folder containing configs
|
||||
LOGDIR="logs" # Folder containing logs
|
||||
YMLFILE="docker-compose.yml"
|
||||
|
||||
# Messages.
|
||||
MSG_MISSINGDEP="Fatal: missing dependency"
|
||||
|
||||
# Basic environment settings.
|
||||
LANG=C
|
||||
LC_ALL=C
|
||||
# We need also the sbin directories.
|
||||
if ! [[ "$PATH" =~ '/sbin:' ]]; then
|
||||
PATH="$PATH:/usr/local/sbin:/usr/sbin:/sbin"; fi
|
||||
|
||||
# Checks the dependencies.
|
||||
TR=$(which tr 2>/dev/null)
|
||||
if [ -z "$TR" ]; then echo "$MSG_MISSINGDEP tr."; exit 1 ; fi
|
||||
for item in basename date dirname docker-compose logrotate 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.
|
||||
|
||||
# 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
|
||||
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 )" #"
|
||||
SCRFILE="$("$BASENAME" "$(test -L "$0" && "$READLINK" "$0" || echo "$0")")" #"
|
||||
|
||||
# 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
|
||||
|
||||
# Sets the absolute paths.
|
||||
CONFDIR="${PAR_CONFDIR:-$BASE_DIR/$CONFDIR}"
|
||||
CONFFILE="$CONFDIR/.${SCRFILE%.*}.conf"
|
||||
STATEFILE="$CONFDIR/.${SCRFILE%.*}.state"
|
||||
LOGDIR="${PAR_LOGDIR:-$BASE_DIR/$LOGDIR}"
|
||||
|
||||
# Doesn't rotate logs for stopped services.
|
||||
[[ -z "$(cd "$BASE_DIR"; "$DOCKER_COMPOSE" ps --services --filter "status=running")" ]] \
|
||||
&& exit 0
|
||||
|
||||
# Locates the worker script.
|
||||
WORKERSCRIPT="$SCRPATH/copytruncate"
|
||||
[[ ! -x "$WORKERSCRIPT" ]] && WORKERSCRIPT="$(which copytruncate)"
|
||||
[[ ! -x "$WORKERSCRIPT" ]] \
|
||||
&& echo -e "$MSG_MISSINGDEP $WORKERSCRIPT." >&2 \
|
||||
&& exit 1
|
||||
|
||||
# Creates the configuration if it doesn't exist yet.
|
||||
if [ ! -e "$CONFFILE" ]; then
|
||||
cat > "$CONFFILE" << EOF
|
||||
$LOGDIR/*.log {
|
||||
missingok
|
||||
daily
|
||||
rotate 30
|
||||
# Must be consistent with prerotate script's settings!
|
||||
dateext
|
||||
dateyesterday
|
||||
dateformat %Y-%m-%d.
|
||||
extension log
|
||||
compress
|
||||
# We'll use our own copytruncate script, because:
|
||||
# * we haven't permission to change ownership, so the built-in copytruncate
|
||||
# method would fail (this is a bug in logrotate, I think);
|
||||
# * we haven't permission to reload the service, so the create new log method
|
||||
# doesn't work - the service would still write to the already rotated file.
|
||||
# The custom script:
|
||||
# * copytruncates files having (by default) .log extesion and compresses the copy
|
||||
# (and returns with 1 exit code, thus, the logrotate will skip this file).
|
||||
# * does nothing with files having any other extensions - e.g .1, .2 and so on
|
||||
# (and returns with 0 exit code, thus, the logrotate can process this file);
|
||||
prerotate
|
||||
$WORKERSCRIPT \$1
|
||||
endscript
|
||||
# Only if the prerotate script didn't process it yet.
|
||||
copytruncate
|
||||
}
|
||||
|
||||
$LOGDIR/web/*.log {
|
||||
missingok
|
||||
daily
|
||||
rotate 60
|
||||
# Must be consistent with prerotate script's settings!
|
||||
dateext
|
||||
dateyesterday
|
||||
dateformat %Y-%m-%d.
|
||||
extension log
|
||||
compress
|
||||
# We'll use our own copytruncate script, because:
|
||||
# * we haven't permission to change ownership, so the built-in copytruncate
|
||||
# method would fail (this is a bug in logrotate, I think);
|
||||
# * we haven't permission to reload the service, so the create new log method
|
||||
# doesn't work - the service would still write to the already rotated file.
|
||||
# The custom script:
|
||||
# * copytruncates files having (by default) .log extesion and compresses the copy
|
||||
# (and returns with 1 exit code, thus, the logrotate will skip this file).
|
||||
# * does nothing with files having any other extensions - e.g .1, .2 and so on
|
||||
# (and returns with 0 exit code, thus, the logrotate can process this file);
|
||||
prerotate
|
||||
$WORKERSCRIPT \$1
|
||||
endscript
|
||||
# Only if the prerotate script didn't process it yet.
|
||||
copytruncate
|
||||
}
|
||||
EOF
|
||||
fi
|
||||
|
||||
# Rotates the logs.
|
||||
"$LOGROTATE" -s "$STATEFILE" "$CONFFILE"
|
||||
|
||||
# That's all, Folks! :)
|
1
tools/shutdown
Symbolic link
1
tools/shutdown
Symbolic link
@ -0,0 +1 @@
|
||||
.launcher
|
68
tools/shutdown.d/100-docker-compose.sh
Executable file
68
tools/shutdown.d/100-docker-compose.sh
Executable file
@ -0,0 +1,68 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Stops a dockerized service defined by a docker-compose.yml file.
|
||||
# This is a general purpose stopper, doesn't requires customization.
|
||||
# Actually it is a humble wrapper script for a 'docker-compose down'
|
||||
# command.
|
||||
#
|
||||
# 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.
|
||||
|
||||
# Other initialisations.
|
||||
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 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 stop if it isn't running.
|
||||
if [ -z "$(cd "$BASE_DIR"; "$DOCKER_COMPOSE" ps --services --filter "status=running")" ]; then
|
||||
echo "$MSG_DOESNOTRUN" >&2; exit 1
|
||||
fi
|
||||
# Stops the service.
|
||||
(cd "$BASE_DIR"; "$DOCKER_COMPOSE" down)
|
||||
|
||||
# That's all, Folks!
|
19
tools/shutdown.d/110-notification-email.sh
Executable file
19
tools/shutdown.d/110-notification-email.sh
Executable file
@ -0,0 +1,19 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Sends a mail message about shutdown to the Linux user itself.
|
||||
# Hopes the appropriate message forward rule has been set.
|
||||
#
|
||||
# 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-05 v0.1 Initial release
|
||||
|
||||
MAIL=$(which mail)
|
||||
if [ -n "$MAIL" -a -x "$MAIL" ]; then
|
||||
subject="[Maintenance] A Docker service has been stopped intentionally on $HOSTNAME"
|
||||
message="This is a message from $0 script on $HOSTNAME.\n"
|
||||
message+="This Docker service has been stopped intentionally few seconds ago.\n\n"
|
||||
message+="Best regards: the Maintenance Bot"
|
||||
echo -e "$message" | "$MAIL" -s "$subject" "$USER"
|
||||
fi
|
||||
|
||||
# That's all, Folks!
|
1
tools/startup
Symbolic link
1
tools/startup
Symbolic link
@ -0,0 +1 @@
|
||||
.launcher
|
88
tools/startup.d/100-docker-compose-withlogs.sh
Executable file
88
tools/startup.d/100-docker-compose-withlogs.sh
Executable file
@ -0,0 +1,88 @@
|
||||
#!/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!
|
Reference in New Issue
Block a user