Created
January 22, 2018 20:24
-
-
Save qubitrenegade/88df929bdfb5d198a20ca6667ba33797 to your computer and use it in GitHub Desktop.
This bash script attempts to create and lock a Semaphore using Consul's session and KV Store
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
# This is an example based on: https://www.consul.io/docs/guides/semaphore.html | |
# And https://github.com/hashicorp/consul/issues/1940 | |
# we want to see how our variables resolve to URLs | |
set -x | |
# Set up some variables | |
SERVICE=foobartest | |
PREFIX=service/${SERVICE}/lock | |
LOCK=${PREFIX}/.lock | |
BASE_URL=http://consul01:8500 | |
SESSION=${BASE_URL}/v1/session/create | |
CURL='curl -s' | |
# do some debugging, we want to ensure these values are correct | |
echo "PREFIX: ${PREFIX}" | |
echo "SESSION: ${SESSION}" | |
echo "LOCK: ${LOCK}" | |
# Set up some functions to make our calls to the API easier | |
function get_session { | |
${CURL} -s -X PUT -d "{\"Name\": \"${SERVICE}\"}" $SESSION | jq -r .ID | |
} | |
function create_contender { | |
${CURL} -X PUT -d "{\"Name\": \"${1}\"}" ${BASE_URL}/v1/kv/${PREFIX}/${2}?acquire=${2} | |
} | |
# Create and print our session IDs | |
sess1=$(get_session) | |
sess2=$(get_session) | |
sess3=$(get_session) | |
echo "sess1: ${sess1}" | |
echo "sess2: ${sess2}" | |
echo "sess3: ${sess3}" | |
# Create our contenders, then print the result | |
contender1=$(create_contender 'foo', $sess1) | |
contender2=$(create_contender 'bar', $sess2) | |
contender3=$(create_contender 'baz', $sess3) | |
# these should all return true | |
echo "contender1: ${contender1}" | |
echo "contender2: ${contender2}" | |
echo "contender3: ${contender3}" | |
# curl the current state of the semaphore, this does not produce the same output as indicated by the documentation. | |
${CURL} "${BASE_URL}/v1/kv/${PREFIX}?recurse" |jq . | |
############## OUTPUT: | |
# + SERVICE=foobartest | |
# + PREFIX=service/foobartest/lock | |
# + LOCK=service/foobartest/lock/.lock | |
# + BASE_URL=http://consul01:8500 | |
# + SESSION=http://consul01:8500/v1/session/create | |
# + CURL='curl -s' | |
# + echo 'PREFIX: service/foobartest/lock' | |
# PREFIX: service/foobartest/lock | |
# + echo 'SESSION: http://consul01:8500/v1/session/create' | |
# SESSION: http://consul01:8500/v1/session/create | |
# + echo 'LOCK: service/foobartest/lock/.lock' | |
# LOCK: service/foobartest/lock/.lock | |
# ++ get_session | |
# ++ curl -s -s -X PUT -d '{"Name": "foobartest"}' http://consul01:8500/v1/session/create | |
# ++ jq -r .ID | |
# + sess1=1ae3df71-bd38-96b1-c575-9a6f75aa7f76 | |
# ++ get_session | |
# ++ curl -s -s -X PUT -d '{"Name": "foobartest"}' http://consul01:8500/v1/session/create | |
# ++ jq -r .ID | |
# + sess2=9b215f58-4893-a68e-7868-715c22dd4194 | |
# ++ get_session | |
# ++ curl -s -s -X PUT -d '{"Name": "foobartest"}' http://consul01:8500/v1/session/create | |
# ++ jq -r .ID | |
# + sess3=b4ddf605-2649-22cc-3a19-d9fcb6916cbf | |
# + echo 'sess1: 1ae3df71-bd38-96b1-c575-9a6f75aa7f76' | |
# sess1: 1ae3df71-bd38-96b1-c575-9a6f75aa7f76 | |
# + echo 'sess2: 9b215f58-4893-a68e-7868-715c22dd4194' | |
# sess2: 9b215f58-4893-a68e-7868-715c22dd4194 | |
# + echo 'sess3: b4ddf605-2649-22cc-3a19-d9fcb6916cbf' | |
# sess3: b4ddf605-2649-22cc-3a19-d9fcb6916cbf | |
# ++ create_contender foo, 1ae3df71-bd38-96b1-c575-9a6f75aa7f76 | |
# ++ curl -s -X PUT -d '{"Name": "foo,"}' 'http://consul01:8500/v1/kv/service/foobartest/lock/1ae3df71-bd38-96b1-c575-9a6f75aa7f76?acquire=1ae3df71-bd38-96b1-c575-9a6f75aa7f76' | |
# + contender1=true | |
# ++ create_contender bar, 9b215f58-4893-a68e-7868-715c22dd4194 | |
# ++ curl -s -X PUT -d '{"Name": "bar,"}' 'http://consul01:8500/v1/kv/service/foobartest/lock/9b215f58-4893-a68e-7868-715c22dd4194?acquire=9b215f58-4893-a68e-7868-715c22dd4194' | |
# + contender2=true | |
# ++ create_contender baz, b4ddf605-2649-22cc-3a19-d9fcb6916cbf | |
# ++ curl -s -X PUT -d '{"Name": "baz,"}' 'http://consul01:8500/v1/kv/service/foobartest/lock/b4ddf605-2649-22cc-3a19-d9fcb6916cbf?acquire=b4ddf605-2649-22cc-3a19-d9fcb6916cbf' | |
# + contender3=true | |
# + echo 'contender1: true' | |
# contender1: true | |
# + echo 'contender2: true' | |
# contender2: true | |
# + echo 'contender3: true' | |
# contender3: true | |
# + curl -s 'http://consul01:8500/v1/kv/service/foobartest/lock?recurse' | |
# + jq . | |
# [ | |
# { | |
# "ModifyIndex": 6149978, | |
# "CreateIndex": 6149978, | |
# "Session": "1ae3df71-bd38-96b1-c575-9a6f75aa7f76", | |
# "Value": "eyJOYW1lIjogImZvbywifQ==", | |
# "Flags": 0, | |
# "Key": "service/foobartest/lock/1ae3df71-bd38-96b1-c575-9a6f75aa7f76", | |
# "LockIndex": 1 | |
# }, | |
# { | |
# "ModifyIndex": 6149979, | |
# "CreateIndex": 6149979, | |
# "Session": "9b215f58-4893-a68e-7868-715c22dd4194", | |
# "Value": "eyJOYW1lIjogImJhciwifQ==", | |
# "Flags": 0, | |
# "Key": "service/foobartest/lock/9b215f58-4893-a68e-7868-715c22dd4194", | |
# "LockIndex": 1 | |
# }, | |
# { | |
# "ModifyIndex": 6149980, | |
# "CreateIndex": 6149980, | |
# "Session": "b4ddf605-2649-22cc-3a19-d9fcb6916cbf", | |
# "Value": "eyJOYW1lIjogImJheiwifQ==", | |
# "Flags": 0, | |
# "Key": "service/foobartest/lock/b4ddf605-2649-22cc-3a19-d9fcb6916cbf", | |
# "LockIndex": 1 | |
# } | |
# ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment