#!/bin/bash
    # Debian/Ubuntu Linux systems. (May 10, 2019)
    # (GNU/General Public License version 2.0)
    # For Centos 7 and up, Linux lzone_zkong_cluster_01 3.10.0-862.el7.x86_64.
    # ...And away we go!
    
    source /etc/profile
    source ~/.bash_profile
    
    current_directory=$(pwd)
    Installation_Logs="esl-install.log"
    Kernel_conf=/etc/sysctl.conf
    Iptables=net.bridge.bridge-nf-call-iptables=1
    Ip6tables=net.bridge.bridge-nf-call-ip6tables=1
    Ip_forward=net.ipv4.ip_forward=1
    Log_time=`date +'%F %H:%M:%S'`
    
    Have_Network(){
        echo -n -e "\033[1;32m $Log_time  Start install  \n\033[0m"
        cp -n docker-compose /usr/local/bin/
        if [ ! -f "/usr/bin/dockerd" ];then
               if [ ! -f "/etc/yum.repos.d/docker-ce.repo" ];then
               cp docker-ce.repo /etc/yum.repos.d/
               fi
        fi
    
        echo -n -e "\033[1;32m $Log_time Install Docker  \n\033[0m" > $Installation_Logs
        yum -y install docker-ce-19.03.4-3.el7 > $Installation_Logs 2>&1
        result=$?
        if [ $result == "0" ];then
            systemctl daemon-reload && systemctl start docker && systemctl enable docker.service >> $Installation_Logs
            systemctl status docker  >> $Installation_Logs  && docker -v >> $Installation_Logs
        else
            echo 'Please make sure your network can access the public network and reinstall'
            exit
        fi
    
        echo -n -e "\033[1;32m $Log_time  Modify kernel parameters  \n\033[0m" >> $Installation_Logs
        echo $Iptables >> $Kernel_conf
        echo $Ip6tables >> $Kernel_conf
        echo $Ip_forward >> /etc/sysctl.conf
        sysctl -p >> $Installation_Logs 2>&1
    
        echo -n -e "\033[1;32m $Log_time Load mirror \n\033[0m" >> $Installation_Logs
        for image in `ls docker-images`
        do
            docker load -i docker-images/$image >> $Installation_Logs 2>&1
        done
        sed -i "s@/usr/local/esl@$current_directory@" docker-compose.yaml
    
        echo -n -e "\033[1;32m $Log_time Start Docker-compose..  \n\033[0m" >> $Installation_Logs
        docker-compose up -d >> $Installation_Logs 2>&1
        if [ $? -eq 0 ];then
            echo "The installation is complete"
        fi
        echo -n -e "\033[1;32m $Log_time Stop firewalld..  \n\033[0m" >> $Installation_Logs
        systemctl status firewalld | grep "running" >> $Installation_Logs  2>&1
        if [ $? -eq 0 ];then
           systemctl stop firewalld >> $Installation_Logs 2>&1
           systemctl disable firewalld >> $Installation_Logs 2>&1
        fi
        echo -n -e "\033[1;32m $Log_time Disabled Selinux  \n\033[0m" >> $Installation_Logs
        if [ `getenforce` != "Disabled" ];then
           sed -i 's/SELINUX=enabled/SELINUX=disabled/g' /etc/selinux/config >> $Installation_Logs 2>&1
           setenforce 0 >> $Installation_Logs 2>&1
        fi
    
        echo "$Log_time Installing qrencode" >> Installation_Logs  2>&1
        yum -y install qrencode  >> esl-install.log 2>&1
        echo Configure monitoring alarm >> esl-install.log 2>&1
        echo "#Monitoring alarm - CPU, memory, web, disk" >>/var/spool/cron/root
        echo "* * * * * bash /usr/local/esl/scripts/Monitoring.sh"  >>/var/spool/cron/root
        echo "#Clear docker log every Sunday" >>/var/spool/cron/root
        echo "0 0 * * 0 /bin/bash /usr/local/esl/scripts/docker-log.sh" >>/var/spool/cron/root
        source ./scripts/Project_name.sh
        echo -n -e '\033[1;32m钉钉扫描下方二维码加入公共服务告警群\n\033[0m'
        echo "https://qr.dingtalk.com/action/joingroup?code=v1,k1,/6KAfTMKiXmaHdLt3JhEnfposcJEIFgRP3de0epIa2Y=&_dt_no_comment=1&origin=11" | qrencode -t UTF8
    
    }
    No_Network(){
         echo "======================================================================================="
         echo -n -e '\033[1;32m当前机器无外网,正在执行离线安装脚本   \n\033[0m'
         sh /usr/local/esl/scripts/docker-install-and-uninstall.sh
         echo -n -e "\033[1;32m $Log_time  Modify kernel parameters  \n\033[0m" >> $Installation_Logs
         echo $Iptables >> $Kernel_conf
         echo $Ip6tables >> $Kernel_conf
         echo $Ip_forward >> /etc/sysctl.conf
         sysctl -p >> $Installation_Logs 2>&1
         echo -n -e "\033[1;32m $Log_time Load mirror \n\033[0m" >> $Installation_Logs
         for image in `ls docker-images`
         do
            docker load -i docker-images/$image >> $Installation_Logs 2>&1
         done
         sed -i "s@/usr/local/esl@$current_directory@" docker-compose.yaml
    
         echo -n -e "\033[1;32m $Log_time Start Docker-compose..  \n\033[0m" >> $Installation_Logs
         docker-compose up -d >> $Installation_Logs 2>&1
         if [ $? -eq 0 ];then
             echo "The installation is complete"
         fi
         echo -n -e "\033[1;32m $Log_time Stop firewalld..  \n\033[0m" >> $Installation_Logs
         systemctl status firewalld | grep "running" >> $Installation_Logs  2>&1
         if [ $? -eq 0 ];then
            systemctl stop firewalld >> $Installation_Logs 2>&1
            systemctl disable firewalld >> $Installation_Logs 2>&1
         fi
         echo -n -e "\033[1;32m $Log_time Disabled Selinux  \n\033[0m" >> $Installation_Logs
         if [ `getenforce` != "Disabled" ];then
            sed -i 's/SELINUX=enabled/SELINUX=disabled/g' /etc/selinux/config >> $Installation_Logs 2>&1
            setenforce 0 >> $Installation_Logs 2>&1
         fi
    
         echo -n -e '\033[1;32m正在执行docker-compose up -d --build  \n\033[0m'
         docker-compose up -d --build  >> $Installation_Logs 2>&1
    
         echo -n -e '\033[1;32m===========================================================================================================================================================================  \n\033[0m'
         docker ps
         echo -n -e '\033[1;32m===========================================================================================================================================================================  \n\033[0m'
    
         exit
    }
    
    Network(){
    Network=`ping -c 3 www.baidu.com >> $Installation_Logs 2>&1`
    if [ $? == 0 ];then
        Have_Network
    else
        No_Network
    fi
    }
    Network