Skip to content

Instantly share code, notes, and snippets.

@thpham
Created March 27, 2015 12:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thpham/5596479d7dd877d50f2b to your computer and use it in GitHub Desktop.
Save thpham/5596479d7dd877d50f2b to your computer and use it in GitHub Desktop.
Consul.io Leader discovery and election to invoque migration...
# check if leader exist (means associated session with the )
hasSession=$(curl -s http://localhost:8500/v1/kv/service/migration/leader | jq -r .[] | jq 'has("Session")')
if [ -z "$hasSession" ] || [ "$hasSession" = false ]; then
# create session
session=$(curl -s -X PUT -d '{"Name": "migration"}' http://localhost:8500/v1/session/create | jq -r .ID)
echo "session=$session"
leaderValue=$(curl -s http://localhost:8500/v1/kv/service/migration/leader | jq -r .[].Value | base64 --decode)
echo "leaderValue=$leaderValue"
if [ -z "$leaderValue" ] || [ "$leaderValue" != "JOB_DONE" ]; then
# try to put a lock
noLeader=$(curl -s -X PUT -d $HOSTNAME http://localhost:8500/v1/kv/service/migration/leader?acquire=$session)
echo "noLeader=$noLeader"
leaderValue=$(curl -s http://localhost:8500/v1/kv/service/migration/leader | jq -r .[].Value | base64 --decode)
echo "leaderValue=$leaderValue"
if [ -z "$leaderValue" ] || [ "$noLeader" = true ]; then
existingSession=$(curl -s http://localhost:8500/v1/kv/service/migration/leader | jq -r .[].Session)
echo "existingSession=$existingSession"
if [ "$session" = "$existingSession" ] && [ "$leaderValue" = "$HOSTNAME" ]; then
echo "Migration..."
sleep 1s
# execute the migration
# mark it as done (safe)
curl -s -X PUT -d 'JOB_DONE' http://localhost:8500/v1/kv/service/migration/leader
fi
fi
else
echo "JOB already done!"
fi
clearSession=$(curl -s -X PUT http://localhost:8500/v1/session/destroy/$session)
echo "Session cleared: $clearSession"
else
echo "Session Exist!"
existingSession=$(curl -s http://localhost:8500/v1/kv/service/migration/leader | jq -r .[].Session)
echo "existingSession=$existingSession"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment