-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlaunch.ps1
More file actions
320 lines (282 loc) · 9.77 KB
/
launch.ps1
File metadata and controls
320 lines (282 loc) · 9.77 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
$ErrorActionPreference = "Stop"
function Write-ColorOutput {
param(
[string]$Message,
[ValidateSet("Black", "DarkBlue", "DarkGreen", "DarkCyan", "DarkRed", "DarkMagenta", "DarkYellow", "Gray", "DarkGray", "Blue", "Green", "Cyan", "Red", "Magenta", "Yellow", "White")]
[string]$Color = "White"
)
Write-Host $Message -ForegroundColor $Color
}
function Write-Header {
param([string]$Title)
Write-ColorOutput "=====================================" "Blue"
Write-ColorOutput " $Title" "Blue"
Write-ColorOutput "=====================================" "Blue"
Write-Host ""
}
function Show-Usage {
Write-Header "WeatherTrack Pro - Launch Script"
Write-Host "Usage: .\launch.ps1 [option]"
Write-Host ""
Write-Host "Options:"
Write-Host " (no option) Launch both old and new versions"
Write-Host " old Launch only the old version"
Write-Host " new Launch only the new version"
Write-Host " stop Stop all running services"
Write-Host " restart Restart all services"
Write-Host " logs Show logs for all services"
Write-Host " clean Stop services and remove volumes"
Write-Host ""
}
function Check-Service {
param(
[string]$Url,
[string]$Name,
[int]$MaxAttempts = 30
)
Write-ColorOutput "Waiting for $Name to be ready..." "Yellow"
for ($i = 1; $i -le $MaxAttempts; $i++) {
try {
$response = Invoke-WebRequest -Uri "$Url/weather/data/Lyon" -Method Get -TimeoutSec 2 -ErrorAction Stop
Write-Host "[" -NoNewline
Write-Host "OK" -ForegroundColor Green -NoNewline
Write-Host "] $Name is ready!"
return $true
}
catch {
Write-Host "." -NoNewline
Start-Sleep -Seconds 1
}
}
Write-Host ""
Write-Host "[" -NoNewline
Write-Host "FAIL" -ForegroundColor Red -NoNewline
Write-Host "] $Name did not start properly"
return $false
}
function Launch-All {
Write-ColorOutput "Launching both versions of WeatherTrack Pro..." "Yellow"
Write-Host ""
try {
docker-compose up -d
}
catch {
Write-ColorOutput "Error: Failed to start Docker containers" "Red"
Write-Host $_.Exception.Message
exit 1
}
Write-Host ""
Write-ColorOutput "Checking services availability..." "Yellow"
Write-Host ""
$oldReady = Check-Service -Url "http://localhost:3000" -Name "Old API"
$newReady = Check-Service -Url "http://localhost:3001" -Name "New API"
Write-Host ""
if ($oldReady -and $newReady) {
Write-Host "[" -NoNewline
Write-Host "OK" -ForegroundColor Green -NoNewline
Write-Host "] All services started successfully!"
Write-Host ""
Write-ColorOutput "Services URLs:" "Blue"
Write-ColorOutput " Old API: http://localhost:3000" "Green"
Write-ColorOutput " New API: http://localhost:3001" "Green"
Write-ColorOutput " Old DB: localhost:5432" "Green"
Write-ColorOutput " New DB: localhost:5433" "Green"
Write-Host ""
Write-ColorOutput "Use 'docker-compose logs -f' to view logs" "Yellow"
}
else {
Write-Host "[" -NoNewline
Write-Host "WARN" -ForegroundColor Yellow -NoNewline
Write-Host "] Some services failed to start properly"
Write-Host ""
if ($oldReady) {
Write-ColorOutput "Services available:" "Blue"
Write-ColorOutput " Old API: http://localhost:3000" "Green"
Write-ColorOutput " Old DB: localhost:5432" "Green"
}
if ($newReady) {
Write-ColorOutput "Services available:" "Blue"
Write-ColorOutput " New API: http://localhost:3001" "Green"
Write-ColorOutput " New DB: localhost:5433" "Green"
}
Write-Host ""
Write-ColorOutput "Check logs with: docker-compose logs" "Yellow"
Write-ColorOutput "Troubleshooting:" "Red"
Write-ColorOutput " 1. Check if ports 3000, 3001, 5432, 5433 are available" "Red"
Write-ColorOutput " 2. Verify Docker containers are running: docker-compose ps" "Red"
Write-ColorOutput " 3. Check individual service logs: docker-compose logs [service-name]" "Red"
}
}
function Launch-Old {
Write-ColorOutput "Launching old version of WeatherTrack Pro..." "Yellow"
Write-Host ""
try {
docker-compose up -d old-db old-api
}
catch {
Write-ColorOutput "Error: Failed to start Docker containers" "Red"
Write-Host $_.Exception.Message
exit 1
}
Write-Host ""
Write-ColorOutput "Checking service availability..." "Yellow"
Write-Host ""
$oldReady = Check-Service -Url "http://localhost:3000" -Name "Old API"
Write-Host ""
if ($oldReady) {
Write-Host "[" -NoNewline
Write-Host "OK" -ForegroundColor Green -NoNewline
Write-Host "] Old version started successfully!"
Write-Host ""
Write-ColorOutput "Service URLs:" "Blue"
Write-ColorOutput " Old API: http://localhost:3000" "Green"
Write-ColorOutput " Old DB: localhost:5432" "Green"
Write-Host ""
Write-ColorOutput "Use 'docker-compose logs -f old-api old-db' to view logs" "Yellow"
}
else {
Write-Host "[" -NoNewline
Write-Host "FAIL" -ForegroundColor Red -NoNewline
Write-Host "] Old version failed to start properly"
Write-Host ""
Write-ColorOutput "Check logs with: docker-compose logs old-api old-db" "Yellow"
Write-ColorOutput "Troubleshooting:" "Red"
Write-ColorOutput " 1. Check if ports 3000, 5432 are available" "Red"
Write-ColorOutput " 2. Verify Docker containers are running: docker-compose ps" "Red"
Write-ColorOutput " 3. Check service logs: docker-compose logs old-api old-db" "Red"
}
}
function Launch-New {
Write-ColorOutput "Launching new version of WeatherTrack Pro..." "Yellow"
Write-Host ""
try {
docker-compose up -d new-db new-api
}
catch {
Write-ColorOutput "Error: Failed to start Docker containers" "Red"
Write-Host $_.Exception.Message
exit 1
}
Write-Host ""
Write-ColorOutput "Checking service availability..." "Yellow"
Write-Host ""
$newReady = Check-Service -Url "http://localhost:3001" -Name "New API"
Write-Host ""
if ($newReady) {
Write-Host "[" -NoNewline
Write-Host "OK" -ForegroundColor Green -NoNewline
Write-Host "] New version started successfully!"
Write-Host ""
Write-ColorOutput "Service URLs:" "Blue"
Write-ColorOutput " New API: http://localhost:3001" "Green"
Write-ColorOutput " New DB: localhost:5433" "Green"
Write-Host ""
Write-ColorOutput "Use 'docker-compose logs -f new-api new-db' to view logs" "Yellow"
}
else {
Write-Host "[" -NoNewline
Write-Host "FAIL" -ForegroundColor Red -NoNewline
Write-Host "] New version failed to start properly"
Write-Host ""
Write-ColorOutput "Check logs with: docker-compose logs new-api new-db" "Yellow"
Write-ColorOutput "Troubleshooting:" "Red"
Write-ColorOutput " 1. Check if ports 3001, 5433 are available" "Red"
Write-ColorOutput " 2. Verify Docker containers are running: docker-compose ps" "Red"
Write-ColorOutput " 3. Check service logs: docker-compose logs new-api new-db" "Red"
}
}
function Stop-Services {
Write-ColorOutput "Stopping all services..." "Yellow"
Write-Host ""
try {
docker-compose down
Write-Host ""
Write-Host "[" -NoNewline
Write-Host "OK" -ForegroundColor Green -NoNewline
Write-Host "] All services stopped successfully!"
}
catch {
Write-ColorOutput "Error: Failed to stop services" "Red"
Write-Host $_.Exception.Message
exit 1
}
}
function Restart-Services {
Write-ColorOutput "Restarting all services..." "Yellow"
Write-Host ""
try {
docker-compose restart
Write-Host ""
Write-Host "[" -NoNewline
Write-Host "OK" -ForegroundColor Green -NoNewline
Write-Host "] All services restarted successfully!"
Write-Host ""
Write-ColorOutput "Note: Services may take a few seconds to be fully operational" "Yellow"
}
catch {
Write-ColorOutput "Error: Failed to restart services" "Red"
Write-Host $_.Exception.Message
exit 1
}
}
function Show-Logs {
Write-ColorOutput "Showing logs for all services..." "Yellow"
Write-Host ""
try {
docker-compose logs -f
}
catch {
Write-ColorOutput "Error: Failed to retrieve logs" "Red"
Write-Host $_.Exception.Message
exit 1
}
}
function Clean-All {
Write-ColorOutput "Stopping services and removing volumes..." "Yellow"
Write-Host ""
try {
docker-compose down -v
Write-Host ""
Write-Host "[" -NoNewline
Write-Host "OK" -ForegroundColor Green -NoNewline
Write-Host "] All services stopped and volumes removed!"
}
catch {
Write-ColorOutput "Error: Failed to clean services" "Red"
Write-Host $_.Exception.Message
exit 1
}
}
$option = if ($args.Count -eq 0) { "all" } else { $args[0] }
switch ($option) {
"all" {
Launch-All
}
"old" {
Launch-Old
}
"new" {
Launch-New
}
"stop" {
Stop-Services
}
"restart" {
Restart-Services
}
"logs" {
Show-Logs
}
"clean" {
Clean-All
}
{ $_ -in "help", "--help", "-h" } {
Show-Usage
}
default {
Write-ColorOutput "Error: Invalid option '$option'" "Red"
Write-Host ""
Show-Usage
exit 1
}
}