env.sh ClusterName 4
#!/bin/bash

virt_cpus=$3
virt_memory=8196
stack_name=$1
stack_size=$2
swarm_master=$4
echo "This will create a swarm master and n number of worker"
if [ -z "$virt_cpus" ]
then 
    virt_cpus=4
fi

join_token=$(docker-machine ls \
|grep "Running" |awk '{print $1}' \
|while read line; do eval $(docker-machine env "$line") && docker swarm join-token worker 2>/dev/null |grep "docker swarm join"; done) 


if [ -z "$join_token" ]
then 
    echo "Need to INIT $join_token"
fi

if [ -z $1 ]
then
    echo "Example: ./$(basename $0) SomeName NumberOfServers"
    exit 1
    else

    for i in $(seq $stack_size)
        do
join_token=$(docker-machine ls \
|grep "Running" |awk '{print $1}' \
|while read line; do eval $(docker-machine env "$line") && docker swarm join-token worker 2>/dev/null |grep "docker swarm join"; done | tail -1) 

            docker-machine create $stack_name-$i --virtualbox-cpu-count $virt_cpus --virtualbox-memory $virt_memory
            if [ -z "$join_token" ]
                then
                    eval $(docker-machine env $stack_name-$i)
                    docker swarm init --advertise-addr eth1
                else
                    eval $(docker-machine env $stack_name-$i)
                    $join_token
            fi
    done
fi
#Will error of not a manager node.
#eval $(docker-machine env $stack_name-1)
#docker stack deploy --with-registry-auth --compose-file docker-compose.yml your-stack-name