4
0

package-server.sh 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. # shellcheck source=deploy/openwrt/sdk.sh
  35. . "$SCRIPT_DIR/sdk.sh"
  36. SDK_DIR="$(download_verified_sdk "$SDK_DIR_URL" "$WORK_DIR")" || exit 1
  37. cd "$WORK_DIR"
  38. # Package architecture for the .ipk control file. OpenWrt names the staging
  39. # dir "target-<arch>_musl", so derive it from there instead of hardcoding a
  40. # single arch (mediatek/filogic -> aarch64_cortex-a53, x86/64 -> x86_64).
  41. PKG_ARCH="$(find "$SDK_DIR/staging_dir" -maxdepth 1 -type d -name 'target-*_musl*' -print -quit | sed -E 's#.*/target-(.*)_musl.*#\1#')"
  42. if [ -z "$PKG_ARCH" ]; then
  43. echo "ERROR: could not determine package architecture from the SDK" >&2
  44. exit 1
  45. fi
  46. echo " Arch: $PKG_ARCH"
  47. # Stage binary and package files (package name: netpulse, like the Makefile)
  48. cp "$BINARY" "$WORK_DIR/netpulse"
  49. chmod 755 "$WORK_DIR/netpulse"
  50. PKG_DIR="$WORK_DIR/netpulse-pkg"
  51. mkdir -p "$PKG_DIR/CONTROL" "$PKG_DIR/usr/sbin" "$PKG_DIR/etc/init.d" "$PKG_DIR/etc/config" "$PKG_DIR/etc/uci-defaults"
  52. cp "$WORK_DIR/netpulse" "$PKG_DIR/usr/sbin/netpulse"
  53. # Init script and uci-defaults MUST be 0755 (#1701): ipkg-build does not fix
  54. # permissions, and a 0644 init script fails with "Permission denied" during
  55. # uci-defaults on GL.iNet/OpenWrt 21.02, so the service never gets enabled.
  56. install -m 0755 "$REPO_ROOT/deploy/openwrt/netpulse-server/files/netpulse-server.init" "$PKG_DIR/etc/init.d/netpulse"
  57. cp "$REPO_ROOT/deploy/openwrt/netpulse-server/files/netpulse-server.config" "$PKG_DIR/etc/config/netpulse"
  58. install -m 0755 "$REPO_ROOT/deploy/openwrt/netpulse-server/files/netpulse-server.defaults" "$PKG_DIR/etc/uci-defaults/95-netpulse-server"
  59. cat > "$PKG_DIR/CONTROL/control" << CTRL
  60. Package: netpulse
  61. Version: ${PKG_VERSION}-${PKG_RELEASE}
  62. Depends: curl, ca-bundle
  63. License: AGPL-3.0-only
  64. Section: net
  65. Architecture: ${PKG_ARCH}
  66. Maintainer: Nacho <netpulse@cloudless.club>
  67. Description: NetPulse network monitor (on-box server)
  68. NetPulse server running on-box: serves the web app and ingests agent
  69. data over HTTPS with self-signed TLS + SPKI pinning. Stores data in
  70. SQLite (overlay or USB). Same binary as the dedicated-node deployment.
  71. CTRL
  72. cat > "$PKG_DIR/CONTROL/postinst" << 'POSTINST'
  73. #!/bin/sh
  74. # NetGrip takeover rule (#363): a router running NetGrip manages its own
  75. # panel; installing the on-box server here is still allowed (they can
  76. # coexist: different ports), so no guard needed for the server.
  77. [ -f /etc/uci-defaults/95-netpulse-server ] && sh /etc/uci-defaults/95-netpulse-server
  78. /etc/init.d/netpulse restart 2>/dev/null || true
  79. exit 0
  80. POSTINST
  81. chmod 755 "$PKG_DIR/CONTROL/postinst"
  82. cat > "$PKG_DIR/CONTROL/prerm" << 'PRERM'
  83. #!/bin/sh
  84. /etc/init.d/netpulse stop 2>/dev/null || true
  85. /etc/init.d/netpulse disable 2>/dev/null || true
  86. exit 0
  87. PRERM
  88. chmod 755 "$PKG_DIR/CONTROL/prerm"
  89. echo "/etc/config/netpulse" > "$PKG_DIR/CONTROL/conffiles"
  90. OUT_DIR="$REPO_ROOT/dist/packages"
  91. mkdir -p "$OUT_DIR"
  92. if [ "$FORMAT" = "ipk" ]; then
  93. echo " Building .ipk..."
  94. "$SDK_DIR/scripts/ipkg-build" "$PKG_DIR" "$OUT_DIR"
  95. echo " IPK: $(ls "$OUT_DIR"/netpulse_*.ipk 2>/dev/null || echo 'NOT FOUND')"
  96. verify_ipk_modes "$OUT_DIR"/netpulse_*.ipk \
  97. "usr/sbin/netpulse=755" \
  98. "etc/init.d/netpulse=755" \
  99. "etc/uci-defaults/95-netpulse-server=755"
  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 ==="