#!/bin/bash

check_docker_install_status(){
	ssh root@${ip}  docker --version || DOCKER_INSTALL_STATUS=1
}

check_docker_run_status(){
    ssh root@${ip} systemctl status docker |grep running &> /dev/null || DOCKER_RUN_STATUS=1
}


config_daemon_json(){
     DAEMON_FILE=Template/daemon.json.temp
       if [ -f "${DAEMON_FILE}" ];then
       sed -i /bip/d "${DAEMON_FILE}"
       sed -i "1a\"bip\": \"${DOCKER0_NETWORK}\"," ${DAEMON_FILE}
       sed -i /default-address-pools/d ${DAEMON_FILE}
       sed -i "2a\"default-address-pools\": [{\"base\":\"${DOCKER_GWBRIDGE_NETWORK}\",\"size\":24}]," ${DAEMON_FILE}
       sed -i /^$/d ${DAEMON_FILE}
       NUM=$(($(wc -l ${DAEMON_FILE}|awk '{print $1}')-1))
       sed -i "${NUM}s/\,$//g" ${DAEMON_FILE}
    else   
      cat > ${DAEMON_FILE} <<EOF
{
  "insecure-registries": ["192.168.251.150","192.168.251.78"],
  "bip": "${DOCKER0_NETWORK}",
  "default-address-pools": [{"base":"${DOCKER_GWBRIDGE_NETWORK}","size":24}]
}
EOF
     fi
}

docker_ce_install(){
	#scp Install_Packages/docker-ce/* root@${ip}:/usr/bin/
        #scp Install_Packages/system/* root@${ip}:/usr/lib/systemd/system/
        #ssh root@${ip} chmod +x /usr/bin/docker*
        scp Install_Packages/docker-19.03.8-1.el7.x86_64.rpm root@${ip}:/tmp
        ssh root@${ip} rpm -ivh /tmp/docker-19.03.8-1.el7.x86_64.rpm
        if [ $? -ne 0 ];then
              echo "ERROR: docker install failed !!!"
              exit 1
        fi
       #config_daemon_json
       ssh root@${ip} "mkdir -p /etc/docker"
       scp Template/daemon.json.temp root@${ip}:/etc/docker/daemon.json
       ssh root@${ip} "systemctl enable docker && systemctl start docker"
}

docker_swarm_init(){
        ssh root@${ip} docker swarm leave --force &> /dev/null
	ssh root@${ip} docker swarm init --advertise-addr=${ip} &> /dev/null 
        ssh root@${ip} docker swarm join-token manager | grep "docker swarm join" > docker-swarm-join.sh
}

docker_swarm_join(){
        scp docker-swarm-join.sh root@$ip:/tmp/docker-swarm-join.sh
        ssh root@${ip} docker swarm leave --force &> /dev/null
	ssh root@${ip} "sh /tmp/docker-swarm-join.sh"
        ssh root@${ip} rm -rf /tmp/docker-swarm-join.sh
}


docker_swarm_config(){
   local ip=
   for ((i=0;i<${#IP_LIST[@]};i++))
   do
       ip=${IP_LIST[i]} 
       if [ ${i} -eq 0 ];then
           echo "docker swarm init"
           docker_swarm_init
       else
            if [ -n "${ip}" ];then
                echo "docker swarm cluster join"
                docker_swarm_join
            fi
       fi
       unset ip
   done
}


docker_install(){
    config_daemon_json
    for ip in ${IP_LIST[@]}
    do
       check_docker_install_status
       if [ a"${DOCKER_INSTALL_STATUS}" == a1 ];then
           echo "$ip install docker-ce"
           docker_ce_install
           #docker_compose_install
           unset DOCKER_INSTALL_STATUS
       else
           check_docker_run_status
           if [ a"${DOCKER_RUN_STATUS}" == a1 ];then
              ssh root@${ip} systemctl enable docker
              ssh root@${ip} systemctl start docker
              unset DOCKE_RUN_STATUS
           fi
       fi
    done
    docker_swarm_config
    config_node_labels
    overlay_network_create
}

config_node_labels(){
    # add indrive labels
    for NODE in $(ssh root@${IP_LIST[0]} docker node ls |grep -v ID|awk '{print $1}')
    do 
         ssh root@${IP_LIST[0]} "docker node update --label-add nodetype=InDrive ${NODE}"
    done
    # add middleware labels
    if [ "${MIDDLEWARE_MODE}" = "standalone"  ];then
        if [ "${MIDDLEWARE_STANDALONE_HA}" = "true" ] && [ "${#IP_LIST[@]}" -gt 1 ] ;then
            check_share_storage ${MIDDLEWARE_DATA_PATH}
            for NODE in $(ssh root@${IP_LIST[0]} docker node ls |grep -v ID|awk '{print $1}')
            do 
                ssh root@${IP_LIST[0]} "docker node update --label-add nodelabels=Middleware ${NODE}"
            done
        else
            ssh root@${IP_LIST[0]} "docker node update --label-add nodelabels=Middleware $(docker node ls|grep \*|grep -v ID |awk '{print $1}')"
        fi
    elif [ "${MIDDLEWARE_MODE}" = "cluster" ];then
       if [ ${#IP_LIST[@]} -lt 3 ];then
            echo "host is less than 3"
            exit 5
       fi
       for ((i=0;i<=2;i++))
       do
          NODE_ID=$(ssh root@${IP_LIST[${i}]} docker node ls |grep \*|grep -v ID |awk '{print $1}')
          for MID in mysql redis rabbitmq es
          do 
            if [ "${MID}" = "mysql" ];then
                docker node update --label-add ${MID}labels=${MID}${i} ${NODE_ID}
            else
                docker node update --label-add ${MID}labels=${MID}$((${i}+1)) ${NODE_ID}
            fi 
          done 
       done
    fi

}

overlay_network_create(){
    ssh root@${IP_LIST[0]} "docker network ls |grep macrowing &> /dev/null || docker network create --driver overlay macrowing --subnet ${MACROWING_NETWORK}"
}
