Skip to content

Commit b77bd93

Browse files
authored
feat: add script to build from source and support binary path with install.sh (#58)
* feat: add script to build from source and support binary path with install.sh * Address pr comments * validate OUTPUT_DIR is a full path
1 parent e77731d commit b77bd93

File tree

2 files changed

+128
-3
lines changed

2 files changed

+128
-3
lines changed

scripts/build-from-source.sh

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

scripts/install.sh

Lines changed: 41 additions & 3 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)
@@ -99,13 +100,30 @@ command -v systemctl >/dev/null 2>&1 || error "systemctl is required but not ins
99100
command -v setcap >/dev/null 2>&1 || error "setcap is required but not installed (install libcap2-bin)"
100101
command -v openssl >/dev/null 2>&1 || error "openssl is required but not installed"
101102

103+
# Count how many of BRANCH, VERSION, BINARY_DIR are set
104+
count=0
105+
[ -n "$BRANCH" ] && ((count++)) || true
106+
[ -n "$VERSION" ] && ((count++)) || true
107+
[ -n "$BINARY_DIR" ] && ((count++)) || true
108+
109+
if [ "$count" -gt 1 ]; then
110+
error "BRANCH, VERSION, and BINARY_DIR are mutually exclusive"
111+
fi
112+
102113
# Additional checks for build-from-source mode
103114
if [ -n "$BRANCH" ]; then
104115
command -v git >/dev/null 2>&1 || error "git is required for BRANCH mode but not installed"
105116
command -v go >/dev/null 2>&1 || error "go is required for BRANCH mode but not installed"
106117
command -v make >/dev/null 2>&1 || error "make is required for BRANCH mode but not installed"
107118
fi
108119

120+
# Additional checks for BINARY_DIR mode
121+
if [ -n "$BINARY_DIR" ]; then
122+
if [ ! -d "$BINARY_DIR" ]; then
123+
error "BINARY_DIR does not exist: ${BINARY_DIR}. Are you sure you provided the correct path?"
124+
fi
125+
fi
126+
109127
# Detect OS
110128
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
111129
if [ "$OS" != "linux" ]; then
@@ -184,10 +202,30 @@ TMP_DIR=$(mktemp -d)
184202
trap "rm -rf $TMP_DIR" EXIT
185203

186204
# =============================================================================
187-
# Get binaries (either download release or build from source)
205+
# Get binaries (either use BINARY_DIR, download release, or build from source)
188206
# =============================================================================
189207

190-
if [ -n "$BRANCH" ]; then
208+
if [ -n "$BINARY_DIR" ]; then
209+
# Use binaries from specified directory
210+
info "Using binaries from ${BINARY_DIR}..."
211+
212+
# Copy binaries to TMP_DIR
213+
info "Copying binaries from ${BINARY_DIR}..."
214+
215+
for f in "${BINARY_NAME}" "hypeman-token" ".env.example"; do
216+
[ -f "${BINARY_DIR}/${f}" ] || error "File ${f} not found in ${BINARY_DIR}"
217+
done
218+
219+
cp "${BINARY_DIR}/${BINARY_NAME}" "${TMP_DIR}/${BINARY_NAME}"
220+
cp "${BINARY_DIR}/hypeman-token" "${TMP_DIR}/hypeman-token"
221+
cp "${BINARY_DIR}/.env.example" "${TMP_DIR}/.env.example"
222+
223+
# Make binaries executable
224+
chmod +x "${TMP_DIR}/${BINARY_NAME}"
225+
chmod +x "${TMP_DIR}/hypeman-token"
226+
227+
VERSION="custom (from binary)"
228+
elif [ -n "$BRANCH" ]; then
191229
# Build from source mode
192230
info "Building from source (branch: $BRANCH)..."
193231

@@ -405,7 +443,7 @@ $SUDO systemctl start "$SERVICE_NAME"
405443

406444
CLI_REPO="kernel/hypeman-cli"
407445

408-
if [ -z "$CLI_VERSION" ]; then
446+
if [ -z "$CLI_VERSION" ] || [ "$CLI_VERSION" == "latest" ]; then
409447
info "Fetching latest CLI version with available artifacts..."
410448
CLI_VERSION=$(find_release_with_artifact "$CLI_REPO" "hypeman" "$OS" "$ARCH")
411449
if [ -z "$CLI_VERSION" ]; then

0 commit comments

Comments
 (0)