Last active
August 18, 2021 19:12
-
-
Save steigr/e723d0a21e369c246d01 to your computer and use it in GitHub Desktop.
Download files from consul kv store and save them to a file or environment variables as base64'd gipped tar-archive.
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
#!/bin/bash | |
[[ "$TRACE" ]] && set -x | |
set -eo pipefail | |
need() { | |
command -v "${1:-$2}" > /dev/null 2>&1 \ | |
&& export "$1=$(command -v "${1:-$2}")" | |
} | |
vars() { | |
export CONSUL="${CONSUL:-http://localhost:8500}" | |
export ENV="${INLINE:+INLINE_BASE64}" | |
export ENV="$ENV" | |
} | |
save_from() { | |
[[ "$($uname)" = "Darwin" ]] && base64opt="-D" || base64opt="-d" | |
"$jq" -r --arg base "$1" --arg temp "$2" --arg base64 "$base64" --arg base64opt "$base64opt" \ | |
'.[] | "mkdir -p \"$(dirname \"" + ( .Key | sub($base;$temp) ) + "\")\"; echo " + .Value + " | " + $base64 + " " + $base64opt + " > " + ( .Key | sub($base;$temp) ) + ";"' | sh | |
} | |
download() { | |
url="$(printf '%s' "$1" | sed -e "s@^/v1/kv/@@")" | |
if [[ "$curl" ]]; then | |
"$curl" -s -L "${CONSUL}/v1/kv/$url/?recurse" | save_from "$url" "$2" | |
elif [[ "$wget" ]]; then | |
"$wget" -qO- "${CONSUL}/v1/kv/$url/?recurse" | save_from "$url" "$2" | |
fi | |
} | |
compress() { | |
"$tar" c * | "$gzip" -9c | "$base64" | "$xargs" -n1 printf | |
} | |
untarblette() { | |
need grep | |
need cut | |
need base64 | |
need tar | |
printenv \ | |
| "$grep" _BASE64 \ | |
| "$cut" -f2- -d= \ | |
| while read -r trblt; do \ | |
echo "$trblt" \ | |
| "$base64" -d \ | |
| "$tar" zvxC / | |
done | |
} | |
tarblette() { | |
need xargs | |
need mktemp | |
need curl || need wget | |
need jq | |
need base64 | |
need tar | |
need uname | |
need gzip | |
need sed | |
download "$1" "${temp:=$("$mktemp" -d)}" | |
pushd "$temp" >/dev/null | |
tarblette="${ENV}" | |
tarblette="${tarblette:-$2}" | |
tarblette="${tarblette:-$("$mktemp")}" | |
printf '%s%s=%s' "${ENV:+--env=}" "${ENV:-TGZ_BASE64}" "$(compress)" > "${tarblette//$ENV//dev/stdout}" | |
[[ -f "$tarblette" ]] && echo "--envfile $tarblette" | |
popd >/dev/null | |
rm -rf "$temp" | |
} | |
update() { | |
need curl | |
"$curl" -sfL https://gist.github.com/steigr/e723d0a21e369c246d01/raw | install -m 0755 /dev/stdin "$0" | |
} | |
vars | |
cmd=$(basename "$0") | |
if [[ "$1" = "update" ]]; then | |
update | |
exit $? | |
fi | |
[[ "${cmd:0:1}" = "u" ]] && set -- untarblette "$@" || set -- tarblette "$@" | |
"$@" |
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
#!/bin/bash | |
[[ "$TRACE" ]] && set -x | |
set -eo pipefail | |
usage() { | |
cat<<'EODOC' | |
Consul KV-Store to Tarblette converter | |
A Tarblette is an base64 encoded gzipped tar archive. | |
You may pass it to container runtimes like docker to inject config files via environment variables. | |
untarblette: | |
#!/bin/bash | |
untarblette() {} | |
for tgzb64 in $(printenv | grep _BASE64 | cut -f1); do | |
echo "${!tgzb64}" | base64 -d | tar zxC / | |
unset "$tgzb64" | |
done | |
} | |
Example (with docker): | |
1. Create an entry inside your consul kv named busybox/files/etc/test.conf with arbitrary content. | |
2. run the following commands (download this tool and run plain busybox container with untrblt) | |
# wget -qO/tmp/consulkv-to-tarblette https://gist.github.com/steigr/e723d0a21e369c246d01/raw | |
# chmod +x /tmp/consulkv-to-tarblette | |
# export CONSUL=http://localhost:8500 | |
# docker run --rm -ti -v/tmp/consulkv-to-tarblette:/bin/untrblt $(/tmp/consulkv-to-tarblette busybox/files) busybox sh -c 'untrblt; cat /etc/test.conf' | |
EODOC | |
exit 0 | |
} | |
need() { | |
command -v "${1:-$2}" > /dev/null 2>&1 \ | |
&& export "$1=$(command -v "${1:-$2}")" | |
} | |
vars() { | |
export CONSUL="${CONSUL:-http://localhost:8500}" | |
export ENV="${INLINE:+INLINE_BASE64}" | |
export ENV="$ENV" | |
need jq | |
need curl | |
need mktemp | |
need base64 | |
need xargs | |
need tar | |
need uname | |
need gzip | |
} | |
save_from() { | |
[[ "$($uname)" = "Darwin" ]] && base64opt="-D" || base64opt="-d" | |
"$jq" -r --arg base "$1" --arg temp "$2" --arg base64 "$base64" --arg base64opt "$base64opt" \ | |
'.[] | "mkdir -pv \"$(dirname \"" + ( .Key | sub($base;$temp) ) + "\")\"; echo " + .Value + " | " + $base64 + " " + $base64opt + " > " + ( .Key | sub($base;$temp) ) + ";"' | sh | |
} | |
download() { | |
"$curl" -s -L "${CONSUL}/v1/kv/${1}/?recurse" | save_from "$1" "$2" | |
} | |
compress() { | |
"$tar" c * | "$gzip" -9c | "$base64" | "$xargs" -n1 printf | |
} | |
untarblette() { | |
printenv \ | |
| grep _BASE64 \ | |
| cut -f2- -d= \ | |
| while read -r trblt; do \ | |
echo "$trblt" \ | |
| base64 -d \ | |
| tar zvxC / | |
done | |
exit $? | |
} | |
tarblette() { | |
download "$1" "${temp:=$("$mktemp" -d)}" | |
pushd "$temp" >/dev/null | |
tarblette="${ENV}" | |
tarblette="${tarblette:-$2}" | |
tarblette="${tarblette:-$("$mktemp")}" | |
printf '%s%s=%s' "${ENV:+--env }" "${ENV:-TGZ_BASE64}" "$(compress)" > "${tarblette//$ENV//dev/stdout}" | |
[[ -f "$tarblette" ]] && echo "--envfile $tarblette" | |
popd >/dev/null | |
rm -rf "$temp" | |
exit $? | |
} | |
vars | |
[[ "$1" = "--help" ]] && usage | |
[[ "${0:0:1}" = "u" ]] && untarblette "$@" | |
[[ "${0:0:1}" = "t" ]] && tarblette "$@" | |
exit 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment