Browse Source

new: database query now support new networks in docker.

raw-remaining-args
Valentin Lab 6 years ago
parent
commit
2164311f10
  1. 76
      bin/compose

76
bin/compose

@ -401,6 +401,7 @@ _clean_docker() {
debug "Removing container $_DB_NAME" debug "Removing container $_DB_NAME"
docker stop "$container_id" docker stop "$container_id"
docker rm "$_DB_NAME" docker rm "$_DB_NAME"
docker network rm "${_DB_NAME}"
rm -vf "/tmp/${_DB_NAME}.state" rm -vf "/tmp/${_DB_NAME}.state"
) )
} }
@ -443,12 +444,16 @@ export -f are_files_locked_in_dir
export _PID="$$" export _PID="$$"
ensure_db_docker_running () { ensure_db_docker_running () {
local _STATE_FILE
_DB_NAME="db_${DB_NAME}_${_PID}" _DB_NAME="db_${DB_NAME}_${_PID}"
if [ -e "/tmp/${_DB_NAME}.state" ]; then
export DOCKER_IP="$(cat "/tmp/${_DB_NAME}.state")"
_STATE_FILE=/tmp/${_DB_NAME}.state
if [ -e "$_STATE_FILE" ]; then
IFS=: read DOCKER_NETWORK DOCKER_IP <<<"$(cat "$_STATE_FILE")"
debug "Re-using previous docker/connection '$DOCKER_IP'." debug "Re-using previous docker/connection '$DOCKER_IP'."
_set_db_params "$DOCKER_IP"
_set_db_params "$DOCKER_IP" "$DOCKER_NETWORK"
return 0 return 0
fi fi
@ -458,7 +463,7 @@ ensure_db_docker_running () {
err "Currently figuring up DOCKER_IP, please set it yourself before this call if needed." err "Currently figuring up DOCKER_IP, please set it yourself before this call if needed."
return 1 return 1
else else
debug "Ignoring recursive call."
debug "ignoring recursive call of 'ensure_db_docker_running'."
fi fi
return 0 return 0
fi fi
@ -499,15 +504,24 @@ ensure_db_docker_running () {
verb "Found and using '$DB_PASSFILE'." verb "Found and using '$DB_PASSFILE'."
docker_opts="$db_docker_opts -v $SERVER_ROOT_PREFIX$DB_PASSFILE:$DB_PASSFILE" docker_opts="$db_docker_opts -v $SERVER_ROOT_PREFIX$DB_PASSFILE:$DB_PASSFILE"
fi fi
debug docker network create "$_DB_NAME"
if ! network_id=$(docker network create "$_DB_NAME"); then
err "'docker network create' failed !"
_clean_docker "$_DB_NAME" "$container_id"
rm "/tmp/${_DB_NAME}.working"
return 1
fi
debug docker run -d \ debug docker run -d \
--name "$_DB_NAME" \ --name "$_DB_NAME" \
$docker_opts \ $docker_opts \
--network "$_DB_NAME" \
-v "$host_db_working_dir:$DB_DATADIR" \ -v "$host_db_working_dir:$DB_DATADIR" \
"$DOCKER_BASE_IMAGE" "$DOCKER_BASE_IMAGE"
if ! container_id=$( if ! container_id=$(
docker run -d \ docker run -d \
--name "$_DB_NAME" \ --name "$_DB_NAME" \
$docker_opts \ $docker_opts \
--network "$_DB_NAME" \
-v "$host_db_working_dir:$DB_DATADIR" \ -v "$host_db_working_dir:$DB_DATADIR" \
"$DOCKER_BASE_IMAGE" "$DOCKER_BASE_IMAGE"
); then ); then
@ -517,15 +531,14 @@ ensure_db_docker_running () {
return 1 return 1
fi fi
trap_add EXIT,ERR "_clean_docker '$_DB_NAME' '$container_id'" trap_add EXIT,ERR "_clean_docker '$_DB_NAME' '$container_id'"
fi fi
if DOCKER_IP=$(wait_for_docker_ip "$container_id"); then
echo "$DOCKER_IP" > "/tmp/${_DB_NAME}.state"
debug "written /tmp/${_DB_NAME}.state"
if docker_ip=$(wait_for_docker_ip "$container_id"); then
IFS=: read DOCKER_NETWORK DOCKER_IP <<<"$docker_ip"
echo "$docker_ip" > "$_STATE_FILE"
debug "written '$_STATE_FILE'"
rm "/tmp/${_DB_NAME}.working" rm "/tmp/${_DB_NAME}.working"
_set_db_params "$DOCKER_IP"
_set_db_params "$DOCKER_IP" "$DOCKER_NETWORK"
return 0 return 0
else else
err "Db not found. Docker logs follows:" err "Db not found. Docker logs follows:"
@ -550,10 +563,10 @@ _dcmd() {
fi fi
debug docker run -i --rm \ debug docker run -i --rm \
$db_docker_opts \
$db_docker_opts --network $_DB_NAME \
--entrypoint "$command" "$DOCKER_BASE_IMAGE" $db_cmd_opts "$@" --entrypoint "$command" "$DOCKER_BASE_IMAGE" $db_cmd_opts "$@"
docker run -i --rm \ docker run -i --rm \
$db_docker_opts \
$db_docker_opts --network $_DB_NAME \
--entrypoint "$command" "$DOCKER_BASE_IMAGE" $db_cmd_opts "$@" --entrypoint "$command" "$DOCKER_BASE_IMAGE" $db_cmd_opts "$@"
} }
export -f _dcmd export -f _dcmd
@ -577,12 +590,26 @@ dcmd () {
export -f dcmd export -f dcmd
get_docker_ips() {
local name="$1" ip format network_id
if ! docker inspect --format='{{ .NetworkSettings.Networks }}' "$name" >/dev/null 2>&1; then
echo "default:$(docker inspect --format='{{ .NetworkSettings.IPAdress }}' "$name" 2>/dev/null)"
else
format='{{range $name, $conf := .NetworkSettings.Networks}}{{$name}}{{"\x00"}}{{$conf.IPAddress}}{{"\x00"}}{{end}}'
while read-0 network_id ip; do
printf "%s:%s\n" "$network_id" "$ip"
done < <(docker inspect --format="$format" "$name")
fi
}
export -f get_docker_ips
get_docker_ip() { get_docker_ip() {
local name="$1" local name="$1"
docker inspect --format='{{.NetworkSettings.IPAddress}}' "$name" 2>/dev/null
get_docker_ips "$name" | grep "^${_DB_NAME}:"
} }
export -f get_docker_ip export -f get_docker_ip
wait_docker_ip() { wait_docker_ip() {
local name="$1" timeout="${2:-15}" timeout_count=0 docker_ip= local name="$1" timeout="${2:-15}" timeout_count=0 docker_ip=
start=$SECONDS start=$SECONDS
@ -599,18 +626,24 @@ wait_docker_ip() {
verb "Waiting for docker $name... ($elapsed/$timeout)" verb "Waiting for docker $name... ($elapsed/$timeout)"
old_elapsed="$elapsed" old_elapsed="$elapsed"
done done
verb "Found docker $name IP: $docker_ip"
verb "Found docker $name network and IP: $docker_ip"
echo "$docker_ip" echo "$docker_ip"
} }
export -f wait_docker_ip export -f wait_docker_ip
## Warning: requires a ``ddb`` matching current database to be checked ## Warning: requires a ``ddb`` matching current database to be checked
wait_for_docker_ip() { wait_for_docker_ip() {
local name=$1 timeout=5 timeout_count=0 DOCKER_IP= DB_OK=
DOCKER_IP=$(wait_docker_ip "$name" 5) || return 1
local name=$1 timeout=15 timeout_count=0 DOCKER_IP= DOCKER_NETWORK= docker_ips= docker_ip= DB_OK=
docker_ip=$(wait_docker_ip "$name" 5) || return 1
IFS=: read DOCKER_NETWORK DOCKER_IP <<<"$docker_ip"
if ! str_is_ipv4 "$DOCKER_IP"; then
err "internal 'wait_docker_ip' did not return a valid IP. Returned IP is '$DOCKER_IP'."
return 1
fi
timeout_count=0 timeout_count=0
DB_OK= DB_OK=
_set_db_params "$DOCKER_IP"
_set_db_params "$DOCKER_IP" "$DOCKER_NETWORK"
while [ -z "$DB_OK" ]; do while [ -z "$DB_OK" ]; do
sleep 1 sleep 1
echo "SELECT 1;" | ddb >/dev/null && DB_OK=1 echo "SELECT 1;" | ddb >/dev/null && DB_OK=1
@ -618,21 +651,24 @@ wait_for_docker_ip() {
((timeout_count++)) || true ((timeout_count++)) || true
if [ "$timeout_count" == "$timeout" ]; then if [ "$timeout_count" == "$timeout" ]; then
err "${RED}timeout error${NORMAL}(${timeout}s):"\ err "${RED}timeout error${NORMAL}(${timeout}s):"\
"Could not connect to db on $DOCKER_IP." \
"Could not connect to db on $DOCKER_IP" \
"container's IP." "container's IP."
return 1 return 1
fi fi
done done
verb "[2/2] Db is ready !" verb "[2/2] Db is ready !"
echo "$DOCKER_IP"
echo "${DOCKER_NETWORK}:${DOCKER_IP}"
return 0 return 0
} }
export -f wait_for_docker_ip export -f wait_for_docker_ip
docker_add_host_declaration() { docker_add_host_declaration() {
local src_docker=$1 domain=$2 dst_docker=$3 dst_docker_ip=
local src_docker=$1 domain=$2 dst_docker=$3 dst_docker_ip= dst_docker_network
dst_docker_ip=$(wait_docker_ip "$dst_docker") || exit 1 dst_docker_ip=$(wait_docker_ip "$dst_docker") || exit 1
IFS=: read dst_docker_ip dst_docker_network <<<"$dst_docker_ip"
docker exec -i "$src_docker" bash <<EOF 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 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 sed -ri "s/^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\s+$domain\$/$dst_docker_ip $domain/g" /etc/hosts

Loading…
Cancel
Save