update.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/bin/bash
  2. # NetPulse — script de actualización (lo lanza el updater del backend, corre
  3. # como netpulse). Flujo dual según el backend instalado:
  4. # - Go (/opt/netpulse/server-go/netpulse existe): git pull → binario
  5. # precompilado de CI (go-latest) → swap atómico → reinicio diferido.
  6. # - Node (legacy): git pull → deps → dist precompilado (dist-latest) →
  7. # swap atómico del dist → reinicio diferido.
  8. set -e
  9. cd /opt/netpulse
  10. echo "STEP:fetch"
  11. git fetch origin main
  12. git reset --hard origin/main
  13. SHA=$(git rev-parse HEAD)
  14. if [ -f /opt/netpulse/server-go/netpulse ]; then
  15. # ---------------- Flujo Go ----------------
  16. echo "STEP:binary"
  17. ARCH=$(uname -m)
  18. case "$ARCH" in
  19. x86_64) ARCH=amd64 ;;
  20. aarch64) ARCH=arm64 ;;
  21. *) echo "arquitectura no soportada: $ARCH"; exit 1 ;;
  22. esac
  23. URL="https://github.com/gnacho/netpulse/releases/download/go-latest/netpulse-server-$SHA-linux-$ARCH.tar.gz"
  24. rm -rf /opt/netpulse/server-go/pkg.new
  25. mkdir -p /opt/netpulse/server-go/pkg.new
  26. # 1) asset exacto del commit; 2) si no existe (commit sin cambios en
  27. # server-go/ ni app/), el asset más reciente de esta arquitectura.
  28. if curl -fsSL -m 300 "$URL" -o /tmp/netpulse-go.tgz && tar xzf /tmp/netpulse-go.tgz -C /opt/netpulse/server-go/pkg.new; then
  29. echo "binario precompilado descargado de CI ($SHA)"
  30. else
  31. ASSET=$(curl -fsSL -m 30 https://api.github.com/repos/gnacho/netpulse/releases/tags/go-latest \
  32. | grep -oP '"name":\s*"\Knetpulse-server-[0-9a-f]+-linux-'"$ARCH"'\.tar\.gz' | head -1)
  33. if [ -n "$ASSET" ] && curl -fsSL -m 300 "https://github.com/gnacho/netpulse/releases/download/go-latest/$ASSET" -o /tmp/netpulse-go.tgz \
  34. && tar xzf /tmp/netpulse-go.tgz -C /opt/netpulse/server-go/pkg.new; then
  35. echo "binario de CI más reciente disponible ($ASSET)"
  36. else
  37. echo "ERROR: sin binario en CI para $ARCH (el CT no puede compilar Go)"
  38. exit 1
  39. fi
  40. fi
  41. # Swap atómico del binario: si algo falló antes, el anterior queda intacto
  42. chmod +x /opt/netpulse/server-go/pkg.new/netpulse
  43. mv /opt/netpulse/server-go/netpulse /opt/netpulse/server-go/netpulse.old
  44. mv /opt/netpulse/server-go/pkg.new/netpulse /opt/netpulse/server-go/netpulse
  45. rm -rf /opt/netpulse/server-go/pkg.new /opt/netpulse/server-go/netpulse.old /tmp/netpulse-go.tgz
  46. echo "STEP:restart"
  47. # Reinicio vía unidad systemd.path del CT (netpulse-go-restart.path vigila
  48. # este flag y ejecuta systemctl restart netpulse-go.service como root).
  49. # El dir puede no existir (DATA_DIR real vive en /opt/netpulse/server/data):
  50. # crearlo antes de tocar el flag (bug visto 1-Ago-2026: touch fallaba y el
  51. # binario nuevo quedaba instalado sin reiniciar).
  52. mkdir -p /opt/netpulse/server-go/data
  53. touch /opt/netpulse/server-go/data/.restart-me
  54. else
  55. # ---------------- Flujo Node (legacy) ----------------
  56. echo "STEP:server-deps"
  57. if git diff --name-only 'HEAD@{1}' HEAD 2>/dev/null | grep -q 'server/package-lock.json'; then
  58. cd /opt/netpulse/server
  59. npm ci --omit=dev --no-audit --no-fund
  60. fi
  61. echo "STEP:frontend-build"
  62. cd /opt/netpulse
  63. URL="https://github.com/gnacho/netpulse/releases/download/dist-latest/app-dist-$SHA.tar.gz"
  64. rm -rf /opt/netpulse/app/dist.new
  65. mkdir -p /opt/netpulse/app/dist.new
  66. # Vía preferida: dist precompilado por CI (el CT no tiene RAM para compilar).
  67. # 1) asset exacto del commit; 2) si no existe (commit sin cambios en app/),
  68. # el asset más reciente disponible (el dist no ha cambiado entonces).
  69. if curl -fsSL -m 300 "$URL" -o /tmp/app-dist.tgz && tar xzf /tmp/app-dist.tgz -C /opt/netpulse/app/dist.new; then
  70. echo "dist precompilado descargado de CI ($SHA)"
  71. else
  72. ASSET=$(curl -fsSL -m 30 https://api.github.com/repos/gnacho/netpulse/releases/tags/dist-latest \
  73. | grep -oP '"name":\s*"\Kapp-dist-[0-9a-f]+\.tar\.gz' | head -1)
  74. if [ -n "$ASSET" ] && curl -fsSL -m 300 "https://github.com/gnacho/netpulse/releases/download/dist-latest/$ASSET" -o /tmp/app-dist.tgz \
  75. && tar xzf /tmp/app-dist.tgz -C /opt/netpulse/app/dist.new; then
  76. echo "dist de CI más reciente disponible ($ASSET)"
  77. else
  78. echo "sin dist en CI; build local (riesgo OOM en 512MB)"
  79. cd /opt/netpulse/app
  80. # npm ci SOLO si cambió el lockfile (656 paquetes: OOM seguro en el CT)
  81. if git diff --name-only 'HEAD@{1}' HEAD 2>/dev/null | grep -q 'app/package-lock.json'; then
  82. npm ci --no-audit --no-fund
  83. fi
  84. npm run build -- --outDir dist.new
  85. fi
  86. fi
  87. # Swap atómico: si algo falló antes, el dist anterior queda intacto
  88. rm -rf /opt/netpulse/app/dist.old
  89. [ -d /opt/netpulse/app/dist ] && mv /opt/netpulse/app/dist /opt/netpulse/app/dist.old
  90. mv /opt/netpulse/app/dist.new /opt/netpulse/app/dist
  91. rm -rf /opt/netpulse/app/dist.old /tmp/app-dist.tgz
  92. echo "STEP:restart"
  93. # Reinicio vía unidad systemd.path del CT (netpulse-restart.path vigila este
  94. # flag y ejecuta systemctl restart netpulse.service como root)
  95. touch /opt/netpulse/server/data/.restart-me
  96. fi
  97. echo "STEP:done"