package-server.sh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. # Stage binary and package files (package name: netpulse, like the Makefile)
  46. cp "$BINARY" "$WORK_DIR/netpulse"
  47. chmod 755 "$WORK_DIR/netpulse"
  48. PKG_DIR="$WORK_DIR/netpulse-pkg"
  49. mkdir -p "$PKG_DIR/CONTROL" "$PKG_DIR/usr/sbin" "$PKG_DIR/etc/init.d" "$PKG_DIR/etc/config" "$PKG_DIR/etc/uci-defaults"
  50. cp "$WORK_DIR/netpulse" "$PKG_DIR/usr/sbin/netpulse"
  51. cp "$REPO_ROOT/deploy/openwrt/netpulse-server/files/netpulse-server.init" "$PKG_DIR/etc/init.d/netpulse"
  52. cp "$REPO_ROOT/deploy/openwrt/netpulse-server/files/netpulse-server.config" "$PKG_DIR/etc/config/netpulse"
  53. cp "$REPO_ROOT/deploy/openwrt/netpulse-server/files/netpulse-server.defaults" "$PKG_DIR/etc/uci-defaults/95-netpulse-server"
  54. cat > "$PKG_DIR/CONTROL/control" << CTRL
  55. Package: netpulse
  56. Version: ${PKG_VERSION}-${PKG_RELEASE}
  57. Depends: curl, ca-bundle
  58. License: AGPL-3.0-only
  59. Section: net
  60. Architecture: aarch64_cortex-a53
  61. Maintainer: Nacho <netpulse@cloudless.club>
  62. Description: NetPulse network monitor (on-box server)
  63. NetPulse server running on-box: serves the web app and ingests agent
  64. data over HTTPS with self-signed TLS + SPKI pinning. Stores data in
  65. SQLite (overlay or USB). Same binary as the dedicated-node deployment.
  66. CTRL
  67. cat > "$PKG_DIR/CONTROL/postinst" << 'POSTINST'
  68. #!/bin/sh
  69. # NetGrip takeover rule (#363): a router running NetGrip manages its own
  70. # panel; installing the on-box server here is still allowed (they can
  71. # coexist: different ports), so no guard needed for the server.
  72. [ -f /etc/uci-defaults/95-netpulse-server ] && sh /etc/uci-defaults/95-netpulse-server
  73. /etc/init.d/netpulse restart 2>/dev/null || true
  74. exit 0
  75. POSTINST
  76. chmod 755 "$PKG_DIR/CONTROL/postinst"
  77. cat > "$PKG_DIR/CONTROL/prerm" << 'PRERM'
  78. #!/bin/sh
  79. /etc/init.d/netpulse stop 2>/dev/null || true
  80. /etc/init.d/netpulse disable 2>/dev/null || true
  81. exit 0
  82. PRERM
  83. chmod 755 "$PKG_DIR/CONTROL/prerm"
  84. echo "/etc/config/netpulse" > "$PKG_DIR/CONTROL/conffiles"
  85. OUT_DIR="$REPO_ROOT/dist/packages"
  86. mkdir -p "$OUT_DIR"
  87. if [ "$FORMAT" = "ipk" ]; then
  88. echo " Building .ipk..."
  89. "$SDK_DIR/scripts/ipkg-build" "$PKG_DIR" "$OUT_DIR"
  90. echo " IPK: $(ls "$OUT_DIR"/netpulse_*.ipk 2>/dev/null || echo 'NOT FOUND')"
  91. elif [ "$FORMAT" = "apk" ]; then
  92. echo " Building .apk via SDK..."
  93. PKG_DIR_SDK="$SDK_DIR/package/utils/netpulse"
  94. mkdir -p "$PKG_DIR_SDK/files"
  95. cp -r "$PKG_DIR/usr" "$PKG_DIR/etc" "$PKG_DIR_SDK/files/"
  96. cat > "$PKG_DIR_SDK/Makefile" << 'MAKE'
  97. include $(TOPDIR)/rules.mk
  98. PKG_NAME:=netpulse
  99. PKG_VERSION:=@PKG_VERSION@
  100. PKG_RELEASE:=@PKG_RELEASE@
  101. PKG_LICENSE:=AGPL-3.0-only
  102. PKG_MAINTAINER:=Nacho <netpulse@cloudless.club>
  103. include $(INCLUDE_DIR)/package.mk
  104. define Package/netpulse
  105. SECTION:=net
  106. CATEGORY:=Network
  107. TITLE:=NetPulse network monitor (on-box server)
  108. URL:=https://github.com/gnacho/netpulse
  109. DEPENDS:=+curl +ca-bundle @(aarch64||arm||x86_64)
  110. endef
  111. define Package/netpulse/description
  112. NetPulse server running on-box: serves the web app and ingests agent
  113. data over HTTPS with self-signed TLS + SPKI pinning. SQLite storage.
  114. endef
  115. define Build/Compile
  116. endef
  117. define Package/netpulse/install
  118. $(INSTALL_DIR) $(1)/usr/sbin
  119. $(INSTALL_BIN) ./files/usr/sbin/netpulse $(1)/usr/sbin/netpulse
  120. $(INSTALL_DIR) $(1)/etc/init.d
  121. $(INSTALL_BIN) ./files/etc/init.d/netpulse $(1)/etc/init.d/netpulse
  122. $(INSTALL_DIR) $(1)/etc/config
  123. $(INSTALL_CONF) ./files/etc/config/netpulse $(1)/etc/config/netpulse
  124. $(INSTALL_DIR) $(1)/etc/uci-defaults
  125. $(INSTALL_BIN) ./files/etc/uci-defaults/95-netpulse-server $(1)/etc/uci-defaults/95-netpulse-server
  126. endef
  127. define Package/netpulse/postinst
  128. #!/bin/sh
  129. if [ -z "$${IPKG_INSTROOT}" ]; then
  130. [ -f /etc/uci-defaults/95-netpulse-server ] && sh /etc/uci-defaults/95-netpulse-server
  131. /etc/init.d/netpulse restart 2>/dev/null || true
  132. fi
  133. exit 0
  134. endef
  135. $(eval $(call BuildPackage,netpulse))
  136. MAKE
  137. sed -i "s/@PKG_VERSION@/${PKG_VERSION}/; s/@PKG_RELEASE@/${PKG_RELEASE}/" "$PKG_DIR_SDK/Makefile"
  138. cd "$SDK_DIR"
  139. make defconfig >/dev/null 2>&1 || true
  140. make package/netpulse/compile V=s 2>&1 | tail -15
  141. find bin/packages -name "netpulse-*.apk" -exec cp {} "$OUT_DIR/" \;
  142. echo " APK: $(ls "$OUT_DIR"/netpulse-*.apk 2>/dev/null || echo 'NOT FOUND')"
  143. else
  144. echo "ERROR: FORMAT must be ipk or apk" >&2
  145. exit 1
  146. fi
  147. # El .apk del SERVER se llama netpulse-<v>.apk (sin sufijo de arch en 25.12
  148. # cuando el paquete es arch-specific por DEPENDS); renombrar para evitar
  149. # colisionar con el tarball netpulse_<v>_linux_arm64.tar.gz en la release.
  150. echo "=== package-server.sh: OK ==="