| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- #!/bin/bash
- # package-server.sh builds the on-box netpulse SERVER package (.ipk/.apk)
- # from a prebuilt static binary, mirroring package.sh (agent). Pure Go
- # (modernc sqlite) so the arm64 build is fully static.
- #
- # Usage:
- # package-server.sh <SDK_VERSION> <SDK_TARGET> <SDK_SUBTARGET> <FORMAT> <BINARY_PATH>
- #
- # FORMAT: ipk or apk
- # BINARY_PATH: prebuilt netpulse server binary (arm64)
- #
- # PKG_VERSION / PKG_RELEASE overridable via env (CI uses the release tag).
- #
- # Example:
- # package-server.sh 25.12.5 qualcommax/ipq807x "" apk ./netpulse
- set -euo pipefail
- SDK_VERSION="${1:?missing SDK_VERSION}"
- SDK_TARGET="${2:?missing SDK_TARGET}"
- SDK_SUBTARGET="${3:-}"
- FORMAT="${4:?missing FORMAT (ipk|apk)}"
- BINARY="${5:?missing BINARY_PATH}"
- if [ ! -f "$BINARY" ]; then
- echo "ERROR: binary not found: $BINARY" >&2
- exit 1
- fi
- SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
- REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
- PKG_VERSION="${PKG_VERSION:-2.0.0}"
- PKG_RELEASE="${PKG_RELEASE:-1}"
- echo "=== package-server.sh: $FORMAT SDK=$SDK_VERSION target=$SDK_TARGET version=${PKG_VERSION}-${PKG_RELEASE} ==="
- WORK_DIR="$(mktemp -d)"
- trap 'rm -rf "$WORK_DIR"' EXIT
- SDK_DIR_URL="https://downloads.openwrt.org/releases/${SDK_VERSION}/targets/${SDK_TARGET}/"
- # shellcheck source=deploy/openwrt/sdk.sh
- . "$SCRIPT_DIR/sdk.sh"
- SDK_DIR="$(download_verified_sdk "$SDK_DIR_URL" "$WORK_DIR")" || exit 1
- cd "$WORK_DIR"
- # Package architecture for the .ipk control file. OpenWrt names the staging
- # dir "target-<arch>_musl", so derive it from there instead of hardcoding a
- # single arch (mediatek/filogic -> aarch64_cortex-a53, x86/64 -> x86_64).
- PKG_ARCH="$(find "$SDK_DIR/staging_dir" -maxdepth 1 -type d -name 'target-*_musl*' -print -quit | sed -E 's#.*/target-(.*)_musl.*#\1#')"
- if [ -z "$PKG_ARCH" ]; then
- echo "ERROR: could not determine package architecture from the SDK" >&2
- exit 1
- fi
- echo " Arch: $PKG_ARCH"
- # Stage binary and package files (package name: netpulse, like the Makefile)
- cp "$BINARY" "$WORK_DIR/netpulse"
- chmod 755 "$WORK_DIR/netpulse"
- PKG_DIR="$WORK_DIR/netpulse-pkg"
- mkdir -p "$PKG_DIR/CONTROL" "$PKG_DIR/usr/sbin" "$PKG_DIR/etc/init.d" "$PKG_DIR/etc/config" "$PKG_DIR/etc/uci-defaults"
- cp "$WORK_DIR/netpulse" "$PKG_DIR/usr/sbin/netpulse"
- # Init script and uci-defaults MUST be 0755 (#1701): ipkg-build does not fix
- # permissions, and a 0644 init script fails with "Permission denied" during
- # uci-defaults on GL.iNet/OpenWrt 21.02, so the service never gets enabled.
- install -m 0755 "$REPO_ROOT/deploy/openwrt/netpulse-server/files/netpulse-server.init" "$PKG_DIR/etc/init.d/netpulse"
- cp "$REPO_ROOT/deploy/openwrt/netpulse-server/files/netpulse-server.config" "$PKG_DIR/etc/config/netpulse"
- install -m 0755 "$REPO_ROOT/deploy/openwrt/netpulse-server/files/netpulse-server.defaults" "$PKG_DIR/etc/uci-defaults/95-netpulse-server"
- cat > "$PKG_DIR/CONTROL/control" << CTRL
- Package: netpulse
- Version: ${PKG_VERSION}-${PKG_RELEASE}
- Depends: curl, ca-bundle
- License: AGPL-3.0-only
- Section: net
- Architecture: ${PKG_ARCH}
- Maintainer: Nacho <netpulse@cloudless.club>
- Description: NetPulse network monitor (on-box server)
- NetPulse server running on-box: serves the web app and ingests agent
- data over HTTPS with self-signed TLS + SPKI pinning. Stores data in
- SQLite (overlay or USB). Same binary as the dedicated-node deployment.
- CTRL
- cat > "$PKG_DIR/CONTROL/postinst" << 'POSTINST'
- #!/bin/sh
- # NetGrip takeover rule (#363): a router running NetGrip manages its own
- # panel; installing the on-box server here is still allowed (they can
- # coexist: different ports), so no guard needed for the server.
- [ -f /etc/uci-defaults/95-netpulse-server ] && sh /etc/uci-defaults/95-netpulse-server
- /etc/init.d/netpulse restart 2>/dev/null || true
- exit 0
- POSTINST
- chmod 755 "$PKG_DIR/CONTROL/postinst"
- cat > "$PKG_DIR/CONTROL/prerm" << 'PRERM'
- #!/bin/sh
- /etc/init.d/netpulse stop 2>/dev/null || true
- /etc/init.d/netpulse disable 2>/dev/null || true
- exit 0
- PRERM
- chmod 755 "$PKG_DIR/CONTROL/prerm"
- echo "/etc/config/netpulse" > "$PKG_DIR/CONTROL/conffiles"
- OUT_DIR="$REPO_ROOT/dist/packages"
- mkdir -p "$OUT_DIR"
- if [ "$FORMAT" = "ipk" ]; then
- echo " Building .ipk..."
- "$SDK_DIR/scripts/ipkg-build" "$PKG_DIR" "$OUT_DIR"
- echo " IPK: $(ls "$OUT_DIR"/netpulse_*.ipk 2>/dev/null || echo 'NOT FOUND')"
- verify_ipk_modes "$OUT_DIR"/netpulse_*.ipk \
- "usr/sbin/netpulse=755" \
- "etc/init.d/netpulse=755" \
- "etc/uci-defaults/95-netpulse-server=755"
- elif [ "$FORMAT" = "apk" ]; then
- echo " Building .apk via SDK..."
- PKG_DIR_SDK="$SDK_DIR/package/utils/netpulse"
- mkdir -p "$PKG_DIR_SDK/files"
- cp -r "$PKG_DIR/usr" "$PKG_DIR/etc" "$PKG_DIR_SDK/files/"
- cat > "$PKG_DIR_SDK/Makefile" << 'MAKE'
- include $(TOPDIR)/rules.mk
- PKG_NAME:=netpulse
- PKG_VERSION:=@PKG_VERSION@
- PKG_RELEASE:=@PKG_RELEASE@
- PKG_LICENSE:=AGPL-3.0-only
- PKG_MAINTAINER:=Nacho <netpulse@cloudless.club>
- include $(INCLUDE_DIR)/package.mk
- define Package/netpulse
- SECTION:=net
- CATEGORY:=Network
- TITLE:=NetPulse network monitor (on-box server)
- URL:=https://github.com/gnacho/netpulse
- DEPENDS:=+curl +ca-bundle @(aarch64||arm||x86_64)
- endef
- define Package/netpulse/description
- NetPulse server running on-box: serves the web app and ingests agent
- data over HTTPS with self-signed TLS + SPKI pinning. SQLite storage.
- endef
- define Build/Compile
- endef
- define Package/netpulse/install
- $(INSTALL_DIR) $(1)/usr/sbin
- $(INSTALL_BIN) ./files/usr/sbin/netpulse $(1)/usr/sbin/netpulse
- $(INSTALL_DIR) $(1)/etc/init.d
- $(INSTALL_BIN) ./files/etc/init.d/netpulse $(1)/etc/init.d/netpulse
- $(INSTALL_DIR) $(1)/etc/config
- $(INSTALL_CONF) ./files/etc/config/netpulse $(1)/etc/config/netpulse
- $(INSTALL_DIR) $(1)/etc/uci-defaults
- $(INSTALL_BIN) ./files/etc/uci-defaults/95-netpulse-server $(1)/etc/uci-defaults/95-netpulse-server
- endef
- define Package/netpulse/postinst
- #!/bin/sh
- if [ -z "$${IPKG_INSTROOT}" ]; then
- [ -f /etc/uci-defaults/95-netpulse-server ] && sh /etc/uci-defaults/95-netpulse-server
- /etc/init.d/netpulse restart 2>/dev/null || true
- fi
- exit 0
- endef
- $(eval $(call BuildPackage,netpulse))
- MAKE
- sed -i "s/@PKG_VERSION@/${PKG_VERSION}/; s/@PKG_RELEASE@/${PKG_RELEASE}/" "$PKG_DIR_SDK/Makefile"
- cd "$SDK_DIR"
- make defconfig >/dev/null 2>&1 || true
- make package/netpulse/compile V=s 2>&1 | tail -15
- # The apk filename carries no arch, so append it: otherwise the armv8 and
- # x86/64 builds would collide in the release (same name, --clobber).
- find bin/packages -name "netpulse-*.apk" | while read -r f; do
- cp "$f" "$OUT_DIR/$(basename "$f" .apk)_${PKG_ARCH}.apk"
- done
- echo " APK: $(ls "$OUT_DIR"/netpulse-*.apk 2>/dev/null || echo 'NOT FOUND')"
- else
- echo "ERROR: FORMAT must be ipk or apk" >&2
- exit 1
- fi
- # El .apk del SERVER se llama netpulse-<v>.apk (sin sufijo de arch en 25.12
- # cuando el paquete es arch-specific por DEPENDS); renombrar para evitar
- # colisionar con el tarball netpulse_<v>_linux_arm64.tar.gz en la release.
- echo "=== package-server.sh: OK ==="
|