🗃️ Update codebase 🗃️

detect_and_update_services.sh
enhanced_functions.sh
rootfs/usr/local/bin/entrypoint.sh
rootfs/usr/local/etc/docker/functions/entrypoint.sh
test_solution.sh
This commit is contained in:
casjay
2025-10-31 11:45:46 -04:00
parent f5a819d9bb
commit 38bd66d87c
5 changed files with 215 additions and 539 deletions

View File

@@ -1,66 +0,0 @@
#!/usr/bin/env bash
# Script to detect services and update all repositories
detect_services_for_repo() {
local repo_dir="$1"
local init_dir="$repo_dir/rootfs/usr/local/etc/docker/init.d"
local dockerfile="$repo_dir/Dockerfile"
local entrypoint="$repo_dir/rootfs/usr/local/bin/entrypoint.sh"
local services_list=""
local init_system="tini"
echo "🔍 Analyzing repository: $(basename "$repo_dir")"
# Check if systemd is used instead of tini
if [ -f "$dockerfile" ] && grep -q "systemd.*enable\|systemctl.*enable" "$dockerfile"; then
init_system="systemd"
echo " 📋 Using systemd as init system"
else
echo " 📋 Using tini as init system"
fi
services_list="$init_system"
# Auto-detect services from init.d scripts
if [ -d "$init_dir" ]; then
echo " 📂 Scanning init.d directory: $init_dir"
for script in "$init_dir"/*.sh; do
if [ -f "$script" ]; then
# Extract service name (remove number prefix and .sh suffix)
local service=$(basename "$script" | sed 's/^[0-9]*-//;s|\.sh$||g')
services_list="$services_list,$service"
echo " ✅ Detected service: $service"
fi
done
else
echo " ⚠️ No init.d directory found"
fi
echo " 🎯 Final services list: $services_list"
echo ""
# Update the entrypoint.sh file if it exists
if [ -f "$entrypoint" ]; then
# Update SERVICES_LIST in entrypoint.sh
sed -i "s/^SERVICES_LIST=.*/SERVICES_LIST=\"$services_list\"/" "$entrypoint"
echo " ✏️ Updated SERVICES_LIST in entrypoint.sh"
else
echo " ⚠️ No entrypoint.sh found"
fi
return 0
}
# Test with bind repo first
echo "🧪 Testing service detection with bind repository"
echo "================================================="
detect_services_for_repo "/root/Projects/github/casjaysdevdocker/bind"
echo ""
echo "🚀 Ready to process all repositories"
echo "===================================="
echo "The script can now:"
echo "1. Auto-detect services from each repo's init.d scripts"
echo "2. Use tini as default init (or detect systemd if used)"
echo "3. Update each repo's SERVICES_LIST automatically"
echo "4. Apply the enhanced service supervision solution"

View File

@@ -1,184 +0,0 @@
#!/usr/bin/env bash
# Enhanced functions for proper service supervision
# Enhanced __no_exit function with service monitoring
__no_exit() {
local monitor_services="${SERVICES_LIST:-tini,named,nginx,php-fpm}"
local check_interval="${SERVICE_CHECK_INTERVAL:-30}"
local max_failures="${MAX_SERVICE_FAILURES:-3}"
local failure_counts=""
# Initialize failure counters
IFS=',' read -ra services <<< "$monitor_services"
for service in "${services[@]}"; do
failure_counts["$service"]=0
done
echo "Starting service supervisor - monitoring: $monitor_services"
echo "Check interval: ${check_interval}s, Max failures: $max_failures"
# Set up trap to handle termination
trap 'echo "🛑 Container terminating - cleaning up services"; kill $(jobs -p) 2>/dev/null; rm -f /run/*.pid /run/init.d/*.pid; exit 0' TERM INT
# Main supervision loop
while true; do
local failed_services=""
local running_services=""
# Check each service
IFS=',' read -ra services <<< "$monitor_services"
for service in "${services[@]}"; do
service="${service// /}" # trim whitespace
[ -z "$service" ] && continue
if __pgrep "$service" >/dev/null 2>&1; then
running_services="$running_services $service"
failure_counts["$service"]=0 # reset failure count on success
else
failed_services="$failed_services $service"
failure_counts["$service"]=$((${failure_counts["$service"]:-0} + 1))
echo "⚠️ Service '$service' not running (failure ${failure_counts["$service"]}/$max_failures)"
# Check if we've exceeded max failures for this service
if [ ${failure_counts["$service"]} -ge $max_failures ]; then
echo "💥 Service '$service' failed $max_failures times - terminating container"
echo "Failed services: $failed_services"
echo "Running services: $running_services"
kill -TERM 1 # Send TERM to init process (PID 1)
exit 1
fi
fi
done
# Log status every 10 checks (5 minutes with 30s interval)
if [ $(($(date +%s) % 300)) -lt $check_interval ]; then
echo "📊 Service status - Running:$running_services Failed:$failed_services"
# Write to start.log for backward compatibility
echo "$(date): Services running:$running_services failed:$failed_services" >> "/data/logs/start.log"
fi
sleep "$check_interval"
done &
# Keep the original behavior for log tailing
[ -f "/data/logs/start.log" ] && tail -f "/data/logs/start.log" >/dev/null 2>&1 &
wait
}
# Enhanced __start_init_scripts function with better error handling
__start_init_scripts() {
set -e
trap 'echo "❌ Fatal error in service startup - killing container"; rm -f /run/__start_init_scripts.pid; kill -TERM 1' ERR
[ "$1" = " " ] && shift 1
[ "$DEBUGGER" = "on" ] && echo "Enabling debugging" && set -o pipefail -x$DEBUGGER_OPTIONS || set -o pipefail
local basename=""
local init_pids=""
local retstatus="0"
local initStatus="0"
local failed_services=""
local successful_services=""
local init_dir="${1:-/usr/local/etc/docker/init.d}"
local init_count="$(find "$init_dir" -name "*.sh" 2>/dev/null | wc -l)"
if [ -n "$SERVICE_DISABLED" ]; then
echo "$SERVICE_DISABLED is disabled"
unset SERVICE_DISABLED
return 0
fi
echo "🚀 Starting container services initialization"
echo "Init directory: $init_dir"
echo "Services to start: $init_count"
# Create a new PID file to track this startup session
echo $$ > /run/__start_init_scripts.pid
mkdir -p "/tmp" "/run" "/run/init.d" "/usr/local/etc/docker/exec" "/data/logs/init"
chmod -R 777 "/tmp" "/run" "/run/init.d" "/usr/local/etc/docker/exec" "/data/logs/init"
if [ "$init_count" -eq 0 ] || [ ! -d "$init_dir" ]; then
echo "⚠️ No init scripts found in $init_dir"
# Still create a minimal keep-alive for containers without services
while true; do
echo "$(date): No services - container keep-alive" >> "/data/logs/start.log"
sleep 3600
done &
else
echo "📋 Found $init_count service scripts to execute"
if [ -d "$init_dir" ]; then
# Remove sample files
find "$init_dir" -name "*.sample" -delete 2>/dev/null
# Make scripts executable
find "$init_dir" -name "*.sh" -exec chmod 755 {} \; 2>/dev/null
# Execute scripts in order
for init in "$init_dir"/*.sh; do
if [ -x "$init" ]; then
basename="$(basename "$init")"
service="$(printf '%s' "$basename" | sed 's/^[0-9]*-//;s|\.sh$||g')"
printf '\n🔧 Executing service script: %s (service: %s)\n' "$init" "$service"
# Execute the init script
if eval "$init"; then
sleep 3 # Give service time to start
# Verify the service actually started
retPID=$(__get_pid "$service")
if [ -n "$retPID" ]; then
initStatus="0"
successful_services="$successful_services $service"
printf '✅ Service %s started successfully - PID: %s\n' "$service" "$retPID"
else
initStatus="1"
failed_services="$failed_services $service"
printf '❌ Service %s failed to start (no PID found)\n' "$service"
fi
else
initStatus="1"
failed_services="$failed_services $service"
printf '💥 Init script %s failed with exit code %s\n' "$init" "$?"
fi
else
printf '⚠️ Script %s is not executable, skipping\n' "$init"
fi
retstatus=$(($retstatus + $initStatus))
done
echo ""
printf '📊 Service startup summary:\n'
printf ' ✅ Successful: %s\n' "${successful_services:-none}"
printf ' ❌ Failed: %s\n' "${failed_services:-none}"
printf ' 📈 Total status code: %s\n' "$retstatus"
# If any critical services failed, exit the container
if [ $retstatus -gt 0 ]; then
echo "💥 Service startup failures detected - container will terminate"
echo "This allows the orchestrator (Docker/Kubernetes) to restart the container"
rm -f /run/__start_init_scripts.pid
exit $retstatus
fi
fi
fi
# Write startup completion status
{
echo "$(date): Container startup completed"
echo "Successful services: $successful_services"
[ -n "$failed_services" ] && echo "Failed services: $failed_services"
echo "Status code: $retstatus"
} >> "/data/logs/start.log"
printf '🎉 Service initialization completed successfully\n\n'
return $retstatus
}
# Export the enhanced functions
export -f __no_exit __start_init_scripts

View File

@@ -1,13 +1,13 @@
#!/usr/bin/env bash
# shellcheck shell=bash
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
##@Version : 202509161146-git
# - - - - - - - - - - - - - - - - - - - - - - - - -
##@Version : 202510311144-git
# @@Author : Jason Hempstead
# @@Contact : jason@casjaysdev.pro
# @@License : LICENSE.md
# @@License : WTFPL
# @@ReadME : entrypoint.sh --help
# @@Copyright : Copyright: (c) 2025 Jason Hempstead, Casjays Developments
# @@Created : Tuesday, Sep 16, 2025 11:46 EDT
# @@Created : Friday, Oct 31, 2025 11:44 EDT
# @@File : entrypoint.sh
# @@Description : Entrypoint file for bind
# @@Changelog : New script
@@ -17,33 +17,30 @@
# @@Terminal App : no
# @@sudo/root : no
# @@Template : other/docker-entrypoint
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# shellcheck disable=SC1001,SC1003,SC2001,SC2003,SC2016,SC2031,SC2120,SC2155,SC2199,SC2317,SC2329
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
set -e
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# run trap command on exit
trap 'echo "❌ Fatal error, killing container"; kill -TERM 1' ERR
trap 'retVal=$?;[ "$SERVICE_IS_RUNNING" != "yes" ] && [ -f "$SERVICE_PID_FILE" ] && rm -Rf "$SERVICE_PID_FILE";exit $retVal' SIGINT SIGTERM SIGPWR
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
trap 'retVal=$?;[ "$SERVICE_IS_RUNNING" != "yes" ] && [ -f "$SERVICE_PID_FILE" ] && rm -Rf "$SERVICE_PID_FILE";exit $retVal' INT TERM PWR
# - - - - - - - - - - - - - - - - - - - - - - - - -
# setup debugging - https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html
[ -f "/config/.debug" ] && [ -z "$DEBUGGER_OPTIONS" ] && export DEBUGGER_OPTIONS="$(<"/config/.debug")" || DEBUGGER_OPTIONS="${DEBUGGER_OPTIONS:-}"
{ [ "$DEBUGGER" = "on" ] || [ -f "/config/.debug" ]; } && echo "Enabling debugging" && set -o pipefail -x$DEBUGGER_OPTIONS && export DEBUGGER="on" || set -o pipefail
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
PATH="/usr/local/etc/docker/bin:/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Set bash options
SCRIPT_FILE="$0"
CONTAINER_NAME="bind"
SCRIPT_NAME="$(basename -- "$SCRIPT_FILE" 2>/dev/null)"
CONTAINER_NAME="${ENV_CONTAINER_NAME:-$CONTAINER_NAME}"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# remove whitespaces from beginning argument
while :; do [ "$1" = " " ] && shift 1 || break; done
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
[ "$1" = "$SCRIPT_FILE" ] && shift 1
[ "$1" = "$SCRIPT_NAME" ] && shift 1
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# import the functions file
if [ -f "/usr/local/etc/docker/functions/entrypoint.sh" ]; then
. "/usr/local/etc/docker/functions/entrypoint.sh"
@@ -51,7 +48,7 @@ else
echo "Can not load functions from /usr/local/etc/docker/functions/entrypoint.sh"
exit 1
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
case "$1" in
# Help message
-h | --help)
@@ -65,60 +62,60 @@ case "$1" in
shift
;;
esac
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Create the default env files
__create_env_file "/config/env/default.sh" "/root/env.sh" &>/dev/null
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# import variables from files
for set_env in "/root/env.sh" "/usr/local/etc/docker/env"/*.sh "/config/env"/*.sh; do
[ -f "$set_env" ] && . "$set_env"
done
unset set_env
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# User to use to launch service - IE: postgres
RUNAS_USER="root" # normally root
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Set user and group from env
SERVICE_USER="${PUID:-$SERVICE_USER}"
SERVICE_GROUP="${PGID:-$SERVICE_GROUP}"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Set user and group ID
SERVICE_UID="${SERVICE_UID:-0}" # set the user id
SERVICE_GID="${SERVICE_GID:-0}" # set the group id
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# User and group in which the service switches to - IE: nginx,apache,mysql,postgres
SERVICE_USER="${SERVICE_USER:-$bind}" # execute command as another user
SERVICE_USER="${SERVICE_USER:-bind}" # execute command as another user
SERVICE_GROUP="${SERVICE_GROUP:-bind}" # Set the service group
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Secondary ports
SERVER_PORTS="" # specifiy other ports
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Primary server port- will be added to server ports
WEB_SERVER_PORT="" # port : 80,443
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Healthcheck variables
HEALTH_ENABLED="yes" # enable healthcheck [yes/no]
SERVICES_LIST="tini" # comma seperated list of processes for the healthcheck
HEALTH_ENDPOINTS="" # url endpoints: [http://localhost/health,http://localhost/test]
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Update path var
export PATH RUNAS_USER SERVICE_USER SERVICE_GROUP SERVICE_UID SERVICE_GID WWW_ROOT_DIR DATABASE_DIR
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Custom variables
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# show message
__run_message() {
return
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
################## END OF CONFIGURATION #####################
# Lets get containers ip address
IP4_ADDRESS="$(__get_ip4)"
IP6_ADDRESS="$(__get_ip6)"
CONTAINER_IP4_ADDRESS="${CONTAINER_IP4_ADDRESS:-$IP4_ADDRESS}"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Startup variables
export INIT_DATE="${INIT_DATE:-$(date)}"
export CONTAINER_INIT="${CONTAINER_INIT:-no}"
@@ -128,14 +125,14 @@ export ENTRYPOINT_FIRST_RUN="${ENTRYPOINT_FIRST_RUN:-yes}"
export DATA_DIR_INITIALIZED="${DATA_DIR_INITIALIZED:-no}"
export CONFIG_DIR_INITIALIZED="${CONFIG_DIR_INITIALIZED:-no}"
export CONTAINER_NAME="${ENV_CONTAINER_NAME:-$CONTAINER_NAME}"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# System
export LANG="${LANG:-C.UTF-8}"
export LC_ALL="${LANG:-C.UTF-8}"
export TZ="${TZ:-${TIMEZONE:-America/New_York}}"
export HOSTNAME="$(hostname -s)"
export DOMAINNAME="$(hostname -d)"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Default directories
export SSL_DIR="${SSL_DIR:-/config/ssl}"
export SSL_CA="${SSL_CERT:-/config/ssl/ca.crt}"
@@ -145,12 +142,12 @@ export LOCAL_BIN_DIR="${LOCAL_BIN_DIR:-/usr/local/bin}"
export DEFAULT_DATA_DIR="${DEFAULT_DATA_DIR:-/usr/local/share/template-files/data}"
export DEFAULT_CONF_DIR="${DEFAULT_CONF_DIR:-/usr/local/share/template-files/config}"
export DEFAULT_TEMPLATE_DIR="${DEFAULT_TEMPLATE_DIR:-/usr/local/share/template-files/defaults}"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Backup settings
export BACKUP_MAX_DAYS="${BACKUP_MAX_DAYS:-}"
export BACKUP_RUN_CRON="${BACKUP_RUN_CRON:-}"
export BACKUP_DIR="${BACKUP_DIR:-/data/backups}"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Additional
export PHP_INI_DIR="${PHP_INI_DIR:-$(__find_php_ini)}"
export PHP_BIN_DIR="${PHP_BIN_DIR:-$(__find_php_bin)}"
@@ -163,7 +160,7 @@ export ENTRYPOINT_PID_FILE="${ENTRYPOINT_PID_FILE:-$ENTRYPOINT_PID_FILE}"
export ENTRYPOINT_INIT_FILE="${ENTRYPOINT_INIT_FILE:-/config/.entrypoint.done}"
export ENTRYPOINT_DATA_INIT_FILE="${ENTRYPOINT_DATA_INIT_FILE:-/data/.docker_has_run}"
export ENTRYPOINT_CONFIG_INIT_FILE="${ENTRYPOINT_CONFIG_INIT_FILE:-/config/.docker_has_run}"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
if [ -n "$CONTAINER_WEB_SERVER_WWW_REPO" ]; then
www_temp_dir="/tmp/git/$(basename -- "$CONTAINER_WEB_SERVER_WWW_REPO")"
rm -Rf "${WWW_ROOT_DIR:?}"/* "${www_temp_dir:?}"/*
@@ -173,42 +170,42 @@ if [ -n "$CONTAINER_WEB_SERVER_WWW_REPO" ]; then
rsync -ra "$www_temp_dir/" "$WWW_ROOT_DIR" --delete >/dev/null 2>&1
rm -Rf "$www_temp_dir"
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# variables based on env/files
[ -f "/config/enable/ssl" ] && SSL_ENABLED="yes"
[ -f "/config/enable/ssh" ] && SSH_ENABLED="yes"
[ "$WEB_SERVER_PORT" = "443" ] && SSL_ENABLED="yes"
[ "$CONTAINER_WEB_SERVER_PROTOCOL" = "https" ] && SSL_ENABLED="yes"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# export variables
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# is already Initialized
[ -f "$ENTRYPOINT_DATA_INIT_FILE" ] && DATA_DIR_INITIALIZED="yes" || DATA_DIR_INITIALIZED="no"
[ -f "$ENTRYPOINT_CONFIG_INIT_FILE" ] && CONFIG_DIR_INITIALIZED="yes" || CONFIG_DIR_INITIALIZED="no"
{ [ -f "$ENTRYPOINT_PID_FILE" ] || [ -f "$ENTRYPOINT_INIT_FILE" ]; } && ENTRYPOINT_FIRST_RUN="no" || ENTRYPOINT_FIRST_RUN="yes"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# clean ENV_PORTS variables
ENV_PORTS="${ENV_PORTS//,/ }" #
ENV_PORTS="${ENV_PORTS//\/*/}" #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# clean SERVER_PORTS variables
SERVER_PORTS="${SERVER_PORTS//,/ }" #
SERVER_PORTS="${SERVER_PORTS//\/*/}" #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# clean WEB_SERVER_PORTS variables
WEB_SERVER_PORTS="${WEB_SERVER_PORT//\/*/}" #
WEB_SERVER_PORTS="${WEB_SERVER_PORTS//\/*/}" #
WEB_SERVER_PORTS="${WEB_SERVER_PORT//,/ } ${ENV_WEB_SERVER_PORTS//,/ }" #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# rewrite and merge variables
ENV_PORTS="$(__format_variables "$ENV_PORTS" || false)"
WEB_SERVER_PORTS="$(__format_variables "$WEB_SERVER_PORTS" || false)"
ENV_PORTS="$(__format_variables "$SERVER_PORTS" "$WEB_SERVER_PORTS" "$ENV_PORTS" "$SERVER_PORTS" || false)"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Remove the commas from env
HEALTH_ENDPOINTS="${HEALTH_ENDPOINTS//,/ }"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# create required directories
mkdir -p "/run"
mkdir -p "/tmp"
@@ -221,11 +218,11 @@ mkdir -p "/run/init.d"
mkdir -p "/config/enable"
mkdir -p "/config/secure"
mkdir -p "/usr/local/etc/docker/exec"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# create required files
touch "/data/logs/start.log"
touch "/data/logs/entrypoint.log"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# fix permissions
chmod -f 777 "/run"
chmod -f 777 "/tmp"
@@ -239,48 +236,40 @@ chmod -f 777 "/config/enable"
chmod -f 777 "/config/secure"
chmod -f 777 "/data/logs/entrypoint.log"
chmod -f 777 "/usr/local/etc/docker/exec"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# lets ensure everyone can write to std*
[ -f "/dev/stdin" ] && chmod -f 777 "/dev/stdin"
[ -f "/dev/stderr" ] && chmod -f 777 "/dev/stderr"
[ -f "/dev/stdout" ] && chmod -f 777 "/dev/stdout"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
cat <<EOF | tee /etc/profile.d/locales.shadow /etc/profile.d/locales.sh >/dev/null
export LANG="\${LANG:-C.UTF-8}"
export LC_ALL="\${LANG:-C.UTF-8}"
export TZ="\${TZ:-\${TIMEZONE:-America/New_York}}"
EOF
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Create the backup dir
[ -n "$BACKUP_DIR" ] && { [ -d "$BACKUP_DIR" ] || mkdir -p "$BACKUP_DIR"; }
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if [ -f "$ENTRYPOINT_PID_FILE" ]; then
START_SERVICES="no"
touch "$ENTRYPOINT_PID_FILE"
else
echo "$$" >"$ENTRYPOINT_PID_FILE"
# Clean any stale PID files on first run
rm -f /run/init.d/*.pid 2>/dev/null || true
fi
# - - - - - - - - - - - - - - - - - - - - - - - - -
if [ -f "$ENTRYPOINT_INIT_FILE" ]; then
ENTRYPOINT_MESSAGE="no" ENTRYPOINT_FIRST_RUN="no"
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
if [ "$ENTRYPOINT_FIRST_RUN" != "no" ]; then
# Show start message
if [ "$CONFIG_DIR_INITIALIZED" = "no" ] || [ "$DATA_DIR_INITIALIZED" = "no" ]; then
[ "$ENTRYPOINT_MESSAGE" = "yes" ] && echo "Executing entrypoint script for bind"
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Set reusable variables
{ { [ -w "/etc" ] && [ ! -f "/etc/hosts" ]; } || [ -w "/etc/hosts" ]; } && UPDATE_FILE_HOSTS="yes" && touch "/etc/hosts"
{ { [ -w "/etc" ] && [ ! -f "/etc/timezone" ]; } || [ -w "/etc/timezone" ]; } && UPDATE_FILE_TZ="yes" && touch "/etc/timezone"
{ { [ -w "/etc" ] && [ ! -f "/etc/resolv.conf" ]; } || [ -w "/etc/resolv.conf" ]; } && UPDATE_FILE_RESOLV="yes" && touch "/etc/resolv.conf"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Set timezone
[ -n "$TZ" ] && [ "$UPDATE_FILE_TZ" = "yes" ] && echo "$TZ" >"/etc/timezone"
[ -f "/usr/share/zoneinfo/$TZ" ] && [ "$UPDATE_FILE_TZ" = "yes" ] && ln -sf "/usr/share/zoneinfo/$TZ" "/etc/localtime"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# if ipv6 add it to /etc/hosts
if [ "$UPDATE_FILE_HOSTS" = "yes" ]; then
echo "# known hostname mappings" >"/etc/hosts"
@@ -291,98 +280,104 @@ if [ "$ENTRYPOINT_FIRST_RUN" != "no" ]; then
__printf_space "40" "127.0.0.1" "localhost" >>"/etc/hosts"
fi
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# add .internal domain
if [ "$UPDATE_FILE_HOSTS" = "yes" ] && [ -n "$HOSTNAME" ]; then
__grep_test " $HOSTNAME" "/etc/hosts" || __printf_space "40" "${CONTAINER_IP4_ADDRESS:-127.0.0.1}" "$HOSTNAME" >>"/etc/hosts"
__grep_test " ${HOSTNAME%%.*}.internal" "/etc/hosts" || __printf_space "40" "${CONTAINER_IP4_ADDRESS:-127.0.0.1}" "${HOSTNAME%%.*}.internal" >>"/etc/hosts"
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# add domainname
if [ "$UPDATE_FILE_HOSTS" = "yes" ] && [ "$DOMAINNAME" != "internal" ] && [ -n "$DOMAINNAME" ] && [ "$HOSTNAME.$DOMAINNAME" != "$DOMAINNAME" ]; then
__grep_test " ${HOSTNAME%%.*}.$DOMAINNAME" "/etc/hosts" || __printf_space "40" "${CONTAINER_IP4_ADDRESS:-127.0.0.1}" "${HOSTNAME%%.*}.$DOMAINNAME" >>"/etc/hosts"
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Set containers hostname
[ -n "$HOSTNAME" ] && [ "$UPDATE_FILE_HOSTS" = "yes" ] && echo "$HOSTNAME" >"/etc/hostname"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
if [ -f "/etc/hostname" ]; then
[ -n "$(type -P hostname)" ] && hostname -F "/etc/hostname" &>/dev/null || HOSTNAME="$(<"/etc/hostname")"
export HOSTNAME
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# import hosts file into container
[ -f "/usr/local/etc/hosts" ] && [ "$UPDATE_FILE_HOSTS" = "yes" ] && cat "/usr/local/etc/hosts" | grep -vF "$HOSTNAME" >>"/etc/hosts"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# import resolv.conf file into container
[ "$CUSTOM_DNS" != "yes" ] && [ -f "/usr/local/etc/resolv.conf" ] && [ "$UPDATE_FILE_RESOLV" = "yes" ] && cat "/usr/local/etc/resolv.conf" >"/etc/resolv.conf"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
if [ -n "$HOME" ] && [ -d "/usr/local/etc/skel" ]; then
[ -d "$HOME" ] && cp -Rf "/usr/local/etc/skel/." "$HOME/"
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Delete any .gitkeep files
[ -d "/data" ] && rm -Rf "/data/.gitkeep" "/data"/*/*.gitkeep
[ -d "/config" ] && rm -Rf "/config/.gitkeep" "/config"/*/*.gitkeep
[ -f "/usr/local/bin/.gitkeep" ] && rm -Rf "/usr/local/bin/.gitkeep"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Setup bin directory - /config/bin > /usr/local/bin
__initialize_custom_bin_dir
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Copy default system configs - /usr/local/share/template-files/defaults > /config/
__initialize_default_templates
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Copy custom config files - /usr/local/share/template-files/config > /config/
__initialize_config_dir
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Copy custom data files - /usr/local/share/template-files/data > /data/
__initialize_data_dir
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__initialize_ssl_certs
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
if [ -f "$ENTRYPOINT_INIT_FILE" ]; then
ENTRYPOINT_FIRST_RUN="no"
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
if [ -d "/config" ]; then
echo "Initialized on: $INIT_DATE" >"$ENTRYPOINT_INIT_FILE"
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Check if this is a new container
if [ -f "$ENTRYPOINT_DATA_INIT_FILE" ]; then
DATA_DIR_INITIALIZED="yes"
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
if [ -d "/data" ]; then
echo "Initialized on: $INIT_DATE" >"$ENTRYPOINT_DATA_INIT_FILE"
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
if [ -f "$ENTRYPOINT_CONFIG_INIT_FILE" ]; then
CONFIG_DIR_INITIALIZED="yes"
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
if [ -d "/config" ]; then
echo "Initialized on: $INIT_DATE" >"$ENTRYPOINT_CONFIG_INIT_FILE"
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
if [ "$ENTRYPOINT_FIRST_RUN" != "no" ]; then
# setup the smtp server
__setup_mta
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# if no pid assume container restart - clean stale files on restart
if [ ! -f "$ENTRYPOINT_PID_FILE" ]; then
START_SERVICES="yes"
# Clean stale pid files from previous container runs
if [ -f "$ENTRYPOINT_PID_FILE" ]; then
START_SERVICES="no"
touch "$ENTRYPOINT_PID_FILE"
else
START_SERVICES=yes
# Clean any stale PID files on first run
rm -f /run/__start_init_scripts.pid /run/init.d/*.pid /run/*.pid 2>/dev/null || true
elif [ ! -f "/run/__start_init_scripts.pid" ]; then
START_SERVICES="yes"
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[ "$ENTRYPOINT_MESSAGE" = "yes" ] && __printf_space "40" "Container ip address is:" "$CONTAINER_IP4_ADDRESS"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
if [ ! -f "/run/__start_init_scripts.pid" ]; then
START_SERVICES="yes"
touch /run/__start_init_scripts.pid
fi
# - - - - - - - - - - - - - - - - - - - - - - - - -
[ "$ENTRYPOINT_MESSAGE" = "yes" ] && __printf_space "40" "The containers ip address is:" "$CONTAINER_IP4_ADDRESS"
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Show configured listing processes
if [ "$ENTRYPOINT_MESSAGE" = "yes" ] && [ -n "$ENV_PORTS" ]; then
show_port=""
@@ -390,22 +385,22 @@ if [ "$ENTRYPOINT_MESSAGE" = "yes" ] && [ -n "$ENV_PORTS" ]; then
__printf_space "40" "The following ports are open:" "$show_port"
unset port show_port
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# execute init script
if [ -f "/tmp/init" ]; then sh "/tmp/init"; fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# create user if needed
__create_service_user "$SERVICE_USER" "$SERVICE_GROUP" "${WORK_DIR:-/home/$SERVICE_USER}" "${SERVICE_UID:-}" "${SERVICE_GID:-}"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Modify user if needed
__set_user_group_id $SERVICE_USER ${SERVICE_UID:-} ${SERVICE_GID:-}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Show message
__run_message
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Just start services
START_SERVICES="${START_SERVICES:-SYSTEM_INIT}"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Start all services if no pidfile
if [ "$START_SERVICES" = "yes" ] && [ "$1" != "backup" ] && [ "$1" != "healthcheck" ] && [ "$1" != "cron" ] && [ "$1" != "tail" ] && [ "$1" != "logs" ] && [ "$1" != "cron" ]; then
[ "$1" = "start" ] && shift 1
@@ -417,7 +412,7 @@ if [ "$START_SERVICES" = "yes" ] && [ "$1" != "backup" ] && [ "$1" != "healthche
START_SERVICES="no"
CONTAINER_INIT="${CONTAINER_INIT:-no}"
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Begin options
case "$1" in
init)
@@ -603,8 +598,8 @@ start)
exit $?
;;
esac
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# end of entrypoint
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# ex: ts=2 sw=2 et filetype=sh

View File

@@ -1,6 +1,6 @@
#!/usr/bin/env bash
# shellcheck shell=bash
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
##@Version : 202407241259-git
# @@Author : Jason Hempstead
# @@Contact : git-admin@casjaysdev.pro
@@ -17,15 +17,15 @@
# @@Terminal App : no
# @@sudo/root : no
# @@Template : functions/docker-entrypoint
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# shellcheck disable=SC1001,SC1003,SC2001,SC2003,SC2016,SC2031,SC2120,SC2155,SC2199,SC2317,SC2329
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# setup debugging - https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html
[ -f "/config/.debug" ] && [ -z "$DEBUGGER_OPTIONS" ] && export DEBUGGER_OPTIONS="$(<"/config/.debug")" || DEBUGGER_OPTIONS="${DEBUGGER_OPTIONS:-}"
{ [ "$DEBUGGER" = "on" ] || [ -f "/config/.debug" ]; } && echo "Enabling debugging" && set -xo pipefail -x$DEBUGGER_OPTIONS && export DEBUGGER="on" || set -o pipefail
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
[ -f "/config/.debug" ] && [ -z "$DEBUGGER_OPTIONS" ] && export DEBUGGER_OPTIONS="$(<"/config/.debug")" || true
{ [ "$DEBUGGER" = "on" ] || [ -f "/config/.debug" ]; } && set -xo pipefail -x$DEBUGGER_OPTIONS && export DEBUGGER="on" || set -o pipefail
# - - - - - - - - - - - - - - - - - - - - - - - - -
__remove_extra_spaces() { sed 's/\( \)*/\1/g;s|^ ||g'; }
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__printf_space() {
local pad=$(printf '%0.1s' " "{1..60})
local padlength=$1
@@ -37,7 +37,7 @@ __printf_space() {
message+="$(printf '%s\n' "$string2") "
printf '%s\n' "$message"
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__rm() { [ -n "$1" ] && [ -e "$1" ] && rm -Rf "${1:?}"; }
__grep_test() { grep -sh "$1" "$2" | grep -qwF "${3:-$1}" || return 1; }
__netstat() { [ -f "$(type -P netstat)" ] && netstat "$@" || return 10; }
@@ -52,7 +52,8 @@ __ps() { [ -f "$(type -P ps)" ] && ps "$@" 2>/dev/null | sed 's|:||g' | grep -Fw
__is_dir_empty() { if [ -n "$1" ]; then [ "$(ls -A "$1" 2>/dev/null | wc -l)" -eq 0 ] && return 0 || return 1; else return 1; fi; }
__get_ip6() { ip a 2>/dev/null | grep -w 'inet6' | awk '{print $2}' | grep -vE '^::1|^fe' | sed 's|/.*||g' | head -n1 | grep '.' || echo ''; }
__get_ip4() { ip a 2>/dev/null | grep -w 'inet' | awk '{print $2}' | grep -vE '^127.0.0' | sed 's|/.*||g' | head -n1 | grep '.' || echo '127.0.0.1'; }
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
__find_and_remove() { find "${2:-/etc}" -iname "$1" -exec rm -Rfv {} \; 2>/dev/null; }
# - - - - - - - - - - - - - - - - - - - - - - - - -
__pgrep() {
local count=3
local srvc="${1:-SERVICE_NAME}"
@@ -64,17 +65,17 @@ __pgrep() {
done
return 10
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__find_file_relative() {
[ -e "$1" ] || return 0
find "$1"/* -not -path '*env/*' -not -path '.git*' -type f 2>/dev/null | sed 's|'$1'/||g' | sort -u | grep -v '^$' | grep '.' || false
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__find_directory_relative() {
[ -d "$1" ] || return 0
find "$1"/* -not -path '*env/*' -not -path '.git*' -type d 2>/dev/null | sed 's|'$1'/||g' | sort -u | grep -v '^$' | grep '.' || false
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__pid_exists() {
local result=""
result="$(ps -ax --no-header 2>/dev/null | sed 's/^[[:space:]]*//g' | awk -F' ' '{print $1}' | sed 's|:||g' | grep '[0-9]' | sort -uV | grep "^$1$" 2>/dev/null || echo '')"
@@ -95,9 +96,9 @@ __get_pid() {
return 1
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__format_variables() { printf '%s\n' "${@//,/ }" | tr ' ' '\n' | sort -RVu | grep -v '^$' | tr '\n' ' ' | __clean_variables | grep '.' || return 0; }
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__clean_variables() {
local var="$*"
var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters
@@ -105,7 +106,7 @@ __clean_variables() {
var="$(printf '%s\n' "$var" | sed 's/\( \)*/\1/g;s|^ ||g')"
printf '%s' "$var" | grep -v '^$'
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__no_exit() {
local monitor_interval="${SERVICE_MONITOR_INTERVAL:-60}"
local failure_threshold="${SERVICE_FAILURE_THRESHOLD:-3}"
@@ -145,7 +146,7 @@ __no_exit() {
wait
"
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__trim() {
local var="${*//;/ }"
var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters
@@ -153,14 +154,14 @@ __trim() {
var="$(echo "$var" | __remove_extra_spaces | sed "s| |; |g;s|;$| |g" | __remove_extra_spaces)"
printf '%s' "$var" | sed 's|;||g' | grep -v '^$'
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__banner() {
local message="$*"
local total_width=80
local content_width=$((total_width - 14)) # Account for "# - - - " and " - - - #"
printf '# - - - %-*s - - - #\n' "$content_width" "$message"
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__service_banner() {
local icon="${1:-🔧}"
local message="${2:-Processing}"
@@ -172,23 +173,23 @@ __service_banner() {
local text_width=$((content_width - icon_width * 2 - 2)) # Account for both icons and spaces
printf '# - - - %s %-*s %s - - - #\n' "$icon" "$text_width" "$full_message" "$icon"
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__find_php_bin() { find -L '/usr'/*bin -maxdepth 4 -name 'php-fpm*' 2>/dev/null | head -n1 | grep '.' || echo ''; }
__find_php_ini() { find -L '/etc' -maxdepth 4 -name 'php.ini' 2>/dev/null | head -n1 | sed 's|/php.ini||g' | grep '.' || echo ''; }
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__find_nginx_conf() { find -L '/etc' -maxdepth 4 -name 'nginx.conf' 2>/dev/null | head -n1 | grep '.' || echo ''; }
__find_caddy_conf() { find -L '/etc' -maxdepth 4 -type f -iname 'caddy.conf' 2>/dev/null | head -n1 | grep '.' || echo ''; }
__find_lighttpd_conf() { find -L '/etc' -maxdepth 4 -type f -iname 'lighttpd.conf' 2>/dev/null | head -n1 | grep '.' || echo ''; }
__find_cherokee_conf() { find -L '/etc' -maxdepth 4 -type f -iname 'cherokee.conf' 2>/dev/null | head -n1 | grep '.' || echo ''; }
__find_httpd_conf() { find -L '/etc' -maxdepth 4 -type f -iname 'httpd.conf' -o -iname 'apache2.conf' 2>/dev/null | head -n1 | grep '.' || echo ''; }
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__find_mysql_conf() { find -L '/etc' -maxdepth 4 -type f -name 'my.cnf' 2>/dev/null | head -n1 | grep '.' || echo ''; }
__find_pgsql_conf() { find -L '/var/lib' '/etc' -maxdepth 8 -type f -name 'postgresql.conf' 2>/dev/null | head -n1 | grep '.' || echo ''; }
__find_couchdb_conf() { return; }
__find_mongodb_conf() { return; }
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__random_password() { cat "/dev/urandom" | tr -dc '0-9a-zA-Z' | head -c${1:-16} && echo ""; }
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__init_working_dir() {
local service_name="$SERVICE_NAME" # get service name
local workdir="$(eval echo "${WORK_DIR:-}")" # expand variables
@@ -206,15 +207,15 @@ __init_working_dir() {
[ -n "$workdir" ] && { [ -d "$workdir" ] || mkdir -p "$workdir"; }
[ "$SERVICE_USER" = "root" ] || [ -d "$home" ] && chmod -f 777 "$home"
[ "$SERVICE_USER" = "root" ] || [ -d "$workdir" ] && chmod -f 777 "$workdir"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# cd to dir
__cd "${workdir:-$home}"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
echo "Setting the working directory to: $PWD"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
export WORK_DIR="$workdir" HOME="$home"
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__exec_service() {
local count=6
echo "Starting $1"
@@ -224,7 +225,7 @@ __exec_service() {
__pgrep $1 && touch "/run/init.d/$1.pid" && break || count=$((count - 1))
done
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__update_ssl_certs() {
[ -f "/config/env/ssl.sh" ] && . "/config/env/ssl.sh"
if [ -f "$SSL_CERT" ] && [ -f "$SSL_KEY" ]; then
@@ -234,7 +235,7 @@ __update_ssl_certs() {
[ -f "$SSL_CERT" ] && cp -Rf "$SSL_CERT" "/etc/ssl/$SSL_CERT"
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__certbot() {
[ -n "$(type -P 'certbot')" ] || return 1
local options="$1"
@@ -295,7 +296,7 @@ __certbot() {
[ $statusCode -eq 0 ] && __update_ssl_certs
return $statusCode
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__display_user_info() {
if [ -n "$user_name" ] || [ -n "$user_pass" ] || [ -n "$root_user_name" ] || [ -n "$root_user_pass" ]; then
__banner "User info"
@@ -306,7 +307,7 @@ __display_user_info() {
__banner ""
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__init_config_etc() {
local copy="no"
local name="$(find "/etc/$SERVICE_NAME" -maxdepth 0 2>/dev/null | head -n1)"
@@ -321,7 +322,7 @@ __init_config_etc() {
__copy_templates "$etc_dir" "$conf_dir"
fi
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
}
__create_ssl_cert() {
local SSL_DIR="${SSL_DIR:-/etc/ssl}"
@@ -353,7 +354,7 @@ __create_ssl_cert() {
return 2
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__init_apache() {
local etc_dir="" conf_dir="" conf_dir="" www_dir="" apache_bin=""
etc_dir="/etc/${1:-apache2}"
@@ -363,7 +364,7 @@ __init_apache() {
#
return 0
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__init_nginx() {
local etc_dir="/etc/${1:-nginx}"
local conf_dir="/config/${1:-nginx}"
@@ -371,14 +372,14 @@ __init_nginx() {
local nginx_bin="$(type -P 'nginx')"
return 0
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__init_php() {
local etc_dir="/etc/${1:-php}"
local conf_dir="/config/${1:-php}"
local php_bin="${PHP_BIN_DIR:-$(__find_php_bin)}"
return 0
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__init_mysql() {
local db_dir="/data/db/mysql"
local etc_dir="${home:-/etc/${1:-mysql}}"
@@ -391,28 +392,28 @@ __init_mysql() {
local mysqld_bin="$(type -P 'mysqld')"
return 0
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__init_mongodb() {
local home="${MONGODB_CONFIG_FILE:-$(__find_mongodb_conf)}"
local user_name="${INITDB_ROOT_USERNAME:-root}"
local user_pass="${MONGO_INITDB_ROOT_PASSWORD:-$_ROOT_PASSWORD}"
return
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__init_postgres() {
local home="${PGSQL_CONFIG_FILE:-$(__find_pgsql_conf)}"
local user_name="${POSTGRES_USER:-root}"
local user_pass="${POSTGRES_PASSWORD:-$POSTGRES_ROOT_PASSWORD}"
return
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__init_couchdb() {
local home="${COUCHDB_CONFIG_FILE:-$(__find_couchdb_conf)}"
local user_name="${COUCHDB_USER:-root}"
local user_pass="${COUCHDB_PASSWORD:-$SET_RANDOM_PASS}"
return
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Show available init functions
__init_help() {
echo '
@@ -422,7 +423,7 @@ __create_ssl_cert
'
return
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__run_once() {
if [ "$CONFIG_DIR_INITIALIZED" = "false" ] || [ "$DATA_DIR_INITIALIZED" = "false" ] || [ ! -f "/config/.docker_has_run" ]; then
return 0
@@ -430,7 +431,7 @@ __run_once() {
return 1
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# run program ever n minutes
__cron() {
trap 'retVal=$?;[ -f "/run/cron/$bin.run" ] && rm -Rf "/run/cron/$bin.run";[ -f "/run/cron/$bin.pid" ] && rm -Rf "/run/cron/$bin.pid";exit ${retVal:-0}' SIGINT ERR EXIT
@@ -449,19 +450,19 @@ __cron() {
[ -f "/run/cron/$bin.run" ] || break
done 2>/dev/stderr >>"/data/logs/cron.log"
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__replace() {
local search="$1" replace="$2" file="${3:-$2}"
[ -e "$file" ] || return 1
__sed "$search" "$replace" "$file" || return 0
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__find_replace() {
local search="$1" replace="$2" file="${3:-$2}"
[ -e "$file" ] || return 1
find "$file" -type f -not -path '.git*' -exec sed -i "s|$search|$replace|g" {} \; 2>/dev/null
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# /config > /etc
__copy_templates() {
local from="$1" to="$2"
@@ -471,7 +472,7 @@ __copy_templates() {
__file_copy "$from" "$to"
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# /config/file > /etc/file
__symlink() {
local from="$1" to="$2"
@@ -480,7 +481,7 @@ __symlink() {
ln -sf "$to" "$from" && echo "Created symlink to $from > $to"
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__file_copy() {
local from="$1"
local dest="$2"
@@ -510,7 +511,7 @@ __file_copy() {
fi
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__generate_random_uids() {
local set_random_uid="$(seq 100 999 | sort -R | head -n 1)"
while :; do
@@ -522,7 +523,7 @@ __generate_random_uids() {
fi
done
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__setup_directories() {
APPLICATION_DIRS="${APPLICATION_DIRS//,/ }"
APPLICATION_FILES="${APPLICATION_FILES//,/ }"
@@ -555,7 +556,7 @@ __setup_directories() {
fi
done
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# set user on files/folders
__fix_permissions() {
change_user="${1:-${SERVICE_USER:-root}}"
@@ -580,14 +581,14 @@ __fix_permissions() {
fi
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__get_gid() { grep "^$1:" /etc/group | awk -F ':' '{print $3}' || false; }
__get_uid() { grep "^$1:" /etc/passwd | awk -F ':' '{print $3}' || false; }
__check_for_uid() { cat "/etc/passwd" 2>/dev/null | awk -F ':' '{print $3}' | sort -u | grep -q "^$1$" || false; }
__check_for_guid() { cat "/etc/group" 2>/dev/null | awk -F ':' '{print $3}' | sort -u | grep -q "^$1$" || false; }
__check_for_user() { cat "/etc/passwd" 2>/dev/null | awk -F ':' '{print $1}' | sort -u | grep -q "^$1$" || false; }
__check_for_group() { cat "/etc/group" 2>/dev/null | awk -F ':' '{print $1}' | sort -u | grep -q "^$1$" || false; }
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# check if process is already running
__proc_check() {
cmd_bin="$(type -P "${1:-$EXEC_CMD_BIN}")"
@@ -601,7 +602,7 @@ __proc_check() {
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__set_user_group_id() {
local exitStatus=0
local set_user="${1:-$SERVICE_USER}"
@@ -614,16 +615,16 @@ __set_user_group_id() {
[ -n "$set_user" ] && [ "$set_user" != "root" ] || return
if grep -shq "^$set_user:" "/etc/passwd" "/etc/group"; then
if __check_for_guid "$set_gid"; then
groupmod -g "${set_gid}" $set_user 2>/dev/stderr | tee -p -a "/data/logs/init.txt" >/dev/null && chown -Rf ":$set_gid"
groupmod -g "${set_gid}" $set_user 2>/dev/stderr | tee -p -a "/data/logs/init.txt" >/dev/null
fi
if __check_for_uid "$set_uid"; then
usermod -u "${set_uid}" -g "${set_gid}" $set_user 2>/dev/stderr | tee -p -a "/data/logs/init.txt" >/dev/null && chown -Rf $set_uid:$set_gid
usermod -u "${set_uid}" -g "${set_gid}" $set_user 2>/dev/stderr | tee -p -a "/data/logs/init.txt" >/dev/null
fi
fi
export SERVICE_UID="$set_uid"
export SERVICE_GID="$set_gid"
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__create_service_user() {
local exitStatus=0
local create_user="${1:-$SERVICE_USER}"
@@ -634,7 +635,9 @@ __create_service_user() {
local random_id="$(__generate_random_uids)"
local create_home_dir="${create_home_dir:-/home/$create_user}"
grep -shq "^$create_user:" "/etc/passwd" && grep -shq "^$create_group:" "/etc/group" && return
[ "$create_user" = "root" ] && [ "$create_group" = "root" ] && return 0
if [ "$create_user" = "root" ] && [ "$create_group" = "root" ]; then
return 0
fi
if [ "$RUNAS_USER" != "root" ] && [ "$RUNAS_USER" != "" ]; then
create_user="$RUNAS_USER"
create_group="$RUNAS_USER"
@@ -653,17 +656,17 @@ __create_service_user() {
break
fi
done
if ! __check_for_group "$create_group"; then
if [ -n "$create_group" ] && ! __check_for_group "$create_group"; then
echo "creating system group $create_group"
groupadd --force --system -g $create_gid $create_group 2>/dev/stderr | tee -p -a "/data/logs/init.txt" >/dev/null
fi
if ! __check_for_user "$create_user"; then
echo "creating system user $create_user"
useradd --system -u $create_uid -g $create_group -c "Account for $create_user" -d "$create_home_dir" -s /bin/false $create_user 2>/dev/stderr | tee -p -a "/data/logs/init.txt" >/dev/null
fi
groupadd --force --system -g $create_gid $create_group 2>/dev/stderr | tee -a "/data/logs/init.txt" >/dev/null
grep -shq "$create_group" "/etc/group" || exitStatus=$((exitStatus + 1))
grep -shq "$create_user" "/etc/passwd" || exitStatus=$((exitCode + 1))
if [ $exitStatus -eq 0 ]; then
fi
if [ -n "$create_user" ] && ! __check_for_user "$create_user"; then
echo "creating system user $create_user"
useradd --system --uid $create_uid --gid $create_group --comment "Account for $create_user" --home-dir "$create_home_dir" --shell /bin/false $create_user 2>/dev/stderr | tee -a "/data/logs/init.txt" >/dev/null
grep -shq "$create_user" "/etc/passwd" || exitStatus=$((exitStatus + 1))
fi
if [ $exitStatus -eq 0 ] && [ -n "$create_group" ] && [ -n "$create_user" ]; then
export WORK_DIR="${create_home_dir:-}"
if [ -n "$WORK_DIR" ]; then
[ -d "$WORK_DIR" ] || mkdir -p "$WORK_DIR"
@@ -674,20 +677,22 @@ __create_service_user() {
elif [ -f "/etc/sudoers" ] && ! grep -qs "$create_user" "/etc/sudoers"; then
echo "$create_user ALL=(ALL) NOPASSWD: ALL" >"/etc/sudoers"
fi
export SERVICE_UID="$create_uid"
export SERVICE_GID="$create_gid"
export SERVICE_USER="$create_user"
export SERVICE_GROUP="$create_group"
exitStatus=0
SERVICE_UID="$create_uid"
SERVICE_GID="$create_gid"
SERVICE_USER="$create_user"
SERVICE_GROUP="$create_group"
else
export USER_UID=0
export USER_GID=0
export SERVICE_USER=root
export SERVICE_GROUP=root
SERVICE_UID=0
SERVICE_GID=0
SERVICE_USER=root
SERVICE_GROUP=root
exitStatus=2
fi
export SERVICE_UID SERVICE_GID SERVICE_USER SERVICE_GROUP
return $exitStatus
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__create_env_file() {
local dir=""
local envStatus=0
@@ -707,7 +712,7 @@ EOF
rm -f "$sample_file"
return $envStatus
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__exec_command() {
local bin=""
local arg=("$@")
@@ -730,7 +735,7 @@ __exec_command() {
fi
return $exitCode
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Setup the server init scripts
__start_init_scripts() {
[ "$1" = " " ] && shift 1
@@ -741,6 +746,7 @@ __start_init_scripts() {
local retstatus="0"
local initStatus="0"
local critical_failures="0"
local pidFile="/run/__start_init_scripts.pid"
local init_dir="${1:-/usr/local/etc/docker/init.d}"
local init_count="$(ls -A "$init_dir"/* 2>/dev/null | grep -v '\.sample' | wc -l)"
local exit_on_failure="${EXIT_ON_SERVICE_FAILURE:-true}"
@@ -751,7 +757,7 @@ __start_init_scripts() {
rm -f /run/*.pid /run/init.d/*.pid 2>/dev/null || true
fi
touch /run/__start_init_scripts.pid
touch "$pidFile"
if [ "$init_count" -eq 0 ] || [ ! -d "$init_dir" ]; then
mkdir -p "/data/logs/init"
@@ -769,6 +775,7 @@ __start_init_scripts() {
for init in "$init_dir"/*.sh; do
if [ -x "$init" ]; then
touch "$pidFile"
name="$(basename "$init")"
service="$(printf '%s' "$name" | sed 's/^[^-]*-//;s|.sh$||g')"
__service_banner "🔧" "Executing service script:" "$(basename "$init")"
@@ -859,7 +866,7 @@ __start_init_scripts() {
printf '%s\n' "$SERVICE_NAME started on $(date)" >"/data/logs/start.log"
return $retstatus
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__setup_mta() {
[ -d "/etc/ssmtp" ] || [ -d "/etc/postfix" ] || return
if [ ! -d "/config/ssmtp" ] || [ ! -d "/config/postfix" ]; then
@@ -966,7 +973,7 @@ EOF
[ -f "/root/dead.letter" ] && __rm "/root/dead.letter"
return $exitCode
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__initialize_web_health() {
local www_dir="${1:-${WWW_ROOT_DIR:-/usr/local/share/httpd/default}}"
if [ -d "$www_dir" ]; then
@@ -975,7 +982,7 @@ __initialize_web_health() {
__find_replace "REPLACE_LAST_UPDATED_ON_MESSAGE" "${LAST_UPDATED_ON_MESSAGE:-$(date +'Last updated on: %Y-%m-%d at %H:%M:%S')}" "/usr/local/share/httpd"
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# file_dir
__initialize_replace_variables() {
local set_dir="" get_dir="$*"
@@ -1018,7 +1025,7 @@ __initialize_replace_variables() {
mkdir -p "${TMP_DIR:-/tmp/$SERVICE_NAME}" "${RUN_DIR:-/run/$SERVICE_NAME}" "${LOG_DIR:-/data/logs/$SERVICE_NAME}"
chmod -f 777 "${TMP_DIR:-/tmp/$SERVICE_NAME}" "${RUN_DIR:-/run/$SERVICE_NAME}" "${LOG_DIR:-/data/logs/$SERVICE_NAME}"
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__initialize_database() {
[ "$IS_DATABASE_SERVICE" = "yes" ] || [ "$USES_DATABASE_SERVICE" = "yes" ] || return 0
local dir="${1:-$ETC_DIR}"
@@ -1049,7 +1056,7 @@ __initialize_database() {
__find_replace "REPLACE_DATABASE_DIR" "$DATABASE_DIR" "/etc"
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__initialize_db_users() {
[ "$IS_DATABASE_SERVICE" = "yes" ] || [ "$USES_DATABASE_SERVICE" = "yes" ] || return 0
db_normal_user="${DATABASE_USER_NORMAL:-$user_name}"
@@ -1062,7 +1069,7 @@ __initialize_db_users() {
export DATABASE_PASS_ROOT="$db_admin_pass"
export db_normal_user db_normal_pass db_admin_user db_admin_pass
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__initialize_system_etc() {
local conf_dir="$1"
local dir=""
@@ -1089,7 +1096,7 @@ __initialize_system_etc() {
done
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__initialize_custom_bin_dir() {
local SET_USR_BIN=""
[ -d "/data/bin" ] && SET_USR_BIN+="$(__find /data/bin f) "
@@ -1107,7 +1114,7 @@ __initialize_custom_bin_dir() {
unset create_bin_template create_bin_name SET_USR_BIN
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__initialize_default_templates() {
if [ -n "$DEFAULT_TEMPLATE_DIR" ]; then
if [ "$CONFIG_DIR_INITIALIZED" = "false" ] && [ -d "/config" ]; then
@@ -1127,7 +1134,7 @@ __initialize_default_templates() {
fi
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__initialize_config_dir() {
if [ -n "$DEFAULT_CONF_DIR" ]; then
if [ "$CONFIG_DIR_INITIALIZED" = "false" ] && [ -d "/config" ]; then
@@ -1147,7 +1154,7 @@ __initialize_config_dir() {
fi
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__initialize_data_dir() {
if [ -d "/data" ]; then
if [ "$DATA_DIR_INITIALIZED" = "false" ] && [ -n "$DEFAULT_DATA_DIR" ]; then
@@ -1167,7 +1174,7 @@ __initialize_data_dir() {
fi
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__initialize_www_root() {
local WWW_INIT=""
local WWW_TEMPLATE=""
@@ -1180,7 +1187,7 @@ __initialize_www_root() {
fi
__initialize_web_health "$WWW_ROOT_DIR"
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__is_htdocs_mounted() {
WWW_ROOT_DIR="${WWW_ROOT_DIR:-/data/htdocs}"
[ -n "$ENV_WWW_ROOT_DIR" ] && WWW_ROOT_DIR="$ENV_WWW_ROOT_DIR"
@@ -1207,7 +1214,7 @@ __is_htdocs_mounted() {
[ -d "$WWW_ROOT_DIR" ] || mkdir -p "$WWW_ROOT_DIR"
export WWW_ROOT_DIR="${WWW_ROOT_DIR:-/usr/local/share/httpd/default}"
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__initialize_ssl_certs() {
[ "$SSL_ENABLED" = "yes" ] && __certbot
if [ -d "/config/letsencrypt" ]; then
@@ -1234,7 +1241,7 @@ __initialize_ssl_certs() {
fi
type update-ca-certificates &>/dev/null && update-ca-certificates &>/dev/null
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__start_php_dev_server() {
if [ "$2" = "yes" ]; then
if [ -d "/usr/local/share/httpd" ]; then
@@ -1247,7 +1254,7 @@ __start_php_dev_server() {
fi
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__check_service() {
if [ "$1" = "check" ]; then
shift $#
@@ -1255,7 +1262,7 @@ __check_service() {
exit $?
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
__switch_to_user() {
if [ "$RUNAS_USER" = "root" ]; then
su_exec=""
@@ -1278,7 +1285,7 @@ __switch_to_user() {
fi
export su_exec
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# usage backup "days" "hours"
__backup() {
local dirs="" backup_dir backup_name backup_exclude runTime cronTime maxDays
@@ -1316,7 +1323,7 @@ __backup() {
[ -n "$cronTime" ] && runTime=$((cronTime * 3600)) || return $exitStatus
sleep $runTime && __backup "$maxDays" "$cronTime"
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# set variables from function calls
export INIT_DATE="${INIT_DATE:-$(date)}"
export START_SERVICES="${START_SERVICES:-yes}"
@@ -1324,13 +1331,13 @@ export ENTRYPOINT_MESSAGE="${ENTRYPOINT_MESSAGE:-yes}"
export ENTRYPOINT_FIRST_RUN="${ENTRYPOINT_FIRST_RUN:-yes}"
export DATA_DIR_INITIALIZED="${DATA_DIR_INITIALIZED:-false}"
export CONFIG_DIR_INITIALIZED="${CONFIG_DIR_INITIALIZED:-false}"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# System
export LANG="${LANG:-C.UTF-8}"
export LC_ALL="${LANG:-C.UTF-8}"
export TZ="${TZ:-${TIMEZONE:-America/New_York}}"
export HOSTNAME="${FULL_DOMAIN_NAME:-${SERVER_HOSTNAME:-$HOSTNAME}}"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Default directories
export SSL_DIR="${SSL_DIR:-/config/ssl}"
export SSL_CA="${SSL_CERT:-/config/ssl/ca.crt}"
@@ -1340,15 +1347,15 @@ export LOCAL_BIN_DIR="${LOCAL_BIN_DIR:-/usr/local/bin}"
export DEFAULT_DATA_DIR="${DEFAULT_DATA_DIR:-/usr/local/share/template-files/data}"
export DEFAULT_CONF_DIR="${DEFAULT_CONF_DIR:-/usr/local/share/template-files/config}"
export DEFAULT_TEMPLATE_DIR="${DEFAULT_TEMPLATE_DIR:-/usr/local/share/template-files/defaults}"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Backup settings
export BACKUP_MAX_DAYS="${BACKUP_MAX_DAYS:-}"
export BACKUP_RUN_CRON="${BACKUP_RUN_CRON:-}"
export BACKUP_DIR="${BACKUP_DIR:-/data/backups}"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
CONTAINER_IP4_ADDRESS="${CONTAINER_IP4_ADDRESS:-$(__get_ip4)}"
CONTAINER_IP6_ADDRESS="${CONTAINER_IP6_ADDRESS:-$(__get_ip6)}"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# Additional
export WORK_DIR="${ENV_WORK_DIR:-$WORK_DIR}"
export SET_RANDOM_PASS="${SET_RANDOM_PASS:-$(__random_password 16)}"
@@ -1366,15 +1373,15 @@ export ENTRYPOINT_PID_FILE="${ENTRYPOINT_PID_FILE:-/run/init.d/entrypoint.pid}"
export ENTRYPOINT_INIT_FILE="${ENTRYPOINT_INIT_FILE:-/config/.entrypoint.done}"
export ENTRYPOINT_DATA_INIT_FILE="${ENTRYPOINT_DATA_INIT_FILE:-/data/.docker_has_run}"
export ENTRYPOINT_CONFIG_INIT_FILE="${ENTRYPOINT_CONFIG_INIT_FILE:-/config/.docker_has_run}"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# is already Initialized
[ -z "$DATA_DIR_INITIALIZED" ] && { [ -f "$ENTRYPOINT_DATA_INIT_FILE" ] && DATA_DIR_INITIALIZED="true" || DATA_DIR_INITIALIZED="false"; }
[ -z "$CONFIG_DIR_INITIALIZED" ] && { [ -f "$ENTRYPOINT_CONFIG_INIT_FILE" ] && CONFIG_DIR_INITIALIZED="true" || CONFIG_DIR_INITIALIZED="false"; }
[ -z "$ENTRYPOINT_FIRST_RUN" ] && { { [ -f "$ENTRYPOINT_PID_FILE" ] || [ -f "$ENTRYPOINT_INIT_FILE" ]; } && ENTRYPOINT_FIRST_RUN="no" || ENTRYPOINT_FIRST_RUN="true"; }
export ENTRYPOINT_DATA_INIT_FILE DATA_DIR_INITIALIZED ENTRYPOINT_CONFIG_INIT_FILE CONFIG_DIR_INITIALIZED
export ENTRYPOINT_PID_FILE ENTRYPOINT_INIT_FILE ENTRYPOINT_FIRST_RUN
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# export the functions
export -f __get_pid __start_init_scripts __is_running __certbot __update_ssl_certs __create_ssl_cert
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - -
# end of functions

View File

@@ -1,76 +0,0 @@
#!/usr/bin/env bash
# Test script to validate the enhanced service supervision solution
echo "🧪 Testing Enhanced Service Supervision Solution"
echo "================================================"
# Test the enhanced functions
cd /root/Projects/github/casjaysdevdocker/bind
echo ""
echo "📋 Solution Summary:"
echo "-------------------"
echo "✅ Enhanced __start_init_scripts function:"
echo " - Better error handling with immediate container exit on service failures"
echo " - Improved service verification after startup"
echo " - Detailed logging and status reporting"
echo " - Proper cleanup of stale PID files on restart"
echo ""
echo "✅ Enhanced __no_exit function (service supervisor):"
echo " - Continuous monitoring of all services"
echo " - Configurable failure thresholds (default: 3 failures per service)"
echo " - Container termination when critical services fail"
echo " - Periodic status logging"
echo " - Graceful cleanup on container shutdown"
echo ""
echo "✅ Fixed container restart issues:"
echo " - Stale PID files are cleaned up on restart"
echo " - Services restart properly after container restart"
echo " - No more 'zombie' containers that appear running but have dead services"
echo ""
echo "🔧 Key Improvements Made:"
echo "------------------------"
echo "1. Modified entrypoint.sh to clean stale PIDs on restart"
echo "2. Enhanced __start_init_scripts with better error handling and exit codes"
echo "3. Replaced __no_exit with a proper service supervisor"
echo "4. Added comprehensive service monitoring with configurable thresholds"
echo "5. Ensured container exits when critical services fail (allowing orchestrator restart)"
echo ""
echo "⚙️ Configuration Options:"
echo "-------------------------"
echo "Environment variables you can set to customize behavior:"
echo "• SERVICES_LIST: Comma-separated list of services to monitor (default: tini,named,nginx,php-fpm)"
echo "• SERVICE_CHECK_INTERVAL: How often to check services in seconds (default: 30)"
echo "• MAX_SERVICE_FAILURES: Max failures before terminating container (default: 3)"
echo ""
echo "🎯 Expected Behavior:"
echo "--------------------"
echo "• Container starts and initializes all services"
echo "• If any service fails to start, container exits immediately"
echo "• Once running, supervisor monitors all services continuously"
echo "• If any service dies and exceeds failure threshold, container exits"
echo "• On container restart, all services start fresh (no stale state)"
echo "• Orchestrator (Docker/Kubernetes) can restart failed containers automatically"
echo ""
echo "📝 Files Modified/Created:"
echo "-------------------------"
echo "• rootfs/usr/local/bin/entrypoint.sh (PID cleanup logic)"
echo "• rootfs/usr/local/etc/docker/functions/entrypoint.sh (enhanced functions)"
echo ""
echo "🚀 To apply this solution to all repositories:"
echo "---------------------------------------------"
echo "1. Copy the enhanced functions file to each repo's rootfs/usr/local/etc/docker/functions/"
echo "2. Apply the entrypoint.sh PID cleanup changes to each repo's entrypoint.sh"
echo "3. Rebuild and test your containers"
echo ""
echo "✨ Testing completed! The solution should resolve both issues:"
echo " - Services will restart properly after container restarts"
echo " - Containers will exit (die) when critical services fail"