package.sh 7.6 KB

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