118 lines
4.6 KiB
Bash
118 lines
4.6 KiB
Bash
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
# nota di Renato
|
|
# fatto da Claude Sonnet 4.6 (preferisco tagliarmi le palle che scrivere un bash a mano)
|
|
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
NC='\033[0m'
|
|
|
|
info() { echo -e "${GREEN}[+]${NC} $*"; }
|
|
warn() { echo -e "${YELLOW}[!]${NC} $*"; }
|
|
die() { echo -e "${RED}[✗] FATAL:${NC} $*" >&2; exit 1; }
|
|
|
|
if [[ "${1:-}" == "logs" ]]; then
|
|
tmux has-session -t "$SESSION" 2>/dev/null || die "nessuna sessione '$SESSION' attiva"
|
|
exec tmux attach -t "$SESSION"
|
|
fi
|
|
|
|
# ── 1. ensure we're in thePlug ────────────────────────────────────────────────
|
|
cd /srv
|
|
if [[ "$(basename "$PWD")" != "thePlug" ]]; then
|
|
warn "Not in thePlug — cloning..."
|
|
git clone https://git.reloia.uk/reloia/thePlug || die "git clone failed"
|
|
cd thePlug
|
|
fi
|
|
|
|
chmod +x ./setup.sh
|
|
|
|
PLUG_DIR="$(pwd)"
|
|
if [[ ! -L /usr/bin/theplug ]]; then
|
|
info "Creating symlink /usr/bin/theplug..."
|
|
sudo ln -sf "$PLUG_DIR/setup.sh" /usr/bin/theplug
|
|
sudo chmod +x /usr/bin/theplug
|
|
info "Puoi ora usare 'theplug' e 'theplug logs' da qualsiasi directory"
|
|
fi
|
|
|
|
# 2 env
|
|
if [[ ! -f .env ]]; then
|
|
warn ".env not found — copyinh from .env.example with random stuff..."
|
|
cp .env.example .env || die ".env.example not found (impossibile)"
|
|
|
|
MONGO_PASS=$(openssl rand -hex 8)
|
|
JWT_SECRET=$(openssl rand -hex 20)
|
|
AUTH_PASS=$(openssl rand -hex 10)
|
|
|
|
sed -i "s|^MONGO_INITDB_ROOT_PASSWORD=.*|MONGO_INITDB_ROOT_PASSWORD=$MONGO_PASS|" .env
|
|
sed -i "s|^MONGODB_URI=.*|MONGODB_URI=mongodb://theplug:$MONGO_PASS@localhost:27017/theplug?authSource=admin|" .env
|
|
sed -i "s|^JWT_SECRET_KEY=.*|JWT_SECRET_KEY=$JWT_SECRET|" .env
|
|
sed -i "s|^AUTH_PASSWORD=.*|AUTH_PASSWORD=$AUTH_PASS|" .env
|
|
fi
|
|
|
|
# ── 3. spin up dependencies ───────────────────────────────────────────────────
|
|
info "Starting Redis and MongoDB..."
|
|
docker compose up -d redis mongodb || die "docker compose failed — are Redis/MongoDB defined in compose.yml?"
|
|
|
|
# ── 4. ensure uv is available ─────────────────────────────────────────────────
|
|
if ! command -v uv &>/dev/null; then
|
|
warn "uv not found — installing..."
|
|
curl -LsSf https://astral.sh/uv/install.sh | sh || die "uv installation failed"
|
|
export PATH="$HOME/.local/bin:$PATH"
|
|
command -v uv &>/dev/null || die "uv installed but still not in PATH — open a new shell and retry"
|
|
info "uv installed: $(uv --version)"
|
|
fi
|
|
|
|
# ── 5. ensure tmux is available ───────────────────────────────────────────────
|
|
if ! command -v tmux &>/dev/null; then
|
|
warn "tmux not found — installing..."
|
|
sudo apt-get install -y tmux || die "tmux installation failed"
|
|
info "tmux installed"
|
|
fi
|
|
|
|
# ── 6. venv + deps ────────────────────────────────────────────────────────────
|
|
info "Pinning Python 3.14..."
|
|
uv python pin 3.14
|
|
|
|
info "Creating/updating venv..."
|
|
uv venv
|
|
source .venv/bin/activate
|
|
|
|
info "Syncing dependencies..."
|
|
uv sync
|
|
|
|
# ── 7. run in tmux with autorestart ───────────────────────────────────────────
|
|
SESSION="theplug"
|
|
|
|
if tmux has-session -t "$SESSION" 2>/dev/null; then
|
|
warn "tmux session '$SESSION' already exists — killing it..."
|
|
tmux kill-session -t "$SESSION"
|
|
fi
|
|
|
|
info "Launching FastAPI in tmux session '$SESSION'..."
|
|
|
|
API_PORT=$(grep -E '^API_PORT=' .env | cut -d '=' -f2)
|
|
API_PORT=${API_PORT:-3671}
|
|
tmux new-session -d -s "$SESSION" "bash -c '
|
|
cd $(pwd)
|
|
source .venv/bin/activate
|
|
while true; do
|
|
echo \"[\$(date +%T)] Starting uvicorn...\"
|
|
PYTHONPATH=app uv run uvicorn app.main:app --host 0.0.0.0 --port $API_PORT 2>&1 | tee uvicorn.log
|
|
echo \"[\$(date +%T)] Process exited — restarting in 3s (Ctrl+C to stop)\"
|
|
sleep 3
|
|
done
|
|
'"
|
|
|
|
info "Done — attach with: tmux attach -t $SESSION"
|
|
|
|
echo -e "";
|
|
info "Automatically generated Secrets:"
|
|
info " MONGO_PASS: $MONGO_PASS"
|
|
info " JWT_SECRET: $JWT_SECRET"
|
|
info " AUTH_PASS: $AUTH_PASS - questa è importante"
|
|
warn "Save them — these won't be showed again (non è vero basta che leggi $PLUG_DIR/.env)"
|
|
echo -e "";
|
|
|
|
info "Installed in $PLUG_DIR, run setup.sh again to restart or similar" |