nom-nom-nix-gc/run-dev-server

112 lines
3.1 KiB
Bash
Executable File

#!/usr/bin/env bash
set -eau
dbname="nomnomdev"
port="12345"
dbdir="$(mktemp -d)"
garagedir="$(mktemp -d)"
garageaddr="[::1]:3900"
garagebucket="nix-cache"
cfgfile="${dbdir}/config.json"
trap 'rm -rf ${dbdir}' EXIT
initdb "$dbdir"
postgres -D "${dbdir}" -c unix_socket_directories="${dbdir}" -c listen_addresses= -c port="${port}" > /dev/null &
pgpid=$!
# Trick to help the "./psql" script to find the DB dir & co
cat <<EOF > "/tmp/nom-nom-dev-args"
export host="$dbdir"
export port="$port"
export dbname="$dbname"
export cfgfile="$cfgfile"
EOF
# Garage Directory
cat <<EOF > "$garagedir/config.toml"
metadata_dir = "$garagedir/meta"
data_dir = "$garagedir/data"
block_size = 1048576
replication_mode = "1"
compression_level = 1
rpc_secret = "4425f5c26c5e11581d3223904324dcb5b5d5dfb14e5e7f35e38c595424f5f1e6"
rpc_bind_addr = "[::]:3901"
rpc_public_addr = "[::]:3901"
bootstrap_peers = [
]
consul_host = "consul.service"
consul_service_name = "garage-daemon"
sled_cache_capacity = 134217728
sled_flush_every_ms = 2000
[s3_api]
api_bind_addr = "${garageaddr}"
s3_region = "garage"
root_domain = ".s3.garage"
[s3_web]
bind_addr = "[::]:3902"
root_domain = ".web.garage"
index = "index.html"
EOF
garage -c "$garagedir/config.toml" server &
garagepid=$!
trap 'rm -rf ${dbdir} && rm -rf ${garagedir} && rm /tmp/nom-nom-dev-args && kill ${pgpid} && kill ${garagepid}' EXIT
# Yeah, this is very meh. We need to wait for the server to be ready
# to receive requests to create the DB.
sleep 2
createdb -h "${dbdir}" -p "${port}" "${dbname}"
garage -c "$garagedir/config.toml" status
garagenodeid=$(garage -c "$garagedir/config.toml" node id | cut -f 1 -d '@')
garage -c "$garagedir/config.toml" layout assign "$garagenodeid" -c 500MB -z zone
garage -c "$garagedir/config.toml" layout show
garage -c "$garagedir/config.toml" layout apply --version 1
garage -c "$garagedir/config.toml" status
garage -c "$garagedir/config.toml" bucket create "${garagebucket}"
garage -c "$garagedir/config.toml" key create nomnom-key
garage -c "$garagedir/config.toml" bucket allow --read --write --owner "${garagebucket}" --key nomnom-key
garagekeyid=$(garage -c "$garagedir/config.toml" key info nomnom-key | grep "Key ID" | cut -f3 -d " ")
garagekeysecret=$(garage -c "$garagedir/config.toml" key info --show-secret nomnom-key | grep "Secret key" | cut -f3 -d " ")
access_key_filepath=$(mktemp)
echo "${garagekeyid}" > "${access_key_filepath}"
secret_key_filepath=$(mktemp)
echo "${garagekeysecret}" > "${secret_key_filepath}"
cat <<EOF > "${cfgfile}"
{
"url": "http://localhost:8001",
"db_host": "${dbdir}",
"db_port": ${port},
"db_name": "${dbname}",
"s3_endpoint": "http://${garageaddr}",
"s3_region": "garage",
"s3_bucket": "${garagebucket}",
"s3_access_key_filepath": "${access_key_filepath}",
"s3_secret_key_filepath": "${secret_key_filepath}"
}
EOF
# Check if there's a dump to seed the DB.
if [ -f dump.sql ]; then
echo ""
echo "[+] Seeding the DB with dump.sql"
echo ""
./psql -f dump.sql
fi
RUST_BACKTRACE=1 cargo run --bin nom-nom-gc-server -- --bind "[::1]:8001" --config "${cfgfile}"