Created
September 3, 2014 10:51
-
-
Save charlesmarshall/03eca5a47a92fb8a053c to your computer and use it in GitHub Desktop.
Cascading data fetch from etcd to docker -e string
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
etcdTestData(){ | |
etcdctl set /env/globals/DB_HOST "global_database" 1> /dev/null | |
etcdctl set /env/staging/DB_HOST "staging_database" 1> /dev/null | |
etcdctl set /env/repo1/DB_HOST "repo1_database" 1> /dev/null | |
etcdctl set /env/repo2/staging/DB_HOST "repo2_staging_database" 1> /dev/null | |
} | |
## | |
# this will fetch data from: | |
# /env/globals | |
# /env/${env}/ | |
# /env/${repo} | |
# /env/${repo}/${env} | |
# in a cascading fashion, overwriting as it gets more specific | |
# this allows us to set things like: | |
# /env/globals/DB_HOST | |
# /env/staging/DB_HOST and pick the correct one depending | |
# | |
environmentFromEtcd(){ | |
local env=$1 | |
local repo=$2 | |
local global=$3 | |
declare -a loop_order=("/env/${global}" "/env/${env}" "/env/${repo}" "/env/${repo}/${env}") | |
declare -A envs=() | |
# loop over of tree order, generate a key based array that get overwritten | |
for x in ${!loop_order[@]}; do | |
local k=${loop_order[$x]} | |
for i in $(etcdctl ls $k 2> /dev/null); do | |
key=$(echo $i | sed -r "s#$k/##gi" | tr '[:lower:]' '[:upper:]') ; | |
val=$(etcdctl get ${i} 2> /dev/null); | |
if [ ! -z $val ]; then | |
envs[$key]=${val}; | |
fi | |
done; | |
done | |
local env_string='' | |
#create a string from the key based array | |
for y in ${!envs[@]}; do | |
v=${envs[$y]} | |
env_string="${env_string}-e ${y}=${v} " | |
done | |
echo " ${env_string} " | |
} | |
# generate some data | |
etcdTestData | |
# fetch the data | |
docker_string_prod_repo=$(environmentFromEtcd "production" "repo" "globals") | |
echo "Production string for repo:" | |
echo "----> ${docker_string_prod_repo}" | |
docker_string_prod_repo1=$(environmentFromEtcd "production" "repo1" "globals") | |
echo "Production string for repo1:" | |
echo "----> ${docker_string_prod_repo1}" | |
docker_string_staging1=$(environmentFromEtcd "staging" "repo" "globals") | |
echo "Staging string for repo:" | |
echo "----> ${docker_string_staging1}" | |
docker_string_staging2=$(environmentFromEtcd "staging" "repo2" "globals") | |
echo "Staging string for repo2:" | |
echo "----> ${docker_string_staging2}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment