Production

This commit is contained in:
Shubhankar45
2026-09-22 06:23:46 +00:00
commit 8fdd20b29f
217 changed files with 31212 additions and 0 deletions
+72
View File
@@ -0,0 +1,72 @@
#!/usr/bin/env bash
# Prepare .env for a Docker deployment on this machine. Safe to re-run.
#
# ./scripts/setup.sh # production values
# DOMAIN=app.example.com ./scripts/setup.sh
#
# What it does:
# * creates .env from .env.example if it does not exist
# * generates SECRET_KEY when it is missing or a placeholder
# * sets ENVIRONMENT=production and DEBUG=false
# * generates POSTGRES_PASSWORD only for a brand-new database (an existing database
# volume keeps the password it was created with, so it is never changed here)
# * picks free host ports: if 80/443 are taken by another service (a host nginx, another
# Caddy, ...), it serves plain HTTP on the first free ports from 2000/2443 instead
set -euo pipefail
cd "$(dirname "$0")/.."
ENV_FILE=.env
PROJECT=${COMPOSE_PROJECT_NAME:-$(basename "$PWD" | tr '[:upper:]' '[:lower:]' | tr -cd 'a-z0-9_-')}
[ -f "$ENV_FILE" ] || { cp .env.example "$ENV_FILE"; echo "created $ENV_FILE from .env.example"; }
get() { grep -E "^$1=" "$ENV_FILE" | tail -1 | cut -d= -f2- || true; }
set_kv() {
if grep -qE "^$1=" "$ENV_FILE"; then sed -i -E "s|^$1=.*|$1=$2|" "$ENV_FILE"
else printf '%s=%s\n' "$1" "$2" >> "$ENV_FILE"; fi
}
random() { python3 -c "import secrets;print(secrets.token_urlsafe($1))" 2>/dev/null \
|| openssl rand -base64 "$1" | tr -d '\n=+/'; }
# Ports this project's own containers already publish are not "busy" for us.
own_ports=$(docker ps --filter "label=com.docker.compose.project=$PROJECT" \
--format '{{.Ports}}' 2>/dev/null | grep -oE ':[0-9]+->' | tr -d ':>-' || true)
busy() {
echo "$own_ports" | grep -qx "$1" && return 1
(exec 3<>"/dev/tcp/127.0.0.1/$1") 2>/dev/null
}
free_from() { local p=$1; while busy "$p"; do p=$((p + 1)); done; echo "$p"; }
# --- secrets ---------------------------------------------------------------
key=$(get SECRET_KEY)
case "$key" in ""|replace-with*|change-me*) key="" ;; esac
if [ "${#key}" -lt 32 ]; then set_kv SECRET_KEY "$(random 64)"; echo "generated SECRET_KEY"; fi
set_kv ENVIRONMENT production
set_kv DEBUG false
if docker volume inspect "${PROJECT}_pgdata" >/dev/null 2>&1; then
echo "database volume exists: keeping POSTGRES_PASSWORD"
case "$(get POSTGRES_PASSWORD)" in pharma|replace-with*|"")
echo "WARNING: the database still uses a weak/default password; rotate it (see README)." ;;
esac
else
case "$(get POSTGRES_PASSWORD)" in pharma|replace-with*|"")
set_kv POSTGRES_PASSWORD "$(random 24)"; echo "generated POSTGRES_PASSWORD" ;;
esac
fi
# --- ports -----------------------------------------------------------------
[ -n "${DOMAIN:-}" ] && set_kv DOMAIN "$DOMAIN"
domain=$(get DOMAIN); domain=${domain:-localhost}
if [ "$domain" != ":80" ] && ! busy 80 && ! busy 443; then
set_kv HTTP_PORT 80; set_kv HTTPS_PORT 443
echo "ports 80/443 are free: Caddy serves $domain directly"
else
http=$(free_from "$(get HTTP_PORT | grep -vx 80 || echo 2000)")
https=$(free_from "$(get HTTPS_PORT | grep -vx 443 || echo 2443)")
set_kv DOMAIN ":80"; set_kv HTTP_PORT "$http"; set_kv HTTPS_PORT "$https"
echo "ports 80/443 are in use: Caddy serves plain HTTP on host port $http"
echo " -> point your existing reverse proxy at http://127.0.0.1:$http"
fi
echo "done. Next: docker compose up -d --build"