wait-healthy 947 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/bin/bash
  2. DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  3. . "$DIR/.common.sh"
  4. if [ "$1" == "" ]; then
  5. echo "Waits for a docker container to be healthy."
  6. echo " Usage: $0 docker-container 30"
  7. echo "or use the third parameter to use the docker healthcheck instead of the internal one."
  8. echo " Usage: $0 docker-container 30 true"
  9. exit 1
  10. fi
  11. SERVICE=$1
  12. LIMIT=${2:-90}
  13. USE_DOCKER_HEALTHCHECK=${3:-false}
  14. echo -e "${BLUE}❯ ${CYAN}Waiting for healthy: ${YELLOW}${SERVICE}${RESET}"
  15. is_up() {
  16. if [ "$USE_DOCKER_HEALTHCHECK" == "true" ]; then
  17. docker inspect --format='{{.State.Health.Status}}' "$SERVICE" | grep -qi "healthy"
  18. else
  19. docker exec "$SERVICE" /bin/healthcheck.sh
  20. fi
  21. }
  22. i=0
  23. while ! is_up; do
  24. i=$((i + 1))
  25. if [ "$i" == "$LIMIT" ]; then
  26. echo -e "${BLUE}❯ ${RED}Timed out waiting for healthy${RESET}"
  27. docker logs --tail 50 "$SERVICE"
  28. exit 1
  29. fi
  30. sleep 1
  31. done
  32. echo ""
  33. echo -e "${BLUE}❯ ${GREEN}Healthy!${RESET}"