Skip to content

Instantly share code, notes, and snippets.

@charlesmarshall
Created September 3, 2014 10:51
Show Gist options
  • Save charlesmarshall/03eca5a47a92fb8a053c to your computer and use it in GitHub Desktop.
Save charlesmarshall/03eca5a47a92fb8a053c to your computer and use it in GitHub Desktop.
Cascading data fetch from etcd to docker -e string
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