Files
deployment-scripts/deploy-docker-traefik.sh

141 lines
5.9 KiB
Bash
Executable File

#!/usr/bin/env bash
# ==============================================================================
# Standalone Traefik Deployment
# ==============================================================================
# Deploys the shared Traefik reverse proxy at /opt/traefik/.
# Usually auto-bootstrapped by the first service deploy script — this script
# is only needed if you want to set up infrastructure ahead of time.
#
# Usage: deploy-docker-traefik.sh
# deploy-docker-traefik.sh --remove # Stop, keep certs + configs
# deploy-docker-traefik.sh --remove --purge # Delete everything
# ==============================================================================
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${SCRIPT_DIR}/common.sh"
# --- Parse arguments ---
parse_args "$@"
SERVICE_NAME="traefik"
BASE_DIR="$TRAEFIK_DIR"
UNIT_NAME="traefik-docker"
# --- Handle --remove ---
if [[ "$ARG_REMOVE" == "1" ]]; then
require_root
echo ""
echo -e "${YELLOW}══════════════════════════════════════════════${NC}"
echo -e "${YELLOW} Removing Traefik${NC}"
echo -e "${YELLOW}══════════════════════════════════════════════${NC}"
if [[ "$ARG_PURGE" == "1" ]]; then
echo -e " Mode: ${RED}Purge (delete certs + configs)${NC}"
else
echo -e " Mode: Safe (keep acme.json + dynamic/)"
fi
echo -e " Data: ${BASE_DIR}"
echo -e " Systemd: ${UNIT_NAME}.service"
echo -e "${YELLOW}══════════════════════════════════════════════${NC}"
echo ""
warn "Removing Traefik will break ALL services that depend on it."
if [[ "$ARG_YES" != "1" ]]; then
read -rp "Continue? [y/N] " confirm
if [[ "${confirm,,}" != "y" ]]; then
info "Cancelled."
exit 0
fi
fi
# Stop traefik
if [[ -d "$BASE_DIR" ]] && [[ -f "${BASE_DIR}/docker-compose.yml" ]]; then
(cd "$BASE_DIR" && docker compose down) || true
ok "Traefik stopped."
fi
remove_systemd_unit "$UNIT_NAME"
if [[ "$ARG_PURGE" == "1" ]]; then
warn "This will delete ALL Traefik data including certificates and dynamic configs."
if [[ "$ARG_YES" != "1" ]]; then
read -rp "Permanently delete ${BASE_DIR}? [y/N] " confirm
if [[ "${confirm,,}" != "y" ]]; then
info "Purge cancelled."
exit 0
fi
fi
rm -rf "$BASE_DIR"
ok "Purged: $BASE_DIR"
else
rm -f "${BASE_DIR}/docker-compose.yml"
ok "Removed compose file. Preserved acme.json and dynamic/ configs."
fi
echo ""
echo -e "${GREEN}══════════════════════════════════════════════${NC}"
echo -e "${GREEN} Traefik removed${NC}"
echo -e "${GREEN}══════════════════════════════════════════════${NC}"
if [[ "$ARG_PURGE" != "1" ]]; then
echo -e " Certs kept: ${BASE_DIR}/acme.json"
echo -e " Configs: ${TRAEFIK_DYNAMIC_DIR}/"
fi
echo -e " To redeploy: $0"
echo -e "${GREEN}══════════════════════════════════════════════${NC}"
echo ""
exit 0
fi
# --- Detect current state for banner ---
traefik_status="Not deployed"
if docker ps --format '{{.Names}}' 2>/dev/null | grep -q '^traefik$'; then
traefik_status="Running"
elif [[ -f "${TRAEFIK_DIR}/docker-compose.yml" ]]; then
traefik_status="Stopped"
fi
# --- Print deployment plan ---
echo ""
echo -e "${CYAN}══════════════════════════════════════════════${NC}"
echo -e "${CYAN} Deploying Traefik${NC}"
echo -e "${CYAN}══════════════════════════════════════════════${NC}"
echo -e " Data: ${TRAEFIK_DIR}"
echo -e " Dynamic: ${TRAEFIK_DYNAMIC_DIR}/"
echo -e " ACME Email: ${ACME_EMAIL}"
echo -e " Status: ${traefik_status}"
echo -e "${CYAN}══════════════════════════════════════════════${NC}"
echo ""
# --- Check if already deployed and running ---
if docker ps --format '{{.Names}}' 2>/dev/null | grep -q '^traefik$'; then
echo -e "${GREEN} Traefik is already running — nothing to do.${NC}"
echo ""
echo -e " Logs: docker logs -f traefik"
echo -e " Backup: ${SCRIPT_DIR}/backup-traefik.sh"
echo -e " Remove: $0 --remove [--purge]"
echo ""
exit 0
fi
# --- Deploy ---
require_root
detect_os
install_prerequisites
ensure_docker_network
ensure_traefik
configure_firewall
# --- Summary ---
echo ""
echo -e "${GREEN}══════════════════════════════════════════════${NC}"
echo -e "${GREEN} Traefik deployed successfully${NC}"
echo -e "${GREEN}══════════════════════════════════════════════${NC}"
echo -e " Dynamic: ${TRAEFIK_DYNAMIC_DIR}/"
echo -e " Certs: ${TRAEFIK_DIR}/acme.json"
echo -e " Systemd: systemctl status ${UNIT_NAME}"
echo -e " Logs: docker logs -f traefik"
echo -e "${GREEN}══════════════════════════════════════════════${NC}"
echo ""