Browse Source

new: added ``get_docker_ip``, ``wait_docker_ip``, ``docker_add_host_declaration``.

raw-remaining-args
Valentin Lab 8 years ago
parent
commit
fea23e64a7
  1. 95
      bin/compose

95
bin/compose

@ -569,43 +569,74 @@ dcmd () {
}
export -f dcmd
get_docker_ip() {
local name="$1"
docker inspect --format='{{.NetworkSettings.IPAddress}}' "$name" 2>/dev/null
}
export -f get_docker_ip
wait_docker_ip() {
local name="$1" timeout="${2:-15}" timeout_count=0 docker_ip=
start=$SECONDS
while [ -z "$docker_ip" ]; do
sleep 0.5
docker_ip=$(get_docker_ip "$name") && break
elapsed=$((SECONDS - start))
if ((elapsed > timeout)); then
err "${RED}timeout error${NORMAL}(${timeout}s):" \
"Could not find '$name' docker container's IP."
return 1
fi
[ "$elapsed" == "$old_elapsed" ] ||
verb "Waiting for docker $name... ($elapsed/$timeout)"
old_elapsed="$elapsed"
done
verb "Found docker $name IP: $docker_ip"
echo "$docker_ip"
}
export -f wait_docker_ip
## Warning: requires a ``ddb`` matching current database to be checked
wait_for_docker_ip() {
local name=$1 timeout=5 timeout_count=0 DOCKER_IP= DB_OK=
while [ -z "$DOCKER_IP" ]; do
sleep 1
DOCKER_IP=$(docker inspect --format='{{.NetworkSettings.IPAddress}}' "$name" 2>/dev/null)
verb "[1/2] Waiting for docker $name... ($[timeout_count + 1]/$timeout)"
((timeout_count++)) || true
if [ "$timeout_count" == "$timeout" ]; then
err "${RED}timeout error${NORMAL}(${timeout}s):"\
"Could not find '$name' docker" \
"container's IP."
return 1
fi
done
verb "[1/2] Found docker $name IP: $DOCKER_IP"
timeout_count=0
DB_OK=
_set_db_params "$DOCKER_IP"
while [ -z "$DB_OK" ]; do
sleep 1
echo "SELECT 1;" | ddb >/dev/null && DB_OK=1
verb "[2/2] Waiting for db service from docker $name... ($[timeout_count + 1]/$timeout)"
((timeout_count++)) || true
if [ "$timeout_count" == "$timeout" ]; then
err "${RED}timeout error${NORMAL}(${timeout}s):"\
"Could not connect to db on $DOCKER_IP." \
"container's IP."
return 1
fi
done
verb "[2/2] Db is ready !"
echo "$DOCKER_IP"
return 0
local name=$1 timeout=5 timeout_count=0 DOCKER_IP= DB_OK=
DOCKER_IP=$(wait_docker_ip "$name" 5) || return 1
timeout_count=0
DB_OK=
_set_db_params "$DOCKER_IP"
while [ -z "$DB_OK" ]; do
sleep 1
echo "SELECT 1;" | ddb >/dev/null && DB_OK=1
verb "[2/2] Waiting for db service from docker $name... ($[timeout_count + 1]/$timeout)"
((timeout_count++)) || true
if [ "$timeout_count" == "$timeout" ]; then
err "${RED}timeout error${NORMAL}(${timeout}s):"\
"Could not connect to db on $DOCKER_IP." \
"container's IP."
return 1
fi
done
verb "[2/2] Db is ready !"
echo "$DOCKER_IP"
return 0
}
export -f wait_for_docker_ip
docker_add_host_declaration() {
local src_docker=$1 domain=$2 dst_docker=$3 dst_docker_ip=
dst_docker_ip=$(wait_docker_ip "$dst_docker") || exit 1
docker exec -i "$src_docker" bash <<EOF
if cat /etc/hosts | grep -E "^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\s+$domain\$" > /dev/null 2>&1; then
sed -ri "s/^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\s+$domain\$/$dst_docker_ip $domain/g" /etc/hosts
else
echo "$dst_docker_ip $domain" >> /etc/hosts
fi
EOF
}
export -f docker_add_host_declaration
##
## Arrays
##

Loading…
Cancel
Save