build-agents.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/env bash
  2. # Builds the five netpulse-agent binaries, packs them with UPX and verifies
  3. # the result. Single source of truth for the local build (Makefile) and the
  4. # three workflows that need embedded agents (pr, go-latest, release).
  5. #
  6. # Usage: scripts/build-agents.sh <agent-version> [output-dir]
  7. #
  8. # UPX_BIN upx binary to use (default: upx from PATH)
  9. # UPX_FLAGS packing flags (default: --best --lzma)
  10. # GO go binary (default: go from PATH)
  11. #
  12. # The packed binaries are the ones served by /api/agents/{slug}/binary and
  13. # shipped in the release tarballs, so their sha256 (computed by the server at
  14. # runtime over the embedded bytes) matches what the installer verifies.
  15. set -euo pipefail
  16. VERSION=${1:?usage: build-agents.sh <agent-version> [output-dir]}
  17. OUT=${2:-server-go/internal/agentbin/agents}
  18. UPX_BIN=${UPX_BIN:-upx}
  19. UPX_FLAGS=${UPX_FLAGS:---best --lzma}
  20. GO=${GO:-go}
  21. cd "$(dirname "$0")/.."
  22. if ! command -v "$UPX_BIN" >/dev/null 2>&1; then
  23. echo "build-agents: $UPX_BIN is not in PATH. Install it (pacman -S upx, apt install upx-ucl," >&2
  24. echo " dnf install upx) or point UPX_BIN at the binary. The agents are UPX packed on" >&2
  25. echo " purpose: an unpacked agent needs twice its size free to upgrade (#1890)." >&2
  26. exit 1
  27. fi
  28. mkdir -p "$OUT"
  29. export CGO_ENABLED=0
  30. export GOOS=linux
  31. build() {
  32. local arch=$1 extra=$2
  33. # shellcheck disable=SC2086
  34. (cd agent && env GOARCH="$arch" $extra "$GO" build -trimpath \
  35. -ldflags "-s -w -X main.Version=$VERSION" \
  36. -o "../$OUT/netpulse-agent-$arch" ./cmd/netpulse-agent)
  37. }
  38. # armv7 is "arm" with GOARM=7; MIPS (MT7621, ath79) has no FPU: softfloat.
  39. build amd64 ""
  40. build arm64 ""
  41. build arm "GOARM=7"
  42. build mipsle "GOMIPS=softfloat"
  43. build mips "GOMIPS=softfloat"
  44. # shellcheck disable=SC2086: the flags are a list on purpose
  45. "$UPX_BIN" -q -f $UPX_FLAGS "$OUT"/netpulse-agent-* >/dev/null
  46. # Gate: every binary must be packed, pass UPX's own integrity test and stay
  47. # well under the unpacked size. An unpacked release is a regression of #1890,
  48. # and a silently skipped UPX (goreleaser skips it when it is not in PATH)
  49. # would only show up as a failed upgrade on a router with little storage.
  50. for arch in amd64 arm64 arm mipsle mips; do
  51. bin="$OUT/netpulse-agent-$arch"
  52. [ -s "$bin" ] || { echo "build-agents: missing $bin" >&2; exit 1; }
  53. # UPX keeps a valid ELF header and writes its marker at the tail.
  54. if ! tail -c 256 "$bin" | grep -qa "UPX!"; then
  55. echo "build-agents: $bin is not UPX packed" >&2
  56. exit 1
  57. fi
  58. "$UPX_BIN" -t "$bin" >/dev/null || { echo "build-agents: $bin fails the UPX integrity test" >&2; exit 1; }
  59. done
  60. ls -l "$OUT"/netpulse-agent-*