Skip to content

Instantly share code, notes, and snippets.

@slok
Created March 30, 2017 10:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slok/d5faa3f402f72a17d75faa0b8d00fd31 to your computer and use it in GitHub Desktop.
Save slok/d5faa3f402f72a17d75faa0b8d00fd31 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Wait until consul instance on clust
INSTANCE_IP="127.0.0.1"
NEXT_WAIT_TIME=0
MAX_WAIT_TIME=35
check_consul_sync() {
# If we don't have leaders _last_log_index then get it
if [[ -z "${leader_last_log_index}" ]]; then
# Get leader
echo $(curl -s localhost:8500/v1/agent/self | jq -r '.Stats.consul')
leader_addr=$(curl -s localhost:8500/v1/agent/self | jq -r '.Stats.consul.leader_addr | split(":")[0] | select(. != null)')
if [[ -z "${leader_addr}" ]]; then
echo 'Not known leader'
return 1 # true
fi
echo "Leader addr: ${leader_addr}"
# Get last log index from leader
leader_last_log_index=$(curl -s ${leader_addr}:8500/v1/agent/self | jq -r '.Stats.raft.last_log_index')
echo "Leader agent last_log_index: ${leader_last_log_index}"
fi
# Get last log index from local agent
local_last_log_index=$(curl -s localhost:8500/v1/agent/self | jq -r '.Stats.raft.last_log_index')
echo "Local agent last_log_index: ${local_last_log_index}"
# Check local node greater or equal to leader
if [[ "${local_last_log_index}" -ge "${leader_last_log_index}" ]]; then
echo 'Synced!'
return 0 # true
fi
echo 'Not synced yet'
return 1 # False
}
until [[ "${NEXT_WAIT_TIME}" -eq "${MAX_WAIT_TIME}" ]] || check_consul_sync ; do
(( NEXT_WAIT_TIME++ ))
echo "Waiting ${NEXT_WAIT_TIME} seconds until next check... (${NEXT_WAIT_TIME}/${MAX_WAIT_TIME})"
sleep ${NEXT_WAIT_TIME}
done
# If we used our max retry the exit
if [[ "${NEXT_WAIT_TIME}" -eq "${MAX_WAIT_TIME}" ]]; then
echo 'Consul Server not ready'
false
else
echo 'Consul peer ready! :)'
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment