1#!/usr/bin/env bash
2#
3# Exposes a routine scripts can call to wait for a container if that container set up a health command
4#
5# Please source .ci/functions/imports.sh as a whole not just this file
6#
7# Version 1.0.1
8# - Initial version after refactor
9# - Make sure wait_for_contiainer is silent
10
11function wait_for_container {
12  set +x
13  until ! container_running "$1" || (container_running "$1" && [[ "$(docker inspect -f "{{.State.Health.Status}}" ${1})" != "starting" ]]); do
14    echo ""
15    docker inspect -f "{{range .State.Health.Log}}{{.Output}}{{end}}" ${1}
16    echo -e "\033[34;1mINFO:\033[0m waiting for node $1 to be up\033[0m"
17    sleep 2;
18  done;
19
20  # Always show logs if the container is running, this is very useful both on CI as well as while developing
21  if container_running $1; then
22    docker logs $1
23  fi
24
25  if ! container_running $1 || [[ "$(docker inspect -f "{{.State.Health.Status}}" ${1})" != "healthy" ]]; then
26    cleanup_all_in_network $2
27    echo
28    echo -e "\033[31;1mERROR:\033[0m Failed to start $1 in detached mode beyond health checks\033[0m"
29    echo -e "\033[31;1mERROR:\033[0m dumped the docker log before shutting the node down\033[0m"
30    return 1
31  else
32    echo
33    echo -e "\033[32;1mSUCCESS:\033[0m Detached and healthy: ${1} on docker network: ${network_name}\033[0m"
34    return 0
35  fi
36}
37