Files
AFC-Demo/deploy/docker-entrypoint.sh
T

107 lines
3.9 KiB
Bash
Raw Normal View History

2025-10-23 12:22:27 +02:00
#!/bin/sh
2025-10-30 11:24:56 +01:00
# Docker entrypoint for Nginx + PHP/Laravel
set -eu
2025-10-23 12:22:27 +02:00
2025-10-30 11:24:56 +01:00
# -------- defaults (override via env) --------
: "${APP_DIR:=/var/www/html}"
: "${NGINX_TEMPLATE:=/etc/nginx/templates/default.conf.template}"
: "${NGINX_HTTPD_DIR:=/etc/nginx/http.d}"
: "${NGINX_CONF_OUT:=${NGINX_HTTPD_DIR}/default.conf}"
: "${HTPASSWD_PATH:=/etc/nginx/htpasswd}"
: "${BASIC_AUTH_REALM:=Restricted Content}"
: "${SUPERVISOR_CONF:=/etc/supervisor/supervisord.conf}"
log() { printf '[entrypoint] %s\n' "$*"; }
err() { printf '[entrypoint][ERROR] %s\n' "$*" >&2; }
die() { err "$*"; exit 1; }
need() { command -v "$1" >/dev/null 2>&1 || die "Missing required command: $1"; }
# required tools
need envsubst; need nginx; need php; need supervisord
# server name
SERVER_NAME="${SERVER_NAME:-$(hostname -f 2>/dev/null || hostname 2>/dev/null || echo localhost)}"
2025-10-30 11:05:56 +01:00
export SERVER_NAME
2025-10-23 12:22:27 +02:00
2025-10-30 11:24:56 +01:00
# truthy helper
is_truthy() {
case "${1:-}" in 1|true|TRUE|yes|YES|on|ON) return 0;; *) return 1;; esac
}
# -------- basic auth directives --------
BASIC_AUTH_DIRECTIVES=""
if is_truthy "${ENABLE_BASIC_AUTH:-}"; then
log "Enabling basic authentication"
[ -f "$HTPASSWD_PATH" ] || die "htpasswd not found: $HTPASSWD_PATH"
BASIC_AUTH_DIRECTIVES=$(cat <<'EOF'
auth_basic "__REALM__";
auth_basic_user_file __HTPASSWD__;
2025-10-30 11:03:09 +01:00
EOF
2025-10-30 11:24:56 +01:00
)
# safe placeholder substitution
esc() { printf '%s' "$1" | sed 's/[&/\]/\\&/g'; }
BASIC_AUTH_DIRECTIVES=$(printf '%s' "$BASIC_AUTH_DIRECTIVES" \
| sed "s|__REALM__|$(esc "$BASIC_AUTH_REALM")|g" \
| sed "s|__HTPASSWD__|$(esc "$HTPASSWD_PATH")|g")
2025-10-30 10:54:32 +01:00
else
2025-10-30 11:24:56 +01:00
log "Basic authentication is disabled"
2025-10-30 11:03:09 +01:00
fi
2025-10-30 11:24:56 +01:00
export BASIC_AUTH_DIRECTIVES
2025-10-30 11:03:09 +01:00
2025-10-30 11:24:56 +01:00
# -------- render nginx config (atomic) --------
[ -f "$NGINX_TEMPLATE" ] || die "Missing template: $NGINX_TEMPLATE"
[ -d "$NGINX_HTTPD_DIR" ] || die "Missing nginx conf dir: $NGINX_HTTPD_DIR"
tmpconf="$(mktemp "${NGINX_CONF_OUT}.XXXXXX")"
envsubst '$SERVER_NAME,$BASIC_AUTH_DIRECTIVES' <"$NGINX_TEMPLATE" >"$tmpconf" \
|| die "envsubst failed"
mv -f "$tmpconf" "$NGINX_CONF_OUT"
log "Wrote nginx config -> $NGINX_CONF_OUT"
# final sanity test against active config tree
nginx -t || die "nginx config test failed after install"
2025-11-26 13:59:02 +00:00
# -------- Laravel setup --------
2025-10-30 11:24:56 +01:00
if [ -f "$APP_DIR/artisan" ]; then
2025-11-26 13:59:02 +00:00
log "Setting up Laravel application"
# Ensure cache directories exist with proper permissions
mkdir -p "$APP_DIR/storage/framework/cache/data" "$APP_DIR/bootstrap/cache"
chmod -R 775 "$APP_DIR/storage" "$APP_DIR/bootstrap/cache" 2>/dev/null || true
2025-11-26 14:09:30 +00:00
# Remove bootstrap config cache to force reload from .env
rm -f "$APP_DIR/bootstrap/cache/config.php" 2>/dev/null || true
# Run database migrations FIRST (creates cache table if using database driver)
2025-11-26 13:59:02 +00:00
log "Running database migrations"
( cd "$APP_DIR" && php artisan migrate --force 2>&1 || log "migrations failed (continuing)" ) | head -10
2025-11-27 09:20:47 +00:00
# Run database seeders to create initial users
log "Running database seeders"
( cd "$APP_DIR" && php artisan db:seed --force 2>&1 || log "seeders failed (continuing)" ) | head -10
2025-11-26 14:09:30 +00:00
# Clear caches AFTER migrations (now cache table exists)
2025-10-30 11:24:56 +01:00
log "Clearing Laravel caches"
2025-11-26 14:09:30 +00:00
( cd "$APP_DIR" && php artisan config:clear 2>&1 || log "config:clear failed (continuing)" ) | head -5
( cd "$APP_DIR" && php artisan cache:clear 2>&1 || log "cache:clear failed (continuing)" ) | head -5
2025-10-30 11:03:09 +01:00
else
2025-10-30 11:24:56 +01:00
log "No Laravel app at $APP_DIR; skipping artisan"
2025-10-30 10:54:32 +01:00
fi
2025-10-30 11:24:56 +01:00
# -------- entrypoint behavior --------
# If the first arg is an option (starts with -), assume supervisord.
if [ "${1:-}" ] && [ "${1#-}" != "$1" ]; then
set -- /usr/bin/supervisord -n -c "$SUPERVISOR_CONF" "$@"
2025-10-30 11:00:53 +01:00
fi
2025-10-23 12:22:27 +02:00
2025-10-30 11:24:56 +01:00
# If they provided a command (e.g., sh, bash, php -v), run it.
if [ "${1:-}" ] && [ "$1" != "/usr/bin/supervisord" ] && [ "$1" != "supervisord" ]; then
log "Executing custom command: $*"
exec "$@"
fi
2025-10-23 12:39:40 +02:00
2025-10-30 11:24:56 +01:00
# Default: start supervisord in the foreground (PID 1)
log "Starting supervisord"
exec /usr/bin/supervisord -n -c "$SUPERVISOR_CONF"