Files
archie/deployments/archie/deploy.sh
T

67 lines
2.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# Deploys the single binary's stack to a host over ssh: deploy.sh root@staging-host.
# The first run creates the secrets on the host; they never leave it.
set -euo pipefail
target=${1:?usage: deploy.sh user@host [bind-address:port]}
bind=${2:-}
here=$(cd "$(dirname "$0")" && pwd)
repo=$(cd "$here/../.." && pwd)
dir=/root/archie
version=$(git -C "$repo" describe --tags --always --dirty)
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -C "$repo" -trimpath \
-ldflags "-X github.com/samcharles93/archie-core/internal/buildinfo.Version=$version" \
-o "$here/.build/archie" ./cmd/archie
ssh "$target" "mkdir -p $dir/bin $dir/initdb $dir/extensions"
scp -q "$here/compose.yml" "$target:$dir/compose.yml"
scp -q "$here/seccomp.json" "$target:$dir/seccomp.json"
scp -q "$here/initdb/01-roles.sh" "$target:$dir/initdb/01-roles.sh"
scp -q "$here/.build/archie" "$target:$dir/bin/archie.new"
ssh "$target" BIND="$bind" DIR="$dir" bash -s <<'REMOTE'
set -euo pipefail
cd "$DIR"
chmod 755 initdb/01-roles.sh
mv bin/archie.new bin/archie && chmod 755 bin/archie
if [ ! -f .env ]; then
[ -n "$BIND" ] || { echo "first deploy needs a bind address, e.g. 10.0.0.1:8099" >&2; exit 1; }
umask 077
printf 'ARCHIE_OWNER_PASSWORD=%s\nARCHIE_APP_PASSWORD=%s\nARCHIE_BIND=%s\n' \
"$(openssl rand -hex 24)" "$(openssl rand -hex 24)" "$BIND" > .env
fi
if [ ! -f config.toml ]; then
set -a; . ./.env; set +a
umask 077
cat > config.toml <<CONFIG
listen = "0.0.0.0:8080"
[database]
url = "postgres://archie_app:${ARCHIE_APP_PASSWORD}@archie-db:5432/archie?sslmode=disable"
owner_url = "postgres://archie_owner:${ARCHIE_OWNER_PASSWORD}@archie-db:5432/archie?sslmode=disable"
CONFIG
chown 65532:65532 config.toml
fi
# The admin token guards /api/. Only its hash goes in the config, as a
# top-level key, so it must come before the [database] table.
if ! grep -q '^admin_token_sha256' config.toml; then
umask 077
openssl rand -hex 32 | tr -d '\n' > admin-token
hash=$(sha256sum admin-token | cut -d' ' -f1)
sed -i "1i admin_token_sha256 = \"$hash\"" config.toml
chown 65532:65532 config.toml
fi
# The age identity seals the credentials Archie keeps. It is made here and
# never leaves the host; back it up, or credentials die with it.
if ! grep -q '^age_identity' config.toml; then
umask 077
identity=$(bin/archie -new-age-identity)
sed -i "1i age_identity = \"$identity\"" config.toml
unset identity
chown 65532:65532 config.toml
fi
docker compose up -d --remove-orphans
docker compose up -d --force-recreate archie
REMOTE
echo "deployed $version to $target"