🗃️ Committing everything that changed 🗃️

This commit is contained in:
casjay
2023-03-14 16:16:56 -04:00
parent 67ecbede6b
commit bafade73fa
11 changed files with 276 additions and 220 deletions

View File

@@ -25,12 +25,13 @@ done
WORKDIR="" # set working directory
SERVICE_UID="0" # set the user id
SERVICE_USER="root" # execute command as another user
SERVICE_PORT="${PORT:-6800}" # port which service is listening on
SERVICE_PORT="${PORT:-80}" # port which service is listening on
EXEC_CMD_BIN="nginx" # command to execute
EXEC_CMD_ARGS="-c /etc/nginx/nginx.conf" # command arguments
PRE_EXEC_MESSAGE="" # Show message before execute
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Other variables that are needed
data_dir="/data"
etc_dir="/etc/nginx"
conf_dir="/config/nginx"
www_dir="${WWW_ROOT_DIR:-/data/htdocs}"
@@ -38,10 +39,13 @@ nginx_bin="$(type -P 'nginx')"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# use this function to update config files - IE: change port
__update_conf_files() {
[ -e "$etc_dir" ] && [ -n "$nginx_bin" ] || return 1
echo "Initializing nginx web server in $conf_dir"
mkdir -p "$data_dir/log/nginx"
chmod -Rf 777 "$data_dir/log/nginx"
[ -d "$etc_dir" ] || mkdir -p "$etc_dir"
[ -d "$conf_dir" ] && cp -Rf "$conf_dir/." "$etc_dir/"
ln -sf "/dev/stderr" "var/log/nginx/nginx.log"
ln -sf "/dev/stdout" "/var/log/nginx/access.log"
if [ "$SSL_ENABLED" = "true" ]; then
__file_copy "$conf_dir/nginx.ssl.conf" "$etc_dir/nginx.conf"
__file_copy "$conf_dir/vhosts.d/default.ssl.conf" "$etc_dir/vhosts.d/default.conf"
@@ -50,20 +54,19 @@ __update_conf_files() {
[ -f "$etc_dir/vhosts.d/default.ssl.conf" ] && rm -Rf "$etc_dir/vhosts.d/default.ssl.conf"
#
[ -d "$www_dir" ] || mkdir -p "$www_dir"
[ -d "$www_dir/health" ] || mkdir -p "$www_dir/health"
[ -f "$www_dir/health/index.txt" ] || echo 'ok' >"$www_dir/health/index.txt"
[ -f "$www_dir/health/index.json" ] || echo '{ "status": "ok" }' >"$www_dir/health/index.json"
[ -d "$www_dir/www/health" ] || mkdir -p "$www_dir/www/health"
[ -f "$www_dir/www/health/index.txt" ] || echo 'ok' >"$www_dir/www/health/index.txt"
[ -f "$www_dir/www/health/index.json" ] || echo '{ "status": "ok" }' >"$www_dir/www/health/index.json"
#
__replace "SERVER_PORT" "${SERVICE_PORT:-6800}" "$etc_dir/nginx.conf"
[ -f "$www_dir/www/index.php" ] && __replace "SERVER_SOFTWARE" "nginx" "$www_dir/www/index.php"
[ -f "$www_dir/www/index.html" ] && __replace "SERVER_SOFTWARE" "nginx" "$www_dir/www/index.html"
__replace "SERVER_PORT" "${SERVICE_PORT:-80}" "$etc_dir/nginx.conf"
__replace "SERVER_PORT" "${SERVICE_PORT:-80}" "$etc_dir/vhosts.d/nginx.conf"
[ -f "$www_dir/www/index.php" ] && __replace "SERVER_SOFTWARE" "dns" "$www_dir/www/index.php"
[ -f "$www_dir/www/index.html" ] && __replace "SERVER_SOFTWARE" "dns" "$www_dir/www/index.html"
if [ -z "$PHP_BIN_DIR" ]; then
[ -f "$www_dir/www/info.php" ] && echo "PHP support is not enabled" >"$www_dir/www/info.php"
[ -f "$etc_dir/conf.d/php-fpm.conf" ] && echo "# PHP support is not enabled" >"$etc_dir/conf.d/php-fpm.conf"
fi
if grep -s -q "nginx:" "/etc/passwd"; then
chown -Rf nginx:nginx "$etc_dir" "$www_dir"
fi
return 0
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -75,17 +78,22 @@ __update_ssl_conf() {
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# function to run before executing
__pre_execute() {
[ -n "$PRE_EXEC_MESSAGE" ] && echo "$PRE_EXEC_MESSAGE"
[ -d "/run/init.d" ] || { mkdir -p "/run/init.d" && chmod 777 "/run/init.d"; }
grep -s -q "nginx:" "/etc/passwd" && chown -Rf nginx:nginx "$etc_dir" "$www_dir" "$data_dir/log/nginx"
return 0
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# script to start server
__run_start_script() {
local workdir="${WORKDIR:-$HOME}"
local cmd="$EXEC_CMD_BIN $EXEC_CMD_ARGS"
local user="${SERVICE_USER:-root}"
local lc_type="${LC_ALL:-${LC_CTYPE:-$LANG}}"
local home="${workdir//\/root/\/home\/docker}"
local path="/usr/local/bin:/usr/bin:/bin:/usr/sbin"
case "$1" in
check) shift 1 && __pgrep $EXEC_CMD_BIN || return 5 ;;
*) __pgrep $EXEC_CMD_BIN || su_cmd $EXEC_CMD_BIN $EXEC_CMD_ARGS || return 10 ;;
*) su_cmd env -i PWD="$home" HOME="$home" LC_CTYPE="$lc_type" PATH="$path" USER="$user" sh -c "$cmd" || return 10 ;;
esac
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@@ -129,45 +137,48 @@ fi
# Change to working directory
[ -n "$WORKDIR" ] && mkdir -p "$WORKDIR" && __cd "$WORKDIR" && echo "Changed to $PWD"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Updating config files
__update_conf_files
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Initialize ssl
__update_ssl_conf
__update_ssl_certs
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Updating config files
__update_conf_files
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# run the pre execute commands
[ -n "$PRE_EXEC_MESSAGE" ] && echo "$PRE_EXEC_MESSAGE"
__pre_execute
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
WORKDIR="${WORKDIR:-}"
if [ "$SERVICE_USER" = "root" ] || [ -z "$SERVICE_USER" ]; then
su_cmd_bin="eval"
su_cmd() { "$@" || return 1; }
su_cmd() { eval "$@" || return 1; }
elif [ "$(builtin type -P gosu)" ]; then
su_cmd_bin="gosu $SERVICE_USER"
su_cmd() { eval $su_cmd_bin "$@" || return 1; }
su_cmd() { gosu $SERVICE_USER "$@" || return 1; }
elif [ "$(builtin type -P runuser)" ]; then
su_cmd_bin="runuser -u $SERVICE_USER"
su_cmd() { eval $su_cmd_bin "$@" || return 1; }
su_cmd() { runuser -u $SERVICE_USER "$@" || return 1; }
elif [ "$(builtin type -P sudo)" ]; then
su_cmd_bin="sudo -u $SERVICE_USER"
su_cmd() { eval $su_cmd_bin "$@" || return 1; }
su_cmd() { sudo -u $SERVICE_USER "$@" || return 1; }
elif [ "$(builtin type -P su)" ]; then
su_cmd_bin="su -s /bin/sh - $SERVICE_USER"
su_cmd() { eval $su_cmd_bin -c "$@" || return 1; }
su_cmd() { su -s /bin/sh - $SERVICE_USER -c "$@" || return 1; }
else
echo "Can not switch to $SERVICE_USER"
exit 10
echo "Can not switch to $SERVICE_USER: attempting to run as root"
su_cmd() { eval "$@" || return 1; }
fi
if [ -n "$WORKDIR" ] && [ -n "$SERVICE_USER" ]; then
if [ -n "$WORKDIR" ] && [ "${SERVICE_USER:-$USER}" != "root" ]; then
echo "Fixing file permissions"
su_cmd chown -Rf $SERVICE_USER $WORKDIR
su_cmd chown -Rf $SERVICE_USER $WORKDIR $etc_dir $var_dir $log_dir
fi
if __pgrep $EXEC_CMD_BIN && [ -f "/run/init.d/$EXEC_CMD_BIN.pid" ]; then
SERVICE_EXIT_CODE=1
echo "$EXEC_CMD_BIN" is already running
else
echo "Starting service: $EXEC_CMD_BIN $EXEC_CMD_ARGS"
su_cmd touch /run/init.d/$EXEC_CMD_BIN.pid
__run_start_script "$@" |& tee -a "/tmp/entrypoint.log"
if [ "$?" -ne 0 ]; then
echo "Failed to execute: $EXEC_CMD_BIN $EXEC_CMD_ARGS"
SERVICE_EXIT_CODE=10 SERVICE_IS_RUNNING="false"
su_cmd rm -Rf "/run/init.d/$EXEC_CMD_BIN.pid"
fi
fi
echo "Starting service: $EXEC_CMD_BIN $EXEC_CMD_ARGS"
export SERVICE_IS_RUNNING="true"
su_cmd touch /run/init.d/$EXEC_CMD_BIN.pid
__run_start_script "$@" || echo "Failed to execute: $EXEC_CMD_BIN $EXEC_CMD_ARGS"
[ "$?" -ne 0 ] && SERVICE_IS_RUNNING="false" && SERVICE_EXIT_CODE=10 && rm -Rf "/run/init.d/$EXEC_CMD_BIN.pid"
# su_cmd "$EXEC_CMD_BIN $EXEC_CMD_ARGS"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
exit $SERVICE_EXIT_CODE