75 lines
2.7 KiB
Bash
75 lines
2.7 KiB
Bash
#!/usr/bin/env bash
|
||
set -euo pipefail
|
||
|
||
BASE="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||
API_CONTAINER="${CODEXPI_API_CONTAINER:-codexpi-api-1}"
|
||
DOCKER_SOCKET="/var/run/docker.sock"
|
||
|
||
command -v docker >/dev/null 2>&1 || {
|
||
echo "Docker est introuvable ; contrôle d’accès ignoré." >&2
|
||
exit 0
|
||
}
|
||
if [[ ! -S "$DOCKER_SOCKET" ]]; then
|
||
echo "Le socket Docker n’est pas disponible ; un prochain contrôle réessaiera."
|
||
exit 0
|
||
fi
|
||
if ! docker info >/dev/null 2>&1; then
|
||
echo "Le service de contrôle n’accède pas au daemon Docker." >&2
|
||
exit 1
|
||
fi
|
||
|
||
socket_gid="$(stat -c '%g' "$DOCKER_SOCKET")"
|
||
|
||
repair_api() {
|
||
local api_container="$1"
|
||
local stack_base="$2"
|
||
|
||
# Do not start a stack that an administrator deliberately stopped.
|
||
if ! docker inspect "$api_container" >/dev/null 2>&1; then
|
||
echo "Le conteneur $api_container est arrêté ou absent ; aucune action."
|
||
return 0
|
||
fi
|
||
local container_groups
|
||
container_groups="$(docker inspect --format '{{json .HostConfig.GroupAdd}}' "$api_container")"
|
||
if [[ "$container_groups" == *\"$socket_gid\"* ]]; then
|
||
return 0
|
||
fi
|
||
|
||
# Never interrupt a coding or host-agent task merely to refresh Docker
|
||
# access. The timer will retry after the worker exits.
|
||
if docker top "$api_container" -eo pid,args 2>/dev/null | grep -Eq 'codexpi\.session_worker|codexpi\.host_session_worker|/codex .* exec'; then
|
||
echo "Accès Docker à actualiser dans $api_container, mais une tâche est active ; correction différée."
|
||
return 0
|
||
fi
|
||
|
||
echo "GID Docker obsolète dans $api_container ($container_groups, attendu: $socket_gid) ; recréation de l’API."
|
||
if [[ "$stack_base" == "$BASE" ]]; then
|
||
DOCKER_GID="$socket_gid" "$BASE/scripts/compose.sh" up -d --no-deps --force-recreate api
|
||
else
|
||
(cd "$stack_base" && DOCKER_GID="$socket_gid" docker compose up -d --no-deps --force-recreate api)
|
||
fi
|
||
|
||
local health=""
|
||
for _attempt in $(seq 1 60); do
|
||
health="$(docker inspect --format '{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}' "$api_container" 2>/dev/null || true)"
|
||
if [[ "$health" == "healthy" ]]; then
|
||
break
|
||
fi
|
||
sleep 2
|
||
done
|
||
if [[ "$health" != "healthy" ]]; then
|
||
echo "L’API $api_container recréée n’est pas devenue saine (état: ${health:-absent})." >&2
|
||
return 1
|
||
fi
|
||
if ! docker exec "$api_container" docker info >/dev/null 2>&1; then
|
||
echo "L’API $api_container est saine mais n’accède toujours pas à Docker." >&2
|
||
return 1
|
||
fi
|
||
echo "Accès Docker de $api_container restauré avec le GID $socket_gid."
|
||
}
|
||
|
||
repair_api "$API_CONTAINER" "$BASE"
|
||
if [[ -z "${CODEXPI_API_CONTAINER:-}" && -f /home/config/CodexPi2/docker-compose.yml ]]; then
|
||
repair_api codexpi-v2-api-1 /home/config/CodexPi2
|
||
fi
|