iride-setup/dev-setup.sh

133 lines
6.2 KiB
Bash
Executable File

#!/bin/bash
# ══════════════════════════════════════════════════════════════════════════════
# IRIDE — dev-setup.sh: Gitea Runner (CI) sul server di sviluppo
# Tecnotel Servizi SRL
#
# SOLO sul server di sviluppo Tecnotel (mai su un server cliente). Installa il
# Gitea Runner ufficiale (ex act_runner) in modalità HOST: i job girano sul
# server con il Python e il Node già presenti, senza Docker.
#
# Uso: sudo bash /opt/iride-setup-pkg/dev-setup.sh --token <REGISTRATION_TOKEN>
# [--instance https://repo.argosdefense.io] [--name iride-dev]
# [--labels "ubuntu-latest:host,ubuntu-26.04:host"]
#
# Il token di registrazione si crea su Gitea: Impostazioni utente (tecnotel) →
# Actions → Runners → "Crea nuovo runner". Vale una volta sola.
# ══════════════════════════════════════════════════════════════════════════════
set -euo pipefail
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; CYAN='\033[0;36m'; NC='\033[0m'
info() { echo -e "${CYAN}[INFO]${NC} $1"; }
success() { echo -e "${GREEN}[OK]${NC} $1"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
error() { echo -e "${RED}[ERROR]${NC} $1" >&2; exit 1; }
INSTANCE="https://repo.argosdefense.io"
TOKEN=""
NAME="$(hostname -s)"
LABELS="ubuntu-latest:host,ubuntu-26.04:host,ubuntu-24.04:host"
RUNNER_USER="gitea-runner"
RUNNER_HOME="/opt/gitea-runner"
BIN="/usr/local/bin/gitea-runner"
while [[ $# -gt 0 ]]; do
case "$1" in
--token) TOKEN="$2"; shift 2 ;;
--instance) INSTANCE="$2"; shift 2 ;;
--name) NAME="$2"; shift 2 ;;
--labels) LABELS="$2"; shift 2 ;;
*) error "Opzione sconosciuta: $1" ;;
esac
done
[[ $EUID -eq 0 ]] || error "Eseguire con sudo"
[[ -n "$TOKEN" ]] || error "Serve --token <registration token> (Gitea → Impostazioni → Actions → Runners)"
command -v node >/dev/null || error "Node.js assente: eseguire prima first-setup.sh (serve alle azioni JavaScript in modalità host)"
command -v git >/dev/null || error "git assente"
# ── Utente dedicato ──────────────────────────────────────────────────────────
if ! id "$RUNNER_USER" &>/dev/null; then
useradd -r -s /bin/bash -m -d "$RUNNER_HOME" "$RUNNER_USER"
success "Utente $RUNNER_USER creato"
fi
mkdir -p "$RUNNER_HOME"
chown "$RUNNER_USER:$RUNNER_USER" "$RUNNER_HOME"
# ── Download dell'ultima release (gitea/runner, ex act_runner) ───────────────
info "Cerco l'ultima release di gitea/runner..."
ARCH="$(dpkg --print-architecture)" # amd64 | arm64
ASSET_URL="$(curl -fsSL https://gitea.com/api/v1/repos/gitea/runner/releases/latest | python3 -c "
import json, sys
rel = json.load(sys.stdin)
arch = sys.argv[1]
for a in rel.get('assets', []):
n = a.get('name', '')
if f'linux-{arch}' in n and not n.endswith(('.sha256', '.asc', '.sig', '.txt')):
print(a['browser_download_url']); break
print('', end='')
" "$ARCH")" || ASSET_URL=""
if [[ -z "$ASSET_URL" ]]; then
error "Nessun asset linux-$ARCH nell'ultima release di https://gitea.com/gitea/runner/releases: scaricare il binario a mano in $BIN e rilanciare"
fi
info "Asset: $ASSET_URL"
TMP="$(mktemp -d)"
curl -fsSL -o "$TMP/runner.bin" "$ASSET_URL"
if file "$TMP/runner.bin" | grep -qi "xz compressed"; then
xz -d -c "$TMP/runner.bin" > "$TMP/runner"
else
mv "$TMP/runner.bin" "$TMP/runner"
fi
install -m 0755 "$TMP/runner" "$BIN"
rm -rf "$TMP"
success "Runner installato: $($BIN --version 2>/dev/null | head -1 || echo "$BIN")"
# ── Registrazione (il token vale una volta) ──────────────────────────────────
if [[ -f "$RUNNER_HOME/.runner" ]]; then
warn "Runner già registrato ($RUNNER_HOME/.runner presente): salto la registrazione"
else
info "Registro il runner '$NAME' su $INSTANCE con label $LABELS"
sudo -u "$RUNNER_USER" -H bash -c "cd '$RUNNER_HOME' && '$BIN' register --no-interactive --instance '$INSTANCE' --token '$TOKEN' --name '$NAME' --labels '$LABELS'" \
|| error "Registrazione fallita: verificare token, URL dell'istanza e che Actions sia abilitato su Gitea (Site Administration → Actions)"
success "Runner registrato"
fi
if [[ ! -f "$RUNNER_HOME/config.yaml" ]]; then
# gitea-runner >= 1.0: "config init" scrive una config minima (act_runner usava generate-config)
sudo -u "$RUNNER_USER" -H bash -c "cd '$RUNNER_HOME' && ('$BIN' config init >/dev/null 2>&1 || '$BIN' generate-config > config.yaml)" \
&& success "config.yaml scritto in $RUNNER_HOME" || warn "config non generata: il runner userà i default"
fi
# ── Servizio systemd ─────────────────────────────────────────────────────────
CONFIG_ARG=""
[[ -f "$RUNNER_HOME/config.yaml" ]] && CONFIG_ARG=" --config $RUNNER_HOME/config.yaml"
cat > /etc/systemd/system/gitea-runner.service << UNITEOF
[Unit]
Description=Gitea Runner (IRIDE CI, modalità host)
After=network.target
[Service]
Type=simple
User=$RUNNER_USER
Group=$RUNNER_USER
WorkingDirectory=$RUNNER_HOME
ExecStart=$BIN daemon$CONFIG_ARG
Restart=always
RestartSec=5
Environment=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
StandardOutput=journal
StandardError=journal
SyslogIdentifier=gitea-runner
[Install]
WantedBy=multi-user.target
UNITEOF
systemctl daemon-reload
systemctl enable --now gitea-runner
sleep 2
if systemctl is-active --quiet gitea-runner; then
success "gitea-runner attivo: journalctl -u gitea-runner -f per i job"
else
error "gitea-runner non parte: journalctl -u gitea-runner -n 50"
fi
echo ""
echo " Verifica su Gitea: Impostazioni → Actions → Runners deve mostrare '$NAME' online."
echo " Il workflow .gitea/workflows/ci.yml del repo iride usa runs-on: ubuntu-latest → label ubuntu-latest:host."