From c7bab076dcfc772811b984353f2553b7f108d40b Mon Sep 17 00:00:00 2001 From: ReLoia Date: Mon, 13 Apr 2026 19:18:25 +0200 Subject: [PATCH] refactor(setup): replace tmux with systemd in setup.sh --- setup.sh | 106 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 55 insertions(+), 51 deletions(-) diff --git a/setup.sh b/setup.sh index bf5a459..f612648 100644 --- a/setup.sh +++ b/setup.sh @@ -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 </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" \ No newline at end of file +info "Installed thePlug in $PLUG_DIR" +warn "Use 'systemctl --user status theplug' to check / 'journalctl --user -u theplug -f' to follow logs" +echo -e "" \ No newline at end of file