publish-demo.sh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/bin/sh
  2. # Publish the SPA build of the latest stable NetPulse release as the live demo.
  3. #
  4. # Runs from netpulse-demo-publish.timer on the demo host. It needs no
  5. # credentials: it reads public release metadata and assets from the GitHub API,
  6. # so CI never holds a key for this machine.
  7. #
  8. # Layout under /opt/netpulse-demo:
  9. # releases/<tag>/ unpacked app/dist of each published release
  10. # public symlink to releases/<tag>, nginx serves this path
  11. # .published-tag last tag published successfully
  12. #
  13. # Usage: publish-demo.sh [--force]
  14. # --force republish even when .published-tag already matches the release
  15. #
  16. # Environment overrides (mostly for testing):
  17. # DEMO_REPO default gnacho/netpulse
  18. # DEMO_BASE default /opt/netpulse-demo
  19. # DEMO_PUBLIC_URL default https://demo.netpulse.cloudless.club/
  20. # DEMO_CHECK_PUBLIC set to 0 to skip the served-bundle comparison
  21. # DEMO_KEEP number of release directories to keep, default 5
  22. #
  23. # Exit codes: 0 published or already current, 1 failure (the unit goes failed
  24. # so the drift shows up in systemctl and in the journal).
  25. set -eu
  26. REPO="${DEMO_REPO:-gnacho/netpulse}"
  27. BASE="${DEMO_BASE:-/opt/netpulse-demo}"
  28. PUBLIC_URL="${DEMO_PUBLIC_URL:-https://demo.netpulse.cloudless.club/}"
  29. CHECK_PUBLIC="${DEMO_CHECK_PUBLIC:-1}"
  30. KEEP="${DEMO_KEEP:-5}"
  31. API="${DEMO_RELEASE_API:-https://api.github.com/repos/$REPO/releases/latest}"
  32. FORCE=0
  33. [ "${1:-}" = "--force" ] && FORCE=1
  34. log() { printf '%s %s\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "$*"; }
  35. die() { log "ERROR: $*"; exit 1; }
  36. command -v curl >/dev/null 2>&1 || die "curl no esta instalado"
  37. command -v python3 >/dev/null 2>&1 || die "python3 no esta instalado"
  38. release_json=$(curl -fsSL --max-time 30 -H 'Accept: application/vnd.github+json' "$API") \
  39. || die "no pude consultar $API"
  40. tag=$(printf '%s' "$release_json" | python3 -c 'import json,sys; print(json.load(sys.stdin)["tag_name"])') \
  41. || die "respuesta sin tag_name"
  42. [ -n "$tag" ] || die "tag vacio en la respuesta de la API"
  43. asset_url=$(printf '%s' "$release_json" | python3 -c '
  44. import json, sys
  45. data = json.load(sys.stdin)
  46. want = "app-dist-%s.tar.gz" % data["tag_name"]
  47. for asset in data.get("assets", []):
  48. if asset.get("name") == want:
  49. print(asset["browser_download_url"])
  50. break
  51. ') || die "no pude leer los assets del release"
  52. [ -n "$asset_url" ] || die "el release $tag no trae app-dist-$tag.tar.gz"
  53. if [ "$FORCE" -eq 0 ] && [ -f "$BASE/.published-tag" ] && [ "$(cat "$BASE/.published-tag")" = "$tag" ]; then
  54. log "al dia: $tag ya esta publicado"
  55. exit 0
  56. fi
  57. mkdir -p "$BASE/releases"
  58. work=$(mktemp -d "$BASE/.tmp.XXXXXX")
  59. trap 'rm -rf "$work"' EXIT INT TERM
  60. asset_name="app-dist-$tag.tar.gz"
  61. log "descargando $asset_name"
  62. curl -fsSL --max-time 180 -o "$work/$asset_name" "$asset_url" || die "no pude descargar $asset_url"
  63. sum_url=$(printf '%s' "$release_json" | python3 -c '
  64. import json, sys
  65. for asset in json.load(sys.stdin).get("assets", []):
  66. if asset.get("name") == "app-dist.sha256":
  67. print(asset["browser_download_url"])
  68. break
  69. ')
  70. if [ -n "$sum_url" ]; then
  71. curl -fsSL --max-time 30 -o "$work/app-dist.sha256" "$sum_url" || die "no pude descargar el checksum"
  72. (cd "$work" && sha256sum -c app-dist.sha256) || die "el checksum no casa con el artefacto"
  73. else
  74. log "AVISO: el release $tag no publica app-dist.sha256, sigo sin verificar"
  75. fi
  76. staging="$BASE/releases/$tag.new"
  77. rm -rf "$staging"
  78. mkdir -p "$staging"
  79. tar xzf "$work/$asset_name" -C "$staging" || die "no pude descomprimir $asset_name"
  80. [ -f "$staging/index.html" ] || die "el artefacto no trae index.html"
  81. bundle=$(grep -o 'assets/index-[A-Za-z0-9_-]*\.js' "$staging/index.html" | head -1 || true)
  82. [ -n "$bundle" ] || die "index.html no referencia ningun bundle en assets/"
  83. [ -f "$staging/$bundle" ] || die "el bundle $bundle falta en el artefacto"
  84. chmod -R a+rX "$staging"
  85. chown -R 1000:1000 "$staging" 2>/dev/null || true
  86. dest="$BASE/releases/$tag"
  87. [ -e "$dest" ] && rm -rf "$dest"
  88. mv "$staging" "$dest"
  89. if [ -d "$BASE/public" ] && [ ! -L "$BASE/public" ]; then
  90. legacy="$BASE/public.presymlink-$(date -u +%Y%m%d-%H%M%S)"
  91. log "public es un directorio, lo conservo en $legacy y lo sustituyo por un enlace"
  92. mv "$BASE/public" "$legacy"
  93. fi
  94. ln -sfn "releases/$tag" "$BASE/.public.new"
  95. mv -Tf "$BASE/.public.new" "$BASE/public"
  96. if [ "$CHECK_PUBLIC" = "1" ]; then
  97. attempt=1
  98. served=""
  99. while [ "$attempt" -le 10 ]; do
  100. served=$(curl -fsS --max-time 10 "$PUBLIC_URL" 2>/dev/null \
  101. | grep -o 'assets/index-[A-Za-z0-9_-]*\.js' | head -1 || true)
  102. [ "$served" = "$bundle" ] && break
  103. attempt=$((attempt + 1))
  104. sleep 6
  105. done
  106. [ "$served" = "$bundle" ] || die "el demo sirve '$served' y la release $tag trae '$bundle'"
  107. log "verificado en $PUBLIC_URL: $served"
  108. fi
  109. printf '%s\n' "$tag" > "$BASE/.published-tag"
  110. if [ "$KEEP" -gt 0 ]; then
  111. find "$BASE/releases" -mindepth 1 -maxdepth 1 -type d -printf '%T@ %p\n' 2>/dev/null \
  112. | sort -rn | tail -n +$((KEEP + 1)) | cut -d' ' -f2- \
  113. | while read -r old; do
  114. rm -rf "$old"
  115. done
  116. fi
  117. log "publicado $tag ($bundle)"