BIG BIG BIG BIG BIG UPDATES

Co-authored-by: Copilot <[email protected]>
This commit is contained in:
gabdevele
2026-05-04 23:59:48 +02:00
co-authored by Copilot
Unverified
parent 46f7cd6a5f
commit 93ba8e9ac2
88 changed files with 7591 additions and 447 deletions
Regular → Executable
+56 -96
View File
@@ -6,121 +6,81 @@ 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; }
info() { echo -e "${GREEN}[+]${NC} $*"; }
warn() { echo -e "${YELLOW}[!]${NC} $*"; }
die() { echo -e "${RED}[x] FATAL:${NC} $*" >&2; exit 1; }
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ENV_FILE="$ROOT_DIR/backend/.env"
compose() {
docker compose --env-file "$ENV_FILE" -f "$ROOT_DIR/docker-compose.yml" "$@"
}
ensure_env() {
if [[ -f "$ENV_FILE" ]]; then
return
fi
warn "backend/.env not found, creating it from backend/.env.example"
cp "$ROOT_DIR/backend/.env.example" "$ENV_FILE"
local mongo_pass jwt_secret auth_pass
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_FILE"
sed -i "s|^MONGODB_URI=.*|MONGODB_URI=mongodb://theplug:$mongo_pass@localhost:27017/theplug?authSource=admin|" "$ENV_FILE"
sed -i "s|^JWT_SECRET_KEY=.*|JWT_SECRET_KEY=$jwt_secret|" "$ENV_FILE"
sed -i "s|^AUTH_PASSWORD=.*|AUTH_PASSWORD=$auth_pass|" "$ENV_FILE"
info "Generated credentials in $ENV_FILE"
info "AUTH_USERNAME: admin"
info "AUTH_PASSWORD: $auth_pass"
}
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
Description=thePlug stack
After=network.target
[Service]
WorkingDirectory=/srv/theplug
EnvironmentFile=/srv/theplug/.env
Environment=PYTHONPATH=app
ExecStartPre=/usr/bin/docker compose -f /srv/theplug/compose.yml up -d redis mongodb
ExecStart=$uv_bin run uvicorn app.main:app --host 0.0.0.0 --port $api_port
Restart=always
RestartSec=5
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=$ROOT_DIR
ExecStart=/usr/bin/docker compose --env-file $ENV_FILE -f $ROOT_DIR/docker-compose.yml up -d
ExecStop=/usr/bin/docker compose --env-file $ENV_FILE -f $ROOT_DIR/docker-compose.yml down
[Install]
WantedBy=default.target
EOF
systemctl --user daemon-reload
systemctl --user enable theplug
info "systemd user service installed (port $api_port)"
info "systemd user service installed"
}
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..."
start_stack() {
ensure_env
info "Building and starting backend, frontend, MongoDB and Redis..."
compose up -d --build
info "Backend: http://localhost:$(grep -E '^API_PORT=' "$ENV_FILE" | cut -d '=' -f2 || echo 3671)"
info "Frontend: http://localhost:$(grep -E '^FRONTEND_PORT=' "$ENV_FILE" | cut -d '=' -f2 || echo 5173)"
}
if [[ "${1:-}" == "service" ]]; then
ensure_env
install_service
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 [[ -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
sudo chmod +x /usr/bin/theplug
if [[ -d "/srv/theplug" && "$ROOT_DIR" != "/srv/theplug" ]]; then
info "thePlug found in /srv/theplug, switching there"
cd /srv/theplug
exec ./setup.sh "$@"
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
info "Resetting docker containers..."
docker compose down -v 2>/dev/null || true
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. venv + deps ────────────────────────────────────────────────────────────
info "Pinning Python 3.14..."
uv python pin 3.14
info "Creating venv..."
uv venv
info "Syncing dependencies..."
uv sync
# ── 6. install service + start ────────────────────────────────────────────────
install_service
systemctl --user start theplug
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 thePlug in $PLUG_DIR"
warn "Use 'systemctl --user status theplug' to check / 'journalctl --user -u theplug -f' to follow logs"
echo -e ""
start_stack