v0.0.2 - Add random mnemonic startup flow and multi-instance random socket naming

This commit is contained in:
Laan Tungir
2026-05-02 13:46:01 -04:00
parent 268b33b6d3
commit 21f1258844
25 changed files with 1175 additions and 285 deletions

View File

@@ -1,35 +1,30 @@
#!/bin/bash
# Build fully static MUSL binaries for nsigner using Alpine Docker
# Build fully static MUSL binary for nsigner using Alpine Docker
set -e
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BUILD_DIR="$SCRIPT_DIR/build"
DOCKERFILE="$SCRIPT_DIR/Dockerfile.alpine-musl"
DEBUG_BUILD=false
TARGET_ARCH=""
while [[ $# -gt 0 ]]; do
case "$1" in
--debug)
DEBUG_BUILD=true
shift
;;
--arch)
if [[ -z "$2" ]]; then
if [[ -z "${2:-}" ]]; then
echo "ERROR: --arch requires a value"
echo "Usage: $0 [--debug] [--arch <arm64|armv7|x86_64>]"
echo "Usage: $0 [--arch <x86_64|arm64|armv7>]"
exit 1
fi
case "$2" in
arm64|armv7|x86_64)
x86_64|arm64|armv7)
TARGET_ARCH="$2"
;;
*)
echo "ERROR: Unsupported architecture '$2'"
echo "Supported values: arm64, armv7, x86_64"
echo "Supported values: x86_64, arm64, armv7"
exit 1
;;
esac
@@ -37,48 +32,24 @@ while [[ $# -gt 0 ]]; do
;;
*)
echo "ERROR: Unknown argument '$1'"
echo "Usage: $0 [--debug] [--arch <arm64|armv7|x86_64>]"
echo "Usage: $0 [--arch <x86_64|arm64|armv7>]"
exit 1
;;
esac
done
if [ "$DEBUG_BUILD" = true ]; then
echo "=========================================="
echo "nsigner MUSL Static Binary Builder (DEBUG MODE)"
echo "=========================================="
else
echo "=========================================="
echo "nsigner MUSL Static Binary Builder (PRODUCTION MODE)"
echo "=========================================="
fi
echo "Project directory: $SCRIPT_DIR"
echo "Build directory: $BUILD_DIR"
echo "Debug build: $DEBUG_BUILD"
if [[ -n "$TARGET_ARCH" ]]; then
echo "Requested target architecture: $TARGET_ARCH"
fi
echo ""
mkdir -p "$BUILD_DIR"
if ! command -v docker &> /dev/null; then
if ! command -v docker >/dev/null 2>&1; then
echo "ERROR: Docker is not installed or not in PATH"
exit 1
fi
if ! docker info &> /dev/null; then
echo "ERROR: Docker daemon is not running or user not in docker group"
if ! docker info >/dev/null 2>&1; then
echo "ERROR: Docker daemon is not running or user lacks permissions"
exit 1
fi
DOCKER_CMD="docker"
echo "✓ Docker is available and running"
echo ""
ARCH=$(uname -m)
case "$ARCH" in
HOST_UNAME="$(uname -m)"
case "$HOST_UNAME" in
x86_64)
HOST_ARCH="x86_64"
;;
@@ -89,147 +60,74 @@ case "$ARCH" in
HOST_ARCH="armv7"
;;
*)
HOST_ARCH="unknown"
HOST_ARCH="x86_64"
;;
esac
if [[ -n "$TARGET_ARCH" ]]; then
ARCH="$TARGET_ARCH"
fi
ARCH="${TARGET_ARCH:-$HOST_ARCH}"
case "$ARCH" in
x86_64)
PLATFORM="linux/amd64"
OUTPUT_NAME="nsigner_static_x86_64"
;;
aarch64|arm64)
ARCH="arm64"
arm64)
PLATFORM="linux/arm64"
OUTPUT_NAME="nsigner_static_arm64"
;;
armv7l|armv7)
ARCH="armv7"
armv7)
PLATFORM="linux/arm/v7"
OUTPUT_NAME="nsigner_static_armv7"
;;
*)
echo "WARNING: Unknown architecture: $ARCH"
echo "Defaulting to linux/amd64"
PLATFORM="linux/amd64"
OUTPUT_NAME="nsigner_static_${ARCH}"
echo "ERROR: Unsupported target architecture '$ARCH'"
exit 1
;;
esac
if [[ "$HOST_ARCH" != "unknown" && "$ARCH" != "$HOST_ARCH" ]]; then
echo "NOTE: Cross-building from host '$HOST_ARCH' to target '$ARCH'."
if ! docker buildx version >/dev/null 2>&1; then
echo "ERROR: docker buildx is required for cross-architecture builds but is not available."
exit 1
mkdir -p "$BUILD_DIR"
IMAGE_TAG="nsigner-static-builder:${ARCH}"
CONTAINER_NAME=""
cleanup() {
if [[ -n "$CONTAINER_NAME" ]]; then
docker rm -f "$CONTAINER_NAME" >/dev/null 2>&1 || true
fi
echo "✓ docker buildx is available for cross-architecture build"
echo ""
fi
}
trap cleanup EXIT
if [ "$DEBUG_BUILD" = true ]; then
OUTPUT_NAME="${OUTPUT_NAME}_debug"
fi
echo "Building for platform: $PLATFORM"
echo "Output binary: $OUTPUT_NAME"
echo "=========================================="
echo "nsigner static build"
echo "=========================================="
echo "Project root: $SCRIPT_DIR"
echo "Dockerfile: $DOCKERFILE"
echo "Platform: $PLATFORM"
echo "Output: $BUILD_DIR/$OUTPUT_NAME"
echo ""
echo "=========================================="
echo "Step 1: Building Alpine Docker image"
echo "=========================================="
$DOCKER_CMD build \
echo "[1/3] Building builder stage from project root context"
docker build \
--platform "$PLATFORM" \
--build-arg DEBUG_BUILD=$DEBUG_BUILD \
-f "$DOCKERFILE" \
-t nsigner-musl-builder:latest \
--progress=plain \
.
echo ""
echo "✓ Docker image built successfully"
echo ""
echo "=========================================="
echo "Step 2: Extracting static binary"
echo "=========================================="
$DOCKER_CMD build \
--platform "$PLATFORM" \
--build-arg DEBUG_BUILD=$DEBUG_BUILD \
--target builder \
-f "$DOCKERFILE" \
-t nsigner-static-builder-stage:latest \
. > /dev/null 2>&1
CONTAINER_ID=$($DOCKER_CMD create nsigner-static-builder-stage:latest)
$DOCKER_CMD cp "$CONTAINER_ID:/build/nsigner_static" "$BUILD_DIR/$OUTPUT_NAME"
$DOCKER_CMD rm "$CONTAINER_ID" > /dev/null
-t "$IMAGE_TAG" \
"$SCRIPT_DIR"
echo "[2/3] Extracting static binary"
CONTAINER_NAME="$(docker create "$IMAGE_TAG")"
docker cp "$CONTAINER_NAME:/build/nsigner_static" "$BUILD_DIR/$OUTPUT_NAME"
chmod +x "$BUILD_DIR/$OUTPUT_NAME"
echo "✓ Binary extracted to: $BUILD_DIR/$OUTPUT_NAME"
echo ""
echo "[3/3] Verifying static binary"
file "$BUILD_DIR/$OUTPUT_NAME"
echo "=========================================="
echo "Step 3: Verifying static binary"
echo "=========================================="
echo ""
if LDD_OUTPUT=$(timeout 5 ldd "$BUILD_DIR/$OUTPUT_NAME" 2>&1); then
if echo "$LDD_OUTPUT" | grep -q "not a dynamic executable"; then
echo "✓ Binary is fully static"
TRULY_STATIC=true
elif echo "$LDD_OUTPUT" | grep -q "statically linked"; then
echo "✓ Binary is statically linked"
TRULY_STATIC=true
else
echo "⚠ WARNING: Binary may have dynamic dependencies:"
echo "$LDD_OUTPUT"
TRULY_STATIC=false
fi
LDD_OUTPUT="$(ldd "$BUILD_DIR/$OUTPUT_NAME" 2>&1 || true)"
echo "$LDD_OUTPUT"
if echo "$LDD_OUTPUT" | grep -Eq "not a dynamic executable|statically linked"; then
echo "Static check: PASS"
else
if file "$BUILD_DIR/$OUTPUT_NAME" | grep -q "statically linked"; then
echo "✓ Binary is statically linked (verified with file command)"
TRULY_STATIC=true
else
echo "⚠ Could not verify static linking (ldd check failed)"
TRULY_STATIC=false
fi
echo "Static check: WARNING (verify manually)"
fi
echo ""
echo "File size: $(ls -lh "$BUILD_DIR/$OUTPUT_NAME" | awk '{print $5}')"
echo ""
echo "Testing binary execution:"
if "$BUILD_DIR/$OUTPUT_NAME" --version 2>&1 | head -5; then
echo "✓ Binary executes successfully"
else
echo "⚠ Binary execution test failed"
fi
echo ""
echo "=========================================="
echo "Build Summary"
echo "=========================================="
echo "Binary: $BUILD_DIR/$OUTPUT_NAME"
echo "Size: $(du -h "$BUILD_DIR/$OUTPUT_NAME" | cut -f1)"
echo "Platform: $PLATFORM"
if [ "$DEBUG_BUILD" = true ]; then
echo "Build Type: DEBUG"
else
echo "Build Type: PRODUCTION"
fi
if [ "$TRULY_STATIC" = true ]; then
echo "Linkage: Fully static binary"
else
echo "Linkage: Static binary (verification inconclusive)"
fi
echo ""
echo "✓ Build complete!"
echo "Build complete: $BUILD_DIR/$OUTPUT_NAME"