-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathreload.sh
More file actions
executable file
·196 lines (169 loc) · 5.53 KB
/
Copy pathreload.sh
File metadata and controls
executable file
·196 lines (169 loc) · 5.53 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/bin/bash
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored messages
print_info() {
echo -e "${BLUE}ℹ${NC} $1"
}
print_success() {
echo -e "${GREEN}✅${NC} $1"
}
print_warning() {
echo -e "${YELLOW}⚠${NC} $1"
}
print_error() {
echo -e "${RED}❌${NC} $1"
}
# Function to prompt with timeout
prompt_with_timeout() {
local prompt_message=$1
local default_choice=$2
local timeout=$3
local user_input
read -t "$timeout" -p "$prompt_message" user_input || user_input="$default_choice"
echo "$user_input"
}
# Function to check if command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to wait for service health
wait_for_health() {
local service=$1
local max_attempts=${2:-30}
local attempt=0
print_info "Waiting for $service to be healthy..."
while [ $attempt -lt $max_attempts ]; do
if docker compose ps "$service" | grep -q "healthy"; then
print_success "$service is healthy"
return 0
fi
attempt=$((attempt + 1))
sleep 2
done
print_warning "$service did not become healthy within timeout"
return 1
}
# Function to check service status
check_service_status() {
local service=$1
local status=$(docker compose ps "$service" --format json 2>/dev/null | grep -o '"State":"[^"]*"' | cut -d'"' -f4 || echo "unknown")
echo "$status"
}
echo "--------------------------------------------------"
echo "🚀 DOCKER COMPOSE RELOAD SCRIPT"
echo "--------------------------------------------------"
# Check if docker compose is available
if ! command_exists docker; then
print_error "Docker is not installed or not in PATH"
exit 1
fi
if ! docker compose version >/dev/null 2>&1; then
print_error "Docker Compose is not available"
exit 1
fi
# Check if docker-compose.yaml exists
if [ ! -f "docker-compose.yaml" ]; then
print_error "docker-compose.yaml not found in current directory"
exit 1
fi
# Ask for no-cache build
choice=$(prompt_with_timeout "Do you want to build with --no-cache? (y/N) [auto-skip in 5s]: " "n" 5)
if [[ "$choice" =~ ^[Yy]([Ee][Ss])?$ ]]; then
print_info "Running docker compose build --no-cache..."
if docker compose build --no-cache; then
print_success "Build completed successfully"
else
print_error "Build failed"
exit 1
fi
else
print_info "Skipping no-cache build..."
fi
# Ask to push to Docker Hub (only if build was selected)
if [[ "$choice" =~ ^[Yy]([Ee][Ss])?$ ]]; then
push_choice=$(prompt_with_timeout "Do you want to push the image to Docker Hub? (y/N) [auto-skip in 5s]: " "n" 5)
if [[ "$push_choice" =~ ^[Yy]([Ee][Ss])?$ ]]; then
print_info "Pushing Docker images to Docker Hub..."
if docker compose push; then
print_success "Images pushed successfully"
else
print_error "Failed to push images"
exit 1
fi
else
print_info "Skipping Docker Hub push..."
fi
fi
# Check and create Docker network if it doesn't exist
NETWORK_NAME="nodejs_backend_with_postgresql_network"
print_info "Checking Docker network: $NETWORK_NAME..."
if ! docker network inspect "$NETWORK_NAME" >/dev/null 2>&1; then
print_info "Creating Docker network: $NETWORK_NAME..."
if docker network create --driver bridge "$NETWORK_NAME"; then
print_success "Network created successfully"
else
print_error "Failed to create network"
exit 1
fi
else
print_success "Network already exists"
fi
# Bring down existing containers
print_info "Bringing down Docker containers..."
if docker compose down; then
print_success "Containers stopped and removed"
else
print_warning "Some containers may not have been stopped properly"
fi
# Start containers
print_info "Starting Docker containers in detached mode..."
if docker compose up -d; then
print_success "Containers started"
else
print_error "Failed to start containers"
exit 1
fi
# Wait for critical services to be healthy
print_info "Waiting for services to be ready..."
wait_for_health "db" 30
wait_for_health "redis" 30
wait_for_health "api" 60
# Show service status
echo ""
echo "--------------------------------------------------"
echo "📊 SERVICE STATUS"
echo "--------------------------------------------------"
docker compose ps
# Ask if user wants to follow logs
log_choice=$(prompt_with_timeout "Do you want to follow logs? (y/N) [auto-skip in 5s]: " "n" 5)
if [[ "$log_choice" =~ ^[Yy]([Ee][Ss])?$ ]]; then
service_choice=$(prompt_with_timeout "Which service logs? (api/nginx/db/redis/pgadmin/all) [default: api]: " "api" 5)
if [ "$service_choice" = "all" ]; then
print_info "Following all service logs (Ctrl+C to exit)..."
docker compose logs -f
else
print_info "Following $service_choice logs (Ctrl+C to exit)..."
docker compose logs -f "$service_choice"
fi
else
print_info "Skipping log following..."
echo ""
print_success "Deployment completed successfully!"
echo ""
echo "Access your services:"
echo " - API: http://localhost:8900"
echo " - Nginx: http://localhost:9080"
echo " - pgAdmin: http://localhost:5050"
echo ""
echo "Available services: api, nginx, db, redis, pgadmin"
echo "To view logs later, run: docker compose logs -f [service]"
fi
echo "--------------------------------------------------"
echo "✅ RELOAD SCRIPT FINISHED"
echo "--------------------------------------------------"