vagrant-swarm/provision.sh

45 lines
1.4 KiB
Bash
Executable File

#!/bin/bash
######################################
### Install and setup Docker Swarm ###
######################################
# Print commands and exit on error
set -xe
# Install Docker
which curl &>/dev/null || (apt-get update && apt-get install -y curl)
which docker &>/dev/null || curl -fsSL https://get.docker.com | sh
[ ! "$(id -nG "$USER" | grep -c docker)" -eq 1 ] && usermod -aG docker "$USER"
# Setup Docker Swarm
if [ ! "$(docker info | grep -c 'Swarm: active')" -eq 1 ]; then
# Make hostname: node1 the leader who gives the join token to others
if [ "$(hostname)" == "node1" ]; then
docker swarm init | grep -Eo 'docker swarm join .+:[0-9]+' > /vagrant/.swarm
else
# Waits JOIN_TIMEOUT of seconds to find the swarm join token before giving up
START_TIME="$(date +%s)"
# Initial wait
sleep 5
# Wait until .swarm can be found via Vagrant provider file sharing
while [ ! -f /vagrant/.swarm ]; do
CURRENT_TIME="$(date +%s)"
DIFF_TIME="$((CURRENT_TIME - START_TIME))"
# Timeout
if [ "$DIFF_TIME" -ge "$JOIN_TIMEOUT" ]; then
echo "[ERROR]: $(hostname) waited $DIFF_TIME/$JOIN_TIMEOUT seconds"
exit 1
fi
# Waiting
echo "Waiting ($DIFF_TIME/$JOIN_TIMEOUT seconds) for /vagrant/.swarm file"
sleep 10
done
# /vagrant/.swarm file found, so join the swarm
/bin/bash /vagrant/.swarm
fi
fi