package-server.sh 6.4 KB

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