#!/bin/bash -ex
trap "echo Booh!" SIGINT SIGTERM

export DOCKER_HOST=dkmanager:2376
DKREGISTRY=dkregistry.xai-corp.net:5000

scale_out() {
    REPLICA_COUNT=$(docker service inspect $1 | jq '.[0].Spec.Mode.Replicated.Replicas')
    ./timeout.sh -t "$2" docker service scale $1=$(($REPLICA_COUNT*2))
    ./timeout.sh -t "$2" docker service scale $1=$(($REPLICA_COUNT))
    docker service update -q "$1"
}


#docker login -u richard -p $DKREGISTRY_PASS $DKREGISTRY
< ./password.txt docker login --username richard --password-stdin  $DKREGISTRY

docker stack deploy --with-registry-auth -c gitea/docker-compose.yml gitea &
docker stack deploy --with-registry-auth -c nextcloud/docker-compose-prod.yml xaibox &
docker stack deploy --with-registry-auth -c ui/docker-compose.yml dkui &
docker stack deploy --with-registry-auth -c jenkins/docker-compose.yml jenkins &
docker stack deploy --with-registry-auth -c letsencrypt/docker-compose-update.yml letsencrypt &
#docker stack deploy --with-registry-auth -c prometheus/docker-compose-prod.yml metrics &

wait
sleep 5

docker stack deploy --with-registry-auth -c sslproxy/docker-compose-prod.yml sslproxy


scale_out gitea_app 180 &
scale_out xaibox_app 180 &
scale_out dkui_app 180 &
scale_out jenkins_app 180 &
scale_out sslproxy_app 30 &

wait

