package.sh 8.0 KB

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