Last active
June 30, 2016 13:26
-
-
Save JensRantil/4173e4f1ecf031f3f263e5a1c2f5f861 to your computer and use it in GitHub Desktop.
Consul semaphore implementation in shell script. Unfinished.
This file contains hidden or 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/sh | |
| USAGE="Usage: $0 SERVICENAME CMD [CMDARGS1 ... CMDARGN]" | |
| if [ "$#" == "0" ]; then | |
| echo "$USAGE" | |
| exit 1 | |
| fi | |
| SERVICENAME=$1 | |
| shift | |
| CMD=$1 | |
| shift | |
| export CONSULSESSION=`curl --silent -X PUT -d "{"'"'"Name"'"'": "'"'"$SERVICENAME"'"'"}" 'http://localhost:8500/v1/session/create' | jq --raw-output .ID` | |
| echo Consul session: $CONSULSESSION | |
| # Delegate to command. | |
| $CMD "$@" | |
| curl --silent -X PUT http://localhost:8500/v1/session/destroy/$CONSULSESSION > /dev/null |
This file contains hidden or 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/sh | |
| USAGE="Usage: $0 PATHPREFIX LIMIT CMD [CMDARGS1 ... CMDARGN]" | |
| if [ "$#" == "0" ]; then | |
| echo "$USAGE" | |
| exit 1 | |
| fi | |
| PATHPREFIX=$1 | |
| shift | |
| LIMIT=$1 | |
| shift | |
| CMD=$1 | |
| shift | |
| CONTENDERKEY=$PATHPREFIX/$CONSULSESSION | |
| RESULT=`curl --silent -X PUT "http://localhost:8500/v1/kv/$CONTENDERKEY?acquire=$CONSULSESSION"` | |
| if [ "$RESULT" -!= "true" ];then | |
| echo "Could not create session contender. Session invalidation?" | |
| exit 1 | |
| fi | |
| # Try to take the semaphore | |
| LOCKKEY=$PATHPREFIX/.lock | |
| LOCKKEYCONTENT=`mktemp` | |
| LOCKKEYVALUE=`mktemp` | |
| while [ 1 ];do | |
| curl --silent "http://localhost:8500/v1/kv/$LOCKKEY" > $LOCKKEYCONTENT | |
| cat $LOCKKEYCONTENT | jq --raw-output .Value | base64 --decode >> $LOCKKEYVALUE | |
| if [[ -s $FILE ]] ; then | |
| for HOLDERSESSION in `cat $LOCKKEYVALUE | jq --raw-output .Holders`;do | |
| HOLDERSESSIONKEY=`curl --silent -XGET "http://localhost:8500/v1/kv/$PATHPREFIX/$HOLDERSESSION" | jq '.[0].Session'` | |
| if [ "$HOLDERSESSION" == ""]; then | |
| # TODO: Remove this holder session kv. | |
| # TODO: Remove this holder session in lock key. | |
| continue | |
| fi | |
| # TODO: Check if there is space for another lock. | |
| # TODO: If so, update lock file with CAS. | |
| # TODO: If not, wait for file to be updated with timeout, then `continue` this while loop | |
| else | |
| RESULT=`curl --silent -d "{"'"'"Limit"'"'": $LIMIT, "'"'"Holders: ["'"'"$CONSULSESSION"'"'"]}" -XPUT "http://localhost:8500/v1/kv/$LOCKKEY?cas=0"` | |
| if [ $RESULT -ne "true" ];then | |
| break | |
| fi | |
| fi | |
| done | |
| # We have the semaphore. Delegate to command. | |
| $CMD "$@" | |
| # TODO: Remove | |
| curl --silent -X DELETE http://localhost:8500/v1/session/destroy/$CONSULSESSION > /dev/null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment