build-agents.sh 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. # The agent only uses the encoding/json v1 API. json/v2 is on by default in
  32. # the Go 1.26 toolchain and costs about 124 KB per packed binary (#2095), so it
  33. # is compiled out unless the caller sets GOEXPERIMENT explicitly.
  34. export GOEXPERIMENT=${GOEXPERIMENT:-nojsonv2}
  35. build() {
  36. local arch=$1 extra=$2
  37. # go build does not always rewrite an existing -o target when the build is
  38. # cached, and a target that is already UPX packed makes the packing step
  39. # fail with AlreadyPackedException. Removing it first keeps the script
  40. # idempotent: two runs in a row must both work.
  41. rm -f "$OUT/netpulse-agent-$arch"
  42. # shellcheck disable=SC2086
  43. (cd agent && env GOARCH="$arch" $extra "$GO" build -trimpath \
  44. -ldflags "-s -w -X main.Version=$VERSION" \
  45. -o "../$OUT/netpulse-agent-$arch" ./cmd/netpulse-agent)
  46. }
  47. # armv7 is "arm" with GOARM=7; MIPS (MT7621, ath79) has no FPU: softfloat.
  48. build amd64 ""
  49. build arm64 ""
  50. build arm "GOARM=7"
  51. build mipsle "GOMIPS=softfloat"
  52. build mips "GOMIPS=softfloat"
  53. # shellcheck disable=SC2086: the flags are a list on purpose
  54. "$UPX_BIN" -q -f $UPX_FLAGS "$OUT"/netpulse-agent-* >/dev/null
  55. # Gate: every binary must be packed, pass UPX's own integrity test and stay
  56. # well under the unpacked size. An unpacked release is a regression of #1890,
  57. # and a silently skipped UPX (goreleaser skips it when it is not in PATH)
  58. # would only show up as a failed upgrade on a router with little storage.
  59. for arch in amd64 arm64 arm mipsle mips; do
  60. bin="$OUT/netpulse-agent-$arch"
  61. [ -s "$bin" ] || { echo "build-agents: missing $bin" >&2; exit 1; }
  62. # UPX keeps a valid ELF header and writes its marker at the tail.
  63. if ! tail -c 256 "$bin" | grep -qa "UPX!"; then
  64. echo "build-agents: $bin is not UPX packed" >&2
  65. exit 1
  66. fi
  67. "$UPX_BIN" -t "$bin" >/dev/null || { echo "build-agents: $bin fails the UPX integrity test" >&2; exit 1; }
  68. done
  69. # UPX compresses the version literal passed with -X main.Version, so the #1246
  70. # guard cannot find it by scanning the embedded bytes. The stamp keeps that
  71. # check working without unpacking at runtime (#1914).
  72. printf '%s\n' "$VERSION" > "$OUT/agent.version"
  73. ls -l "$OUT"/netpulse-agent-*