AvanzaCast/deploy/deploy_prod.sh

92 lines
2.4 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
COMPOSE_FILE="$REPO_ROOT/docker-compose.prod.yml"
SERVICE_UNIT="/etc/systemd/system/avanzacast-stack.service"
LOCAL_UNIT_FILE="$REPO_ROOT/deploy/avanzacast-stack.service"
usage() {
cat <<EOF
Usage: $0 [options]
Options:
--build-only Build images with docker compose and exit
--up docker compose up -d (builds first)
--down docker compose down
--install-unit Install systemd unit (requires sudo)
--enable-start Enable & start systemd unit after installation (requires sudo)
--remove-unit Stop, disable and remove systemd unit (requires sudo)
--help Show this help
Examples:
$0 --build-only
sudo $0 --install-unit --enable-start
$0 --up
EOF
}
if [ "$#" -eq 0 ]; then
usage
exit 0
fi
ensure_docker() {
if ! command -v docker >/dev/null 2>&1; then
echo "docker not found; please install Docker on this host" >&2
exit 1
fi
}
build_images() {
echo "[deploy] Building images with docker compose..."
docker compose -f "$COMPOSE_FILE" build --pull
}
compose_up() {
echo "[deploy] Bringing up compose stack..."
docker compose -f "$COMPOSE_FILE" up -d --build
}
compose_down() {
echo "[deploy] Bringing down compose stack..."
docker compose -f "$COMPOSE_FILE" down
}
install_unit() {
if [ ! -f "$LOCAL_UNIT_FILE" ]; then
echo "unit file $LOCAL_UNIT_FILE not found" >&2
exit 1
fi
echo "[deploy] Installing systemd unit to $SERVICE_UNIT (requires sudo)"
sudo cp "$LOCAL_UNIT_FILE" "$SERVICE_UNIT"
sudo systemctl daemon-reload
}
enable_start_unit() {
echo "[deploy] Enabling and starting avanzacast-stack.service"
sudo systemctl enable --now avancacast-stack.service
}
remove_unit() {
echo "[deploy] Stopping and removing avanzacast-stack.service"
sudo systemctl stop avancacast-stack.service || true
sudo systemctl disable avancacast-stack.service || true
sudo rm -f "$SERVICE_UNIT"
sudo systemctl daemon-reload
}
while [ "$#" -gt 0 ]; do
case "$1" in
--build-only) ensure_docker; build_images; shift ;;
--up) ensure_docker; compose_up; shift ;;
--down) ensure_docker; compose_down; shift ;;
--install-unit) install_unit; shift ;;
--enable-start) enable_start_unit; shift ;;
--remove-unit) remove_unit; shift ;;
--help) usage; exit 0 ;;
*) echo "Unknown option: $1" >&2; usage; exit 1 ;;
esac
done