Files
hammer/scripts/farm/harvest-go.sh
T
sergio 254c5a9cc7 Etapa G: ronda 2 pre-siembra finde (+18 Go) + alias gate argo-rollouts/step-cli
terrascan/tfsec/tflint/tparse/richgo/curlie/dockle/vendir/kubefwd/kubeshark/ktunnel/duplicacy/
go-junit-report/argo-rollouts/gowitness/shuffledns/dnscrypt-proxy/step-cli. Dropeados ssh-vault
(sin marca Go) y skopeo (CGO gpgme). Gate: argo-rollouts→kubectl-argo-rollouts, step-cli→step.
2026-06-27 07:04:38 -04:00

151 lines
8.0 KiB
Bash
Executable File

#!/bin/sh
# harvest-go.sh — COSECHA-ONLY automática del frente Go (modo finde desatendido). Determinista, sin
# IA: pensado para un cron en el laptop (el hub). NO elige categorías ni diagnostica fallos; sólo
# convierte en release firmado lo que el worker YA selló y que PASA un smoke-test del binario.
#
# Ciclo (idempotente, seguro de re-correr):
# 1. git pull --rebase (sincroniza con el otro agente que pushea al mismo main)
# 2. baja el store sellado del worker (rsync)
# 3. por cada receta en recipes/incoming-go/: ¿está sellada? (hammer build con `timeout` ⇒ cache-hit
# instantáneo = sí; si se pasa del timeout sería un build local ⇒ se SALTA, no compilamos en el hub)
# 4. SMOKE-TEST del binario instalado: existe + ELF estático + corre version/--help sin panic/segfault.
# - PASA ⇒ promueve (mv recipes/, pack --build) — se firma+commitea al final.
# - FALLA ⇒ mueve a tandas/needs-review-weekend/ con el motivo (no reintenta cada cosecha).
# 5. si promovió algo: firma el repo 1 vez + git add EXPLÍCITO (nunca -A: respeta al otro agente) +
# commit + push (con pull --rebase de reintento).
# Todo se loguea a work/harvest-go.log para revisar el lunes.
#
# Uso (cron): */DohubVPS cd /home/sergio/hammer && scripts/farm/harvest-go.sh root@1.2.3.4 >>work/harvest-go.log 2>&1
# Env: SSH_KEY (def ~/.ssh/github5), REMOTE (def /opt/hammer), STORE (def ./store), REPO (def dist/repo),
# KEY (def dist/keys/release.ed25519), DISTRO (def dev), HAMMER (def target/release/hammer),
# SEAL_TIMEOUT (def 60s: techo del cache-hit; más = sería build local ⇒ skip).
set -u
VPS="${1:?uso: harvest-go.sh user@host}"
ROOT="$(cd "$(dirname "$0")/../.." && pwd)"; cd "$ROOT"
SSH_KEY="${SSH_KEY:-$HOME/.ssh/github5}"
REMOTE="${REMOTE:-/opt/hammer}"
STORE="${STORE:-$ROOT/store}"
REPO="${REPO:-$ROOT/dist/repo}"
KEY="${KEY:-$ROOT/dist/keys/release.ed25519}"
DISTRO="${DISTRO:-dev}"
HAMMER="${HAMMER:-$ROOT/target/release/hammer}"
SEAL_TIMEOUT="${SEAL_TIMEOUT:-60}"
QUEUE="recipes/incoming-go"
REVIEW="tandas/needs-review-weekend"
SSH="ssh -i $SSH_KEY -o StrictHostKeyChecking=accept-new -o ConnectTimeout=20"
# git push/pull a gitea (origin) usa la MISMA clave que el farm (github5 = clave "tawasuyu"). Fijarla
# explícita para que el cron funcione sin ssh-agent.
export GIT_SSH_COMMAND="ssh -i $SSH_KEY -o IdentitiesOnly=yes -o StrictHostKeyChecking=accept-new"
STAMP() { date -u +%FT%TZ; }
# Lock: una sola cosecha a la vez (el cron no se solapa).
LOCK="$ROOT/work/.harvest-go.lock"
mkdir -p "$ROOT/work"
if ! mkdir "$LOCK" 2>/dev/null; then
echo "$(STAMP) harvest-go: ya hay una cosecha en curso ($LOCK) ⇒ salgo"; exit 0
fi
trap 'rmdir "$LOCK" 2>/dev/null' EXIT INT TERM
echo "=================================================================="
echo "$(STAMP) harvest-go: arranco (VPS=$VPS)"
# 1. sincronizar con el otro agente (mismo main). --autostash por si quedó algo sin commitear.
git pull --rebase --autostash origin main 2>&1 | tail -2 || echo "$(STAMP) WARN: git pull falló (sigo)"
# 2. bajar el store sellado del worker.
echo "$(STAMP) bajando store del worker…"
rsync -az -e "$SSH" "$VPS:$REMOTE/store/" "$STORE/" 2>&1 | tail -1 || { echo "$(STAMP) ERROR: rsync store falló"; exit 1; }
mkdir -p "$REVIEW"
promoted=""; rejected=""; skipped=""
# 3+4. por cada receta de la cola aislada Go.
for f in "$QUEUE"/*.toml; do
[ -e "$f" ] || { echo "$(STAMP) cola vacía"; break; }
n=$(basename "$f" .toml)
# Pre-check BARATO: ¿el worker selló algo para este nombre? (tras el rsync el store local lo tiene).
# Sin esto, `hammer build` de una receta NO sellada ARRANCARÍA un build local (vendor+compile) y lo
# mataría el timeout ⇒ CPU del hub desperdiciada cada cosecha. Si no hay seal `*-<n>`, salto ya.
ls "$STORE"/*-"$n" >/dev/null 2>&1 || { skipped="$skipped $n"; continue; }
# ¿sellada con el hash ACTUAL? cache-hit instantáneo imprime el hash; un build real (hash distinto,
# raro) se pasa del timeout ⇒ skip (no compilamos en el hub).
H=$(timeout "$SEAL_TIMEOUT" "$HAMMER" --store "$STORE" build "$f" 2>/dev/null | tail -1)
case "$H" in
b3:*) : ;;
*) skipped="$skipped $n"; continue ;; # no sellada aún (o tardaría en compilar) ⇒ el worker sigue
esac
# NOMBRE ESPERADO del binario: el de la receta, o un alias conocido (pname nixpkgs ≠ binario).
case "$n" in
opentofu) want=tofu ;; minio-client) want=mc ;; natscli) want=nats ;; delve) want=dlv ;;
fluxcd) want=flux ;; chart-testing) want=ct ;; sealed-secrets|kubeseal) want=kubeseal ;;
prometheus-node-exporter|node_exporter) want=node_exporter ;;
prometheus-blackbox-exporter|blackbox_exporter) want=blackbox_exporter ;;
argo-rollouts) want=kubectl-argo-rollouts ;; step-cli) want=step ;;
*) want="$n" ;;
esac
# SMOKE-TEST: el seal debe instalar un binario cuyo nombre == $want. Si instaló otro (un helper:
# functional-test, protoc-gen-buf-lint, gosecutil…), detect_go_main eligió mal el main ⇒ NO es la
# herramienta correcta aunque "corra" → a REVIEW (no firmamos un binario equivocado). Lección eksctl.
d="$STORE/${H#b3:}-$n"
allbins=$(find "$d" -type f -path '*/bin/*' -perm -100 2>/dev/null)
bin=$(printf '%s\n' "$allbins" | while read -r b; do [ "$(basename "$b")" = "$want" ] && { echo "$b"; break; }; done)
if [ -z "$bin" ]; then
got=$(printf '%s\n' "$allbins" | sed 's#.*/##' | tr '\n' ',' | sed 's/,$//')
{ echo "# REVIEW $(STAMP): binario instalado='$got' ≠ esperado='$want' (detect_go_main eligió otro main); fijar flags=[\"./cmd/$want\"]"; cat "$f"; } > "$REVIEW/$n.toml"
rm -f "$f"; rejected="$rejected $n(bin=$got)"; continue
fi
# ELF estático (los del lab son musl estáticos; uno dinámico es sospechoso pero no lo rechazo solo por eso).
isstatic=$(file "$bin" 2>/dev/null | grep -c 'statically linked')
# corre sin panic/segfault: probamos varios flags benignos bajo timeout.
ran_ok=0; out=""
for sub in version --version -v --help -h; do
out=$(timeout 10 "$bin" $sub 2>&1); rc=$?
# panic de Go / segfault ⇒ malo; rc 124 = timeout (se colgó, p.ej. abrió server) ⇒ probamos otro flag.
case "$out" in *panic:*|*"runtime error"*|*SIGSEGV*|*"signal SIGABRT"*) continue ;; esac
[ "$rc" = 124 ] && continue
ran_ok=1; break
done
if [ "$ran_ok" = 1 ]; then
bn=$(basename "$bin")
echo "$(STAMP) ✓ smoke $n (bin=$bn static=$isstatic): ${out%%
*}"
mv "$f" "recipes/$n.toml"
if "$HAMMER" --store "$STORE" pack "recipes/$n.toml" --repo "$REPO" --distro-version "$DISTRO" --build >/dev/null 2>&1; then
promoted="$promoted $n"
else
echo "$(STAMP) ✗ pack falló para $n ⇒ a review"
mv "recipes/$n.toml" "$REVIEW/$n.toml"; rejected="$rejected $n(pack)"
fi
else
{ echo "# REVIEW $(STAMP): smoke-test falló (panic/segfault/cuelga). bin=$(basename "$bin") última salida:"; echo "$out" | sed 's/^/# /' | head -5; cat "$f"; } > "$REVIEW/$n.toml"
rm -f "$f"; rejected="$rejected $n(smoke)"
fi
done
echo "$(STAMP) promovidos:$promoted"
echo "$(STAMP) a-review:$rejected"
echo "$(STAMP) aún-no-sellados (worker sigue):$skipped"
# 5. firmar + commit + push sólo si hubo promociones o movimientos a review.
if [ -n "$promoted" ] || [ -n "$rejected" ]; then
[ -n "$promoted" ] && "$HAMMER" repo sign --repo "$REPO" --key "$KEY" >/dev/null 2>&1 && echo "$(STAMP) repo re-firmado"
# add EXPLÍCITO: jamás `git add -A` (otro agente comparte el working-tree).
git add "$QUEUE" "$REVIEW" 2>/dev/null
for n in $promoted; do git add "recipes/$n.toml" 2>/dev/null; done
if ! git diff --cached --quiet; then
git commit -q -m "Etapa G: cosecha-go finde (auto) — promovidos:$promoted | review:$rejected"
# push con reintento ante el push concurrente del otro agente.
for try in 1 2 3; do
git pull --rebase --autostash origin main >/dev/null 2>&1
if git push origin main >/dev/null 2>&1; then echo "$(STAMP) ✓ pusheado (intento $try)"; break; fi
echo "$(STAMP) push rechazado, reintento $try"; sleep 5
done
fi
fi
echo "$(STAMP) harvest-go: fin"