package-server.sh 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #!/bin/bash
  2. # package-server.sh builds the on-box netpulse SERVER package (.ipk/.apk)
  3. # from a prebuilt static binary, mirroring package.sh (agent). Pure Go
  4. # (modernc sqlite) so the arm64 build is fully static.
  5. #
  6. # Usage:
  7. # package-server.sh <SDK_VERSION> <SDK_TARGET> <SDK_SUBTARGET> <FORMAT> <BINARY_PATH>
  8. #
  9. # FORMAT: ipk or apk
  10. # BINARY_PATH: prebuilt netpulse server binary (arm64)
  11. #
  12. # PKG_VERSION / PKG_RELEASE overridable via env (CI uses the release tag).
  13. #
  14. # Example:
  15. # package-server.sh 25.12.5 qualcommax/ipq807x "" apk ./netpulse
  16. set -euo pipefail
  17. SDK_VERSION="${1:?missing SDK_VERSION}"
  18. SDK_TARGET="${2:?missing SDK_TARGET}"
  19. SDK_SUBTARGET="${3:-}"
  20. FORMAT="${4:?missing FORMAT (ipk|apk)}"
  21. BINARY="${5:?missing BINARY_PATH}"
  22. if [ ! -f "$BINARY" ]; then
  23. echo "ERROR: binary not found: $BINARY" >&2
  24. exit 1
  25. fi
  26. SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
  27. REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
  28. PKG_VERSION="${PKG_VERSION:-2.0.0}"
  29. PKG_RELEASE="${PKG_RELEASE:-1}"
  30. echo "=== package-server.sh: $FORMAT SDK=$SDK_VERSION target=$SDK_TARGET version=${PKG_VERSION}-${PKG_RELEASE} ==="
  31. WORK_DIR="$(mktemp -d)"
  32. trap 'rm -rf "$WORK_DIR"' EXIT
  33. SDK_DIR_URL="https://downloads.openwrt.org/releases/${SDK_VERSION}/targets/${SDK_TARGET}/"
  34. SDK_NAME="$(curl -fsSL "$SDK_DIR_URL" 2>/dev/null | grep -o 'openwrt-sdk-[^"]*\.tar\.zst' | head -1 || true)"
  35. if [ -z "$SDK_NAME" ]; then
  36. echo "ERROR: SDK not found under ${SDK_DIR_URL}" >&2
  37. exit 1
  38. fi
  39. SDK_URL="${SDK_DIR_URL}${SDK_NAME}"
  40. echo " SDK: $SDK_URL"
  41. cd "$WORK_DIR"
  42. curl -fsSL "$SDK_URL" -o sdk.tar.zst
  43. tar --zstd -xf sdk.tar.zst
  44. SDK_DIR="$WORK_DIR/${SDK_NAME%.tar.zst}"
  45. # Package architecture for the .ipk control file. OpenWrt names the staging
  46. # dir "target-<arch>_musl", so derive it from there instead of hardcoding a
  47. # single arch (mediatek/filogic -> aarch64_cortex-a53, x86/64 -> x86_64).
  48. PKG_ARCH="$(find "$SDK_DIR/staging_dir" -maxdepth 1 -type d -name 'target-*_musl*' -print -quit | sed -E 's#.*/target-(.*)_musl.*#\1#')"
  49. if [ -z "$PKG_ARCH" ]; then
  50. echo "ERROR: could not determine package architecture from the SDK" >&2
  51. exit 1
  52. fi
  53. echo " Arch: $PKG_ARCH"
  54. # Stage binary and package files (package name: netpulse, like the Makefile)
  55. cp "$BINARY" "$WORK_DIR/netpulse"
  56. chmod 755 "$WORK_DIR/netpulse"
  57. PKG_DIR="$WORK_DIR/netpulse-pkg"
  58. mkdir -p "$PKG_DIR/CONTROL" "$PKG_DIR/usr/sbin" "$PKG_DIR/etc/init.d" "$PKG_DIR/etc/config" "$PKG_DIR/etc/uci-defaults"
  59. cp "$WORK_DIR/netpulse" "$PKG_DIR/usr/sbin/netpulse"
  60. cp "$REPO_ROOT/deploy/openwrt/netpulse-server/files/netpulse-server.init" "$PKG_DIR/etc/init.d/netpulse"
  61. cp "$REPO_ROOT/deploy/openwrt/netpulse-server/files/netpulse-server.config" "$PKG_DIR/etc/config/netpulse"
  62. cp "$REPO_ROOT/deploy/openwrt/netpulse-server/files/netpulse-server.defaults" "$PKG_DIR/etc/uci-defaults/95-netpulse-server"
  63. cat > "$PKG_DIR/CONTROL/control" << CTRL
  64. Package: netpulse
  65. Version: ${PKG_VERSION}-${PKG_RELEASE}
  66. Depends: curl, ca-bundle
  67. License: AGPL-3.0-only
  68. Section: net
  69. Architecture: ${PKG_ARCH}
  70. Maintainer: Nacho <netpulse@cloudless.club>
  71. Description: NetPulse network monitor (on-box server)
  72. NetPulse server running on-box: serves the web app and ingests agent
  73. data over HTTPS with self-signed TLS + SPKI pinning. Stores data in
  74. SQLite (overlay or USB). Same binary as the dedicated-node deployment.
  75. CTRL
  76. cat > "$PKG_DIR/CONTROL/postinst" << 'POSTINST'
  77. #!/bin/sh
  78. # NetGrip takeover rule (#363): a router running NetGrip manages its own
  79. # panel; installing the on-box server here is still allowed (they can
  80. # coexist: different ports), so no guard needed for the server.
  81. [ -f /etc/uci-defaults/95-netpulse-server ] && sh /etc/uci-defaults/95-netpulse-server
  82. /etc/init.d/netpulse restart 2>/dev/null || true
  83. exit 0
  84. POSTINST
  85. chmod 755 "$PKG_DIR/CONTROL/postinst"
  86. cat > "$PKG_DIR/CONTROL/prerm" << 'PRERM'
  87. #!/bin/sh
  88. /etc/init.d/netpulse stop 2>/dev/null || true
  89. /etc/init.d/netpulse disable 2>/dev/null || true
  90. exit 0
  91. PRERM
  92. chmod 755 "$PKG_DIR/CONTROL/prerm"
  93. echo "/etc/config/netpulse" > "$PKG_DIR/CONTROL/conffiles"
  94. OUT_DIR="$REPO_ROOT/dist/packages"
  95. mkdir -p "$OUT_DIR"
  96. if [ "$FORMAT" = "ipk" ]; then
  97. echo " Building .ipk..."
  98. "$SDK_DIR/scripts/ipkg-build" "$PKG_DIR" "$OUT_DIR"
  99. echo " IPK: $(ls "$OUT_DIR"/netpulse_*.ipk 2>/dev/null || echo 'NOT FOUND')"
  100. elif [ "$FORMAT" = "apk" ]; then
  101. echo " Building .apk via SDK..."
  102. PKG_DIR_SDK="$SDK_DIR/package/utils/netpulse"
  103. mkdir -p "$PKG_DIR_SDK/files"
  104. cp -r "$PKG_DIR/usr" "$PKG_DIR/etc" "$PKG_DIR_SDK/files/"
  105. cat > "$PKG_DIR_SDK/Makefile" << 'MAKE'
  106. include $(TOPDIR)/rules.mk
  107. PKG_NAME:=netpulse
  108. PKG_VERSION:=@PKG_VERSION@
  109. PKG_RELEASE:=@PKG_RELEASE@
  110. PKG_LICENSE:=AGPL-3.0-only
  111. PKG_MAINTAINER:=Nacho <netpulse@cloudless.club>
  112. include $(INCLUDE_DIR)/package.mk
  113. define Package/netpulse
  114. SECTION:=net
  115. CATEGORY:=Network
  116. TITLE:=NetPulse network monitor (on-box server)
  117. URL:=https://github.com/gnacho/netpulse
  118. DEPENDS:=+curl +ca-bundle @(aarch64||arm||x86_64)
  119. endef
  120. define Package/netpulse/description
  121. NetPulse server running on-box: serves the web app and ingests agent
  122. data over HTTPS with self-signed TLS + SPKI pinning. SQLite storage.
  123. endef
  124. define Build/Compile
  125. endef
  126. define Package/netpulse/install
  127. $(INSTALL_DIR) $(1)/usr/sbin
  128. $(INSTALL_BIN) ./files/usr/sbin/netpulse $(1)/usr/sbin/netpulse
  129. $(INSTALL_DIR) $(1)/etc/init.d
  130. $(INSTALL_BIN) ./files/etc/init.d/netpulse $(1)/etc/init.d/netpulse
  131. $(INSTALL_DIR) $(1)/etc/config
  132. $(INSTALL_CONF) ./files/etc/config/netpulse $(1)/etc/config/netpulse
  133. $(INSTALL_DIR) $(1)/etc/uci-defaults
  134. $(INSTALL_BIN) ./files/etc/uci-defaults/95-netpulse-server $(1)/etc/uci-defaults/95-netpulse-server
  135. endef
  136. define Package/netpulse/postinst
  137. #!/bin/sh
  138. if [ -z "$${IPKG_INSTROOT}" ]; then
  139. [ -f /etc/uci-defaults/95-netpulse-server ] && sh /etc/uci-defaults/95-netpulse-server
  140. /etc/init.d/netpulse restart 2>/dev/null || true
  141. fi
  142. exit 0
  143. endef
  144. $(eval $(call BuildPackage,netpulse))
  145. MAKE
  146. sed -i "s/@PKG_VERSION@/${PKG_VERSION}/; s/@PKG_RELEASE@/${PKG_RELEASE}/" "$PKG_DIR_SDK/Makefile"
  147. cd "$SDK_DIR"
  148. make defconfig >/dev/null 2>&1 || true
  149. make package/netpulse/compile V=s 2>&1 | tail -15
  150. # The apk filename carries no arch, so append it: otherwise the armv8 and
  151. # x86/64 builds would collide in the release (same name, --clobber).
  152. find bin/packages -name "netpulse-*.apk" | while read -r f; do
  153. cp "$f" "$OUT_DIR/$(basename "$f" .apk)_${PKG_ARCH}.apk"
  154. done
  155. echo " APK: $(ls "$OUT_DIR"/netpulse-*.apk 2>/dev/null || echo 'NOT FOUND')"
  156. else
  157. echo "ERROR: FORMAT must be ipk or apk" >&2
  158. exit 1
  159. fi
  160. # El .apk del SERVER se llama netpulse-<v>.apk (sin sufijo de arch en 25.12
  161. # cuando el paquete es arch-specific por DEPENDS); renombrar para evitar
  162. # colisionar con el tarball netpulse_<v>_linux_arm64.tar.gz en la release.
  163. echo "=== package-server.sh: OK ==="