diff --git a/builtin/core/roles/image-registry/harbor/tasks/main.yaml b/builtin/core/roles/image-registry/harbor/tasks/main.yaml index 2d5e8153..9b79f6ad 100644 --- a/builtin/core/roles/image-registry/harbor/tasks/main.yaml +++ b/builtin/core/roles/image-registry/harbor/tasks/main.yaml @@ -49,6 +49,31 @@ - name: Harbor | Start and enable Harbor service command: systemctl daemon-reload && systemctl start harbor.service && systemctl enable harbor.service +- name : Harbor | Check harbor api health + command: | + HARBOR_URL="https://{{ .image_registry.auth.registry }}" + MAX_WAIT_SECONDS=300 + CHECK_INTERVAL=2 + MAX_RETRIES=$((MAX_WAIT_SECONDS / CHECK_INTERVAL)) + for ((i=1; i<=MAX_RETRIES; i++)); do + # Check health endpoint + response=$(curl -k -s -w "%{http_code}" -o /tmp/harbor_health_response.json "$HARBOR_URL/api/v2.0/health") + http_code=${response: -3} + # Check if healthy + if [ "$http_code" -eq 200 ] && grep -q "\"status\":\"healthy\"" /tmp/harbor_health_response.json 2>/dev/null; then + rm -f /tmp/harbor_health_response.json + exit 0 + fi + # Wait before next check if not max retries reached + if [ $i -lt $MAX_RETRIES ]; then + sleep $CHECK_INTERVAL + fi + done + # Timeout reached + rm -f /tmp/harbor_health_response.json + echo "ERROR: Harbor service did not became health within 5 minutes!" + exit 1 + - name: Harbor | Configure HA and synchronize Harbor images when: - .image_registry.ha_vip | empty | not @@ -95,10 +120,28 @@ systemctl restart harbor.service - name: Harbor | Wait for Harbor service to become ready command: | - if ! timeout 300 bash -c 'while ! nc -zv localhost 443; do sleep 2; done'; then - echo "ERROR: Harbor did not start within 5 minutes!" - exit 1 - fi + HARBOR_URL="https://{{ .image_registry.auth.registry }}" + MAX_WAIT_SECONDS=300 + CHECK_INTERVAL=2 + MAX_RETRIES=$((MAX_WAIT_SECONDS / CHECK_INTERVAL)) + for ((i=1; i<=MAX_RETRIES; i++)); do + # Check health endpoint + response=$(curl -k -s -w "%{http_code}" -o /tmp/harbor_health_response.json "$HARBOR_URL/api/v2.0/health") + http_code=${response: -3} + # Check if healthy + if [ "$http_code" -eq 200 ] && grep -q "\"status\":\"healthy\"" /tmp/harbor_health_response.json 2>/dev/null; then + rm -f /tmp/harbor_health_response.json + exit 0 + fi + # Wait before next check if not max retries reached + if [ $i -lt $MAX_RETRIES ]; then + sleep $CHECK_INTERVAL + fi + done + # Timeout reached + rm -f /tmp/harbor_health_response.json + echo "ERROR: Harbor service did not became health within 5 minutes!" + exit 1 - name: Harbor | Synchronize harbor-replications script to remote host template: src: harbor-replications.sh