#/bin/bash

#
#  docker swarm setup is:
#    - apollo jenkins_master
#    - artemis,apollo jenkins_slave
#
set -e

if [ `whoami` != "otptest" ]; then
   echo "Must run as otptest user"
   exit 1;
fi

docker build --network host -t sekidocker.rnd.ki.sw.ericsson.se/proj-erlang/jenkins -f Dockerfile.jenkins --pull \
       --build-arg https_proxy=${http_proxy} --build-arg http_proxy=${http_proxy} \
       --build-arg HTTP_PROXY=${http_proxy} --build-arg HTTPS_PROXY=${http_proxy} \
       --build-arg NO_PROXY=${no_proxy} --build-arg no_proxy=${no_proxy} .
docker push sekidocker.rnd.ki.sw.ericsson.se/proj-erlang/jenkins

docker build --network host -t sekidocker.rnd.ki.sw.ericsson.se/proj-erlang/jenkins-swarm-agent -f Dockerfile.jenkins-swarm-agent --pull \
       --build-arg https_proxy=${http_proxy} --build-arg http_proxy=${http_proxy} \
       --build-arg HTTP_PROXY=${http_proxy} --build-arg HTTPS_PROXY=${http_proxy} \
       --build-arg NO_PROXY=${no_proxy} --build-arg no_proxy=${no_proxy} .
docker push sekidocker.rnd.ki.sw.ericsson.se/proj-erlang/jenkins-swarm-agent

# docker stack rm jenkins
# echo "Sleeping for 15 seconds in order for the swarm to clear all network elements"
# echo 'if the sleep fails, do "docker stack deploy --with-registry-auth -c jenkins.yml jenkins"'
# sleep 15 # Sleep to let the default stack networks be deleted
# docker stack deploy --with-registry-auth -c jenkins.yml jenkins
