Skip to content

Instantly share code, notes, and snippets.

@diogok
Last active December 6, 2015 07:55
Show Gist options
  • Save diogok/9604900 to your computer and use it in GitHub Desktop.
Save diogok/9604900 to your computer and use it in GitHub Desktop.
Register running docker containers and vagrant vms into ETCD and reads them out into ENVironment vars.
#!/bin/bash
# this script tries to turn your etcd into env vars
[[ ! $EHOST ]] && EHOST="$(hostname -I | awk '{ print $1 }')"
[[ ! $EPORT ]] && EPORT=4001
[[ ! $ETCD ]] && ETCD="http://${EHOST}:$EPORT"
ETCD_FILE="/tmp/$(date +%s).etcd.json"
wget "$ETCD/v2/keys/?recursive=true" -O $ETCD_FILE
ENV=$(cat $ETCD_FILE | grep '"[^"]\+","value":"[^"]\+"' -o | sed -e 's/"value"://g' -e 's/"//g' -e 's/^\///g' -e 's/\//_/' -e 's/,/=/' -e 's/^\([^=]\+\)/\U\1/' -e 's/^/export /g')
eval "$ENV"
echo "$ENV"
#!/bin/bash
# This scripts tries to catalog running docker containers into etcd
[[ ! $EHOST ]] && EHOST="$(hostname -I | awk '{ print $2 }')"
[[ ! $EPORT ]] && EPORT=4001
[[ ! $ETCD ]] && ETCD="http://${EHOST}:${EPORT}"
IP=$(hostname -I | awk '{ print $2 }')
HOST=$IP # should resolve this sometime
for CONTAINER in $(docker ps | tail -n+2 | awk '{ print $1 }'); do
# the name is from IMAGE column, as: vendor/name:tag
# what if -name is specified?
BOX=$(docker ps | grep $CONTAINER | awk '{ print $2 }' | sed -e 's/^.*\///' -e 's/:.*$//')
curl -L "$ETCD/v2/keys/$BOX/ip" -d value="$IP" -X PUT
curl -L "$ETCD/v2/keys/$BOX/host" -d value="$HOST" -X PUT
PORTS=$(docker ps | grep $CONTAINER | grep '[0-9]\+->[0-9]\+' -o)
PORT=""
for PORT in $PORTS; do
DEST=$(echo ${PORT//->/ } | awk '{ print $1 }')
ORIG=$(echo ${PORT//->/ } | awk '{ print $2 }')
# last non 22 port will be used as main
# must improve this
[[ $ORIG != "22" ]] && PORT=$DEST
done
curl -L "$ETCD/v2/keys/$BOX/port" -d value="$PORT" -X PUT
curl -L "$ETCD/v2/keys/$BOX/url" -d value="http://$IP:$PORT" -X PUT
curl -L "$ETCD/v2/keys/$BOX/timestamp" -d value="$(date +%s)" -X PUT
done
#!/bin/bash
[[ ! $EHOST ]] && EHOST="$(hostname -I | awk '{ print $2 }')"
[[ ! $ETCD ]] && ETCD="http://${EHOST}:4001"
echo $PWD
for BOX in $(ps aux | grep VBoxHeadless | grep -o "comment [a-zA-Z-]\+_default" | awk '{print $2}' | grep -o '^[^_]\+'); do
printf "\n"
echo "$BOX"
DIR=$(find ~/ -name $BOX -type d -print) # enter first folder in user home with this name
echo "$DIR"
cd $DIR
TBOX=$(cat Vagrantfile | grep -o '#name:.*') ## ??
[[ $TBOX ]] && BOX=$(TBOX/name:/)
# grep private_network ip from vagrantfile
IP=$(cat Vagrantfile | grep '"\?:\?private_network"\?, \?"\?:\?ip:\?"\? \?"\([^"]\+\)"' -o | awk '{print $3}')
IP=${IP:1:-1}
HOST=$IP # resolv?
curl -L "$ETCD/v2/keys/$BOX/ip" -d value="$IP" -X PUT
curl -L "$ETCD/v2/keys/$BOX/host" -d value="$HOST" -X PUT
PORTS=$(cat Vagrantfile | grep ':forwarded_port, host: \([0-9]\+\), guest: \([0-9]\+\)' -o | awk '{ print $3 $5 }')
for PORT in $PORTS; do
DEST=$(echo ${PORT//,/ } | awk '{ print $1 }')
ORIG=$(echo ${PORT//,/ } | awk '{ print $2 }')
curl -L "$ETCD/v2/keys/$BOX/port_$ORIG" -d value="$DEST" -X PUT
done
PORT=$(echo ${PORTS//,/ } | awk '{ print $2 }') # assume first forwarded port as main port
curl -L "$ETCD/v2/keys/$BOX/port" -d value="$PORT" -X PUT
curl -L "$ETCD/v2/keys/$BOX/url" -d value="http://$IP:$PORT" -X PUT
curl -L "$ETCD/v2/keys/$BOX/timestamp" -d value="$(date +%s)" -X PUT
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment