Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 184 lines (151 sloc) 4.83 KB
#!/bin/sh
#
# Author: Lucas Roesler <lucas@contiamo.com>
#
# Usage: ./swarm {deploy|teardown|leave|status}
#
# Examples:
#
# To create a new swarm:
#
# $ ./swarm deploy
#
# To see the current state of the vms in the swarm
#
# $ ./swarm status
#
# To teardown the swarm and vms
#
# $ ./swarm teardown
#
# To teardown the swarm but keep the vms
#
# $ ./swarm leave
#
STACKNAME="builderpoc"
HOSTNAME="builderpoc"
DOCKERMACHINEDRIVER="xhyve"
MANAGERNAME="manager1"
WORKERNAME="worker1"
REGISTRYNAME="privateregistry"
REGISTRYPORT="5001"
function setup {
echo "Creating swarm vms"
docker-machine create --driver $DOCKERMACHINEDRIVER $MANAGERNAME
docker-machine create --driver $DOCKERMACHINEDRIVER $WORKERNAME
echo "Setup manager node for the swarm"
MANAGERID=$(docker-machine ls --filter "name=$MANAGERNAME" --format {{.URL}} | grep -oE "([0-9]{1,3}\.){3}[0-9]{1,3}")
docker-machine ssh $MANAGERNAME "docker swarm init --advertise-addr $MANAGERID" 1>/dev/null
WORKERJOINTOKEN=$(docker-machine ssh $MANAGERNAME "docker swarm join-token worker -q")
echo "Setup worker nodes for the swarm"
docker-machine ssh $WORKERNAME "docker swarm join --token $WORKERJOINTOKEN $MANAGERID" 1>/dev/null
echo "Update node labels"
docker-machine ssh lab-manager1 "docker node update --label-add registry=true $MANAGERNAME" 1>/dev/null
echo "creating registry certs"
openssl req -batch -subj /CN=$REGISTRYNAME\
-newkey rsa:4096 -nodes -sha256 -keyout certs/domain.key \
-x509 -days 365 -out certs/domain.crt
echo "Copying registry certs to each machine"
docker-machine scp ./certs/domain.crt $MANAGERNAME:/tmp/ca.crt
docker-machine ssh $MANAGERNAME "sudo mkdir -p /etc/docker/certs.d/$REGISTRYNAME:$REGISTRYPORT/"
docker-machine ssh $MANAGERNAME "sudo mv /tmp/ca.crt /etc/docker/certs.d/$REGISTRYNAME:$REGISTRYPORT/ca.crt"
docker-machine scp ./certs/domain.crt $WORKERNAME:/tmp/ca.crt
docker-machine ssh $WORKERNAME "sudo mkdir -p /etc/docker/certs.d/$REGISTRYNAME:$REGISTRYPORT/"
docker-machine ssh $WORKERNAME "sudo mv /tmp/ca.crt /etc/docker/certs.d/$REGISTRYNAME:$REGISTRYPORT/ca.crt"
echo "# Run this command to configure your docker environment to use the lab-manager1 vm:"
echo "# eval \$(docker-machine env $MANAGERNAME)"
read -p "Would you like us to add $HOSTNAME name to your /etc/hosts for you? " yn
case $yn in
[Yy]* )
addhost
exit
;;
* )
echo "You must manually add this line to your /etc/hosts file"
echo "\t$(docker-machine ls --filter "name=$MANAGERNAME" --format {{.URL}} | grep -oE "([0-9]{1,3}\.){3}[0-9]{1,3}")\t$HOSTNAME"
echo "To use the default '$HOSTNAME' name in your browser."
;;
esac
exit
}
function leave_swarm {
echo "Tear down the swarm"
docker-machine ssh $WORKERNAME "docker swarm leave"
docker-machine ssh $MANAGERNAME "docker swarm leave --force"
}
function remove_vms {
echo "Tear down the vms"
docker-machine rm $WORKERNAME $MANAGERNAME
}
function teardown {
leave_swarm
remove_vms
removehost
exit
}
function status {
docker-machine ssh $MANAGERNAME "docker node ls"
}
function addhost {
echo "Adding $HOSTNAME to your /etc/hosts, this will require your sudo password"
WORKERIP=$(docker-machine ls --filter "name=$MANAGERNAME" --format {{.URL}} | grep -oE "([0-9]{1,3}\.){3}[0-9]{1,3}")
sudo -- sh -c -e "echo '$WORKERIP\t$HOSTNAME' >> /etc/hosts"
}
function removehost {
if [ -n "$(grep $HOSTNAME /etc/hosts)" ]
then
read -p "Would you like remove $HOSTNAME from your /etc/hosts? " yn
if [[ $yn =~ [Yy]* ]]
then
echo "This requires your sudo password"
sudo -- sh -c -e "sed -i '.bak' '/$HOSTNAME/d' /etc/hosts >> /etc/hosts"
fi
else
echo "$HOSTNAME was not found in your /etc/hosts";
fi
}
function deploy_stack {
echo "Deploying the $STACKNAME stack"
docker stack deploy $STACKNAME --prune --compose-file docker-compose.yaml #2>/dev/null
docker stack ps $STACKNAME
echo "Done"
}
function build_image {
docker build -f Dockerfile -t theaxer/builderpoc:latest .
}
function stop {
docker stack rm builderpoc
}
function usage {
echo $"Usage: $0 {init|build|deploy|stop|status|teardown|leave}"
echo ""
echo "\tCreate or destroy a local $STACKNAME docker swarm."
}
case "$1" in
init)
setup
;;
leave)
leave_swarm
;;
teardown)
teardown
;;
status)
status
;;
deploy)
eval $(docker-machine env $MANAGERNAME)
deploy_stack
;;
stop)
eval $(docker-machine env $MANAGERNAME)
stop
;;
build)
build_image
;;
*)
usage
exit 1
esac