Skip to content

Instantly share code, notes, and snippets.

@clofresh
Created May 17, 2017 01:10
Show Gist options
  • Save clofresh/96e4e58f726c1314bdd320ed47dc0f50 to your computer and use it in GitHub Desktop.
Save clofresh/96e4e58f726c1314bdd320ed47dc0f50 to your computer and use it in GitHub Desktop.
atomic counter in consul
#!/bin/bash
KEY=$1
VAL=$2
MAX=${3-10}
if [ -z $KEY ]; then
echo "Must specify a key" >&2
exit 1
fi
if [ -z $VAL ]; then
echo "Must specify a value" >&2
exit 1
fi
for i in $(seq $MAX); do
OUTPUT=$(curl -s --fail -X PUT "http://localhost:8500/v1/kv/$KEY/$i?cas=0" -d "$VAL")
if [ $? -eq 0 ] && [[ $OUTPUT == true ]]; then
echo $i
exit 0
fi
done
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment