-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·169 lines (147 loc) · 5.03 KB
/
deploy.sh
File metadata and controls
executable file
·169 lines (147 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/bin/bash
# ================================================================
# DNS & Reverse Proxy Infrastructure Deployment Script
# ---------------------------------------------------------------
# Automates Docker Compose stack management for:
# - BIND9 DNS
# - Traefik Reverse Proxy (Cloudflare or Namecheap ACME)
# - Nginx test backend
# - Portainer
# Supports: up | down | restart | status
# Includes: Logging, container health status
# Default provider: Cloudflare
# Author: Anganba Singha
# ================================================================
# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
BLUE='\033[1;34m'
NC='\033[0m' # No Color
# Root path
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Compose file paths
BIND_COMPOSE="${PROJECT_ROOT}/bind9/docker-compose.yaml"
TRAEFIK_COMPOSE="${PROJECT_ROOT}/Traefik/docker-compose.yaml"
PORTAINER_COMPOSE="${PROJECT_ROOT}/Portainer-Server/docker-compose.yaml"
NGINX_COMPOSE="${PROJECT_ROOT}/nginx/docker-compose.yaml"
# Detect Docker or Podman
if command -v podman-compose &>/dev/null; then
COMPOSE_CMD="podman-compose"
elif command -v docker-compose &>/dev/null; then
COMPOSE_CMD="docker-compose"
elif command -v docker &>/dev/null; then
COMPOSE_CMD="docker compose"
else
echo -e "${RED}[✗] Neither Docker nor Podman found.${NC}"
exit 1
fi
# ----------------------------------------------------------------
# Provider configuration (Cloudflare by default)
# ----------------------------------------------------------------
PROVIDER=$(echo "${2:-cloudflare}" | tr '[:upper:]' '[:lower:]') # normalize to lowercase
case "$PROVIDER" in
cloudflare)
ENV_FILE="${PROJECT_ROOT}/Traefik/.env.cloudflare"
;;
namecheap)
ENV_FILE="${PROJECT_ROOT}/Traefik/.env.namecheap"
;;
*)
echo -e "${RED}[✗] Unknown provider: $PROVIDER (use 'cloudflare' or 'namecheap')${NC}"
exit 1
;;
esac
# Verify environment file exists
if [[ ! -f "$ENV_FILE" ]]; then
echo -e "${RED}[✗] Environment file not found:${NC} $ENV_FILE"
exit 1
fi
# Export provider to ensure Docker Compose variable expansion
export PROVIDER
export ENV_FILE
# Logging helpers with timestamp
timestamp() { date +"%Y-%m-%d %H:%M:%S"; }
log() { echo -e "${GREEN}[$(timestamp)] [+]${NC} $1"; }
warn() { echo -e "${YELLOW}[$(timestamp)] [!]${NC} $1"; }
error() { echo -e "${RED}[$(timestamp)] [✗]${NC} $1"; }
section(){ echo -e "\n${BLUE}=== $1 ===${NC}"; }
# ----------------------------------------------------------------
# Deploy functions
# ----------------------------------------------------------------
compose_up() {
section "Starting DNS & Reverse Proxy Stack (${PROVIDER^^})"
log "Starting BIND9 DNS Server..."
$COMPOSE_CMD -f "$BIND_COMPOSE" up -d || { error "Failed to start BIND9"; exit 1; }
log "Starting Traefik Reverse Proxy (${PROVIDER})..."
$COMPOSE_CMD --env-file "$ENV_FILE" -f "$TRAEFIK_COMPOSE" up -d || {
error "Failed to start Traefik ($PROVIDER)"
exit 1
}
log "Starting Nginx Backend..."
$COMPOSE_CMD -f "$NGINX_COMPOSE" up -d || { error "Failed to start Nginx"; exit 1; }
log "Starting Portainer UI..."
$COMPOSE_CMD -f "$PORTAINER_COMPOSE" up -d || { error "Failed to start Portainer"; exit 1; }
section "All Services Started Successfully (${PROVIDER^^})"
show_status
}
compose_down() {
section "Stopping All Services (${PROVIDER^^})"
$COMPOSE_CMD -f "$PORTAINER_COMPOSE" down
$COMPOSE_CMD -f "$NGINX_COMPOSE" down
$COMPOSE_CMD -f "$TRAEFIK_COMPOSE" down
$COMPOSE_CMD -f "$BIND_COMPOSE" down
log "All services have been stopped."
}
compose_restart() {
section "Restarting All Services (${PROVIDER^^})"
compose_down
sleep 3
compose_up
}
# ----------------------------------------------------------------
# Show service status and health checks
# ----------------------------------------------------------------
show_status() {
section "Container Status Summary"
containers=$(docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}")
if [[ -z "$containers" ]]; then
warn "No running containers found."
return
fi
echo -e "${YELLOW}Health Checks:${NC}"
echo "$containers" | awk -v green="$GREEN" -v yellow="$YELLOW" -v red="$RED" -v nc="$NC" '
NR==1 {print; next}
/healthy/ {print green $0 nc; next}
/starting/ {print yellow $0 nc; next}
/unhealthy/ {print red $0 nc; next}
{print $0}
'
}
# ----------------------------------------------------------------
# Main control
# ----------------------------------------------------------------
case "$1" in
up)
compose_up
;;
down)
compose_down
;;
restart)
compose_restart
;;
status)
show_status
;;
*)
echo -e "${YELLOW}Usage:${NC} $0 {up|down|restart|status} [cloudflare|namecheap]"
echo
echo "Examples:"
echo " $0 up # Start stack using Cloudflare (default)"
echo " $0 up namecheap # Start stack using Namecheap"
echo " $0 restart cloudflare # Restart stack using Cloudflare"
echo " $0 status # Show running containers"
exit 1
;;
esac