From e2a5e02aada4bc972fccfe9ddfeb24b364af4e05 Mon Sep 17 00:00:00 2001 From: phoenix Date: Fri, 24 Jul 2026 19:12:19 -0400 Subject: [PATCH] Trying --- docker-compose.yaml | 12 +-- garage/Dockerfile | 29 +++---- garage/entrypoint.sh | 201 +++++++++++++++++++------------------------ 3 files changed, 109 insertions(+), 133 deletions(-) diff --git a/docker-compose.yaml b/docker-compose.yaml index a91c80a..9960eaf 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -60,7 +60,9 @@ services: - soaricarus-network # --- storage service --- maze: - image: dxflrs/garage:v2.3.0 + build: + context: ./garage + dockerfile: Dockerfile container_name: maze restart: unless-stopped ports: @@ -81,13 +83,13 @@ services: - AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY} - GARAGE_RPC_SECRET=${GARAGE_RPC_SECRET} - BUCKET_NAME=${S3_BUCKET_NAME} - entrypoint: ["/entrypoint.sh"] - command: [] + # entrypoint: ["/entrypoint.sh"] + # command: [] healthcheck: - # test: ["CMD-SHELL", "curl -f http://localhost:3901/v0/status || exit 1"] + test: ["CMD-SHELL", "curl -f http://localhost:3901/v0/status || exit 1"] # test: ["CMD-SHELL", "echo 'What is to be done?'"] # test: ["CMD-SHELL", "nc -z localhost 3900 || exit 1"] - test: ["CMD-SHELL", "/garage status | grep -q 'Health: ok' || exit 1"] + # test: ["CMD-SHELL", "/garage status | grep -q 'Health: ok' || exit 1"] interval: 10s timeout: 5s retries: 5 diff --git a/garage/Dockerfile b/garage/Dockerfile index a0a6e15..5f4acaa 100644 --- a/garage/Dockerfile +++ b/garage/Dockerfile @@ -1,21 +1,18 @@ -# FROM dxflrs/garage:v2.3.0 -# Stage 1: Extract the garage binary from the official image -FROM dxflrs/garage:v2.3.0 as garage-base - -# Stage 2: Create a new image with a shell +# Use a base image with a shell (Alpine) FROM alpine:latest -# Install necessary tools (curl, busybox for sh, etc.) -# The Garage image is based on Alpine, so we use apk -RUN apk add --no-cache curl busybox +# Install necessary tools +RUN apk add --no-cache curl -# Copy the garage binary from the official image -COPY --from=garage-base /garage /usr/local/bin/garage +# Copy the Garage binary from the official image +COPY --from=dxflrs/garage:v2.3.0 /garage /usr/local/bin/garage -# Copy the init script -COPY init-garage.sh /init-garage.sh -RUN chmod +x /init-garage.sh +# Copy the entrypoint script +COPY entrypoint.sh /entrypoint.sh +RUN chmod +x /entrypoint.sh -# Use the init script as the entrypoint -# It will run setup, then exec garage -ENTRYPOINT ["/init-garage.sh"] +# Set the entrypoint +ENTRYPOINT ["/entrypoint.sh"] + +# Pass through any arguments to Garage +CMD [] diff --git a/garage/entrypoint.sh b/garage/entrypoint.sh index a614ab2..5273235 100755 --- a/garage/entrypoint.sh +++ b/garage/entrypoint.sh @@ -1,126 +1,103 @@ -#!/garage -# This script runs as a garage command -# It will check if setup is needed and run initialization +#!/bin/sh +set -e -# Check if we should run setup (first run) -SETUP_MARKER="/var/lib/garage/.setup_done" BUCKET_NAME="${BUCKET_NAME:-soaricarus-storage}" CREDENTIALS_FILE="/shared/garage-credentials.env" +SETUP_DONE="/shared/setup.done" +GARAGE_BIN="/usr/local/bin/garage" -# Function to check if Garage API is ready -is_garage_ready() { - # Try to get status, if it works, Garage is ready - /garage status 2>/dev/null | grep -q "Health: ok" - return $? -} - -# Function to wait for Garage to be ready -wait_for_garage() { - echo "âŗ Waiting for Garage to be ready..." - local max_attempts=30 - local attempt=0 - while [ $attempt -lt $max_attempts ]; do - if is_garage_ready; then - echo "✅ Garage is ready!" - return 0 - fi - attempt=$((attempt + 1)) - echo " Waiting... (attempt $attempt/$max_attempts)" - sleep 2 - done - echo "❌ Garage failed to become ready" - return 1 -} - -# Function to run setup -run_setup() { - echo "🔧 Running initial setup..." - - # Get node ID - NODE_ID=$(/garage node id | head -n1) - echo " Node ID: $NODE_ID" - - # Apply layout - /garage layout assign --version 1 "$NODE_ID" - /garage layout apply --version 1 - echo " ✅ Layout applied" - sleep 3 - - # Create bucket - if ! /garage bucket info "$BUCKET_NAME" 2>/dev/null; then - /garage bucket create "$BUCKET_NAME" - echo " ✅ Bucket created: $BUCKET_NAME" - else - echo " â„šī¸ Bucket already exists" - fi - - # Handle key - if [ -n "$AWS_ACCESS_KEY_ID" ] && [ -n "$AWS_SECRET_ACCESS_KEY" ]; then - # Import existing key - if ! /garage key info "$AWS_ACCESS_KEY_ID" 2>/dev/null; then - /garage key import "$AWS_ACCESS_KEY_ID" "$AWS_SECRET_ACCESS_KEY" "app-key" - echo " ✅ Key imported: $AWS_ACCESS_KEY_ID" - else - echo " â„šī¸ Key already exists: $AWS_ACCESS_KEY_ID" - fi - ACCESS_KEY="$AWS_ACCESS_KEY_ID" - SECRET_KEY="$AWS_SECRET_ACCESS_KEY" - else - # Generate new key - KEY_OUTPUT=$(/garage key create --name "app-key") - echo "$KEY_OUTPUT" - ACCESS_KEY=$(echo "$KEY_OUTPUT" | grep 'Key ID:' | awk '{print $3}') - SECRET_KEY=$(echo "$KEY_OUTPUT" | grep 'Secret key:' | awk '{print $3}') - echo " ✅ Key generated: $ACCESS_KEY" - fi - - # Grant permissions - /garage bucket allow --read --write --owner "$BUCKET_NAME" --key "$ACCESS_KEY" - echo " ✅ Permissions granted" - - # Write credentials to shared volume +# Function to write credentials +write_credentials() { + local access_key="$1" + local secret_key="$2" mkdir -p "$(dirname "$CREDENTIALS_FILE")" cat > "$CREDENTIALS_FILE" << EOF -GARAGE_ACCESS_KEY_ID=$ACCESS_KEY -GARAGE_SECRET_ACCESS_KEY=$SECRET_KEY +GARAGE_ACCESS_KEY_ID=$access_key +GARAGE_SECRET_ACCESS_KEY=$secret_key GARAGE_BUCKET_NAME=$BUCKET_NAME -GARAGE_ENDPOINT=http://maze:3900 +GARAGE_ENDPOINT=http://localhost:3900 EOF - echo " ✅ Credentials saved to $CREDENTIALS_FILE" - - # Mark setup as done - touch "$SETUP_MARKER" - echo "🎉 Setup complete!" + touch "$SETUP_DONE" + echo "✅ Credentials saved to $CREDENTIALS_FILE" } -# Main execution - -# Check if we need to run setup -if [ ! -f "$SETUP_MARKER" ]; then - echo "đŸ“Ļ First run detected. Starting Garage for setup..." - - # Start Garage in background - /garage "$@" & - GARAGE_PID=$! - - # Wait for it to be ready - if wait_for_garage; then - # Run the setup - run_setup - else - echo "❌ Setup failed - Garage didn't become ready" - kill "$GARAGE_PID" 2>/dev/null - exit 1 - fi - - # Kill the background Garage - echo "âšī¸ Stopping background Garage..." - kill "$GARAGE_PID" - wait "$GARAGE_PID" 2>/dev/null || true - echo "🔄 Restarting Garage in foreground..." -else +# Check if setup already completed +if [ -f "$SETUP_DONE" ]; then echo "â„šī¸ Setup already completed. Starting Garage..." + exec $GARAGE_BIN "$@" fi +echo "🔧 First-time setup - initializing Garage..." + +# Start Garage in background for setup (just the binary, no daemon subcommand) +echo "âŗ Starting Garage in background..." +$GARAGE_BIN "$@" & +GARAGE_PID=$! + +# Wait for Garage to be ready +echo "âŗ Waiting for Garage API..." +until curl -s -f "http://localhost:3901/v0/status" > /dev/null 2>&1; do + echo " Not ready yet, sleeping 2s..." + sleep 2 +done +echo "✅ Garage API is responding!" + +# Apply cluster layout +if ! $GARAGE_BIN layout show 2>/dev/null | grep -q "Current cluster layout"; then + echo "đŸ“Ļ Applying cluster layout..." + NODE_ID=$($GARAGE_BIN node id | head -n1) + $GARAGE_BIN layout assign --version 1 "$NODE_ID" + $GARAGE_BIN layout apply --version 1 + echo " ✅ Layout applied" + sleep 2 +else + echo "â„šī¸ Layout already applied" +fi + +# Create bucket +if ! $GARAGE_BIN bucket info "$BUCKET_NAME" 2>/dev/null; then + echo "đŸ“Ļ Creating bucket: $BUCKET_NAME" + $GARAGE_BIN bucket create "$BUCKET_NAME" +else + echo "â„šī¸ Bucket $BUCKET_NAME already exists" +fi + +# Handle key +if [ -n "$AWS_ACCESS_KEY_ID" ] && [ -n "$AWS_SECRET_ACCESS_KEY" ]; then + echo "🔑 Using pre-defined credentials from .env" + if ! $GARAGE_BIN key info "$AWS_ACCESS_KEY_ID" 2>/dev/null; then + $GARAGE_BIN key import "$AWS_ACCESS_KEY_ID" "$AWS_SECRET_ACCESS_KEY" "app-key" + echo " ✅ Key imported: $AWS_ACCESS_KEY_ID" + else + echo " â„šī¸ Key already exists: $AWS_ACCESS_KEY_ID" + fi + ACCESS_KEY="$AWS_ACCESS_KEY_ID" + SECRET_KEY="$AWS_SECRET_ACCESS_KEY" +else + echo "🔑 Generating new key..." + KEY_OUTPUT=$($GARAGE_BIN key create --name "app-key") + echo "$KEY_OUTPUT" + ACCESS_KEY=$(echo "$KEY_OUTPUT" | grep 'Key ID:' | awk '{print $3}') + SECRET_KEY=$(echo "$KEY_OUTPUT" | grep 'Secret key:' | awk '{print $3}') + echo " ✅ Key generated: $ACCESS_KEY" +fi + +# Grant permissions +$GARAGE_BIN bucket allow --read --write --owner "$BUCKET_NAME" --key "$ACCESS_KEY" +echo " ✅ Permissions granted" + +# Save credentials +write_credentials "$ACCESS_KEY" "$SECRET_KEY" + +echo "🎉 Setup complete!" +echo " Bucket: $BUCKET_NAME" +echo " Access Key: $ACCESS_KEY" + +# Stop background Garage +echo "âšī¸ Stopping background Garage..." +kill "$GARAGE_PID" +wait "$GARAGE_PID" 2>/dev/null || true + # Start Garage in foreground -exec /garage "$@" +echo "🚀 Starting Garage in foreground..." +exec $GARAGE_BIN "$@"