Last active
April 2, 2018 18:15
-
-
Save alkalinecoffee/c6b76457fcd47ddffad21d8a2b87ac2a to your computer and use it in GitHub Desktop.
Bash script to recursively copy consul KVs from one directory to another
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/bash | |
# Recursively copies keys from KEYS_TO_COPY and places them under TO_DIR directory, ie | |
# /alertmanager => /services/alertmanager | |
CONSUL_HOST=http://consul.example.com:443 | |
KEYS_TO_COPY=$(cat <<-END | |
alertmanager | |
consul | |
grafana | |
prometheus | |
END | |
) | |
TO_DIR="/services" | |
while read -r line; do | |
curl $CONSUL_HOST/v1/kv/$line?recurse 2>/dev/null | jq -r '.[] | [.Key, .Value] | join(" ")' | | |
while read line; do | |
KEY=$TO_DIR/$(echo $line | awk '{print $1}') | |
VAL=$(echo $line | awk '{print $2}' | base64 --decode) | |
echo Writing: $KEY | |
curl -XPUT -d "$VAL" "$CONSUL_HOST/v1/kv/$KEY" | |
echo | |
done; | |
done <<< "$KEYS_TO_COPY" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment