refactor(setup): replace tmux with systemd in setup.sh

This commit is contained in:
2026-04-13 19:18:25 +02:00 Unverified
parent 479a03a1e1
commit c7bab076dc
+55 -51
View File
@@ -1,9 +1,6 @@
#!/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'
@@ -13,22 +10,57 @@ info() { echo -e "${GREEN}[+]${NC} $*"; }
warn() { echo -e "${YELLOW}[!]${NC} $*"; }
die() { echo -e "${RED}[✗] FATAL:${NC} $*" >&2; exit 1; }
SESSION="theplug"
install_service() {
local uv_bin
uv_bin=$(command -v uv)
local api_port
api_port=$(grep -E '^API_PORT=' .env 2>/dev/null | cut -d '=' -f2)
api_port=${api_port:-3671}
mkdir -p ~/.config/systemd/user
cat > ~/.config/systemd/user/theplug.service <<EOF
[Unit]
Description=thePlug service
After=network.target
[Service]
WorkingDirectory=/srv/theplug
EnvironmentFile=/srv/theplug/.env
Environment=PYTHONPATH=app
ExecStart=$uv_bin run uvicorn app.main:app --host 0.0.0.0 --port $api_port
Restart=always
RestartSec=5
[Install]
WantedBy=default.target
EOF
systemctl --user daemon-reload
systemctl --user enable theplug
info "systemd user service installed (port $api_port)"
}
start_services() {
info "Starting Redis and MongoDB..."
docker compose up -d redis mongodb || die "docker compose failed — are Redis/MongoDB defined in compose.yml?"
info "Starting thePlug via systemctl..."
systemctl --user start theplug
warn "Use 'systemctl --user status theplug' to check / 'journalctl --user -u theplug -f' to follow logs"
}
# ── 1. ensure we're in thePlug ────────────────────────────────────────────────
if [[ "$PWD" != "/srv/theplug" ]]; then
if tmux has-session -t "$SESSION" 2>/dev/null; then
info "thePlug già in esecuzione — attaching..."
exec tmux attach -t "$SESSION"
fi
cd /srv
git clone https://git.reloia.uk/reloia/thePlug theplug || die "git clone failed"
cd theplug
if [[ -d "/srv/theplug" ]]; then
info "thePlug già presente — avvio diretto..."
cd /srv/theplug
start_services
exit 0
fi
cd /srv
git clone https://git.reloia.uk/reloia/thePlug theplug || die "git clone failed"
cd theplug
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
@@ -66,56 +98,28 @@ if ! command -v uv &>/dev/null; then
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 ────────────────────────────────────────────────────────────
# ── 5. venv + deps ────────────────────────────────────────────────────────────
info "Pinning Python 3.14..."
uv python pin 3.14
info "Creating/updating venv..."
info "Creating venv..."
uv venv
source .venv/bin/activate
info "Syncing dependencies..."
uv sync
# ── 7. run in tmux with autorestart ───────────────────────────────────────────
if tmux has-session -t "$SESSION" 2>/dev/null; then
warn "tmux session '$SESSION' already exists — killing it..."
tmux kill-session -t "$SESSION"
fi
# ── 6. install service + start ────────────────────────────────────────────────
install_service
systemctl --user start theplug
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
'"
echo -e "";
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 "";
echo -e ""
info "Installed this script into /usr/bin/theplug"
info "Installed in $PLUG_DIR"
echo -e "";
info "Done — attach with: 'theplug' or 'tmux attach -t $SESSION'"
warn "To detatch from tmux do: ctrl+b d"
info "Installed thePlug in $PLUG_DIR"
warn "Use 'systemctl --user status theplug' to check / 'journalctl --user -u theplug -f' to follow logs"
echo -e ""