Last active
December 11, 2018 20:16
-
-
Save michaelbaudino/6f2128f95fc2e4a7d7708561cc473e3e to your computer and use it in GitHub Desktop.
Docker Sync start script (only returns once initial sync is complete for all endpoints)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
DOCKER_SYNC_LOGFILE="/tmp/docker-sync.log" | |
DOCKER_SYNC_SCREEN_NAME="docker-sync" | |
function start_docker_sync_screen_session { | |
# We start `docker-sync start` twice because it often updates some images on first run and when it does, | |
# it exits successfully but asking for a `docker-sync clean` and then another `docker-sync start` run ¯\_(ツ)_/¯ | |
local cmd="docker-sync clean && docker-sync start && docker-sync clean && docker-sync start" | |
screen -d -m -S ${DOCKER_SYNC_SCREEN_NAME} script -F ${DOCKER_SYNC_LOGFILE} bash -c "${cmd}" | |
} | |
function kill_docker_sync_screen_session { | |
screen -dr ${DOCKER_SYNC_SCREEN_NAME} -X quit | |
} | |
function print_ids_of_ready_sync_containers { | |
[[ ! -f ${DOCKER_SYNC_LOGFILE} ]] && return 0 | |
cat ${DOCKER_SYNC_LOGFILE} | grep "Connected \[//" | sed -nE "s|//($(hostname))//|//__HOST_NAME__//|p" | sed -nE "s|.*//([0-9a-f]+)//.*|\1|p" | sort | uniq | |
} | |
function print_names_of_ready_sync_containers { | |
print_ids_of_ready_sync_containers | sed 's/^/-f id=/' | xargs docker ps --format '{{.Names}}' | sort | uniq | |
} | |
function wait_for_docker_sync { | |
local expected_sync_services=$(docker-sync list | awk '{print $1}' | grep '.-sync$' | sort) | |
trap kill_docker_sync_screen_session EXIT | |
echo "Waiting for Docker Sync initial sync to be completed... (it may take up to a few minutes)" | |
while [[ $(print_names_of_ready_sync_containers) != ${expected_sync_services} ]]; do | |
sleep 10 | |
done | |
trap - EXIT | |
} | |
if screen -ls | awk '{print $1}' | grep -q "^[0-9]\+.${DOCKER_SYNC_SCREEN_NAME}\$"; then | |
echo "Docker Sync is already running" | |
else | |
start_docker_sync_screen_session && wait_for_docker_sync \ | |
&& echo "Docker Sync successfully started in background" \ | |
|| echo "Failed to start Docker Sync in background" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment