#!/bin/sh # One-time, idempotent setup of a fresh garage_meta volume, run on the host # (the image has no shell): node layout, bucket, the backend's key imported # from .env (so a wiped volume keeps the same credentials), and website access. # Optional: S3_BUCKET, GARAGE_CAPACITY, COMPOSE (to add the dev override). set -eu COMPOSE=${COMPOSE:-docker compose} # .env isn't guaranteed to be valid shell, so it isn't sourced. envval() { eval "current=\${$1:-}" if [ -n "$current" ]; then printf '%s' "$current" return fi [ -f .env ] || return 0 grep -E "^$1=" .env | tail -n 1 | cut -d= -f2- | sed -e 's/^"\(.*\)"$/\1/' -e "s/^'\(.*\)'$/\1/" } garage() { $COMPOSE exec -T garage /garage "$@" } KEY_ID=$(envval S3_ACCESS_KEY_ID) SECRET=$(envval S3_SECRET_ACCESS_KEY) BUCKET=$(envval S3_BUCKET) BUCKET=${BUCKET:-uploads} CAPACITY=$(envval GARAGE_CAPACITY) CAPACITY=${CAPACITY:-20G} KEY_NAME=site-backend # Garage rejects other shapes at import time with a less helpful message. if ! printf '%s' "$KEY_ID" | grep -Eq '^GK[0-9a-f]{24}$'; then echo "S3_ACCESS_KEY_ID must be GK followed by 24 hex chars (try: echo GK\$(openssl rand -hex 12))" >&2 exit 1 fi if ! printf '%s' "$SECRET" | grep -Eq '^[0-9a-f]{64}$'; then echo "S3_SECRET_ACCESS_KEY must be 64 hex chars (try: openssl rand -hex 32)" >&2 exit 1 fi # 1. Layout. `node id -q` prints "@"; the id is what # `layout assign` takes. if garage layout show 2>/dev/null | grep -q "No nodes currently have a role"; then NODE_ID=$(garage node id -q 2>/dev/null | cut -d@ -f1) echo "Assigning node $(printf '%s' "$NODE_ID" | cut -c1-16) (capacity $CAPACITY)" garage layout assign -z dc1 -c "$CAPACITY" "$NODE_ID" garage layout apply --version 1 else echo "Layout already applied" fi # 2. Bucket. if garage bucket info "$BUCKET" >/dev/null 2>&1; then echo "Bucket $BUCKET exists" else garage bucket create "$BUCKET" fi # 3. Key. if garage key info "$KEY_ID" >/dev/null 2>&1; then echo "Key $KEY_ID exists" else garage key import -n "$KEY_NAME" --yes "$KEY_ID" "$SECRET" fi # 4 and 5 are idempotent in Garage itself. garage bucket allow --read --write "$BUCKET" --key "$KEY_ID" garage bucket website --allow "$BUCKET" echo "Garage ready: bucket $BUCKET, key $KEY_ID"