Skip to content

Commit

Permalink
Merge pull request #2 from mtscout6/rollback
Browse files Browse the repository at this point in the history
Added ability to rollback virtual box host only network configuration
  • Loading branch information
frosenberg committed Nov 18, 2014
2 parents 9f9bc4f + 917e3fe commit 4dca35a
Showing 1 changed file with 62 additions and 41 deletions.
103 changes: 62 additions & 41 deletions enable-docker-dns.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,47 @@
# It makes the following assumptions:
# - VirtualBox vboxnet0 can be used for the boot2docker-vm
# - docker0 bridge has the IP 172.17.42.1
#
#
# If these assumptions don't hold, this script may not work and needs to be improved.

function printOK {
if [ $? == 0 ];
then
echo "$(tput setaf 2)[OK]$(tput sgr0)"
else
echo "$(tput setaf 1)[FAIL]$(tput sgr0)"
fi
if [ $? == 0 ];
then
echo "$(tput setaf 2)[OK]$(tput sgr0)"
else
echo "$(tput setaf 1)[FAIL]$(tput sgr0)"
fi
}

function resetVBoxNet0 {
VBoxManage hostonlyif remove vboxnet0 > /dev/null 2>&1 # we ingore an error
VBoxManage hostonlyif create > /dev/null 2>&1
}

function waitForDockerDaemon {
status=1
while [ $status == 1 ]
do
docker version > /dev/null 2>&1
status=$?
sleep 3
done
printOK
status=1
attemptCount=0
while [ $status == 1 ] && [ $attemptCount -lt 20 ]
do
docker version > /dev/null 2>&1
status=$?
sleep 3
let "attemptCount += 1"
done

if [ $status == 0 ];
then
echo "$(tput setaf 2)[OK]$(tput sgr0)"
else
echo "$(tput setaf 1)[FAIL]$(tput sgr0)"
printf "*** Rolling back boot2docker-vm host only network configuration ... "
boot2docker stop > /dev/null 2>&1
resetVBoxNet0
boot2docker start > /dev/null 2>&1
printOK
echo "*** Failed to configure docker dns!"
exit 1
fi
}

# check if root or sudo
Expand All @@ -36,56 +56,57 @@ function waitForDockerDaemon {
# check if boot2docker is installed
command -v boot2docker > /dev/null 2>&1 || { echo >&2 "boot2docker command not found." ; exit 1; }


# check if boot2docker-vm exists
vm=`VBoxManage list vms | grep boot2docker-vm`
if [ $? == 1 ]; # VM does not exist
then
printf "*** boot2docker-vm not found. Creating ..."
boot2docker init > /dev/null 2>&1 || { echo >&2 " \nSkipping boot2docker (already ran)"; }
printOK
printf "*** boot2docker-vm not found. Creating ... "
boot2docker init > /dev/null 2>&1 || { echo >&2 " \nSkipping boot2docker (already ran)"; }
printOK
else
echo "*** Found existing boot2docker-vm"
echo "*** Found existing boot2docker-vm"
fi

printf "*** (Re-)Creating vboxnet0 adapter ..."
VBoxManage hostonlyif remove vboxnet0 > /dev/null 2>&1 # we ingore an error
VBoxManage hostonlyif create > /dev/null 2>&1
printf "*** (Re-)Creating vboxnet0 adapter ... "
resetVBoxNet0
printOK

printf "*** Configuring vboxnet0 adapter with 172.16.0.1/16 ..."
printf "*** Configuring vboxnet0 adapter with 172.16.0.1/16 ... "
VBoxManage hostonlyif ipconfig vboxnet0 --ip 172.16.0.1 --netmask 255.255.0.0
printOK

printf "*** Booting boot2docker-vm ... "
boot2docker up > /dev/null 2>&1
printf "*** Booting boot2docker-vm ... "
boot2docker up > /dev/null 2>&1
printOK

#printf "*** Setting nic2 to use vboxnet0 ..."
#printf "*** Setting nic2 to use vboxnet0 ... "
#VBoxManage controlvm boot2docker-vm nic2 hostonly vboxnet0
#printOK

printf "*** Configuring eth1 to 172.16.0.11/16 ..."
printf "*** Configuring eth1 to 172.16.0.11/16 ... "
boot2docker ssh "sudo ifconfig eth1 172.16.0.11 netmask 255.255.0.0"
printOK

printf "*** Setting up route from this host to containers ..."
printf "*** Setting up route from this host to containers ... "
route=`netstat -nr |grep 172\.17 | awk '{ print $1 }'`
if [ "$route" != "172.17" ];
then
sudo -i route -n add 172.17.0.0/16 172.16.0.11 > /dev/null
sudo -i route -n add 172.17.0.0/16 172.16.0.11 > /dev/null
fi
printOK

printf "*** Killing existing docker daemon ... "
docker_pid=`boot2docker ssh "pgrep /bin/docker"`
if [ $? == 0 ]; # we found a process to kill
then
boot2docker ssh "nohup /usr/bin/sudo sh -c 'kill -KILL ${docker_pid} < /dev/null > /dev/null'" > /dev/null
printOK
boot2docker ssh "nohup /usr/bin/sudo sh -c 'kill -KILL ${docker_pid} < /dev/null > /dev/null'" > /dev/null
printOK
fi

# TODO: Why pause here?
printf "*** Pausing execution for a moment ... "
sleep 30 # short wait used to cause troubles
printOK

printf "*** Prepare docker daemon to be used with SkyDock and SkyDNS ... "
boot2docker ssh "nohup /usr/bin/sudo sh -c '/usr/local/bin/docker -d -g /var/lib/docker -H unix:// -H tcp://0.0.0.0:2375 --bip=172.17.42.1/16 --dns=172.17.42.1 < /dev/null > /var/log/docker.log 2>&1 &'"
Expand All @@ -101,22 +122,22 @@ printf "*** Starting SkyDNS ... "
id=`docker ps | grep skydns:latest`
if [ $? == 1 ];
then
echo "--------"
docker run -d -p 172.17.42.1:53:53/udp --name skydns crosbymichael/skydns -nameserver 8.8.8.8:53 -domain docker
printOK
echo "--------"
docker run -d -p 172.17.42.1:53:53/udp --name skydns crosbymichael/skydns -nameserver 8.8.8.8:53 -domain docker
printOK
else
echo "$(tput setaf 3)[SKIPPED]$(tput sgr0)"
echo "$(tput setaf 3)[SKIPPED]$(tput sgr0)"
fi

printf "*** Starting SkyDock ... "
id=`docker ps | grep skydock:latest`
if [ $? == 1 ];
then
echo "--------"
docker run -d -v /var/run/docker.sock:/docker.sock --name skydock crosbymichael/skydock -ttl 30 -environment dev -s /docker.sock -domain docker -name skydns
printOK
echo "--------"
docker run -d -v /var/run/docker.sock:/docker.sock --name skydock crosbymichael/skydock -ttl 30 -environment dev -s /docker.sock -domain docker -name skydns
printOK
else
echo "$(tput setaf 3)[SKIPPED]$(tput sgr0)"
echo "$(tput setaf 3)[SKIPPED]$(tput sgr0)"
fi

echo "\n Add export DOCKER_HOST=\"tcp://:2375\" to your .bash_profile if you haven't already ..."
echo "\n Add export DOCKER_HOST=\"tcp://:2375\" to your .bash_profile if you haven't already ... "

0 comments on commit 4dca35a

Please sign in to comment.