# Einfache CDN-Verwaltung mit Make

.PHONY: deploy check health purge-cache warm-cache status reload

# Standard deployment
deploy:
	@echo "🚀 Deploying Simple CDN..."
	chmod +x scripts/deploy.sh
	./scripts/deploy.sh

# Deployment mit Check-Modus (Dry-Run)
check:
	@echo "🔍 Checking deployment (dry-run)..."
	ansible-playbook -i inventories/production/hosts.yml playbooks/deploy-simple-cdn.yml --check --diff

# Health check aller Nodes
health:
	@echo "🏥 Checking CDN health..."
	ansible cdn_nodes -i inventories/production/hosts.yml -m uri -a "url=https://{{ inventory_hostname }}/health method=GET"

# Cache leeren
purge-cache:
	@echo "🧹 Purging cache on all nodes..."
	ansible cdn_nodes -i inventories/production/hosts.yml -m shell -a "find /var/cache/nginx/ -type f -delete"
	@echo "✅ Cache purged on all nodes"

# Cache warming
warm-cache:
	@echo "🔥 Warming cache..."
	chmod +x scripts/warm-cache.sh
	./scripts/warm-cache.sh

# Status-Report
status:
	@echo "📊 CDN Status Report..."
	ansible cdn_nodes -i inventories/production/hosts.yml -m shell -a "echo '=== {{ inventory_hostname }} ===' && /usr/local/bin/cdn-monitor && echo ''"

# Nginx neuladen
reload:
	@echo "⚙️ Reloading nginx configuration..."
	ansible cdn_nodes -i inventories/production/hosts.yml -m systemd -a "name=nginx state=reloaded"

# SSL-Zertifikate erneuern
renew-ssl:
	@echo "🔐 Renewing SSL certificates..."
	ansible cdn_nodes -i inventories/production/hosts.yml -m shell -a "certbot renew --quiet"

# Interaktive Verwaltung
manage:
	@echo "🔧 Starting interactive management..."
	ansible-playbook -i inventories/production/hosts.yml playbooks/manage-cdn.yml

# Hilfe
help:
	@echo "📖 Available commands:"
	@echo "  make deploy      - Deploy CDN"
	@echo "  make check       - Test deployment (dry-run)"
	@echo "  make health      - Check all nodes health"
	@echo "  make purge-cache - Clear all cache"
	@echo "  make warm-cache  - Warm cache with popular URLs"
	@echo "  make status      - Show detailed status"
	@echo "  make reload      - Reload nginx config"
	@echo "  make renew-ssl   - Renew SSL certificates"
	@echo "  make manage      - Interactive management"
