Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-25 08:29:12

0001 #!/bin/bash
0002 # First-time server setup for swf-remote.
0003 # Creates PostgreSQL database, .env file, and runs migrations.
0004 # Idempotent — safe to re-run.
0005 
0006 set -euo pipefail
0007 
0008 SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
0009 SRC_DIR="$SCRIPT_DIR/src"
0010 ENV_FILE="$SRC_DIR/.env"
0011 
0012 # These are swf-remote's own database. Do not inherit from environment.
0013 DB_NAME=swf_remote
0014 DB_USER=swf_remote
0015 DB_PASSWORD=swf_remote
0016 DB_HOST=localhost
0017 DB_PORT=5432
0018 
0019 echo "=== swf-remote server setup ==="
0020 
0021 # ── PostgreSQL database and user ────────────────────────────────────────────
0022 
0023 if sudo -u postgres psql -c "SELECT 1 FROM pg_roles WHERE rolname='$DB_USER'" 2>/dev/null | grep -q 1; then
0024     echo "PostgreSQL user '$DB_USER' exists"
0025 else
0026     sudo -u postgres psql -c "CREATE USER $DB_USER WITH PASSWORD '$DB_PASSWORD';"
0027     echo "PostgreSQL user '$DB_USER' created"
0028 fi
0029 
0030 if sudo -u postgres psql -c "SELECT 1 FROM pg_database WHERE datname='$DB_NAME'" 2>/dev/null | grep -q 1; then
0031     echo "PostgreSQL database '$DB_NAME' exists"
0032 else
0033     sudo -u postgres psql -c "CREATE DATABASE $DB_NAME OWNER $DB_USER;"
0034     echo "PostgreSQL database '$DB_NAME' created"
0035 fi
0036 
0037 # ── .env file ───────────────────────────────────────────────────────────────
0038 
0039 if [ -f "$ENV_FILE" ]; then
0040     echo ".env exists: $ENV_FILE"
0041 else
0042     SECRET_KEY=$(python3 -c "import secrets; print(secrets.token_urlsafe(50))")
0043     cat > "$ENV_FILE" <<EOF
0044 # Generated by setup-server.sh
0045 # All prefixed SWF_REMOTE_ to avoid env var collisions.
0046 SWF_REMOTE_SECRET_KEY=$SECRET_KEY
0047 SWF_REMOTE_DEBUG=True
0048 SWF_REMOTE_ALLOWED_HOSTS=localhost,127.0.0.1
0049 
0050 # Database
0051 SWF_REMOTE_DB_NAME=$DB_NAME
0052 SWF_REMOTE_DB_USER=$DB_USER
0053 SWF_REMOTE_DB_PASSWORD=$DB_PASSWORD
0054 SWF_REMOTE_DB_HOST=$DB_HOST
0055 SWF_REMOTE_DB_PORT=$DB_PORT
0056 
0057 # Static files
0058 SWF_REMOTE_STATIC_URL=/static/
0059 
0060 # swf-monitor connection (via SSH tunnel to pandaserver02)
0061 SWF_REMOTE_MONITOR_URL=https://localhost:18443/swf-monitor
0062 EOF
0063     echo ".env created: $ENV_FILE"
0064 fi
0065 
0066 # ── Run dev setup if not done ───────────────────────────────────────────────
0067 
0068 if [ ! -d "$SCRIPT_DIR/.venv" ]; then
0069     echo "Running setup-dev.sh first..."
0070     bash "$SCRIPT_DIR/setup-dev.sh"
0071 fi
0072 
0073 # ── Migrate ─────────────────────────────────────────────────────────────────
0074 
0075 echo "Running migrations..."
0076 cd "$SRC_DIR"
0077 "$SCRIPT_DIR/.venv/bin/python" manage.py migrate
0078 
0079 echo ""
0080 echo "=== Server setup complete ==="
0081 echo "Start dev server: cd src && ../.venv/bin/python manage.py runserver"