Repository navigation
Expand file tree
/
Copy pathupdate
More file actions
executable file
·489 lines (436 loc) · 14.7 KB
/
Copy pathupdate
File metadata and controls
executable file
·489 lines (436 loc) · 14.7 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#!/usr/bin/env bash
# update — Mise a jour des services Mascarade
# Usage : ./update [options]
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Charger les modules
source "$SCRIPT_DIR/scripts/lib.sh"
source "$SCRIPT_DIR/scripts/services.sh"
AUTO_CONFIRM=false
PRESELECT_MODE=false
WITH_SERVICES_RAW=""
STRATEGY_OVERRIDE=""
SKIP_GIT_PULL=false
SKIP_TESTS=false
show_update_help() {
cat <<'HELP'
Usage: ./update [options]
Mise a jour TUI des services Mascarade.
Detecte les services actifs et applique une strategie de mise a jour.
Options:
-h, --help Afficher cette aide et quitter
-v, --verbose Afficher les traces de debug
-y, --yes Repondre "oui" aux confirmations
--with <ids> Preselection services (csv), ex: core,api,redis
--strategy <name> Strategie: full|rebuild|restart|pull
--no-git-pull Ignorer git pull avant build local
--skip-tests Ignorer les tests Python optionnels
Services valides (--with):
core, api, litellm, n8n, langfuse, firecrawl, mem0, dify, clickhouse, comfyui,
ollama, ops-console, redis, postgres, qdrant, grafana, prometheus
HELP
}
parse_update_args() {
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
show_update_help
exit 0
;;
-v|--verbose)
VERBOSE=true
;;
-y|--yes)
AUTO_CONFIRM=true
;;
--with)
if [[ $# -lt 2 || -z "${2:-}" ]]; then
err "Option --with attend une liste de services (csv)."
exit 2
fi
WITH_SERVICES_RAW="$2"
PRESELECT_MODE=true
shift
;;
--strategy)
if [[ $# -lt 2 || -z "${2:-}" ]]; then
err "Option --strategy attend une valeur: full|rebuild|restart|pull"
exit 2
fi
STRATEGY_OVERRIDE="$2"
shift
;;
--no-git-pull)
SKIP_GIT_PULL=true
;;
--skip-tests)
SKIP_TESTS=true
;;
*)
err "Option inconnue: $1"
echo "Utilise: ./update --help"
exit 2
;;
esac
shift
done
}
confirm_or_auto() {
local prompt="$1"
if [[ "$AUTO_CONFIRM" == true ]]; then
info "$prompt [auto-oui]"
return 0
fi
confirm "$prompt"
}
strategy_name_to_index() {
case "$1" in
full) echo 0 ;;
rebuild) echo 1 ;;
restart) echo 2 ;;
pull) echo 3 ;;
*) return 1 ;;
esac
}
apply_update_preselected_services() {
local csv="$1"
local token
local valid
local present
for id in "${SVC_IDS[@]}"; do
SVC_ON[$id]="0"
done
IFS=',' read -ra _req_svcs <<< "$csv"
for token in "${_req_svcs[@]}"; do
token="${token// /}"
[[ -z "$token" ]] && continue
valid=false
for id in "${SVC_IDS[@]}"; do
if [[ "$token" == "$id" ]]; then
valid=true
break
fi
done
if [[ "$valid" != true ]]; then
err "Service inconnu dans --with: '$token'"
echo "Services valides: ${SVC_IDS[*]}"
exit 2
fi
present=false
for svc in "${compose_services[@]}"; do
if [[ "$svc" == "$token" || "$svc" == "${token}-"* ]]; then
present=true
break
fi
done
if [[ "$present" != true ]]; then
err "Service '$token' absent du docker-compose actif"
exit 2
fi
SVC_ON[$token]="1"
done
}
parse_update_args "$@"
if [[ -n "$STRATEGY_OVERRIDE" ]]; then
if ! strategy_name_to_index "$STRATEGY_OVERRIDE" >/dev/null; then
err "Strategie invalide: '$STRATEGY_OVERRIDE'"
echo "Valeurs attendues: full|rebuild|restart|pull"
exit 2
fi
fi
dbg "=== update demarre ==="
dbg " REPO_DIR=$REPO_DIR"
dbg " SCRIPT_DIR=$SCRIPT_DIR"
dbg " AUTO_CONFIRM=$AUTO_CONFIRM PRESELECT_MODE=$PRESELECT_MODE STRATEGY_OVERRIDE=${STRATEGY_OVERRIDE:-none} SKIP_GIT_PULL=$SKIP_GIT_PULL SKIP_TESTS=$SKIP_TESTS"
_ensure_docker_access
banner
echo -e " ${BOLD}Mise a jour de Mascarade${NC}"
echo -e " ${DIM}Selectionne les services a mettre a jour${NC}"
# ── Verifier docker-compose.yml ──
if [[ ! -f "$REPO_DIR/docker-compose.yml" ]]; then
err "Pas de docker-compose.yml trouve. Lance d'abord ./setup"
dbg " fichier absent: $REPO_DIR/docker-compose.yml"
exit 1
fi
dbg "docker-compose.yml: $(wc -l < "$REPO_DIR/docker-compose.yml") lignes, $(wc -c < "$REPO_DIR/docker-compose.yml") octets"
# ── Detecter les services du compose ──
dbg "Detection des services dans docker-compose.yml..."
compose_services=()
while IFS= read -r svc; do
[[ -n "$svc" ]] && compose_services+=("$svc")
done < <(docker_compose_cmd -f "$REPO_DIR/docker-compose.yml" config --services 2>/dev/null)
dbg "Services dans compose: ${compose_services[*]:-aucun} (${#compose_services[@]} total)"
if [[ ${#compose_services[@]} -eq 0 ]]; then
err "Aucun service trouve dans docker-compose.yml"
exit 1
fi
# ── Etape 1 : Statut actuel ──
step_progress 1 4 "Statut actuel"
dbg "Interrogation Docker pour le statut..."
# Un seul appel docker compose ps pour tout le statut
_cache_docker_ps
printf "\n ${BOLD}%-20s %-12s %-30s${NC}\n" "Service" "Statut" "Image"
line
for svc in "${compose_services[@]}"; do
svc_status="${DIM}○ Absent${NC}"
svc_image="—"
# Chercher dans le cache
cached_line=$(echo "$_docker_ps_cache" | grep -i "$svc" | head -1 || true)
if [[ -n "$cached_line" ]]; then
c_status=$(echo "$cached_line" | cut -f2)
c_image=$(echo "$cached_line" | cut -f3 | sed 's|.*/||')
svc_image="${c_image:-$svc_image}"
dbg " $svc: status='$c_status' image='$c_image'"
case "$c_status" in
*Up*|*running*) svc_status="${GREEN}● Running${NC}" ;;
*Exited*|*exited*) svc_status="${YELLOW}○ Stopped${NC}" ;;
*) svc_status="${RED}◌ ${c_status:0:10}${NC}" ;;
esac
else
dbg " $svc: pas de container trouve"
fi
printf " %-20s ${svc_status} ${DIM}%-30s${NC}\n" "$svc" "$svc_image"
done
echo ""
# ── Etape 2 : Selection ──
step_progress 2 4 "Selection des services"
# Reutiliser le SVC_IDS pour le checkbox, mais seulement les services detectes
dbg "Initialisation SVC_ON depuis les services du compose..."
for id in "${SVC_IDS[@]}"; do SVC_ON[$id]="0"; done
for svc in "${compose_services[@]}"; do
for id in "${SVC_IDS[@]}"; do
if [[ "$svc" == "$id" || "$svc" == "${id}-"* ]]; then
SVC_ON[$id]="1"
dbg " $id → ON (match '$svc')"
fi
done
done
selectable_ids=()
for id in "${SVC_IDS[@]}"; do
[[ "${SVC_ON[$id]}" == "1" ]] && selectable_ids+=("$id")
done
dbg "IDs selectionnables pour update: ${selectable_ids[*]:-aucun}"
if [[ "$PRESELECT_MODE" == true ]]; then
apply_update_preselected_services "$WITH_SERVICES_RAW"
ok "Services preselectionnes via --with"
else
checkbox_select_ids "${selectable_ids[@]}"
fi
# Collecter les services a updater
update_services=()
for svc in "${compose_services[@]}"; do
for id in "${SVC_IDS[@]}"; do
if [[ ("$svc" == "$id" || "$svc" == "${id}-"*) && "${SVC_ON[$id]}" == "1" ]]; then
update_services+=("$svc")
dbg " update: $svc (via $id)"
break
fi
done
done
dbg "Services a mettre a jour: ${update_services[*]:-aucun} (${#update_services[@]} total)"
if [[ ${#update_services[@]} -eq 0 ]]; then
err "Aucun service selectionne pour la mise a jour."
exit 1
fi
echo ""
echo -e " ${BOLD}Services a mettre a jour :${NC}"
for svc in "${update_services[@]}"; do
echo -e " ${CYAN}▸${NC} $svc"
done
echo ""
if ! confirm_or_auto "Lancer la mise a jour ?"; then
dbg "Abandon par l'utilisateur"
exit 0
fi
# ── Etape 3 : Strategie et execution ──
step_progress 3 4 "Mise a jour"
if [[ -n "$STRATEGY_OVERRIDE" ]]; then
strategy=$(strategy_name_to_index "$STRATEGY_OVERRIDE")
ok "Strategie forcee: $STRATEGY_OVERRIDE"
elif [[ "$AUTO_CONFIRM" == true ]]; then
strategy=0
info "Strategie par defaut [auto-oui]: full"
else
menu_select "Comment mettre a jour ?" \
"Pull + rebuild + restart (complet)" \
"Rebuild seulement (core/api)" \
"Restart seulement" \
"Pull images seulement (sans restart)"
strategy=$MENU_RESULT
fi
dbg "Strategie choisie: $strategy (${strategy}: $(case $strategy in 0) echo 'complet';; 1) echo 'rebuild';; 2) echo 'restart';; 3) echo 'pull';; esac))"
# Detecter si on a des services buildables
has_build=false
for svc in "${update_services[@]}"; do
[[ "$svc" == "core" || "$svc" == "api" ]] && has_build=true && break
done
dbg "Services buildables presents: $has_build"
# Git pull optionnel
if [[ "$has_build" == true && $strategy -le 1 ]]; then
do_git_pull=false
if [[ "$SKIP_GIT_PULL" == true ]]; then
dbg "git pull ignore (--no-git-pull)"
elif [[ "$AUTO_CONFIRM" == true ]]; then
do_git_pull=true
elif confirm "Faire un git pull avant le build ?"; then
do_git_pull=true
fi
if [[ "$do_git_pull" == true ]]; then
dbg "git pull --ff-only dans $REPO_DIR..."
dbg " HEAD actuel: $(git -C "$REPO_DIR" rev-parse --short HEAD 2>/dev/null || echo '?')"
dbg " branch: $(git -C "$REPO_DIR" branch --show-current 2>/dev/null || echo '?')"
spin_start "Git pull..."
pull_rc=0
pull_output=$(git -C "$REPO_DIR" pull --ff-only 2>&1) || pull_rc=$?
spin_stop
if [[ $pull_rc -eq 0 ]]; then
ok "Code a jour"
dbg " git pull OK"
dbg " HEAD apres: $(git -C "$REPO_DIR" rev-parse --short HEAD 2>/dev/null || echo '?')"
dbg " output: $pull_output"
else
warn "git pull echoue (code $pull_rc)"
dbg " git pull FAILED rc=$pull_rc"
dbg " output: $pull_output"
if ! confirm_or_auto "Continuer quand meme ?"; then
exit 1
fi
fi
else
dbg "git pull saute"
fi
fi
# Tests optionnels
if [[ "$has_build" == true && $strategy -le 1 ]]; then
if [[ "$SKIP_TESTS" == true ]]; then
dbg "Tests ignores (--skip-tests)"
else
run_pytest true || true
fi
fi
# ── Helper: pull images non-buildables ──
_pull_images() {
dbg "_pull_images: debut"
spin_start "Pull des images..."
for svc in "${update_services[@]}"; do
if [[ "$svc" != "core" && "$svc" != "api" ]]; then
dbg " pull $svc..."
pull_rc=0
docker_compose_cmd -f "$REPO_DIR/docker-compose.yml" pull "$svc" 2>&1 || pull_rc=$?
if [[ $pull_rc -ne 0 ]]; then
warn "Pull $svc echoue (code $pull_rc)"
else
dbg " pull $svc OK"
fi
else
dbg " skip pull $svc (image locale)"
fi
done
spin_stop
ok "Images a jour"
}
# ── Helper: build services locaux ──
_build_local() {
dbg "_build_local: debut"
for svc in "${update_services[@]}"; do
if [[ "$svc" == "core" || "$svc" == "api" ]]; then
dbg " build --no-cache $svc..."
spin_start "Build $svc..."
build_rc=0
build_output=$(docker_compose_cmd -f "$REPO_DIR/docker-compose.yml" build --no-cache "$svc" 2>&1) || build_rc=$?
spin_stop
if [[ $build_rc -eq 0 ]]; then
ok "Build $svc"
dbg " build $svc OK"
else
warn "Build $svc echoue (code $build_rc)"
dbg " build $svc FAILED rc=$build_rc"
if [[ "$VERBOSE" == true ]]; then
echo "$build_output" | tail -5 | while IFS= read -r bl; do dbg " | $bl"; done
fi
fi
fi
done
}
# ── Helper: restart services ──
_restart_services() {
dbg "_restart_services: ${update_services[*]}"
spin_start "Restart des services..."
restart_rc=0
docker_compose_cmd -f "$REPO_DIR/docker-compose.yml" up -d --force-recreate "${update_services[@]}" 2>&1 || restart_rc=$?
spin_stop
if [[ $restart_rc -eq 0 ]]; then
ok "Services redemarres"
else
warn "Restart partiel (code $restart_rc)"
fi
dbg " restart rc=$restart_rc"
}
# Execution
dbg "Execution strategie $strategy sur ${#update_services[@]} service(s)..."
case $strategy in
0) # Pull + rebuild + restart
dbg "=== Strategie: Pull + rebuild + restart ==="
_pull_images
_build_local
_restart_services
;;
1) # Rebuild seulement
dbg "=== Strategie: Rebuild seulement ==="
_build_local
_restart_services
;;
2) # Restart seulement
dbg "=== Strategie: Restart seulement ==="
spin_start "Restart des services..."
restart_rc=0
docker_compose_cmd -f "$REPO_DIR/docker-compose.yml" restart "${update_services[@]}" 2>&1 || restart_rc=$?
spin_stop
if [[ $restart_rc -eq 0 ]]; then
ok "Services redemarres"
else
warn "Restart partiel (code $restart_rc)"
fi
dbg " restart rc=$restart_rc"
;;
3) # Pull seulement
dbg "=== Strategie: Pull seulement ==="
_pull_images
echo ""
log "Images mises a jour. Lance 'docker compose up -d' pour appliquer."
;;
esac
# ── Etape 4 : Health checks + resume ──
step_progress 4 4 "Verification"
if [[ $strategy -le 2 ]]; then
dbg "Attente 3s avant health checks..."
sleep 3
load_env "$REPO_DIR/.env" 2>/dev/null || true
invalidate_docker_cache
for svc in "${update_services[@]}"; do
svc_port=""
for id in "${SVC_IDS[@]}"; do
if [[ "$svc" == "$id" || "$svc" == "${id}-"* ]]; then
svc_port="${SVC_PORT[$id]}"
break
fi
done
if [[ "$svc_port" == "—" || -z "$svc_port" ]]; then
dbg " skip health check $svc (pas de port)"
continue
fi
health_check "http://localhost:${svc_port}/" "$svc" 5 2 || true
done
else
dbg "Skip health checks (strategie=$strategy, pull seulement)"
fi
echo ""
line
echo ""
echo -e " ${GREEN}${BOLD}Mise a jour terminee !${NC}"
echo ""
invalidate_docker_cache
show_status_table
line
echo -e " ${DIM}mascarade v0.1.0${NC}"
dbg "=== update termine === duree=$(ps -o etime= -p $$ 2>/dev/null || echo '?')"
echo ""