Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Replace custom health check with docker-compose --wait (#3345)

Eric Bariaux127998645262
diff --git a/.ci_cd/deploy.sh b/.ci_cd/deploy.sh
index 0a9558d5c..b11d955cd 100644
--- a/.ci_cd/deploy.sh
+++ b/.ci_cd/deploy.sh
@@ -274,17 +274,11 @@ else
fi
echo "Private IP \$PRIVATE_IP"
-# Start the stack
-echo "Starting the stack"
-docker-compose -f temp/docker-compose.yml -p or up -d
-
-if [ \$? -ne 0 ]; then
- echo "Deployment failed to start the stack"
+# Start the stack, including any one-shot deployment service, and wait for readiness
+if ! bash temp/host_init/start.sh temp/docker-compose.yml or; then
exit 1
fi
-temp/host_init/healthy.sh
-
# Run host post init
hostPostInitCmd=
if [ -f "temp/host_init/post_init_${ENVIRONMENT}.sh" ]; then
@@ -403,17 +397,11 @@ fi
echo "Deleting existing deployment data volume"
docker volume rm or_deployment-data 1>/dev/null
-# Start the stack
-echo "Starting the stack"
-docker-compose -f temp/docker-compose.yml -p or up -d
-
-if [ \$? -ne 0 ]; then
- echo "Deployment failed to start the stack"
+# Start the stack, including any one-shot deployment service, and wait for readiness
+if ! bash temp/host_init/start.sh temp/docker-compose.yml or; then
exit 1
fi
-temp/host_init/healthy.sh
-
# Run host post init
hostPostInitCmd=
if [ -f "temp/host_init/post_init_${ENVIRONMENT}.sh" ]; then
diff --git a/.ci_cd/host_init/healthy.sh b/.ci_cd/host_init/healthy.sh
deleted file mode 100644
index d96b4e88b..000000000
--- a/.ci_cd/host_init/healthy.sh
+++ /dev/null
@@ -1,42 +0,0 @@
-#!/bin/bash
-# -----------------------------------------------------------------------------------------------------
-# Script for waiting until all docker services are healthy
-#
-# An argument can be provided to filter on a specific service name
-# -----------------------------------------------------------------------------------------------------
-echo "Waiting for up to 5mins for all services to be healthy"
-COUNT=1
-STATUSES_OK=false
-IFS=$'\n'
-while [ "$STATUSES_OK" != 'true' ] && [ $COUNT -le 60 ]; do
-
- echo "Checking service health...attempt $COUNT"
- STATUSES=$(docker ps --format "{{.Names}} {{.Status}}" | grep ${1:-""} || true)
- STATUSES_OK=true
-
- if [ "$STATUSES" == "" ]; then
- STATUSES_OK=false
- fi
-
- for STATUS in $STATUSES; do
- if [[ "$STATUS" != *"healthy"* ]]; then
- STATUSES_OK=false
- break
- fi
- done
-
- if [ "$STATUSES_OK" == 'true' ]; then
- break
- fi
-
- sleep 5
- COUNT=$((COUNT+1))
-done
-
-if [ "$STATUSES_OK" == 'true' ]; then
- echo "All services are healthy"
-else
- echo "One or more services are unhealthy"
- docker ps -a
- exit 1
-fi
diff --git a/.ci_cd/host_init/start.sh b/.ci_cd/host_init/start.sh
new file mode 100644
index 000000000..9a3f14b3c
--- /dev/null
+++ b/.ci_cd/host_init/start.sh
@@ -0,0 +1,55 @@
+#!/bin/bash
+# Start a Compose deployment and wait for its services to become ready.
+# Usage: start.sh <compose-file> <project-name>
+
+if [ "$#" -ne 2 ]; then
+ echo "Usage: $0 <compose-file> <project-name>" >&2
+ exit 1
+fi
+
+COMPOSE=(docker-compose -f "$1" -p "$2")
+if ! SERVICES=$("${COMPOSE[@]}" config --services); then
+ echo "Deployment failed: could not determine Compose services" >&2
+ exit 1
+fi
+
+start_stack() {
+ if printf '%s\n' "$SERVICES" | grep -qx deployment; then
+ # The deployment image populates a shared volume and then exits. Mark it as
+ # a successful-completion dependency so Compose --wait accepts exit code 0.
+ "${COMPOSE[@]}" -f - up -d --wait --wait-timeout 300 <<'COMPOSE_OVERRIDE'
+services:
+ manager:
+ depends_on:
+ deployment:
+ condition: service_completed_successfully
+COMPOSE_OVERRIDE
+ else
+ "${COMPOSE[@]}" up -d --wait --wait-timeout 300
+ fi
+}
+
+echo "Starting the stack and waiting for services to be ready"
+if ! start_stack; then
+ echo "Deployment failed: services could not start or become ready"
+
+ echo "Deployment container states:"
+ "${COMPOSE[@]}" ps -a
+
+ echo "Container health diagnostics:"
+ for CONTAINER_ID in $("${COMPOSE[@]}" ps -a -q); do
+ docker inspect --format '
+{{.Name}}: state={{.State.Status}}, exit={{.State.ExitCode}}
+{{if .State.Error}}Error: {{.State.Error}}{{end}}
+{{if .State.Health}}Health: {{.State.Health.Status}}
+{{if ne .State.Health.Status "healthy"}}Recent health checks:
+{{range .State.Health.Log}} {{.End}} exit={{.ExitCode}}
+{{.Output}}
+{{end}}{{end}}{{else}}No health check configured
+{{end}}' "$CONTAINER_ID"
+ done
+
+ exit 1
+fi
+
+echo "All deployment services are ready"