Files
lab/bastion/scripts/build-rpm.sh

181 lines
4.6 KiB
Bash
Raw Normal View History

#!/bin/bash
set -e
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
cd "$PROJECT_ROOT"
# Load .env if present
if [ -f .env ]; then
set -a; source .env; set +a
fi
# Ensure tools are on PATH
export PATH="$HOME/.npm-global/bin:$HOME/.bun/bin:$HOME/.local/bin:$PATH"
# ── Argument parsing ───────────────────────────────────────────────
BUILD_ALL=false
TARGET_ARCH=""
SKIP_TESTS=false
usage() {
cat <<EOF
Usage: $(basename "$0") [OPTIONS]
Build labctl binary and produce RPM/DEB packages.
Options:
--arch ARCH Target architecture: x86_64 or arm64 (default: host arch)
--all Build for both x86_64 and arm64
--skip-tests Skip unit tests (useful in CI where tests ran separately)
-h, --help Show this help message
EOF
exit 0
}
while [[ $# -gt 0 ]]; do
case "$1" in
--arch)
TARGET_ARCH="$2"
shift 2
;;
--all)
BUILD_ALL=true
shift
;;
--skip-tests)
SKIP_TESTS=true
shift
;;
-h|--help)
usage
;;
*)
echo "Unknown option: $1"
usage
;;
esac
done
# ── Resolve host architecture ─────────────────────────────────────
detect_host_arch() {
local machine
machine="$(uname -m)"
case "$machine" in
x86_64) echo "x86_64" ;;
aarch64) echo "arm64" ;;
arm64) echo "arm64" ;;
*) echo "$machine" ;;
esac
}
# ── Architecture mapping helpers ──────────────────────────────────
# Maps our canonical arch names to the values each tool expects.
bun_target_for() {
case "$1" in
x86_64) echo "bun-linux-x64" ;;
arm64) echo "bun-linux-arm64" ;;
esac
}
nfpm_arch_for() {
case "$1" in
x86_64) echo "amd64" ;;
arm64) echo "arm64" ;;
esac
}
rpm_arch_for() {
case "$1" in
x86_64) echo "x86_64" ;;
arm64) echo "aarch64" ;;
esac
}
deb_arch_for() {
case "$1" in
x86_64) echo "amd64" ;;
arm64) echo "arm64" ;;
esac
}
# ── Build one architecture ────────────────────────────────────────
build_arch() {
local arch="$1"
local bun_target nfpm_arch binary_name
bun_target="$(bun_target_for "$arch")"
nfpm_arch="$(nfpm_arch_for "$arch")"
binary_name="dist/labctl-${arch}"
echo ""
echo "==> Bundling standalone binary for ${arch}..."
bun build src/cli/src/index.ts --compile --target="${bun_target}" --outfile "${binary_name}"
echo "==> Packaging RPM (${arch})..."
# Create a temporary nfpm config with the correct arch and binary path
local tmpconfig
tmpconfig="$(mktemp /tmp/nfpm-XXXXXX.yaml)"
sed -e "s|^arch:.*|arch: ${nfpm_arch}|" \
-e "s|src: ./dist/labctl$|src: ./${binary_name}|" \
nfpm.yaml > "$tmpconfig"
nfpm pkg --config "$tmpconfig" --packager rpm --target dist/
rm -f "$tmpconfig"
local rpm_arch
rpm_arch="$(rpm_arch_for "$arch")"
RPM_FILE=$(ls dist/labctl-*.${rpm_arch}.rpm 2>/dev/null | head -1)
echo "==> Built: $RPM_FILE"
echo " Size: $(du -h "$RPM_FILE" | cut -f1)"
echo ""
echo "==> Packaging DEB (${arch})..."
local deb_arch
deb_arch="$(deb_arch_for "$arch")"
tmpconfig="$(mktemp /tmp/nfpm-XXXXXX.yaml)"
sed -e "s|^arch:.*|arch: ${nfpm_arch}|" \
-e "s|src: ./dist/labctl$|src: ./${binary_name}|" \
nfpm.yaml > "$tmpconfig"
nfpm pkg --config "$tmpconfig" --packager deb --target dist/
rm -f "$tmpconfig"
DEB_FILE=$(ls dist/labctl_*_${deb_arch}.deb 2>/dev/null | head -1)
echo "==> Built: $DEB_FILE"
echo " Size: $(du -h "$DEB_FILE" | cut -f1)"
}
# ── Main ──────────────────────────────────────────────────────────
if [ "$SKIP_TESTS" = false ]; then
echo "==> Running unit tests..."
pnpm test:run
echo ""
fi
echo "==> Building TypeScript..."
pnpm build
echo "==> Generating shell completions..."
pnpm completions:generate
mkdir -p dist
rm -f dist/labctl dist/labctl-x86_64 dist/labctl-arm64 dist/labctl-*.rpm dist/labctl*.deb
if [ "$BUILD_ALL" = true ]; then
build_arch "x86_64"
build_arch "arm64"
elif [ -n "$TARGET_ARCH" ]; then
build_arch "$TARGET_ARCH"
else
# Default to host architecture
HOST_ARCH="$(detect_host_arch)"
build_arch "$HOST_ARCH"
fi
echo ""
echo "==> Build complete. Artifacts in dist/:"
ls -lh dist/labctl* 2>/dev/null || echo " (none)"