package.sh 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #!/bin/bash
  2. # package.sh builds the netpulse-agent OpenWrt package as .ipk or .apk
  3. # using the OpenWrt SDK.
  4. #
  5. # Usage:
  6. # package.sh <SDK_VERSION> <SDK_TARGET> <SDK_SUBTARGET> <FORMAT> <BINARY_PATH>
  7. #
  8. # SDK_VERSION: 24.10.5 or 25.12.5
  9. # SDK_TARGET: mediatek/filogic or qualcommax/ipq807x
  10. # SDK_SUBTARGET: (empty, detected automatically)
  11. # FORMAT: ipk or apk
  12. # BINARY_PATH: path to the prebuilt netpulse-agent binary (arm64)
  13. #
  14. # The package version defaults to the netpulse-agent Makefile (PKG_VERSION /
  15. # PKG_RELEASE) and can be overridden through the PKG_VERSION / PKG_RELEASE env
  16. # vars, which is what the CI does to match the release tag.
  17. #
  18. # Example:
  19. # package.sh 24.10.5 mediatek/filogic "" ipk ./netpulse-agent-arm64
  20. # package.sh 25.12.5 qualcommax/ipq807x "" apk ./netpulse-agent-arm64
  21. set -euo pipefail
  22. SDK_VERSION="${1:?missing SDK_VERSION}"
  23. SDK_TARGET="${2:?missing SDK_TARGET}"
  24. SDK_SUBTARGET="${3:-}"
  25. FORMAT="${4:?missing FORMAT (ipk|apk)}"
  26. BINARY="${5:?missing BINARY_PATH}"
  27. if [ ! -f "$BINARY" ]; then
  28. echo "ERROR: binary not found: $BINARY" >&2
  29. exit 1
  30. fi
  31. SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
  32. REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
  33. PKG_VERSION="${PKG_VERSION:-$(sed -n 's/^PKG_VERSION:=//p' "$SCRIPT_DIR/netpulse-agent/Makefile" | head -1)}"
  34. PKG_RELEASE="${PKG_RELEASE:-$(sed -n 's/^PKG_RELEASE:=//p' "$SCRIPT_DIR/netpulse-agent/Makefile" | head -1)}"
  35. echo "=== package.sh: $FORMAT SDK=$SDK_VERSION target=$SDK_TARGET version=${PKG_VERSION:-0.0.0}-${PKG_RELEASE:-1} ==="
  36. WORK_DIR="$(mktemp -d)"
  37. trap 'rm -rf "$WORK_DIR"' EXIT
  38. # SDK URL. The toolchain name in the archive differs across OpenWrt releases
  39. # (24.10 uses gcc-13.3.0, 25.12 uses gcc-14.3.0), so resolve the real name
  40. # from the download index instead of hardcoding it.
  41. SDK_DIR_URL="https://downloads.openwrt.org/releases/${SDK_VERSION}/targets/${SDK_TARGET}/"
  42. SDK_NAME="$(curl -fsSL "$SDK_DIR_URL" 2>/dev/null | grep -o 'openwrt-sdk-[^"]*\.tar\.zst' | head -1 || true)"
  43. if [ -z "$SDK_NAME" ]; then
  44. echo "ERROR: SDK not found under ${SDK_DIR_URL}" >&2
  45. exit 1
  46. fi
  47. SDK_URL="${SDK_DIR_URL}${SDK_NAME}"
  48. echo " SDK: $SDK_URL"
  49. # Download and extract the SDK
  50. cd "$WORK_DIR"
  51. curl -fsSL "$SDK_URL" -o sdk.tar.zst
  52. tar --zstd -xf sdk.tar.zst
  53. SDK_DIR="$WORK_DIR/${SDK_NAME%.tar.zst}"
  54. # Package architecture for the .ipk control file. OpenWrt names the staging
  55. # dir "target-<arch>_musl", so derive it from there instead of hardcoding a
  56. # single arch (mediatek/filogic -> aarch64_cortex-a53, x86/64 -> x86_64).
  57. PKG_ARCH="$(find "$SDK_DIR/staging_dir" -maxdepth 1 -type d -name 'target-*_musl*' -print -quit | sed -E 's#.*/target-(.*)_musl.*#\1#')"
  58. if [ -z "$PKG_ARCH" ]; then
  59. echo "ERROR: could not determine package architecture from the SDK" >&2
  60. exit 1
  61. fi
  62. echo " Arch: $PKG_ARCH"
  63. # Stage the binary and package files
  64. cp "$BINARY" "$WORK_DIR/netpulse-agent"
  65. chmod 755 "$WORK_DIR/netpulse-agent"
  66. PKG_DIR="$WORK_DIR/netpulse-agent-pkg"
  67. mkdir -p "$PKG_DIR/CONTROL" "$PKG_DIR/usr/sbin" "$PKG_DIR/etc/init.d" "$PKG_DIR/etc/config" "$PKG_DIR/etc/uci-defaults"
  68. cp "$WORK_DIR/netpulse-agent" "$PKG_DIR/usr/sbin/netpulse-agent"
  69. cp "$REPO_ROOT/deploy/openwrt/netpulse-agent/files/netpulse-agent.init" "$PKG_DIR/etc/init.d/netpulse-agent"
  70. cp "$REPO_ROOT/deploy/openwrt/netpulse-agent/files/netpulse-agent.config" "$PKG_DIR/etc/config/netpulse-agent"
  71. cp "$REPO_ROOT/deploy/openwrt/netpulse-agent/files/netpulse-agent.defaults" "$PKG_DIR/etc/uci-defaults/90-netpulse-agent"
  72. # CONTROL files
  73. cat > "$PKG_DIR/CONTROL/control" << CTRL
  74. Package: netpulse-agent
  75. Version: ${PKG_VERSION:-0.0.0}-${PKG_RELEASE:-1}
  76. Depends: iw
  77. License: AGPL-3.0-only
  78. Section: utils
  79. Architecture: ${PKG_ARCH}
  80. Maintainer: Nacho <netpulse@cloudless.club>
  81. Description: NetPulse native agent for OpenWrt
  82. Native OpenWrt agent for NetPulse network monitor. Probes the local
  83. router (system, wireless, DHCP, FDB) and pushes data to a NetPulse
  84. server. Listens for nl80211 events (assoc/disassoc) in real time and
  85. receives commands from the server via SSE. Stateless: no writes to
  86. flash; logs go to syslog via stderr.
  87. CTRL
  88. cat > "$PKG_DIR/CONTROL/postinst" << 'POSTINST'
  89. #!/bin/sh
  90. if [ -f /etc/init.d/netpulse-agent ] && /etc/init.d/netpulse-agent enabled; then
  91. /etc/init.d/netpulse-agent stop 2>/dev/null || true
  92. fi
  93. [ -f /etc/uci-defaults/90-netpulse-agent ] && sh /etc/uci-defaults/90-netpulse-agent
  94. exit 0
  95. POSTINST
  96. chmod 755 "$PKG_DIR/CONTROL/postinst"
  97. cat > "$PKG_DIR/CONTROL/prerm" << 'PRERM'
  98. #!/bin/sh
  99. [ -f /etc/init.d/netpulse-agent ] && /etc/init.d/netpulse-agent stop 2>/dev/null || true
  100. [ -f /etc/init.d/netpulse-agent ] && /etc/init.d/netpulse-agent disable 2>/dev/null || true
  101. crontab -l 2>/dev/null | grep -v netpulse-watchdog | crontab - 2>/dev/null || true
  102. exit 0
  103. PRERM
  104. chmod 755 "$PKG_DIR/CONTROL/prerm"
  105. echo "/etc/config/netpulse-agent" > "$PKG_DIR/CONTROL/conffiles"
  106. # Build the package
  107. OUT_DIR="$REPO_ROOT/dist/packages"
  108. mkdir -p "$OUT_DIR"
  109. if [ "$FORMAT" = "ipk" ]; then
  110. echo " Building .ipk..."
  111. "$SDK_DIR/scripts/ipkg-build" "$PKG_DIR" "$OUT_DIR"
  112. echo " IPK: $(ls "$OUT_DIR"/netpulse-agent_*.ipk)"
  113. elif [ "$FORMAT" = "apk" ]; then
  114. echo " Building .apk via SDK..."
  115. # SDK 25.12+ builds .apk with make. Generate a source-less Makefile with a
  116. # no-op Build/Compile: the prebuilt binary and its files are staged in
  117. # files/, so the SDK neither clones the repo nor compiles Go.
  118. PKG_DIR_SDK="$SDK_DIR/package/utils/netpulse-agent"
  119. mkdir -p "$PKG_DIR_SDK/files"
  120. cp -r "$PKG_DIR/usr" "$PKG_DIR/etc" "$PKG_DIR_SDK/files/"
  121. cat > "$PKG_DIR_SDK/Makefile" << 'MAKE'
  122. include $(TOPDIR)/rules.mk
  123. PKG_NAME:=netpulse-agent
  124. PKG_VERSION:=@PKG_VERSION@
  125. PKG_RELEASE:=@PKG_RELEASE@
  126. PKG_LICENSE:=AGPL-3.0-only
  127. PKG_MAINTAINER:=Nacho <netpulse@cloudless.club>
  128. include $(INCLUDE_DIR)/package.mk
  129. define Package/netpulse-agent
  130. SECTION:=utils
  131. CATEGORY:=Utilities
  132. TITLE:=NetPulse native agent for OpenWrt
  133. URL:=https://github.com/gnacho/netpulse
  134. DEPENDS:=+iw @(aarch64||arm||x86_64)
  135. endef
  136. define Package/netpulse-agent/description
  137. Native OpenWrt agent for NetPulse network monitor. Probes the local
  138. router (system, wireless, DHCP, FDB) and pushes data to a NetPulse
  139. server. Stateless: no writes to flash.
  140. endef
  141. define Build/Compile
  142. endef
  143. define Package/netpulse-agent/install
  144. $(INSTALL_DIR) $(1)/usr/sbin
  145. $(INSTALL_BIN) ./files/usr/sbin/netpulse-agent $(1)/usr/sbin/netpulse-agent
  146. $(INSTALL_DIR) $(1)/etc/init.d
  147. $(INSTALL_BIN) ./files/etc/init.d/netpulse-agent $(1)/etc/init.d/netpulse-agent
  148. $(INSTALL_DIR) $(1)/etc/config
  149. $(INSTALL_CONF) ./files/etc/config/netpulse-agent $(1)/etc/config/netpulse-agent
  150. $(INSTALL_DIR) $(1)/etc/uci-defaults
  151. $(INSTALL_BIN) ./files/etc/uci-defaults/90-netpulse-agent $(1)/etc/uci-defaults/90-netpulse-agent
  152. endef
  153. define Package/netpulse-agent/postinst
  154. #!/bin/sh
  155. if [ -z "$${IPKG_INSTROOT}" ]; then
  156. /etc/init.d/netpulse-agent stop 2>/dev/null || true
  157. [ -f /etc/uci-defaults/90-netpulse-agent ] && sh /etc/uci-defaults/90-netpulse-agent
  158. fi
  159. exit 0
  160. endef
  161. $(eval $(call BuildPackage,netpulse-agent))
  162. MAKE
  163. sed -i "s/@PKG_VERSION@/${PKG_VERSION:-0.0.0}/; s/@PKG_RELEASE@/${PKG_RELEASE:-1}/" "$PKG_DIR_SDK/Makefile"
  164. cd "$SDK_DIR"
  165. # Minimal feeds so the package index resolves iw; best effort.
  166. ./scripts/feeds update packages 2>/dev/null || true
  167. ./scripts/feeds install iw 2>/dev/null || true
  168. # Genera un .config válido sin terminal (el SDK 25.12 invoca menuconfig en
  169. # cuanto falta .config y muere con 'Error opening terminal' en CI).
  170. make defconfig >/dev/null 2>&1 || true
  171. make package/netpulse-agent/compile V=s 2>&1 | tail -20
  172. # The apk filename carries no arch, so append it: otherwise the armv8 and
  173. # x86/64 builds would collide in the release (same name, --clobber).
  174. find bin/packages -name "netpulse-agent*.apk" | while read -r f; do
  175. cp "$f" "$OUT_DIR/$(basename "$f" .apk)_${PKG_ARCH}.apk"
  176. done
  177. echo " APK: $(ls "$OUT_DIR"/netpulse-agent*.apk 2>/dev/null || echo 'NOT FOUND')"
  178. else
  179. echo "ERROR: FORMAT must be ipk or apk" >&2
  180. exit 1
  181. fi
  182. echo "=== package.sh: OK ==="