feat(setup): add setup.sh script for automated environment provisioning and app launch
This commit is contained in:
+5
-5
@@ -1,10 +1,10 @@
|
|||||||
API_PORT=3671
|
API_PORT=3671
|
||||||
MONGO_INITDB_ROOT_PASSWORD=pass
|
MONGO_INITDB_ROOT_PASSWORD=pass# esegui 'openssl rand -hex 5' in un terminale e copia qui
|
||||||
MONGODB_URI=mongodb://theplug:pass@mongodb:27017/theplug?authSource=admin
|
MONGODB_URI=mongodb://theplug:pass@localhost:27017/theplug?authSource=admin
|
||||||
MONGODB_DB_NAME=theplug
|
MONGODB_DB_NAME=theplug
|
||||||
REDIS_URL=redis://redis:6379
|
REDIS_URL=redis://localhost:6379
|
||||||
JWT_SECRET_KEY=change-this-secret
|
JWT_SECRET_KEY= # esegui 'openssl rand -hex 20' in un terminale e copia qui
|
||||||
JWT_ALGORITHM=HS256
|
JWT_ALGORITHM=HS256
|
||||||
JWT_EXPIRE_MINUTES=30
|
JWT_EXPIRE_MINUTES=30
|
||||||
AUTH_USERNAME=admin
|
AUTH_USERNAME=admin
|
||||||
AUTH_PASSWORD=change-this-password
|
AUTH_PASSWORD= # esegui 'openssl rand -hex 20' in un terminale e copia qui
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
name: Build and Push to Registry
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
container: catthehacker/ubuntu:act-latest
|
|
||||||
steps:
|
|
||||||
- name: Checkout
|
|
||||||
uses: actions/checkout@v6
|
|
||||||
|
|
||||||
- name: Login to Gitea Registry
|
|
||||||
uses: docker/login-action@v3
|
|
||||||
with:
|
|
||||||
registry: git.reloia.uk
|
|
||||||
username: reloia
|
|
||||||
password: ${{ secrets.LOGIN_TOKEN }}
|
|
||||||
|
|
||||||
- name: Build and Push (Shell)
|
|
||||||
run: |
|
|
||||||
IMAGE_NAME="git.reloia.uk/reloia/theplug:latest"
|
|
||||||
|
|
||||||
echo "Building $IMAGE_NAME..."
|
|
||||||
docker build -t "$IMAGE_NAME" .
|
|
||||||
|
|
||||||
echo "Pushing $IMAGE_NAME..."
|
|
||||||
docker push "$IMAGE_NAME"
|
|
||||||
|
|
||||||
@@ -8,7 +8,13 @@ icona di una presa della spina idk
|
|||||||
FastAPI + MongoDB + Redis
|
FastAPI + MongoDB + Redis
|
||||||
```
|
```
|
||||||
|
|
||||||
### Setup Dev
|
## Quick Setup (prod)
|
||||||
|
|
||||||
|
```bash
|
||||||
|
bash <(curl -s https://git.reloia.uk/reloia/thePlug/raw/branch/master/setup.sh)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Setup Dev
|
||||||
|
|
||||||
imposta le variabili d'ambiente
|
imposta le variabili d'ambiente
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import os
|
||||||
import sys
|
import sys
|
||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
@@ -43,9 +44,15 @@ async def lifespan(_: FastAPI):
|
|||||||
await close_redis()
|
await close_redis()
|
||||||
await close_mongodb()
|
await close_mongodb()
|
||||||
|
|
||||||
|
|
||||||
|
DEBUG = os.getenv("DEBUG", "false").lower() == "true"
|
||||||
|
|
||||||
app = FastAPI(
|
app = FastAPI(
|
||||||
title="thePlugAPI",
|
title="thePlugAPI",
|
||||||
lifespan=lifespan,
|
lifespan=lifespan,
|
||||||
|
docs_url="/docs" if DEBUG else None,
|
||||||
|
redoc_url="/redoc" if DEBUG else None,
|
||||||
|
openapi_url="/openapi.json" if DEBUG else None,
|
||||||
)
|
)
|
||||||
|
|
||||||
app.include_router(auth_router)
|
app.include_router(auth_router)
|
||||||
|
|||||||
+1
-30
@@ -1,40 +1,11 @@
|
|||||||
services:
|
services:
|
||||||
api:
|
|
||||||
build:
|
|
||||||
context: .
|
|
||||||
dockerfile: Dockerfile
|
|
||||||
container_name: theplug-api
|
|
||||||
restart: unless-stopped
|
|
||||||
ports:
|
|
||||||
- "${API_PORT:-3671}:3671"
|
|
||||||
environment:
|
|
||||||
MONGODB_URI: mongodb://${MONGO_USER:-theplug}:${MONGO_PASSWORD:-theplug_dev_password}@mongodb:27017/${MONGO_DB:-theplug}?authSource=admin
|
|
||||||
MONGODB_DB_NAME: ${MONGO_DB:-theplug}
|
|
||||||
REDIS_URL: redis://redis:6379/0
|
|
||||||
JWT_SECRET_KEY: ${JWT_SECRET_KEY:-change-this-secret}
|
|
||||||
JWT_ALGORITHM: ${JWT_ALGORITHM:-HS256}
|
|
||||||
JWT_EXPIRE_MINUTES: ${JWT_EXPIRE_MINUTES:-30}
|
|
||||||
AUTH_USERNAME: ${AUTH_USERNAME:-admin}
|
|
||||||
AUTH_PASSWORD: ${AUTH_PASSWORD:-change-this-password}
|
|
||||||
depends_on:
|
|
||||||
mongodb:
|
|
||||||
condition: service_healthy
|
|
||||||
redis:
|
|
||||||
condition: service_healthy
|
|
||||||
healthcheck:
|
|
||||||
test: ["CMD-SHELL", "python -c \"import urllib.request; urllib.request.urlopen('http://127.0.0.1:3671/').read()\""]
|
|
||||||
interval: 10s
|
|
||||||
timeout: 5s
|
|
||||||
retries: 5
|
|
||||||
start_period: 10s
|
|
||||||
|
|
||||||
mongodb:
|
mongodb:
|
||||||
image: mongo:8.2
|
image: mongo:8.2
|
||||||
container_name: theplug-mongodb
|
container_name: theplug-mongodb
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
environment:
|
environment:
|
||||||
MONGO_INITDB_ROOT_USERNAME: ${MONGO_USER:-theplug}
|
MONGO_INITDB_ROOT_USERNAME: ${MONGO_USER:-theplug}
|
||||||
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_PASSWORD:-pass}
|
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD}
|
||||||
MONGO_INITDB_DATABASE: ${MONGO_DB:-theplug}
|
MONGO_INITDB_DATABASE: ${MONGO_DB:-theplug}
|
||||||
ports:
|
ports:
|
||||||
- "27017:27017"
|
- "27017:27017"
|
||||||
|
|||||||
@@ -0,0 +1,121 @@
|
|||||||
|
#!/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; }
|
||||||
|
|
||||||
|
SESSION="theplug"
|
||||||
|
|
||||||
|
# ── 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
|
||||||
|
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
|
||||||
|
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. 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 ───────────────────────────────────────────
|
||||||
|
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
|
||||||
|
'"
|
||||||
|
|
||||||
|
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 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"
|
||||||
Reference in New Issue
Block a user