🗃 Modified: rootfs/usr/local/etc/docker/functions/entrypoint.sh 🗃

Modified: rootfs/usr/local/etc/docker/functions/entrypoint.sh
This commit is contained in:
2026-06-26 15:45:41 -04:00
parent 4429a1f970
commit 08bef0b31d
@@ -1,7 +1,7 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# shellcheck shell=bash # shellcheck shell=bash
# - - - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - - -
##@Version : 202606041110-git ##@Version : 202606261430-git
# @@Author : Jason Hempstead # @@Author : Jason Hempstead
# @@Contact : git-admin@casjaysdev.pro # @@Contact : git-admin@casjaysdev.pro
# @@License : LICENSE.md # @@License : LICENSE.md
@@ -25,11 +25,11 @@ if [ -f "/config/.debug" ] && [ -z "$DEBUGGER_OPTIONS" ]; then
export DEBUGGER_OPTIONS="$(<"/config/.debug")" export DEBUGGER_OPTIONS="$(<"/config/.debug")"
fi fi
if [ "$DEBUGGER" = "on" ] || [ -f "/config/.debug" ]; then if [ "$DEBUGGER" = "on" ] || [ -f "/config/.debug" ]; then
set -o pipefail set -eo pipefail
[ -n "$DEBUGGER_OPTIONS" ] && set -"$DEBUGGER_OPTIONS" [ -n "$DEBUGGER_OPTIONS" ] && set -"$DEBUGGER_OPTIONS"
export DEBUGGER="on" export DEBUGGER="on"
else else
set -o pipefail set -eo pipefail
fi fi
# - - - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - - -
__remove_extra_spaces() { sed -E 's/ +/ /g; s|^ ||'; } __remove_extra_spaces() { sed -E 's/ +/ /g; s|^ ||'; }
@@ -257,7 +257,8 @@ __trim() {
__banner() { __banner() {
local message="$*" local message="$*"
local total_width=80 local total_width=80
local content_width=$((total_width - 14)) # Account for "# - - - " and " - - - #" # Account for "# - - - " and " - - - #"
local content_width=$((total_width - 14))
printf '# - - - %-*s - - - #\n' "$content_width" "$message" printf '# - - - %-*s - - - #\n' "$content_width" "$message"
} }
# - - - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -267,36 +268,73 @@ __service_banner() {
local service="${3:-service}" local service="${3:-service}"
local full_message="$message $service" local full_message="$message $service"
local total_width=80 local total_width=80
local content_width=$((total_width - 14)) # Account for "# - - - " and " - - - #" # Account for "# - - - " and " - - - #"
local icon_width=2 # Most emojis are 2 chars wide local content_width=$((total_width - 14))
local text_width=$((content_width - icon_width * 2 - 2)) # Account for both icons and spaces # Most emojis are 2 chars wide
local icon_width=2
# Account for both icons and spaces
local text_width=$((content_width - icon_width * 2 - 2))
printf '# - - - %s %-*s %s - - - #\n' "$icon" "$text_width" "$full_message" "$icon" 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; } __find_php_bin() {
command -v php-fpm &>/dev/null || command -v php &>/dev/null || return 0
find -L '/usr'/*bin -maxdepth 4 -name 'php-fpm*' 2>/dev/null | head -n1
}
__find_php_ini() { __find_php_ini() {
command -v php &>/dev/null || return 0
local f local f
f=$(find -L '/etc' -maxdepth 4 -name 'php.ini' 2>/dev/null | head -n1) f=$(find -L '/etc' -maxdepth 4 -name 'php.ini' 2>/dev/null | head -n1)
[ -n "$f" ] && printf '%s\n' "${f%/php.ini}" [ -n "$f" ] && printf '%s\n' "${f%/php.ini}"
} }
# - - - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - - -
__find_nginx_conf() { find -L '/etc' -maxdepth 4 -name 'nginx.conf' 2>/dev/null | head -n1; } __find_nginx_conf() {
__find_caddy_conf() { find -L '/etc' -maxdepth 4 -type f -iname 'caddy.conf' 2>/dev/null | head -n1; } command -v nginx &>/dev/null || return 0
__find_lighttpd_conf() { find -L '/etc' -maxdepth 4 -type f -iname 'lighttpd.conf' 2>/dev/null | head -n1; } find -L '/etc' -maxdepth 4 -name 'nginx.conf' 2>/dev/null | head -n1
__find_cherokee_conf() { find -L '/etc' -maxdepth 4 -type f -iname 'cherokee.conf' 2>/dev/null | head -n1; } }
__find_httpd_conf() { find -L '/etc' -maxdepth 4 -type f -iname 'httpd.conf' -o -iname 'apache2.conf' 2>/dev/null | head -n1; } __find_caddy_conf() {
command -v caddy &>/dev/null || return 0
find -L '/etc' -maxdepth 4 -type f -iname 'caddy.conf' 2>/dev/null | head -n1
}
__find_lighttpd_conf() {
command -v lighttpd &>/dev/null || return 0
find -L '/etc' -maxdepth 4 -type f -iname 'lighttpd.conf' 2>/dev/null | head -n1
}
__find_cherokee_conf() {
command -v cherokee &>/dev/null || command -v cherokee-admin &>/dev/null || return 0
find -L '/etc' -maxdepth 4 -type f -iname 'cherokee.conf' 2>/dev/null | head -n1
}
__find_httpd_conf() {
command -v httpd &>/dev/null || command -v apache2 &>/dev/null || return 0
find -L '/etc' -maxdepth 4 -type f \( -iname 'httpd.conf' -o -iname 'apache2.conf' \) 2>/dev/null | head -n1
}
# - - - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - - -
__find_mysql_conf() { find -L '/etc' -maxdepth 4 -type f -name 'my.cnf' 2>/dev/null | head -n1; } __find_mysql_conf() {
__find_pgsql_conf() { find -L '/var/lib' '/etc' -maxdepth 8 -type f -name 'postgresql.conf' 2>/dev/null | head -n1; } command -v mysqld &>/dev/null || command -v mariadbd &>/dev/null || command -v mysql &>/dev/null || return 0
__find_couchdb_conf() { return; } find -L '/etc' -maxdepth 4 -type f -name 'my.cnf' 2>/dev/null | head -n1
__find_mongodb_conf() { return; } }
__find_pgsql_conf() {
command -v postgres &>/dev/null || command -v pg_ctl &>/dev/null || return 0
find -L '/var/lib' '/etc' -maxdepth 8 -type f -name 'postgresql.conf' 2>/dev/null | head -n1
}
__find_couchdb_conf() {
command -v couchdb &>/dev/null || return 0
find -L '/opt/couchdb/etc' '/etc/couchdb' -maxdepth 4 -type f \( -name 'local.ini' -o -name 'default.ini' \) 2>/dev/null | head -n1
}
__find_mongodb_conf() {
command -v mongod &>/dev/null || return 0
find -L '/etc/mongodb' '/etc' -maxdepth 4 -type f \( -name 'mongod.conf' -o -name 'mongodb.conf' \) 2>/dev/null | head -n1
}
# - - - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - - -
__random_password() { tr -dc '0-9a-zA-Z' < /dev/urandom | head -c${1:-16} && echo ""; } __random_password() { tr -dc '0-9a-zA-Z' < /dev/urandom | head -c${1:-16} && echo ""; }
# - - - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - - -
__init_working_dir() { __init_working_dir() {
local service_name="$SERVICE_NAME" # get service name # get service name
local workdir="$(eval echo "${WORK_DIR:-}")" # expand variables local service_name="$SERVICE_NAME"
local home="$(eval echo "${workdir//\/root/\/tmp\/docker}")" # expand variables # expand variables
local workdir="$(eval echo "${WORK_DIR:-}")"
# expand variables
local home="$(eval echo "${workdir//\/root/\/tmp\/docker}")"
# set working directories # set working directories
[ "$home" = "$workdir" ] && workdir="" [ "$home" = "$workdir" ] && workdir=""
[ "$home" = "/root" ] && home="/tmp/$service_name" [ "$home" = "/root" ] && home="/tmp/$service_name"
@@ -326,14 +364,14 @@ __init_working_dir() {
# cd to dir # cd to dir
__cd "${workdir:-$home}" __cd "${workdir:-$home}"
# - - - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - - -
echo "Setting the working directory to: $PWD" [ "$DEBUGGER" = "on" ] && echo "Setting the working directory to: $PWD"
# - - - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - - -
export WORK_DIR="$workdir" HOME="$home" export WORK_DIR="$workdir" HOME="$home"
} }
# - - - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - - -
__exec_service() { __exec_service() {
local count=6 local count=6
echo "Starting $1" [ "$DEBUGGER" = "on" ] && echo "Starting $1"
eval "$@" 2>>/dev/stderr >>/data/logs/start.log & eval "$@" 2>>/dev/stderr >>/data/logs/start.log &
while [ $count -ne 0 ]; do while [ $count -ne 0 ]; do
sleep 3 sleep 3
@@ -426,70 +464,105 @@ __create_ssl_cert() {
fi fi
} }
# - - - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - - -
__init_apache() { __init_service_conf() {
local etc_dir="" conf_dir="" conf_dir="" www_dir="" apache_bin="" # Seed /config/$svc/ from build-time baked /etc sources on first container start.
etc_dir="/etc/${1:-apache2}" # Copy only — no symlinks. Symlinking /etc back to /config/ is the service's own
conf_dir="/config/${1:-apache2}" # responsibility, done inside __update_conf_files in each init.d/*.sh script so
www_dir="${WWW_ROOT_DIR:-/data/htdocs}" # each service controls its exact paths and variable substitution order.
apache_bin="$(type -P 'httpd' || type -P 'apache2')"
# #
# Usage: __init_service_conf <conf_dir> <primary_etc_dir> [extra_etc_path ...]
#
# primary_etc_dir directory → contents copied into conf_dir/ when conf_dir is empty
# extra_etc_path directory → copied into conf_dir/<name>/ when that subdir is empty
# extra_etc_path file → copied to conf_dir/<filename> when absent
local conf_dir="$1"
local primary_etc="$2"
shift 2
local src name
mkdir -p "$conf_dir"
if [ -d "$primary_etc" ] && __is_dir_empty "$conf_dir"; then
__copy_templates "$primary_etc/." "$conf_dir/"
fi
for src in "$@"; do
[ -e "$src" ] || continue
name="${src##*/}"
if [ -d "$src" ] && __is_dir_empty "$conf_dir/$name"; then
mkdir -p "$conf_dir/$name"
__copy_templates "$src/." "$conf_dir/$name/"
elif [ -f "$src" ] && [ ! -f "$conf_dir/$name" ]; then
cp -f "$src" "$conf_dir/$name"
fi
done
}
# - - - - - - - - - - - - - - - - - - - - - - - - -
__init_apache() {
command -v httpd &>/dev/null || command -v apache2 &>/dev/null || return 0
local svc="${1:-apache2}"
__init_service_conf "/config/$svc" "/etc/$svc"
return 0 return 0
} }
# - - - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - - -
__init_nginx() { __init_nginx() {
local etc_dir="/etc/${1:-nginx}" command -v nginx &>/dev/null || return 0
local conf_dir="/config/${1:-nginx}" local svc="${1:-nginx}"
local www_dir="${WWW_ROOT_DIR:-/data/htdocs}" __init_service_conf "/config/$svc" "/etc/$svc"
local nginx_bin="$(type -P 'nginx')"
return 0 return 0
} }
# - - - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - - -
__init_php() { __init_php() {
local etc_dir="/etc/${1:-php}" command -v php &>/dev/null || return 0
local conf_dir="/config/${1:-php}" local php_etc="${PHP_INI_DIR:-$(__find_php_ini)}"
local php_bin="${PHP_BIN_DIR:-$(__find_php_bin)}" __init_service_conf "/config/php" "${php_etc:-/etc/php}" \
"/etc/php.ini" "/etc/php-fpm" "/etc/php-fpm.conf"
return 0 return 0
} }
# - - - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - - -
__init_mysql() { __init_mysql() {
local db_dir="/data/db/mysql" command -v mysqld &>/dev/null || command -v mariadbd &>/dev/null || return 0
local etc_dir="${home:-/etc/${1:-mysql}}" local svc="${1:-mysql}"
local db_user="${SERVICE_USER:-mysql}" __init_service_conf "/config/$svc" "/etc/$svc" "/etc/my.ini" "/etc/my.cnf"
local conf_dir="/config/${1:-mysql}" [ -d "${DATABASE_DIR:-/data/db/$svc}" ] || mkdir -p "${DATABASE_DIR:-/data/db/$svc}"
local user_name="${MARIADB_USER:-root}"
local user_pass="${MARIADB_PASSWORD:-$MARIADB_ROOT_PASSWORD}"
local user_db="${MARIADB_DATABASE}"
local root_pass="$MARIADB_ROOT_PASSWORD"
local mysqld_bin="$(type -P 'mysqld')"
return 0 return 0
} }
# - - - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - - -
__init_mongodb() { __init_mongodb() {
local home="${MONGODB_CONFIG_FILE:-$(__find_mongodb_conf)}" command -v mongod &>/dev/null || return 0
local user_name="${INITDB_ROOT_USERNAME:-root}" __init_service_conf "/config/mongodb" "/etc/mongodb" "/etc/mongod.conf"
local user_pass="${MONGO_INITDB_ROOT_PASSWORD:-$_ROOT_PASSWORD}" return 0
return
} }
# - - - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - - -
__init_postgres() { __init_postgres() {
local home="${PGSQL_CONFIG_FILE:-$(__find_pgsql_conf)}" command -v postgres &>/dev/null || command -v pg_ctl &>/dev/null || return 0
local user_name="${POSTGRES_USER:-root}" local pg_etc
local user_pass="${POSTGRES_PASSWORD:-$POSTGRES_ROOT_PASSWORD}" pg_etc="${PGSQL_CONFIG_FILE:+${PGSQL_CONFIG_FILE%/*}}"
return [ -n "$pg_etc" ] || pg_etc="$(__find_pgsql_conf)"
[ -n "$pg_etc" ] && pg_etc="${pg_etc%/*}"
[ -n "$pg_etc" ] && __init_service_conf "/config/postgres" "$pg_etc"
return 0
} }
# - - - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - - -
__init_couchdb() { __init_couchdb() {
local home="${COUCHDB_CONFIG_FILE:-$(__find_couchdb_conf)}" command -v couchdb &>/dev/null || return 0
local user_name="${COUCHDB_USER:-root}" __init_service_conf "/config/couchdb" "/etc/couchdb"
local user_pass="${COUCHDB_PASSWORD:-$SET_RANDOM_PASS}" return 0
return
} }
# - - - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - - -
# Show available init functions # Show available init functions
__init_help() { __init_help() {
echo ' echo '
__update_ssl_certs Config seeding (copy /etc → /config, no symlinks):
__create_ssl_cert __init_service_conf <conf_dir> <primary_etc_dir> [extra_etc_path ...]
__init_apache [svc] seeds /config/apache2 from /etc/apache2
__init_nginx [svc] seeds /config/nginx from /etc/nginx
__init_php seeds /config/php from /etc/php* + /etc/php.ini + /etc/php-fpm
__init_mysql [svc] seeds /config/mysql from /etc/mysql + /etc/my.{ini,cnf}
__init_mongodb seeds /config/mongodb from /etc/mongodb + /etc/mongod.conf
__init_postgres seeds /config/postgres from pg data dir
__init_couchdb seeds /config/couchdb from /etc/couchdb
SSL:
__update_ssl_certs
__create_ssl_cert
' '
return return
} }
@@ -524,7 +597,7 @@ __cron() {
[ -d "/run/cron" ] || mkdir -p "/run/cron" [ -d "/run/cron" ] || mkdir -p "/run/cron"
echo "$pid" >"/run/cron/$bin.pid" echo "$pid" >"/run/cron/$bin.pid"
echo "$command" >"/run/cron/$bin.run" echo "$command" >"/run/cron/$bin.run"
echo "Log is saved to /data/logs/cron.log" [ "$DEBUGGER" = "on" ] && echo "Log is saved to /data/logs/cron.log"
# eval is intentional: $command is operator-controlled input from this container's init # eval is intentional: $command is operator-controlled input from this container's init
while :; do while :; do
eval "$command" eval "$command"
@@ -562,7 +635,7 @@ __symlink() {
[ "$from" = "$to" ] && return 0 [ "$from" = "$to" ] && return 0
__rm "$from" __rm "$from"
[ -d "${from%/*}" ] || mkdir -p "${from%/*}" 2>/dev/null [ -d "${from%/*}" ] || mkdir -p "${from%/*}" 2>/dev/null
ln -sf "$to" "$from" && echo "Created symlink to $from > $to" ln -sf "$to" "$from" && [ "$DEBUGGER" = "on" ] && echo "Created symlink: $from -> $to"
} }
# - - - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - - -
__file_copy() { __file_copy() {
@@ -574,7 +647,7 @@ __file_copy() {
if [ -n "$from" ] && [ -e "$from" ] && [ -n "$dest" ]; then if [ -n "$from" ] && [ -e "$from" ] && [ -n "$dest" ]; then
if [ -d "$from" ]; then if [ -d "$from" ]; then
if cp -Rf "$from/." "$dest/" &>/dev/null; then if cp -Rf "$from/." "$dest/" &>/dev/null; then
printf '%s\n' "Copied: $from > $dest" [ "$DEBUGGER" = "on" ] && printf '%s\n' "Copied: $from > $dest"
return 0 return 0
else else
printf '%s\n' "Copy failed: $from < $dest" >&2 printf '%s\n' "Copy failed: $from < $dest" >&2
@@ -582,7 +655,7 @@ __file_copy() {
fi fi
else else
if cp -Rf "$from" "$dest" &>/dev/null; then if cp -Rf "$from" "$dest" &>/dev/null; then
printf '%s\n' "Copied: $from > $dest" [ "$DEBUGGER" = "on" ] && printf '%s\n' "Copied: $from > $dest"
return 0 return 0
else else
printf '%s\n' "Copy failed: $from < $dest" >&2 printf '%s\n' "Copy failed: $from < $dest" >&2
@@ -618,25 +691,33 @@ __setup_directories() {
if [ "$IS_WEB_SERVER" = "yes" ]; then if [ "$IS_WEB_SERVER" = "yes" ]; then
APPLICATION_DIRS="$APPLICATION_DIRS $WWW_ROOT_DIR" APPLICATION_DIRS="$APPLICATION_DIRS $WWW_ROOT_DIR"
__initialize_www_root __initialize_www_root
(echo "Creating directory $WWW_ROOT_DIR with permissions 777" && mkdir -p "$WWW_ROOT_DIR" && find "$WWW_ROOT_DIR" -type d -exec chmod -f 777 {} \;) 2>/dev/stderr | tee -p -a "/data/logs/init.txt" mkdir -p "$WWW_ROOT_DIR" 2>/dev/null
find "$WWW_ROOT_DIR" -type d -exec chmod -f 777 {} \; 2>/dev/null
[ "$DEBUGGER" = "on" ] && echo "Created directory $WWW_ROOT_DIR"
fi fi
# Setup DATABASE_DIR # Setup DATABASE_DIR
if [ "$IS_DATABASE_SERVICE" = "yes" ] || [ "$USES_DATABASE_SERVICE" = "yes" ]; then if [ "$IS_DATABASE_SERVICE" = "yes" ] || [ "$USES_DATABASE_SERVICE" = "yes" ]; then
APPLICATION_DIRS="$APPLICATION_DIRS $DATABASE_DIR" APPLICATION_DIRS="$APPLICATION_DIRS $DATABASE_DIR"
if __is_dir_empty "$DATABASE_DIR" || [ ! -d "$DATABASE_DIR" ]; then if __is_dir_empty "$DATABASE_DIR" || [ ! -d "$DATABASE_DIR" ]; then
(echo "Creating directory $DATABASE_DIR with permissions 777" && mkdir -p "$DATABASE_DIR" && chmod -f 777 "$DATABASE_DIR") 2>/dev/stderr | tee -p -a "/data/logs/init.txt" mkdir -p "$DATABASE_DIR" 2>/dev/null
chmod -f 777 "$DATABASE_DIR" 2>/dev/null
[ "$DEBUGGER" = "on" ] && echo "Created directory $DATABASE_DIR"
fi fi
fi fi
# create default directories # create default directories
for filedirs in $ADD_APPLICATION_DIRS $APPLICATION_DIRS; do for filedirs in $ADD_APPLICATION_DIRS $APPLICATION_DIRS; do
if [ -n "$filedirs" ] && [ ! -d "$filedirs" ]; then if [ -n "$filedirs" ] && [ ! -d "$filedirs" ]; then
(echo "Creating directory $filedirs with permissions 777" && mkdir -p "$filedirs" && chmod -f 777 "$filedirs") 2>/dev/stderr | tee -p -a "/data/logs/init.txt" mkdir -p "$filedirs" 2>/dev/null
chmod -f 777 "$filedirs" 2>/dev/null
[ "$DEBUGGER" = "on" ] && echo "Created directory $filedirs"
fi fi
done done
# create default files # create default files
for application_files in $ADD_APPLICATION_FILES $APPLICATION_FILES; do for application_files in $ADD_APPLICATION_FILES $APPLICATION_FILES; do
if [ -n "$application_files" ] && [ ! -e "$application_files" ]; then if [ -n "$application_files" ] && [ ! -e "$application_files" ]; then
(echo "Creating file $application_files with permissions 777" && touch "$application_files" && chmod -Rf 777 "$application_files") 2>/dev/stderr | tee -p -a "/data/logs/init.txt" touch "$application_files" 2>/dev/null
chmod -Rf 777 "$application_files" 2>/dev/null
[ "$DEBUGGER" = "on" ] && echo "Created file $application_files"
fi fi
done done
} }
@@ -650,7 +731,8 @@ __fix_permissions() {
if grep -shq "^$change_user:" "/etc/passwd"; then if grep -shq "^$change_user:" "/etc/passwd"; then
for permissions in $ADD_APPLICATION_DIRS $APPLICATION_DIRS; do for permissions in $ADD_APPLICATION_DIRS $APPLICATION_DIRS; do
if [ -n "$permissions" ] && [ -e "$permissions" ]; then if [ -n "$permissions" ] && [ -e "$permissions" ]; then
(chown -Rf $change_user "$permissions" && echo "changed ownership on $permissions to user:$change_user") 2>/dev/stderr | tee -p -a "/data/logs/init.txt" chown -Rf "$change_user" "$permissions" 2>/dev/null
[ "$DEBUGGER" = "on" ] && echo "Changed ownership of $permissions to $change_user"
fi fi
done done
fi fi
@@ -659,7 +741,8 @@ __fix_permissions() {
if grep -shq "^$change_group:" "/etc/group"; then if grep -shq "^$change_group:" "/etc/group"; then
for permissions in $ADD_APPLICATION_DIRS $APPLICATION_DIRS; do for permissions in $ADD_APPLICATION_DIRS $APPLICATION_DIRS; do
if [ -n "$permissions" ] && [ -e "$permissions" ]; then if [ -n "$permissions" ] && [ -e "$permissions" ]; then
(chgrp -Rf $change_group "$permissions" && echo "changed group ownership on $permissions to group $change_group") 2>/dev/stderr | tee -p -a "/data/logs/init.txt" chgrp -Rf "$change_group" "$permissions" 2>/dev/null
[ "$DEBUGGER" = "on" ] && echo "Changed group of $permissions to $change_group"
fi fi
done done
fi fi
@@ -719,7 +802,7 @@ __set_user_group_id() {
return 0 return 0
fi fi
if [ -z "$set_user" ] || [ "$set_user" = "root" ]; then if [ -z "$set_user" ] || [ "$set_user" = "root" ]; then
return return 0
fi fi
if grep -shq "^$set_user:" "/etc/passwd" "/etc/group"; then if grep -shq "^$set_user:" "/etc/passwd" "/etc/group"; then
if __check_for_guid "$set_gid"; then if __check_for_guid "$set_gid"; then
@@ -912,10 +995,10 @@ __start_init_scripts() {
[ "$1" = " " ] && shift 1 [ "$1" = " " ] && shift 1
if [ "$DEBUGGER" = "on" ]; then if [ "$DEBUGGER" = "on" ]; then
echo "Enabling debugging" echo "Enabling debugging"
set -o pipefail set -eo pipefail
[ -n "$DEBUGGER_OPTIONS" ] && set -"$DEBUGGER_OPTIONS" [ -n "$DEBUGGER_OPTIONS" ] && set -"$DEBUGGER_OPTIONS"
else else
set -o pipefail set -eo pipefail
fi fi
local retPID="" local retPID=""
local basename="" local basename=""
@@ -932,7 +1015,7 @@ __start_init_scripts() {
# Clean stale PID files from previous runs # Clean stale PID files from previous runs
if [ ! -f "/run/.start_init_scripts.pid" ]; then if [ ! -f "/run/.start_init_scripts.pid" ]; then
echo "🧹 Cleaning stale PID files from previous container run" [ "$DEBUGGER" = "on" ] && echo "Cleaning stale PID files from previous container run"
rm -f /run/*.pid /run/init.d/*.pid 2>/dev/null || true rm -f /run/*.pid /run/init.d/*.pid 2>/dev/null || true
fi fi
@@ -949,18 +1032,18 @@ __start_init_scripts() {
done done
(shopt -s nullglob; chmod -Rf 755 "$init_dir"/*.sh 2>/dev/null || true) (shopt -s nullglob; chmod -Rf 755 "$init_dir"/*.sh 2>/dev/null || true)
echo "🚀 Starting container services initialization" if [ "$DEBUGGER" = "on" ]; then
echo "📂 Init directory: $init_dir" echo "Starting container services initialization"
echo "📊 Services to start: $init_count" echo "Init directory: $init_dir$init_count service scripts to execute"
echo "📋 Found $init_count service scripts to execute"
echo "" echo ""
fi
for init in "$init_dir"/*.sh; do for init in "$init_dir"/*.sh; do
if [ -x "$init" ]; then if [ -x "$init" ]; then
touch "$pidFile" touch "$pidFile"
name="${init##*/}" name="${init##*/}"
service="${name#*-}"; service="${service%.sh}" service="${name#*-}"; service="${service%.sh}"
__service_banner "🔧" "Executing service script:" "${init##*/}" [ "$DEBUGGER" = "on" ] && __service_banner "🔧" "Executing service script:" "${init##*/}"
# Execute the init script and capture the exit code (subshell isolates exit calls) # Execute the init script and capture the exit code (subshell isolates exit calls)
if ( source "$init" ); then if ( source "$init" ); then
# Check if service was disabled first # Check if service was disabled first
@@ -1052,10 +1135,9 @@ __start_init_scripts() {
} }
# - - - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - - -
__setup_mta() { __setup_mta() {
[ -d "/etc/ssmtp" ] || [ -d "/etc/postfix" ] || return # Check for an installed MTA binary — directories alone cannot be trusted because
if [ ! -d "/config/ssmtp" ] || [ ! -d "/config/postfix" ]; then # /etc/postfix ships in some base images (casjaysdev/alpine) even without postfix.
echo "Configuring smtp server" command -v ssmtp &>/dev/null || command -v postfix &>/dev/null || return 0
fi
local exitCode=0 local exitCode=0
local relay_port="${EMAIL_RELAY//*:/}" local relay_port="${EMAIL_RELAY//*:/}"
local relay_server="${EMAIL_RELAY//:*/}" local relay_server="${EMAIL_RELAY//:*/}"
@@ -1099,7 +1181,7 @@ EOF
__symlink "/config/ssmtp/revaliases" "/etc/ssmtp/revaliases" __symlink "/config/ssmtp/revaliases" "/etc/ssmtp/revaliases"
__initialize_replace_variables "/etc/ssmtp/revaliases" __initialize_replace_variables "/etc/ssmtp/revaliases"
fi fi
echo "Done setting up ssmtp" [ "$DEBUGGER" = "on" ] && echo "Done setting up ssmtp"
fi fi
################# postfix relay setup ################# postfix relay setup
@@ -1151,7 +1233,7 @@ EOF
if [ ! -f "/run/init.d/postfix.pid" ]; then if [ ! -f "/run/init.d/postfix.pid" ]; then
__exec_service postfix start __exec_service postfix start
fi fi
echo "Done setting up postfix" [ "$DEBUGGER" = "on" ] && echo "Done setting up postfix"
fi fi
fi fi
[ -f "/root/dead.letter" ] && __rm "/root/dead.letter" [ -f "/root/dead.letter" ] && __rm "/root/dead.letter"
@@ -1262,12 +1344,12 @@ __initialize_system_etc() {
if [ -n "$conf_dir" ] && [ -e "$conf_dir" ]; then if [ -n "$conf_dir" ] && [ -e "$conf_dir" ]; then
files=$(find "$conf_dir"/* -not -path '*/env/*' -type f 2>/dev/null | sort -u | sed 's|/config/||') files=$(find "$conf_dir"/* -not -path '*/env/*' -type f 2>/dev/null | sort -u | sed 's|/config/||')
directories=$(find "$conf_dir"/* -not -path '*/env/*' -type d 2>/dev/null | sort -u | sed 's|/config/||') directories=$(find "$conf_dir"/* -not -path '*/env/*' -type d 2>/dev/null | sort -u | sed 's|/config/||')
echo "Copying config files to system: $conf_dir > /etc/${conf_dir//\/config\//}" [ "$DEBUGGER" = "on" ] && echo "Copying config: $conf_dir > /etc/${conf_dir//\/config\//}"
if [ -n "$directories" ]; then if [ -n "$directories" ]; then
for d in $directories; do for d in $directories; do
dir="/etc/$d" dir="/etc/$d"
echo "Creating directory: $dir"
mkdir -p "$dir" mkdir -p "$dir"
[ "$DEBUGGER" = "on" ] && echo "Created directory: $dir"
done done
fi fi
for f in $files; do for f in $files; do
@@ -1286,7 +1368,7 @@ __initialize_custom_bin_dir() {
[ -d "/data/bin" ] && SET_USR_BIN+="$(__find /data/bin f) " [ -d "/data/bin" ] && SET_USR_BIN+="$(__find /data/bin f) "
[ -d "/config/bin" ] && SET_USR_BIN+="$(__find /config/bin f) " [ -d "/config/bin" ] && SET_USR_BIN+="$(__find /config/bin f) "
if [ -n "$SET_USR_BIN" ]; then if [ -n "$SET_USR_BIN" ]; then
echo "Setting up bin $SET_USR_BIN > $LOCAL_BIN_DIR" [ "$DEBUGGER" = "on" ] && echo "Setting up bin: $SET_USR_BIN > $LOCAL_BIN_DIR"
for create_bin_template in $SET_USR_BIN; do for create_bin_template in $SET_USR_BIN; do
if [ -n "$create_bin_template" ]; then if [ -n "$create_bin_template" ]; then
create_bin_name="${create_bin_template##*/}" create_bin_name="${create_bin_template##*/}"
@@ -1299,95 +1381,6 @@ __initialize_custom_bin_dir() {
fi fi
} }
# - - - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - - -
__initialize_default_templates() {
local errors=0
if [ -n "$DEFAULT_TEMPLATE_DIR" ]; then
if [ "$CONFIG_DIR_INITIALIZED" = "no" ] && [ -d "/config" ]; then
[ -d "$DEFAULT_TEMPLATE_DIR" ] || return 0
__log_info "Copying default config files $DEFAULT_TEMPLATE_DIR > /config"
for create_config_template in "$DEFAULT_TEMPLATE_DIR"/*; do
if [ -e "$create_config_template" ]; then
create_template_name="${create_config_template##*/}"
if [ -d "$create_config_template" ]; then
mkdir -p "/config/$create_template_name/" || errors=$((errors + 1))
if __is_dir_empty "/config/$create_template_name"; then
if ! cp -Rf "$create_config_template/." "/config/$create_template_name/" 2>/dev/null; then
__log_warn "Failed to copy template directory: $create_template_name"
errors=$((errors + 1))
fi
fi
elif [ -f "$create_config_template" ]; then
if [ ! -e "/config/$create_template_name" ]; then
if ! cp -Rf "$create_config_template" "/config/$create_template_name" 2>/dev/null; then
__log_warn "Failed to copy template file: $create_template_name"
errors=$((errors + 1))
fi
fi
fi
fi
done
unset create_config_template create_template_name
__log_debug "Template initialization completed with $errors errors"
fi
fi
return 0
}
# - - - - - - - - - - - - - - - - - - - - - - - - -
__initialize_config_dir() {
local errors=0
if [ -n "$DEFAULT_CONF_DIR" ]; then
if [ "$CONFIG_DIR_INITIALIZED" = "no" ] && [ -d "/config" ]; then
[ -d "$DEFAULT_CONF_DIR" ] || return 0
__log_info "Copying custom config files: $DEFAULT_CONF_DIR > /config"
for create_config_template in "$DEFAULT_CONF_DIR"/*; do
if [ -e "$create_config_template" ]; then
create_config_name="${create_config_template##*/}"
if [ -d "$create_config_template" ]; then
mkdir -p "/config/$create_config_name" || errors=$((errors + 1))
if __is_dir_empty "/config/$create_config_name"; then
if ! cp -Rf "$create_config_template/." "/config/$create_config_name/" 2>/dev/null; then
__log_warn "Failed to copy config directory: $create_config_name"
errors=$((errors + 1))
fi
fi
elif [ -f "$create_config_template" ]; then
if [ ! -e "/config/$create_config_name" ]; then
if ! cp -Rf "$create_config_template" "/config/$create_config_name" 2>/dev/null; then
__log_warn "Failed to copy config file: $create_config_name"
errors=$((errors + 1))
fi
fi
fi
fi
done
unset create_config_template create_config_name
__log_debug "Config initialization completed with $errors errors"
fi
fi
return 0
}
# - - - - - - - - - - - - - - - - - - - - - - - - -
__initialize_data_dir() {
[ "$DATA_DIR_INITIALIZED" = "no" ] || return 0
if [ -d "/data" ]; then
if [ -n "$DEFAULT_DATA_DIR" ]; then
[ -d "$DEFAULT_DATA_DIR" ] || return 0
__log_info "Copying data files $DEFAULT_DATA_DIR > /data"
for create_data_template in "$DEFAULT_DATA_DIR"/*; do
create_data_name="${create_data_template##*/}"
if [ -n "$create_data_template" ]; then
if [ -d "$create_data_template" ]; then
mkdir -p "/data/$create_data_name"
__is_dir_empty "/data/$create_data_name" && cp -Rf "$create_data_template/." "/data/$create_data_name/" 2>/dev/null
elif [ -e "$create_data_template" ]; then
[ -e "/data/$create_data_name" ] || cp -Rf "$create_data_template" "/data/$create_data_name" 2>/dev/null
fi
fi
done
unset create_data_template
fi
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - - -
__initialize_www_root() { __initialize_www_root() {
local WWW_INIT="" local WWW_INIT=""
@@ -1401,7 +1394,7 @@ __initialize_www_root() {
WWW_INIT="false" WWW_INIT="false"
fi fi
if [ "$WWW_INIT" = "true" ] && [ -d "$WWW_TEMPLATE" ]; then if [ "$WWW_INIT" = "true" ] && [ -d "$WWW_TEMPLATE" ]; then
cp -Rf "$DEFAULT_DATA_DIR/data/htdocs/." "$WWW_ROOT_DIR/" 2>/dev/null cp -Rf "$WWW_TEMPLATE/." "$WWW_ROOT_DIR/" 2>/dev/null
fi fi
__initialize_web_health "$WWW_ROOT_DIR" __initialize_web_health "$WWW_ROOT_DIR"
} }
@@ -1581,9 +1574,6 @@ export SSL_CA="${SSL_CA:-/config/ssl/ca.crt}"
export SSL_KEY="${SSL_KEY:-/config/ssl/localhost.pem}" export SSL_KEY="${SSL_KEY:-/config/ssl/localhost.pem}"
export SSL_CERT="${SSL_CERT:-/config/ssl/localhost.crt}" export SSL_CERT="${SSL_CERT:-/config/ssl/localhost.crt}"
export LOCAL_BIN_DIR="${LOCAL_BIN_DIR:-/usr/local/bin}" 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 # Backup settings
export BACKUP_MAX_DAYS="${BACKUP_MAX_DAYS:-}" export BACKUP_MAX_DAYS="${BACKUP_MAX_DAYS:-}"
@@ -1640,3 +1630,4 @@ export ENTRYPOINT_PID_FILE ENTRYPOINT_INIT_FILE ENTRYPOINT_FIRST_RUN
export -f __get_pid __start_init_scripts __is_running __update_ssl_certs __create_ssl_cert export -f __get_pid __start_init_scripts __is_running __update_ssl_certs __create_ssl_cert
# - - - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - - -
# end of functions # end of functions
# vim: set ft=sh ts=4 sw=4 st=4 et :