Files
2026-09-14 19:58:31 +02:00

87 lines
3.1 KiB
Bash
Executable File

#!/bin/bash
set -Eeuo pipefail
# Installs artifacts uploaded by a Make deployment target. Run as root or as an
# administrator with passwordless sudo.
if [[ $(id -u) -ne 0 ]]; then
exec sudo -n "$0" "$@"
fi
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
APPLICATION_DIR=/opt/gardomatic
STATE_DIR=/var/lib/gardomatic
ENV_FILE=/etc/gardomatic/gardomatic.env
required_files=(
api
web
cli
api.service
web.service
demo-reset.service
demo-reset.timer
create-admin.sh
)
for file in "${required_files[@]}"; do
[[ -f "$SCRIPT_DIR/$file" ]] || {
printf 'Missing deployment artifact: %s\n' "$file" >&2
exit 1
}
done
[[ -d "$SCRIPT_DIR/migrations" ]] || {
printf 'Missing deployment artifact: migrations\n' >&2
exit 1
}
[[ -f "$ENV_FILE" ]] || {
printf 'Missing runtime configuration: %s; provision this deployment target first.\n' "$ENV_FILE" >&2
exit 1
}
id gardomatic >/dev/null 2>&1 || {
printf 'Missing service user gardomatic; provision this deployment target first.\n' >&2
exit 1
}
install -d -m 0755 -o root -g root "$APPLICATION_DIR"
install -d -m 0750 -o gardomatic -g gardomatic "$STATE_DIR"
install -m 0755 -o root -g root "$SCRIPT_DIR/api" /usr/local/bin/gardomatic-api
install -m 0755 -o root -g root "$SCRIPT_DIR/web" /usr/local/bin/gardomatic-web
install -m 0755 -o root -g root "$SCRIPT_DIR/cli" /usr/local/bin/gardomatic-cli
install -d -m 0755 -o root -g root "$APPLICATION_DIR/migrations"
rsync --archive --delete "$SCRIPT_DIR/migrations/" "$APPLICATION_DIR/migrations/"
install -m 0644 -o root -g root "$SCRIPT_DIR/api.service" /etc/systemd/system/api.service
install -m 0644 -o root -g root "$SCRIPT_DIR/web.service" /etc/systemd/system/web.service
install -m 0644 -o root -g root "$SCRIPT_DIR/demo-reset.service" /etc/systemd/system/gardomatic-demo-reset.service
install -m 0644 -o root -g root "$SCRIPT_DIR/demo-reset.timer" /etc/systemd/system/gardomatic-demo-reset.timer
install -m 0755 -o root -g root "$SCRIPT_DIR/create-admin.sh" /usr/local/sbin/gardomatic-create-admin
# provision-server.sh writes a file compatible with systemd and Bash. Loading it
# here keeps the database DSN out of command-line arguments and process listings.
set -a
# shellcheck disable=SC1091
source "$ENV_FILE"
set +a
migrate -path "$APPLICATION_DIR/migrations" -database "$GARDOMATIC_DB_DSN" up
systemctl daemon-reload
systemctl enable api web
if [[ "${GARDOMATIC_DEMO_RESET_ENABLED:-false}" == true ]]; then
[[ -n "${GARDOMATIC_DEMO_ACCOUNT_EMAIL:-}" ]] || {
printf 'Demo reset is enabled, but GARDOMATIC_DEMO_ACCOUNT_EMAIL is empty.\n' >&2
exit 1
}
[[ -f /etc/gardomatic/demo-password ]] || {
printf 'Demo reset is enabled, but /etc/gardomatic/demo-password is missing.\n' >&2
exit 1
}
chown root:gardomatic /etc/gardomatic/demo-password
chmod 0640 /etc/gardomatic/demo-password
systemctl enable --now gardomatic-demo-reset.timer
systemctl start gardomatic-demo-reset.service
else
systemctl disable --now gardomatic-demo-reset.timer >/dev/null 2>&1 || true
fi
systemctl restart api web
printf 'Gardomatic deployment complete.\n'