-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-server.sh
More file actions
executable file
·81 lines (64 loc) · 2.82 KB
/
setup-server.sh
File metadata and controls
executable file
·81 lines (64 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/bin/bash
# First-time server setup for swf-remote.
# Creates PostgreSQL database, .env file, and runs migrations.
# Idempotent — safe to re-run.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
SRC_DIR="$SCRIPT_DIR/src"
ENV_FILE="$SRC_DIR/.env"
# These are swf-remote's own database. Do not inherit from environment.
DB_NAME=swf_remote
DB_USER=swf_remote
DB_PASSWORD=swf_remote
DB_HOST=localhost
DB_PORT=5432
echo "=== swf-remote server setup ==="
# ── PostgreSQL database and user ────────────────────────────────────────────
if sudo -u postgres psql -c "SELECT 1 FROM pg_roles WHERE rolname='$DB_USER'" 2>/dev/null | grep -q 1; then
echo "PostgreSQL user '$DB_USER' exists"
else
sudo -u postgres psql -c "CREATE USER $DB_USER WITH PASSWORD '$DB_PASSWORD';"
echo "PostgreSQL user '$DB_USER' created"
fi
if sudo -u postgres psql -c "SELECT 1 FROM pg_database WHERE datname='$DB_NAME'" 2>/dev/null | grep -q 1; then
echo "PostgreSQL database '$DB_NAME' exists"
else
sudo -u postgres psql -c "CREATE DATABASE $DB_NAME OWNER $DB_USER;"
echo "PostgreSQL database '$DB_NAME' created"
fi
# ── .env file ───────────────────────────────────────────────────────────────
if [ -f "$ENV_FILE" ]; then
echo ".env exists: $ENV_FILE"
else
SECRET_KEY=$(python3 -c "import secrets; print(secrets.token_urlsafe(50))")
cat > "$ENV_FILE" <<EOF
# Generated by setup-server.sh
# All prefixed SWF_REMOTE_ to avoid env var collisions.
SWF_REMOTE_SECRET_KEY=$SECRET_KEY
SWF_REMOTE_DEBUG=True
SWF_REMOTE_ALLOWED_HOSTS=localhost,127.0.0.1
# Database
SWF_REMOTE_DB_NAME=$DB_NAME
SWF_REMOTE_DB_USER=$DB_USER
SWF_REMOTE_DB_PASSWORD=$DB_PASSWORD
SWF_REMOTE_DB_HOST=$DB_HOST
SWF_REMOTE_DB_PORT=$DB_PORT
# Static files
SWF_REMOTE_STATIC_URL=/static/
# swf-monitor connection (via SSH tunnel to pandaserver02)
SWF_REMOTE_MONITOR_URL=https://localhost:18443/swf-monitor
EOF
echo ".env created: $ENV_FILE"
fi
# ── Run dev setup if not done ───────────────────────────────────────────────
if [ ! -d "$SCRIPT_DIR/.venv" ]; then
echo "Running setup-dev.sh first..."
bash "$SCRIPT_DIR/setup-dev.sh"
fi
# ── Migrate ─────────────────────────────────────────────────────────────────
echo "Running migrations..."
cd "$SRC_DIR"
"$SCRIPT_DIR/.venv/bin/python" manage.py migrate
echo ""
echo "=== Server setup complete ==="
echo "Start dev server: cd src && ../.venv/bin/python manage.py runserver"