refactor(setup): replace tmux with systemd in setup.sh
This commit is contained in:
@@ -1,9 +1,6 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -euo pipefail
|
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'
|
RED='\033[0;31m'
|
||||||
GREEN='\033[0;32m'
|
GREEN='\033[0;32m'
|
||||||
YELLOW='\033[1;33m'
|
YELLOW='\033[1;33m'
|
||||||
@@ -13,22 +10,57 @@ info() { echo -e "${GREEN}[+]${NC} $*"; }
|
|||||||
warn() { echo -e "${YELLOW}[!]${NC} $*"; }
|
warn() { echo -e "${YELLOW}[!]${NC} $*"; }
|
||||||
die() { echo -e "${RED}[✗] FATAL:${NC} $*" >&2; exit 1; }
|
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 ────────────────────────────────────────────────
|
# ── 1. ensure we're in thePlug ────────────────────────────────────────────────
|
||||||
if [[ "$PWD" != "/srv/theplug" ]]; then
|
if [[ -d "/srv/theplug" ]]; then
|
||||||
if tmux has-session -t "$SESSION" 2>/dev/null; then
|
info "thePlug già presente — avvio diretto..."
|
||||||
info "thePlug già in esecuzione — attaching..."
|
cd /srv/theplug
|
||||||
exec tmux attach -t "$SESSION"
|
start_services
|
||||||
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
cd /srv
|
cd /srv
|
||||||
git clone https://git.reloia.uk/reloia/thePlug theplug || die "git clone failed"
|
git clone https://git.reloia.uk/reloia/thePlug theplug || die "git clone failed"
|
||||||
cd theplug
|
cd theplug
|
||||||
fi
|
|
||||||
|
|
||||||
chmod +x ./setup.sh
|
chmod +x ./setup.sh
|
||||||
|
|
||||||
PLUG_DIR="$(pwd)"
|
PLUG_DIR="$(pwd)"
|
||||||
|
|
||||||
if [[ ! -L /usr/bin/theplug ]]; then
|
if [[ ! -L /usr/bin/theplug ]]; then
|
||||||
info "Creating symlink /usr/bin/theplug..."
|
info "Creating symlink /usr/bin/theplug..."
|
||||||
sudo ln -sf "$PLUG_DIR/setup.sh" /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)"
|
info "uv installed: $(uv --version)"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# ── 5. ensure tmux is available ───────────────────────────────────────────────
|
# ── 5. venv + deps ────────────────────────────────────────────────────────────
|
||||||
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..."
|
info "Pinning Python 3.14..."
|
||||||
uv python pin 3.14
|
uv python pin 3.14
|
||||||
|
|
||||||
info "Creating/updating venv..."
|
info "Creating venv..."
|
||||||
uv venv
|
uv venv
|
||||||
source .venv/bin/activate
|
|
||||||
|
|
||||||
info "Syncing dependencies..."
|
info "Syncing dependencies..."
|
||||||
uv sync
|
uv sync
|
||||||
|
|
||||||
# ── 7. run in tmux with autorestart ───────────────────────────────────────────
|
# ── 6. install service + start ────────────────────────────────────────────────
|
||||||
if tmux has-session -t "$SESSION" 2>/dev/null; then
|
install_service
|
||||||
warn "tmux session '$SESSION' already exists — killing it..."
|
systemctl --user start theplug
|
||||||
tmux kill-session -t "$SESSION"
|
|
||||||
fi
|
|
||||||
|
|
||||||
info "Launching FastAPI in tmux session '$SESSION'..."
|
echo -e ""
|
||||||
|
|
||||||
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 "";
|
|
||||||
info "Automatically generated Secrets:"
|
info "Automatically generated Secrets:"
|
||||||
info " MONGO_PASS: $MONGO_PASS"
|
info " MONGO_PASS: $MONGO_PASS"
|
||||||
info " JWT_SECRET: $JWT_SECRET"
|
info " JWT_SECRET: $JWT_SECRET"
|
||||||
info " AUTH_PASS: $AUTH_PASS - questa è importante"
|
info " AUTH_PASS: $AUTH_PASS - questa è importante"
|
||||||
warn "Save them — these won't be showed again (non è vero basta che leggi $PLUG_DIR/.env)"
|
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 thePlug in $PLUG_DIR"
|
||||||
info "Installed in $PLUG_DIR"
|
warn "Use 'systemctl --user status theplug' to check / 'journalctl --user -u theplug -f' to follow logs"
|
||||||
echo -e "";
|
echo -e ""
|
||||||
|
|
||||||
info "Done — attach with: 'theplug' or 'tmux attach -t $SESSION'"
|
|
||||||
warn "To detatch from tmux do: ctrl+b d"
|
|
||||||
Reference in New Issue
Block a user