#!/bin/bash # build-cloudinit.sh — Compile the cloud-init binary as a static Linux binary # # Environment: # TARGET_ARCH Target architecture (default: amd64, also supports: arm64) set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" CACHE_DIR="${CACHE_DIR:-$PROJECT_ROOT/build/cache}" CLOUDINIT_SRC="$PROJECT_ROOT/cloud-init" TARGET_ARCH="${TARGET_ARCH:-amd64}" OUTPUT="$CACHE_DIR/kubesolo-cloudinit" echo "==> Building cloud-init binary (linux/$TARGET_ARCH)..." if ! command -v go >/dev/null 2>&1; then echo "ERROR: Go is not installed. Install Go 1.22+ to build cloud-init." echo " https://go.dev/dl/" exit 1 fi mkdir -p "$CACHE_DIR" cd "$CLOUDINIT_SRC" # Run tests first echo " Running tests..." go test ./... -count=1 || { echo "ERROR: Tests failed. Fix tests before building." exit 1 } # Build static binary echo " Compiling (CGO_ENABLED=0 GOOS=linux GOARCH=$TARGET_ARCH)..." CGO_ENABLED=0 GOOS=linux GOARCH="$TARGET_ARCH" go build \ -ldflags='-s -w' \ -o "$OUTPUT" \ ./cmd/ echo " Built: $OUTPUT ($(du -h "$OUTPUT" | cut -f1))" echo ""