|
#!/usr/bin/env bash |
|
# This is just Laravel sail (docker) specific, don't worry about it |
|
export WWWGROUP="${WWWGROUP:-$(id -g)}" |
|
export WWWUSER="${WWWUSER:-$(id -u)}" |
|
|
|
# These variables should probably be configurable via the .env file, but they work for our set-up |
|
SERVICE="garage_s3" |
|
BUCKET="development" |
|
KEY_NAME="my-key" |
|
ZONE="dev" |
|
CAPACITY="4G" |
|
ENDPOINT="http://localhost:3900" |
|
CORS_CONFIG="$(dirname "$0")/cors.json" |
|
|
|
garage() { |
|
docker compose exec -e RUST_LOG=error "$SERVICE" /garage "$@" |
|
} |
|
|
|
if ! command -v aws &>/dev/null; then |
|
echo "Error: AWS CLI not found in PATH." |
|
echo "Install it via your package manager or https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html" |
|
echo "Then make sure it is available in your PATH and rerun this script." |
|
exit 1 |
|
fi |
|
|
|
# This feels slightly suboptimal, probably better to just exit and let the user try again rather than sleep. |
|
# But it works fine. |
|
echo "Waiting for Garage to start..." |
|
until garage status &>/dev/null; do |
|
sleep 1 |
|
done |
|
|
|
if garage bucket list 2>/dev/null | grep -q "$BUCKET"; then |
|
echo "Garage already initialized, skipping." |
|
echo "If you want to reconfigure garage: run, start the garage_s3 container and rerun the init script" |
|
echo " docker compose down -v garage_s3" |
|
echo " docker compose up -d garage_s3" |
|
echo " bash .docker/garage_s3/garage_init.sh" |
|
exit 0 |
|
fi |
|
|
|
echo "Initializing Garage..." |
|
|
|
# 1. Create layout |
|
echo "1. Creating a layout" |
|
NODE_ID=$(garage status 2>/dev/null | awk '/^[a-f0-9]/{print $1; exit}') |
|
echo "Node ID: $NODE_ID" |
|
garage layout assign -z "$ZONE" -c "$CAPACITY" "$NODE_ID" |
|
|
|
# 2. Apply the layout (first version is always 1) |
|
echo "2. Applying the layout" |
|
garage layout apply --version 1 |
|
|
|
# 3. Create bucket |
|
echo "3. Creating the 'development' bucket" |
|
garage bucket create "$BUCKET" |
|
|
|
# 4. Create API key and capture credentials |
|
echo "4. Creating an API key" |
|
KEY_OUTPUT=$(garage key create "$KEY_NAME") |
|
|
|
ACCESS_KEY=$(echo "$KEY_OUTPUT" | awk '/Key ID/{print $NF}') |
|
SECRET_KEY=$(echo "$KEY_OUTPUT" | awk '/Secret key/{print $NF}') |
|
|
|
# 5. Grant access |
|
echo "5. Granting the created API key access to the development bucket" |
|
garage bucket allow --read --write --owner "$BUCKET" --key "$KEY_NAME" |
|
|
|
echo "6. Configuring CORS on the '$BUCKET' bucket" |
|
AWS_ACCESS_KEY_ID="$ACCESS_KEY" \ |
|
AWS_SECRET_ACCESS_KEY="$SECRET_KEY" \ |
|
aws s3api put-bucket-cors \ |
|
--bucket "$BUCKET" \ |
|
--cors-configuration "file://$CORS_CONFIG" \ |
|
--endpoint-url "$ENDPOINT" \ |
|
--region garage |
|
|
|
echo "Garage initialized successfully!" |
|
echo "" |
|
echo " Bucket: $BUCKET" |
|
echo " Access Key: $ACCESS_KEY" |
|
echo " Secret Key: $SECRET_KEY" |