scripts/swarm_mode_test.sh
#!/bin/bash

set -euo pipefail

# Get the master node IP from the yml file generated by vagrant
contiv_master=$(grep -B 3 master cluster/.cfg_swarm-mode.yaml | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}" | head -n 1 | xargs)

node_os=${CONTIV_NODE_OS:-"centos"}
# Default user is vagrant for non-ubuntu and ubuntu for ubuntu boxes.
if [ "$node_os" == "ubuntu" ]; then
	def_user="ubuntu"
	def_key="$HOME/.ssh/id_rsa"
else
	def_user="vagrant"
	def_key=""
fi
user=${CONTIV_SSH_USER:-"$def_user"}

pushd cluster
ssh_key=${CONTIV_SSH_KEY:-"$def_key"}
if [ "$ssh_key" == "" ]; then
	ssh_key=$(vagrant ssh-config swarm-mode-master | grep IdentityFile | awk '{print $2}' | xargs)
fi
popd

# Extract and launch the installer
mkdir -p release
cd release
# If BUILD_VERSION is not defined, we use a local dev build, that must have been created with make release
release_name="contiv-${BUILD_VERSION:-devbuild}"
release_tarball="${release_name}.tgz"
release_local_tarball="contiv-full-${BUILD_VERSION}.tgz"
if [ -f "${release_local_tarball}" ]; then
	tar oxf "${release_local_tarball}"
else
	if [ ! -f "${release_tarball}" ]; then
		# For release builds, get the build from github releases
		curl -L -O https://github.com/contiv/install/releases/download/${BUILD_VERSION}/${release_name}.tgz
	fi
	tar oxf "${release_name}.tgz"
fi

cd $release_name
./install/ansible/install_swarm.sh -f ../../cluster/.cfg_swarm-mode.yaml -e $ssh_key -u $user -p

# Wait for CONTIV to start for up to 10 minutes
sleep 10
for i in {0..20}; do
	response=$(curl -k -s -H "Content-Type: application/json" -X POST -d '{"username": "admin", "password": "admin"}' https://$contiv_master:10000/api/v1/auth_proxy/login/ || true)
	if [[ $response == *"token"* ]]; then
		echo "Install SUCCESS"
		echo ""
		cat <<EOF
  NOTE: Because the Contiv Admin Console is using a self-signed certificate for this demo,
  you will see a security warning when the page loads.  You can safely dismiss it.
  
  You can access the Contiv master node with:
    cd cluster && vagrant ssh swarm-mode-master
EOF
		exit 0
	else
		echo "$i. Retry login to Contiv"
		sleep 30
	fi
done
echo "Install FAILED"
exit 1
