package.sh 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/bin/bash
  2. # package.sh — empaqueta netpulse-agent en .ipk y .apk usando el SDK de OpenWrt.
  3. #
  4. # Uso:
  5. # package.sh <SDK_VERSION> <SDK_TARGET> <SDK_SUBTARGET> <FORMAT> <BINARY_PATH>
  6. #
  7. # SDK_VERSION: 24.10.5 o 25.12.5
  8. # SDK_TARGET: mediatek/filogic o qualcommax/ipq807x
  9. # SDK_SUBTARGET: (vacío para 24.10, se detecta solo)
  10. # FORMAT: ipk o apk
  11. # BINARY_PATH: ruta al binario netpulse-agent precompilado (arm64)
  12. #
  13. # Ejemplo:
  14. # package.sh 24.10.5 mediatek/filogic "" ipk ./netpulse-agent-arm64
  15. # package.sh 25.12.5 qualcommax/ipq807x "" apk ./netpulse-agent-arm64
  16. set -euo pipefail
  17. SDK_VERSION="${1:?falta SDK_VERSION}"
  18. SDK_TARGET="${2:?falta SDK_TARGET}"
  19. SDK_SUBTARGET="${3:-}"
  20. FORMAT="${4:?falta FORMAT (ipk|apk)}"
  21. BINARY="${5:?falta BINARY_PATH}"
  22. if [ ! -f "$BINARY" ]; then
  23. echo "ERROR: binario no encontrado: $BINARY" >&2
  24. exit 1
  25. fi
  26. echo "=== package.sh: $FORMAT SDK=$SDK_VERSION target=$SDK_TARGET ==="
  27. SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
  28. REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
  29. WORK_DIR="$(mktemp -d)"
  30. trap 'rm -rf "$WORK_DIR"' EXIT
  31. # ── SDK URL ──────────────────────────────────────────────────────────
  32. SDK_NAME="openwrt-sdk-${SDK_VERSION}-${SDK_TARGET//\//-}_gcc-13.3.0_musl.Linux-x86_64"
  33. SDK_URL="https://downloads.openwrt.org/releases/${SDK_VERSION}/targets/${SDK_TARGET}/${SDK_NAME}.tar.zst"
  34. echo " SDK: $SDK_URL"
  35. # ── Descargar y extraer SDK ──────────────────────────────────────────
  36. cd "$WORK_DIR"
  37. curl -fsSL "$SDK_URL" -o sdk.tar.zst
  38. tar --zstd -xf sdk.tar.zst
  39. SDK_DIR="$WORK_DIR/$SDK_NAME"
  40. # ── Preparar binario ─────────────────────────────────────────────────
  41. BIN_SIZE=$(stat -c%s "$BINARY" 2>/dev/null || stat -f%z "$BINARY")
  42. cp "$BINARY" "$WORK_DIR/netpulse-agent"
  43. chmod 755 "$WORK_DIR/netpulse-agent"
  44. # ── Crear estructura de paquete ──────────────────────────────────────
  45. PKG_DIR="$WORK_DIR/netpulse-agent-pkg"
  46. mkdir -p "$PKG_DIR/CONTROL" "$PKG_DIR/usr/sbin" "$PKG_DIR/etc/init.d" "$PKG_DIR/etc/config" "$PKG_DIR/etc/uci-defaults"
  47. cp "$WORK_DIR/netpulse-agent" "$PKG_DIR/usr/sbin/netpulse-agent"
  48. cp "$REPO_ROOT/deploy/openwrt/netpulse-agent/files/netpulse-agent.init" "$PKG_DIR/etc/init.d/netpulse-agent"
  49. cp "$REPO_ROOT/deploy/openwrt/netpulse-agent/files/netpulse-agent.config" "$PKG_DIR/etc/config/netpulse-agent"
  50. cp "$REPO_ROOT/deploy/openwrt/netpulse-agent/files/netpulse-agent.defaults" "$PKG_DIR/etc/uci-defaults/90-netpulse-agent"
  51. # ── CONTROL files ────────────────────────────────────────────────────
  52. cat > "$PKG_DIR/CONTROL/control" << CTRL
  53. Package: netpulse-agent
  54. Version: ${PKG_VERSION:-0.0.0}-${PKG_RELEASE:-1}
  55. Depends: iw
  56. License: AGPL-3.0-only
  57. Section: utils
  58. Architecture: aarch64_cortex-a53
  59. Maintainer: Nacho <netpulse@cloudless.club>
  60. Description: NetPulse native agent for OpenWrt
  61. Native OpenWrt agent for NetPulse network monitor. Probes the local
  62. router (system, wireless, DHCP, FDB) and pushes data to a NetPulse
  63. server. Listens for nl80211 events (assoc/disassoc) in real time and
  64. receives commands from the server via SSE. Stateless: no writes to
  65. flash; logs go to syslog via stderr.
  66. CTRL
  67. cat > "$PKG_DIR/CONTROL/postinst" << 'POSTINST'
  68. #!/bin/sh
  69. if [ -f /etc/init.d/netpulse-agent ] && /etc/init.d/netpulse-agent enabled; then
  70. /etc/init.d/netpulse-agent stop 2>/dev/null || true
  71. fi
  72. [ -f /etc/uci-defaults/90-netpulse-agent ] && sh /etc/uci-defaults/90-netpulse-agent
  73. exit 0
  74. POSTINST
  75. chmod 755 "$PKG_DIR/CONTROL/postinst"
  76. cat > "$PKG_DIR/CONTROL/prerm" << 'PRERM'
  77. #!/bin/sh
  78. [ -f /etc/init.d/netpulse-agent ] && /etc/init.d/netpulse-agent stop 2>/dev/null || true
  79. [ -f /etc/init.d/netpulse-agent ] && /etc/init.d/netpulse-agent disable 2>/dev/null || true
  80. crontab -l 2>/dev/null | grep -v netpulse-watchdog | crontab - 2>/dev/null || true
  81. exit 0
  82. PRERM
  83. chmod 755 "$PKG_DIR/CONTROL/prerm"
  84. echo "/etc/config/netpulse-agent" > "$PKG_DIR/CONTROL/conffiles"
  85. # ── Construir paquete ────────────────────────────────────────────────
  86. OUT_DIR="$REPO_ROOT/dist/packages"
  87. mkdir -p "$OUT_DIR"
  88. if [ "$FORMAT" = "ipk" ]; then
  89. echo " Construyendo .ipk..."
  90. "$SDK_DIR/scripts/ipkg-build" "$PKG_DIR" "$OUT_DIR"
  91. echo " IPK: $(ls "$OUT_DIR"/netpulse-agent_*.ipk)"
  92. elif [ "$FORMAT" = "apk" ]; then
  93. echo " Construyendo .apk vía SDK..."
  94. # El SDK 25.12+ produce .apk con make. Copiamos el Makefile existente
  95. # pero le inyectamos el binario precompilado (sin compilar Go).
  96. mkdir -p "$SDK_DIR/package/utils/netpulse-agent/files"
  97. cp "$REPO_ROOT/deploy/openwrt/netpulse-agent/Makefile" "$SDK_DIR/package/utils/netpulse-agent/"
  98. cp -r "$PKG_DIR/usr" "$PKG_DIR/etc" "$SDK_DIR/package/utils/netpulse-agent/files/" 2>/dev/null || true
  99. # Parche: saltar Build/Compile (el binario YA está en files/)
  100. sed -i 's/^define Build\/Compile/define Build\/Compile_disabled/' "$SDK_DIR/package/utils/netpulse-agent/Makefile"
  101. cd "$SDK_DIR"
  102. # Feeds mínimos para resolver dependencias
  103. ./scripts/feeds update packages 2>/dev/null || true
  104. ./scripts/feeds install iw 2>/dev/null || true
  105. make package/netpulse-agent/compile V=s 2>&1 | tail -20
  106. # Recoger .apk
  107. find bin/packages -name "netpulse-agent*.apk" -exec cp {} "$OUT_DIR/" \;
  108. echo " APK: $(ls "$OUT_DIR"/netpulse-agent*.apk 2>/dev/null || echo 'NO ENCONTRADO')"
  109. else
  110. echo "ERROR: FORMAT debe ser ipk o apk" >&2
  111. exit 1
  112. fi
  113. echo "=== package.sh: OK ==="