Trying
soaricarus_api CI / Rustfmt (pull_request) Successful in 2m21s
soaricarus_api CI / Test Suite (pull_request) Successful in 3m55s
soaricarus_api CI / Check (pull_request) Successful in 4m15s
soaricarus_api CI / Clippy (pull_request) Successful in 4m22s
soaricarus_api CI / build (pull_request) Successful in 3m48s
soaricarus_api CI / Rustfmt (pull_request) Successful in 2m21s
soaricarus_api CI / Test Suite (pull_request) Successful in 3m55s
soaricarus_api CI / Check (pull_request) Successful in 4m15s
soaricarus_api CI / Clippy (pull_request) Successful in 4m22s
soaricarus_api CI / build (pull_request) Successful in 3m48s
This commit is contained in:
+89
-112
@@ -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 "$@"
|
||||
|
||||
Reference in New Issue
Block a user