package.sh 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. # Stage the binary and package files
  55. cp "$BINARY" "$WORK_DIR/netpulse-agent"
  56. chmod 755 "$WORK_DIR/netpulse-agent"
  57. PKG_DIR="$WORK_DIR/netpulse-agent-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-agent" "$PKG_DIR/usr/sbin/netpulse-agent"
  60. cp "$REPO_ROOT/deploy/openwrt/netpulse-agent/files/netpulse-agent.init" "$PKG_DIR/etc/init.d/netpulse-agent"
  61. cp "$REPO_ROOT/deploy/openwrt/netpulse-agent/files/netpulse-agent.config" "$PKG_DIR/etc/config/netpulse-agent"
  62. cp "$REPO_ROOT/deploy/openwrt/netpulse-agent/files/netpulse-agent.defaults" "$PKG_DIR/etc/uci-defaults/90-netpulse-agent"
  63. # CONTROL files
  64. cat > "$PKG_DIR/CONTROL/control" << CTRL
  65. Package: netpulse-agent
  66. Version: ${PKG_VERSION:-0.0.0}-${PKG_RELEASE:-1}
  67. Depends: iw
  68. License: AGPL-3.0-only
  69. Section: utils
  70. Architecture: aarch64_cortex-a53
  71. Maintainer: Nacho <netpulse@cloudless.club>
  72. Description: NetPulse native agent for OpenWrt
  73. Native OpenWrt agent for NetPulse network monitor. Probes the local
  74. router (system, wireless, DHCP, FDB) and pushes data to a NetPulse
  75. server. Listens for nl80211 events (assoc/disassoc) in real time and
  76. receives commands from the server via SSE. Stateless: no writes to
  77. flash; logs go to syslog via stderr.
  78. CTRL
  79. cat > "$PKG_DIR/CONTROL/postinst" << 'POSTINST'
  80. #!/bin/sh
  81. if [ -f /etc/init.d/netpulse-agent ] && /etc/init.d/netpulse-agent enabled; then
  82. /etc/init.d/netpulse-agent stop 2>/dev/null || true
  83. fi
  84. [ -f /etc/uci-defaults/90-netpulse-agent ] && sh /etc/uci-defaults/90-netpulse-agent
  85. exit 0
  86. POSTINST
  87. chmod 755 "$PKG_DIR/CONTROL/postinst"
  88. cat > "$PKG_DIR/CONTROL/prerm" << 'PRERM'
  89. #!/bin/sh
  90. [ -f /etc/init.d/netpulse-agent ] && /etc/init.d/netpulse-agent stop 2>/dev/null || true
  91. [ -f /etc/init.d/netpulse-agent ] && /etc/init.d/netpulse-agent disable 2>/dev/null || true
  92. crontab -l 2>/dev/null | grep -v netpulse-watchdog | crontab - 2>/dev/null || true
  93. exit 0
  94. PRERM
  95. chmod 755 "$PKG_DIR/CONTROL/prerm"
  96. echo "/etc/config/netpulse-agent" > "$PKG_DIR/CONTROL/conffiles"
  97. # Build the package
  98. OUT_DIR="$REPO_ROOT/dist/packages"
  99. mkdir -p "$OUT_DIR"
  100. if [ "$FORMAT" = "ipk" ]; then
  101. echo " Building .ipk..."
  102. "$SDK_DIR/scripts/ipkg-build" "$PKG_DIR" "$OUT_DIR"
  103. echo " IPK: $(ls "$OUT_DIR"/netpulse-agent_*.ipk)"
  104. elif [ "$FORMAT" = "apk" ]; then
  105. echo " Building .apk via SDK..."
  106. # SDK 25.12+ builds .apk with make. Generate a source-less Makefile with a
  107. # no-op Build/Compile: the prebuilt binary and its files are staged in
  108. # files/, so the SDK neither clones the repo nor compiles Go.
  109. PKG_DIR_SDK="$SDK_DIR/package/utils/netpulse-agent"
  110. mkdir -p "$PKG_DIR_SDK/files"
  111. cp -r "$PKG_DIR/usr" "$PKG_DIR/etc" "$PKG_DIR_SDK/files/"
  112. cat > "$PKG_DIR_SDK/Makefile" << 'MAKE'
  113. include $(TOPDIR)/rules.mk
  114. PKG_NAME:=netpulse-agent
  115. PKG_VERSION:=@PKG_VERSION@
  116. PKG_RELEASE:=@PKG_RELEASE@
  117. PKG_LICENSE:=AGPL-3.0-only
  118. PKG_MAINTAINER:=Nacho <netpulse@cloudless.club>
  119. include $(INCLUDE_DIR)/package.mk
  120. define Package/netpulse-agent
  121. SECTION:=utils
  122. CATEGORY:=Utilities
  123. TITLE:=NetPulse native agent for OpenWrt
  124. URL:=https://github.com/gnacho/netpulse
  125. DEPENDS:=+iw @(aarch64||arm||x86_64)
  126. endef
  127. define Package/netpulse-agent/description
  128. Native OpenWrt agent for NetPulse network monitor. Probes the local
  129. router (system, wireless, DHCP, FDB) and pushes data to a NetPulse
  130. server. Stateless: no writes to flash.
  131. endef
  132. define Build/Compile
  133. endef
  134. define Package/netpulse-agent/install
  135. $(INSTALL_DIR) $(1)/usr/sbin
  136. $(INSTALL_BIN) ./files/usr/sbin/netpulse-agent $(1)/usr/sbin/netpulse-agent
  137. $(INSTALL_DIR) $(1)/etc/init.d
  138. $(INSTALL_BIN) ./files/etc/init.d/netpulse-agent $(1)/etc/init.d/netpulse-agent
  139. $(INSTALL_DIR) $(1)/etc/config
  140. $(INSTALL_CONF) ./files/etc/config/netpulse-agent $(1)/etc/config/netpulse-agent
  141. $(INSTALL_DIR) $(1)/etc/uci-defaults
  142. $(INSTALL_BIN) ./files/etc/uci-defaults/90-netpulse-agent $(1)/etc/uci-defaults/90-netpulse-agent
  143. endef
  144. define Package/netpulse-agent/postinst
  145. #!/bin/sh
  146. if [ -z "$${IPKG_INSTROOT}" ]; then
  147. /etc/init.d/netpulse-agent stop 2>/dev/null || true
  148. [ -f /etc/uci-defaults/90-netpulse-agent ] && sh /etc/uci-defaults/90-netpulse-agent
  149. fi
  150. exit 0
  151. endef
  152. $(eval $(call BuildPackage,netpulse-agent))
  153. MAKE
  154. sed -i "s/@PKG_VERSION@/${PKG_VERSION:-0.0.0}/; s/@PKG_RELEASE@/${PKG_RELEASE:-1}/" "$PKG_DIR_SDK/Makefile"
  155. cd "$SDK_DIR"
  156. # Minimal feeds so the package index resolves iw; best effort.
  157. ./scripts/feeds update packages 2>/dev/null || true
  158. ./scripts/feeds install iw 2>/dev/null || true
  159. # Genera un .config válido sin terminal (el SDK 25.12 invoca menuconfig en
  160. # cuanto falta .config y muere con 'Error opening terminal' en CI).
  161. make defconfig >/dev/null 2>&1 || true
  162. make package/netpulse-agent/compile V=s 2>&1 | tail -20
  163. find bin/packages -name "netpulse-agent*.apk" -exec cp {} "$OUT_DIR/" \;
  164. echo " APK: $(ls "$OUT_DIR"/netpulse-agent*.apk 2>/dev/null || echo 'NOT FOUND')"
  165. else
  166. echo "ERROR: FORMAT must be ipk or apk" >&2
  167. exit 1
  168. fi
  169. echo "=== package.sh: OK ==="