Skip to content

Commit 2ed6734

Browse files
committed
refactor: decouple build from source and install and support using binary directory for install
With this change we can build binaries in CI (with go installed) and then copy it to hosts (without go) and install them.
1 parent e77731d commit 2ed6734

File tree

3 files changed

+141
-34
lines changed

3 files changed

+141
-34
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ dist/**
2929

3030
# UTM VM - downloaded ISO files
3131
scripts/utm/images/
32+
scripts/build/

scripts/build-from-source.sh

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#!/bin/bash
2+
#
3+
# Hypeman Build from Source Script
4+
#
5+
# Usage:
6+
# ./scripts/build-from-source.sh [options]
7+
#
8+
# Options (via environment variables:
9+
# BRANCH - Git branch to build from (required)
10+
# OUTPUT_DIR - Full path of directory to place built binaries (optional, default: $(pwd)/bin)
11+
#
12+
# This script can be run standalone or called from install.sh
13+
14+
set -e
15+
16+
# Default values
17+
REPO="kernel/hypeman"
18+
BINARY_NAME="hypeman-api"
19+
20+
# Colors for output (true color)
21+
RED='\033[38;2;255;110;110m'
22+
GREEN='\033[38;2;92;190;83m'
23+
YELLOW='\033[0;33m'
24+
PURPLE='\033[38;2;172;134;249m'
25+
NC='\033[0m' # No Color
26+
27+
info() { echo -e "${GREEN}[INFO]${NC} $1"; }
28+
warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
29+
error() { echo -e "${RED}[ERROR]${NC} $1"; exit 1; }
30+
31+
# =============================================================================
32+
# Pre-flight checks - verify all requirements before doing anything
33+
# =============================================================================
34+
35+
if [ -z "$BRANCH" ]; then
36+
error "BRANCH environment variable is required"
37+
fi
38+
39+
if [ -z "$OUTPUT_DIR" ]; then
40+
OUTPUT_DIR=$(pwd)/bin
41+
fi
42+
43+
# Check for required commands
44+
command -v git >/dev/null 2>&1 || error "git is required but not installed"
45+
command -v go >/dev/null 2>&1 || error "go is required but not installed"
46+
command -v make >/dev/null 2>&1 || error "make is required but not installed"
47+
48+
# =============================================================================
49+
# Setup Output Directory
50+
# =============================================================================
51+
52+
# Create output directory if it doesn't exist
53+
mkdir -p "$OUTPUT_DIR"
54+
55+
# Create temporary build directory
56+
BUILD_DIR="${OUTPUT_DIR}/hypeman"
57+
trap "rm -rf $BUILD_DIR" EXIT
58+
59+
BUILD_LOG="${OUTPUT_DIR}/build.log"
60+
61+
# =============================================================================
62+
# Build from source
63+
# =============================================================================
64+
65+
info "Building from source (branch: $BRANCH)..."
66+
67+
# Clone repo (quiet)
68+
if ! git clone --branch "$BRANCH" --depth 1 -q "https://github.com/${REPO}.git" "${BUILD_DIR}" 2>&1 | tee -a "$BUILD_LOG"; then
69+
error "Failed to clone repository. Build log:\n$(cat "$BUILD_LOG")"
70+
fi
71+
72+
info "Building binaries (this may take a few minutes)..."
73+
cd "${BUILD_DIR}"
74+
75+
# Build main binary (includes dependencies) - capture output, show on error
76+
if ! make build >> "$BUILD_LOG" 2>&1; then
77+
echo ""
78+
echo -e "${RED}Build failed. Full build log:${NC}"
79+
cat "$BUILD_LOG"
80+
error "Build failed"
81+
fi
82+
cp "bin/hypeman" "${OUTPUT_DIR}/${BINARY_NAME}"
83+
84+
# Build hypeman-token (not included in make build)
85+
if ! go build -o "${OUTPUT_DIR}/hypeman-token" ./cmd/gen-jwt >> "$BUILD_LOG" 2>&1; then
86+
echo ""
87+
echo -e "${RED}Build failed. Full build log:${NC}"
88+
cat "$BUILD_LOG"
89+
error "Failed to build hypeman-token"
90+
fi
91+
92+
# Copy .env.example for config template
93+
cp ".env.example" "${OUTPUT_DIR}/.env.example"
94+
95+
info "Build complete"
96+
info "Binaries are available in: ${OUTPUT_DIR}"

scripts/install.sh

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
# VERSION - Install specific API version (default: latest)
1010
# CLI_VERSION - Install specific CLI version (default: latest)
1111
# BRANCH - Build from source using this branch (for development/testing)
12+
# BINARY_DIR - Use binaries from this directory instead of building/downloading
1213
# INSTALL_DIR - Binary installation directory (default: /opt/hypeman/bin)
1314
# DATA_DIR - Data directory (default: /var/lib/hypeman)
1415
# CONFIG_DIR - Config directory (default: /etc/hypeman)
@@ -25,6 +26,8 @@ CONFIG_FILE="${CONFIG_DIR}/config"
2526
SYSTEMD_DIR="/etc/systemd/system"
2627
SERVICE_NAME="hypeman"
2728
SERVICE_USER="hypeman"
29+
BUILD_SCRIPT="build-from-source.sh"
30+
2831

2932
# Colors for output (true color)
3033
RED='\033[38;2;255;110;110m'
@@ -99,13 +102,34 @@ command -v systemctl >/dev/null 2>&1 || error "systemctl is required but not ins
99102
command -v setcap >/dev/null 2>&1 || error "setcap is required but not installed (install libcap2-bin)"
100103
command -v openssl >/dev/null 2>&1 || error "openssl is required but not installed"
101104

105+
# Branch and binary_dir are mutually exclusive
106+
if [ -n "$BRANCH" ] && [ -n "$BINARY_DIR" ]; then
107+
error "BRANCH and BINARY_DIR are mutually exclusive"
108+
fi
109+
102110
# Additional checks for build-from-source mode
103111
if [ -n "$BRANCH" ]; then
104112
command -v git >/dev/null 2>&1 || error "git is required for BRANCH mode but not installed"
105113
command -v go >/dev/null 2>&1 || error "go is required for BRANCH mode but not installed"
106114
command -v make >/dev/null 2>&1 || error "make is required for BRANCH mode but not installed"
107115
fi
108116

117+
# Additional checks for BINARY_DIR mode
118+
if [ -n "$BINARY_DIR" ]; then
119+
if [ ! -d "$BINARY_DIR" ]; then
120+
error "BINARY_DIR does not exist: ${BINARY_DIR}"
121+
fi
122+
if [ ! -f "${BINARY_DIR}/${BINARY_NAME}" ]; then
123+
error "Required binary not found: ${BINARY_DIR}/${BINARY_NAME}"
124+
fi
125+
if [ ! -f "${BINARY_DIR}/hypeman-token" ]; then
126+
error "Required binary not found: ${BINARY_DIR}/hypeman-token"
127+
fi
128+
if [ ! -f "${BINARY_DIR}/.env.example" ]; then
129+
error "Required file not found: ${BINARY_DIR}/.env.example"
130+
fi
131+
fi
132+
109133
# Detect OS
110134
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
111135
if [ "$OS" != "linux" ]; then
@@ -184,48 +208,34 @@ TMP_DIR=$(mktemp -d)
184208
trap "rm -rf $TMP_DIR" EXIT
185209

186210
# =============================================================================
187-
# Get binaries (either download release or build from source)
211+
# Get binaries (either use BINARY_DIR, download release, or build from source)
188212
# =============================================================================
189213

190-
if [ -n "$BRANCH" ]; then
214+
if [ -n "$BINARY_DIR" ]; then
215+
# Use binaries from specified directory
216+
info "Using binaries from ${BINARY_DIR}..."
217+
218+
# Copy binaries to TMP_DIR
219+
info "Copying binaries from ${BINARY_DIR}..."
220+
cp "${BINARY_DIR}/${BINARY_NAME}" "${TMP_DIR}/${BINARY_NAME}"
221+
cp "${BINARY_DIR}/hypeman-token" "${TMP_DIR}/hypeman-token"
222+
cp "${BINARY_DIR}/.env.example" "${TMP_DIR}/.env.example"
223+
224+
# Make binaries executable
225+
chmod +x "${TMP_DIR}/${BINARY_NAME}"
226+
chmod +x "${TMP_DIR}/hypeman-token"
227+
228+
VERSION="custom (from ${BINARY_DIR})"
229+
elif [ -n "$BRANCH" ]; then
191230
# Build from source mode
192231
info "Building from source (branch: $BRANCH)..."
193232

194-
BUILD_DIR="${TMP_DIR}/hypeman"
195-
BUILD_LOG="${TMP_DIR}/build.log"
196-
197-
# Clone repo (quiet)
198-
if ! git clone --branch "$BRANCH" --depth 1 -q "https://github.com/${REPO}.git" "$BUILD_DIR" 2>&1 | tee -a "$BUILD_LOG"; then
199-
error "Failed to clone repository. Build log:\n$(cat "$BUILD_LOG")"
200-
fi
201-
202-
info "Building binaries (this may take a few minutes)..."
203-
cd "$BUILD_DIR"
204-
205-
# Build main binary (includes dependencies) - capture output, show on error
206-
if ! make build >> "$BUILD_LOG" 2>&1; then
207-
echo ""
208-
echo -e "${RED}Build failed. Full build log:${NC}"
209-
cat "$BUILD_LOG"
210-
error "Build failed"
211-
fi
212-
cp "bin/hypeman" "${TMP_DIR}/${BINARY_NAME}"
213-
214-
# Build hypeman-token (not included in make build)
215-
if ! go build -o "${TMP_DIR}/hypeman-token" ./cmd/gen-jwt >> "$BUILD_LOG" 2>&1; then
216-
echo ""
217-
echo -e "${RED}Build failed. Full build log:${NC}"
218-
cat "$BUILD_LOG"
219-
error "Failed to build hypeman-token"
233+
# Run build script
234+
if ! "OUTPUT_DIR=$TMP_DIR $BUILD_SCRIPT"; then
235+
error "Build from source failed"
220236
fi
221237

222-
# Copy .env.example for config template
223-
cp ".env.example" "${TMP_DIR}/.env.example"
224-
225238
VERSION="$BRANCH (source)"
226-
cd - > /dev/null
227-
228-
info "Build complete"
229239
else
230240
# Download release mode
231241
if [ -z "$VERSION" ]; then

0 commit comments

Comments
 (0)