From b3d6d8ece95561455bba749d9475389041ed59c8 Mon Sep 17 00:00:00 2001 From: Tanmay Sardesai Date: Wed, 14 Jan 2026 20:14:16 +0000 Subject: [PATCH 1/3] feat: add script to build from source and support binary path with install.sh --- scripts/build-from-source.sh | 83 ++++++++++++++++++++++++++++++++++++ scripts/install.sh | 39 +++++++++++++++-- 2 files changed, 119 insertions(+), 3 deletions(-) create mode 100755 scripts/build-from-source.sh diff --git a/scripts/build-from-source.sh b/scripts/build-from-source.sh new file mode 100755 index 0000000..5e50a2b --- /dev/null +++ b/scripts/build-from-source.sh @@ -0,0 +1,83 @@ +#!/bin/bash +# +# Hypeman Build from Source Script +# +# Usage: +# ./scripts/build-from-source.sh +# +# Options (via environment variables: +# OUTPUT_DIR - Full path of directory to place built binaries (optional, default: $(pwd)/bin) +# + +set -e + +# Default values +BINARY_NAME="hypeman-api" + +# Colors for output (true color) +RED='\033[38;2;255;110;110m' +GREEN='\033[38;2;92;190;83m' +YELLOW='\033[0;33m' +PURPLE='\033[38;2;172;134;249m' +NC='\033[0m' # No Color + +info() { echo -e "${GREEN}[INFO]${NC} $1"; } +warn() { echo -e "${YELLOW}[WARN]${NC} $1"; } +error() { echo -e "${RED}[ERROR]${NC} $1"; exit 1; } + +# ============================================================================= +# Pre-flight checks - verify all requirements before doing anything +# ============================================================================= + +# Check for required commands +command -v go >/dev/null 2>&1 || error "go is required but not installed" +command -v make >/dev/null 2>&1 || error "make is required but not installed" + +# ============================================================================= +# Setup directories +# ============================================================================= + +# Get the directory where this script is located +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +SOURCE_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)" + +if [ -z "$OUTPUT_DIR" ]; then + OUTPUT_DIR=${SOURCE_DIR}/bin +fi + +# Create output directory if it doesn't exist +mkdir -p "$OUTPUT_DIR" + +BUILD_LOG="${OUTPUT_DIR}/build.log" + +# ============================================================================= +# Build from source +# ============================================================================= + +info "Building from source (${SOURCE_DIR})..." + +info "Building binaries (this may take a few minutes)..." +cd "${SOURCE_DIR}" + +# Build main binary (includes dependencies) - capture output, show on error +if ! make build >> "$BUILD_LOG" 2>&1; then + echo "" + echo -e "${RED}Build failed. Full build log:${NC}" + cat "$BUILD_LOG" + error "Build failed" +fi +cp "bin/hypeman" "${OUTPUT_DIR}/${BINARY_NAME}" + +# Build hypeman-token (not included in make build) +if ! go build -o "${OUTPUT_DIR}/hypeman-token" ./cmd/gen-jwt >> "$BUILD_LOG" 2>&1; then + echo "" + echo -e "${RED}Build failed. Full build log:${NC}" + cat "$BUILD_LOG" + error "Failed to build hypeman-token" +fi + +# Copy .env.example for config template +cp ".env.example" "${OUTPUT_DIR}/.env.example" + +info "Build complete" +info "Binaries are available in: ${OUTPUT_DIR}" diff --git a/scripts/install.sh b/scripts/install.sh index 41a540f..1ac0d12 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -9,6 +9,7 @@ # VERSION - Install specific API version (default: latest) # CLI_VERSION - Install specific CLI version (default: latest) # BRANCH - Build from source using this branch (for development/testing) +# BINARY_DIR - Use binaries from this directory instead of building/downloading # INSTALL_DIR - Binary installation directory (default: /opt/hypeman/bin) # DATA_DIR - Data directory (default: /var/lib/hypeman) # CONFIG_DIR - Config directory (default: /etc/hypeman) @@ -99,6 +100,16 @@ command -v systemctl >/dev/null 2>&1 || error "systemctl is required but not ins command -v setcap >/dev/null 2>&1 || error "setcap is required but not installed (install libcap2-bin)" command -v openssl >/dev/null 2>&1 || error "openssl is required but not installed" +# Count how many of BRANCH, VERSION, BINARY_DIR are set +count=0 +[ -n "$BRANCH" ] && ((count++)) || true +[ -n "$VERSION" ] && ((count++)) || true +[ -n "$BINARY_DIR" ] && ((count++)) || true + +if [ "$count" -gt 1 ]; then + error "BRANCH, VERSION, and BINARY_DIR are mutually exclusive" +fi + # Additional checks for build-from-source mode if [ -n "$BRANCH" ]; then command -v git >/dev/null 2>&1 || error "git is required for BRANCH mode but not installed" @@ -106,6 +117,13 @@ if [ -n "$BRANCH" ]; then command -v make >/dev/null 2>&1 || error "make is required for BRANCH mode but not installed" fi +# Additional checks for BINARY_DIR mode +if [ -n "$BINARY_DIR" ]; then + if [ ! -d "$BINARY_DIR" ]; then + error "BINARY_DIR does not exist: ${BINARY_DIR}. Are you sure you provided the correct path?" + fi +fi + # Detect OS OS=$(uname -s | tr '[:upper:]' '[:lower:]') if [ "$OS" != "linux" ]; then @@ -184,10 +202,25 @@ TMP_DIR=$(mktemp -d) trap "rm -rf $TMP_DIR" EXIT # ============================================================================= -# Get binaries (either download release or build from source) +# Get binaries (either use BINARY_DIR, download release, or build from source) # ============================================================================= -if [ -n "$BRANCH" ]; then +if [ -n "$BINARY_DIR" ]; then + # Use binaries from specified directory + info "Using binaries from ${BINARY_DIR}..." + + # Copy binaries to TMP_DIR + info "Copying binaries from ${BINARY_DIR}..." + cp "${BINARY_DIR}/${BINARY_NAME}" "${TMP_DIR}/${BINARY_NAME}" + cp "${BINARY_DIR}/hypeman-token" "${TMP_DIR}/hypeman-token" + cp "${BINARY_DIR}/.env.example" "${TMP_DIR}/.env.example" + + # Make binaries executable + chmod +x "${TMP_DIR}/${BINARY_NAME}" + chmod +x "${TMP_DIR}/hypeman-token" + + VERSION="custom (from binary)" +elif [ -n "$BRANCH" ]; then # Build from source mode info "Building from source (branch: $BRANCH)..." @@ -405,7 +438,7 @@ $SUDO systemctl start "$SERVICE_NAME" CLI_REPO="kernel/hypeman-cli" -if [ -z "$CLI_VERSION" ]; then +if [ -z "$CLI_VERSION" ] || [ "$CLI_VERSION" == "latest" ]; then info "Fetching latest CLI version with available artifacts..." CLI_VERSION=$(find_release_with_artifact "$CLI_REPO" "hypeman" "$OS" "$ARCH") if [ -z "$CLI_VERSION" ]; then From 21538c670062163d4dd894d6b09a85e18d49b337 Mon Sep 17 00:00:00 2001 From: Tanmay Sardesai Date: Thu, 15 Jan 2026 21:29:19 +0000 Subject: [PATCH 2/3] Address pr comments --- scripts/build-from-source.sh | 11 +++++------ scripts/install.sh | 5 +++++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/scripts/build-from-source.sh b/scripts/build-from-source.sh index 5e50a2b..fac0567 100755 --- a/scripts/build-from-source.sh +++ b/scripts/build-from-source.sh @@ -5,11 +5,11 @@ # Usage: # ./scripts/build-from-source.sh # -# Options (via environment variables: -# OUTPUT_DIR - Full path of directory to place built binaries (optional, default: $(pwd)/bin) +# Options (via environment variables): +# OUTPUT_DIR - Full path of directory to place built binaries (optional, default: repo's root bin directory) # -set -e +set -euo pipefail # Default values BINARY_NAME="hypeman-api" @@ -41,14 +41,13 @@ command -v make >/dev/null 2>&1 || error "make is required but not installed" SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" SOURCE_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)" -if [ -z "$OUTPUT_DIR" ]; then - OUTPUT_DIR=${SOURCE_DIR}/bin -fi +: "${OUTPUT_DIR:=${SOURCE_DIR}/bin}" # Create output directory if it doesn't exist mkdir -p "$OUTPUT_DIR" BUILD_LOG="${OUTPUT_DIR}/build.log" +: > "$BUILD_LOG" # ============================================================================= # Build from source diff --git a/scripts/install.sh b/scripts/install.sh index 1ac0d12..4e50aa2 100755 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -211,6 +211,11 @@ if [ -n "$BINARY_DIR" ]; then # Copy binaries to TMP_DIR info "Copying binaries from ${BINARY_DIR}..." + + for f in "${BINARY_NAME}" "hypeman-token" ".env.example"; do + [ -f "${BINARY_DIR}/${f}" ] || error "File ${f} not found in ${BINARY_DIR}" + done + cp "${BINARY_DIR}/${BINARY_NAME}" "${TMP_DIR}/${BINARY_NAME}" cp "${BINARY_DIR}/hypeman-token" "${TMP_DIR}/hypeman-token" cp "${BINARY_DIR}/.env.example" "${TMP_DIR}/.env.example" From ebef7e87aee0761bbc3cc5cd66cf9ec543011758 Mon Sep 17 00:00:00 2001 From: Tanmay Sardesai Date: Thu, 15 Jan 2026 22:23:34 +0000 Subject: [PATCH 3/3] validate OUTPUT_DIR is a full path --- scripts/build-from-source.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/scripts/build-from-source.sh b/scripts/build-from-source.sh index fac0567..ecfe28c 100755 --- a/scripts/build-from-source.sh +++ b/scripts/build-from-source.sh @@ -43,6 +43,11 @@ SOURCE_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)" : "${OUTPUT_DIR:=${SOURCE_DIR}/bin}" +# Validate OUTPUT_DIR is an absolute path +if [[ "$OUTPUT_DIR" != /* ]]; then + error "OUTPUT_DIR must be an absolute path (got: ${OUTPUT_DIR})" +fi + # Create output directory if it doesn't exist mkdir -p "$OUTPUT_DIR"