# one key deploy km
echo '[KM] pull all image'
\rm -f ./docker_pull_image.sh
cat middleware-master-standalone.yml        |  grep 'image: ' | sed -e 's/image: /docker pull /g' |sed -e 's/\r//g' >> docker_pull_image.sh
cat docker-compose-deploy-searchengine.yml  |  grep 'image: ' | sed -e 's/image: /docker pull /g' |sed -e 's/\r//g' >> docker_pull_image.sh
cat docker-compose-deploy-edoc2.yml         |  grep 'image: ' | sed -e 's/image: /docker pull /g' |sed -e 's/\r//g' >> docker_pull_image.sh
cat docker-compose-deploy-inbiz.yml         |  grep 'image: ' | sed -e 's/image: /docker pull /g' |sed -e 's/\r//g' >> docker_pull_image.sh
cat docker-compose-deploy-wf.yml            |  grep 'image: ' | sed -e 's/image: /docker pull /g' |sed -e 's/\r//g' >> docker_pull_image.sh
cat docker-compose-deploy-km.yml            |  grep 'image: ' | sed -e 's/image: /docker pull /g' |sed -e 's/\r//g' >> docker_pull_image.sh
cat docker-compose-deploy-docflow.yml       |  grep 'image: ' | sed -e 's/image: /docker pull /g' |sed -e 's/\r//g' >> docker_pull_image.sh
# cat docker-compose-deploy-inwise.yml        |  grep 'image: ' | sed -e 's/image: /docker pull /g' |sed -e 's/\r//g' >> docker_pull_image.sh

chmod 744 ./docker_pull_image.sh
cat ./docker_pull_image.sh
./docker_pull_image.sh
\rm -f ./docker_pull_image.sh
echo "[KM] pull all images end"

if [ ! -d '/home/edoc2/macrowing/edoc2v5/data' ];
then
    homePwd=$(pwd)
    echo "[KM] start create base folder"
    mkdir -p /home/edoc2/macrowing/edoc2v5/data
    mkdir -p /home/edoc2/macrowing/edoc2v5/mount
    cd /home/edoc2/macrowing/edoc2v5/data
    mkdir -p dbbackup  edoc2  edoc2Docs  es  mongodb postgresql  rabbitmq  redis mysql sqlserver oracle dm inwise
    cd $homePwd
    echo '[KM] base folder create success'
else
    echo '[KM] base folder is exist'
fi

# if swarm servcie count is 0,then may the labels is not exist 
# echo '[KM] set all swarm nodes labels'
# docker node ls -q > docker-swarm-nodes
# cat docker-swarm-nodes | while read line
# do
#     docker node update --label-rm nodelabels --label-rm nodetype  $line
#     docker node update --label-add nodelabels=Middleware --label-add nodetype=InDrive  $line
#     docker node inspect $line --pretty |grep Labels: -A 6
# done
# #\rm -f ./docker-swarm-nodes
# echo '[KM] set all swarm nodes labels success'

echo '[KM] create network macrowing'
docker network create macrowing --driver "overlay" --scope "swarm"
echo '[KM] create network macrowing success'

# echo '[KM] remove all old stacks'
# docker stack rm  'midd'
# docker stack rm  'search'
# docker stack rm  'edoc2'
# docker stack rm  'inbiz'
# docker stack rm  'wf'
# docker stack rm  'km'
# docker stack rm  'docflow'
# docker stack rm  'inwise'

echo '[KM] deploy new stacks'
docker stack deploy --compose-file 'middleware-master-standalone.yml'  'midd'
docker stack deploy --compose-file 'docker-compose-deploy-searchengine.yml'  'search'
docker stack deploy --compose-file 'docker-compose-deploy-edoc2.yml'  'edoc2'
docker stack deploy --compose-file 'docker-compose-deploy-inbiz.yml'  'inbiz'
docker stack deploy --compose-file 'docker-compose-deploy-wf.yml'  'wf'
docker stack deploy --compose-file 'docker-compose-deploy-km.yml'  'km'
docker stack deploy --compose-file 'docker-compose-deploy-docflow.yml'  'docflow'
# docker stack deploy --compose-file 'docker-compose-deploy-inwise.yml'  'inwise'
echo '[KM] deploy km success'