脚本功能:公网或内网一键安装、钉钉告警
#!/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/profilesource ~/.bash_profilecurrent_directory=$(pwd)Installation_Logs="esl-install.log"Kernel_conf=/etc/sysctl.confIptables=net.bridge.bridge-nf-call-iptables=1Ip6tables=net.bridge.bridge-nf-call-ip6tables=1Ip_forward=net.ipv4.ip_forward=1Log_time=`date +'%F %H:%M:%S'`Have_Network(){echo -n -e "\033[1;32m $Log_time Start install \n\033[0m"source ./scripts/Project_name.shcp -n docker-compose /usr/local/bin/if [ ! -f "/usr/bin/dockerd" ];thenif [ ! -f "/etc/yum.repos.d/docker-ce.repo" ];thencp docker-ce.repo /etc/yum.repos.d/fifiecho -n -e "\033[1;32m $Log_time Install Docker \n\033[0m" > $Installation_Logsyum -y install docker-ce-19.03.4-3.el7 > $Installation_Logs 2>&1result=$?if [ $result == "0" ];thensystemctl daemon-reload && systemctl start dockersystemctl enable docker.service >> $Installation_Logssystemctl status docker >> $Installation_Logs && docker -v >> $Installation_Logselseecho 'Please make sure your network can access the public network and reinstall'exitfiecho -n -e "\033[1;32m $Log_time Modify kernel parameters \n\033[0m" >> $Installation_Logsecho $Iptables >> $Kernel_confecho $Ip6tables >> $Kernel_confecho $Ip_forward >> /etc/sysctl.confsysctl -p >> $Installation_Logs 2>&1echo -n -e "\033[1;32m $Log_time Load mirror \n\033[0m" >> $Installation_Logsfor image in `ls docker-images`dodocker load -i docker-images/$image >> $Installation_Logs 2>&1doneif [ $? -ne 0 ];thenexitfised -i "s@/usr/local/esl@$current_directory@" docker-compose.yamlecho -n -e "\033[1;32m $Log_time Start Docker-compose.. \n\033[0m" >> $Installation_Logsdocker-compose up -d >> $Installation_Logs 2>&1if [ $? -eq 0 ];thenecho "The installation is complete"fiecho -n -e "\033[1;32m $Log_time Stop firewalld.. \n\033[0m" >> $Installation_Logssystemctl status firewalld | grep "running" >> $Installation_Logs 2>&1if [ $? -eq 0 ];thensystemctl stop firewalld >> $Installation_Logs 2>&1systemctl disable firewalld >> $Installation_Logs 2>&1fiecho -n -e "\033[1;32m $Log_time Disabled Selinux \n\033[0m" >> $Installation_Logsif [ `getenforce` != "Disabled" ];thensed -i 's/SELINUX=enabled/SELINUX=disabled/g' /etc/selinux/config >> $Installation_Logs 2>&1setenforce 0 >> $Installation_Logs 2>&1fiecho "$Log_time Installing qrencode" >> Installation_Logs 2>&1#yum -y install qrencode >> esl-install.log 2>&1echo Configure monitoring alarm >> esl-install.log 2>&1echo "#Monitoring alarm - CPU, memory, web, disk" >>/var/spool/cron/rootecho "* * * * * bash /usr/local/esl/scripts/Monitoring.sh" >>/var/spool/cron/rootecho "#Clear docker log every Sunday" >>/var/spool/cron/rootecho "0 0 * * 0 /bin/bash /usr/local/esl/scripts/docker-log.sh" >>/var/spool/cron/rootecho "#Check Web Code " >>/var/spool/cron/rootecho "*/3 * * * * bash /usr/local/esl/scripts/web_code.sh" >>/var/spool/cron/root}No_Network(){echo "======================================================================================="echo -n -e '\033[1;32m当前机器无外网,正在执行离线安装脚本 \n\033[0m'sh /usr/local/esl/scripts/docker-install-and-uninstall.shecho -n -e "\033[1;32m $Log_time Modify kernel parameters \n\033[0m" >> $Installation_Logsecho $Iptables >> $Kernel_confecho $Ip6tables >> $Kernel_confecho $Ip_forward >> /etc/sysctl.confsysctl -p >> $Installation_Logs 2>&1echo -n -e "\033[1;32m $Log_time Load mirror \n\033[0m" >> $Installation_Logsfor image in `ls docker-images`dodocker load -i docker-images/$image >> $Installation_Logs 2>&1donesed -i "s@/usr/local/esl@$current_directory@" docker-compose.yamlecho -n -e "\033[1;32m $Log_time Start Docker-compose.. \n\033[0m" >> $Installation_Logsdocker-compose up -d >> $Installation_Logs 2>&1if [ $? -eq 0 ];thenecho "The installation is complete"fiecho -n -e "\033[1;32m $Log_time Stop firewalld.. \n\033[0m" >> $Installation_Logssystemctl status firewalld | grep "running" >> $Installation_Logs 2>&1if [ $? -eq 0 ];thensystemctl stop firewalld >> $Installation_Logs 2>&1systemctl disable firewalld >> $Installation_Logs 2>&1fiecho -n -e "\033[1;32m $Log_time Disabled Selinux \n\033[0m" >> $Installation_Logsif [ `getenforce` != "Disabled" ];thensed -i 's/SELINUX=enabled/SELINUX=disabled/g' /etc/selinux/config >> $Installation_Logs 2>&1setenforce 0 >> $Installation_Logs 2>&1fiecho -n -e '\033[1;32m正在执行docker-compose up -d --build \n\033[0m'docker-compose up -d --build >> $Installation_Logs 2>&1echo -n -e '\033[1;32m=========================================================================================================================================================================== \n\033[0m'docker psecho -n -e '\033[1;32m=========================================================================================================================================================================== \n\033[0m'}ESL_directory_migration() {Project_Path="/usr/local/esl"Default_Path="/usr/local"Docker_Default_Path="/var/lib/docker"Command=`./docker-compose stop >>$Installation_Logs 2>&1`echo -e -n "\033[1;32m ===============================================================\n\033[0m"echo -e -n "\033[1;32m 当前正在执行迁移动作需要交互, 请勿离开当前终端... \n\033[0m"echo -e -n "\033[1;32m 选择安装最大系统盘. \n\033[0m"echo -e -n "\033[1;32m 例如: \n\033[0m"echo -e -n "\033[1;32m 输入/data 迁移ESL和Docker默认安装路径到/data目录 \n\033[0m"echo -e -n "\033[1;32m 输入为空 默认安装位置/usr/local/,退出程序. \n\033[0m"echo -e -n "\033[1;32m /usr/local 默认安装位置/usr/local/,退出程序. \n\033[0m"echo -e -n "\033[1;32m ===============================================================\n\033[0m"read -t 5 -p "请选择安装到系统最大盘 :" Storage_diskif [ -z $Storage_disk ];thenecho "The value is empty"cd $Project_Path && ./docker-compose up -d --build >>$Installation_Logs 2>&1`exit 1elif [ "$Storage_disk" = "$Default_Path" ];thenecho "Install to default path $Default_Path"cd $Project_Path && ./docker-compose up -d --build >>$Installation_Logs 2>&1`exit 1elseecho "正在迁移esl目录至$Storage_disk..."cd $Project_Path && $Command >>$Installation_Logs 2>&1if [ $? -ne 0 ];thenexit 1fimv $Project_Path $Storage_diskif [ -d $Project_Path ];thenrm -f $Project_Pathfiln -s $Storage_disk/esl $Project_Path && cd $Project_Path && ./docker-compose up -d --build >>$Installation_Logs 2>&1echo -e -n "\033[1;32m ESL迁移到$Storage_disk 成功,请检查$Project_Path 目录是否为软连接目录..\n\033[0m"fiecho -e -n "\033[1;32m 正在迁移Docker默认安装路径到$Storage_disk 目录...\n\033[0m"cd $Project_Path && $Command >>$Installation_Logs 2>&1systemctl stop docker.servicemv /var/lib/docker $Storage_diskln -s $Storage_disk/docker $Docker_Default_Pathsystemctl start docker.serviceecho "Docker安装目录已迁移到$Storage_disk 目录完成,请检查$Docker_Default_Path 是否为软连接目录..."echo "正在检查服务状态.."echo -n -e '\033[1;32m=========================================================================================================================================================================== \n\033[0m'docker psecho -n -e '\033[1;32m=========================================================================================================================================================================== \n\033[0m'}Cront_Jobs(){Job_Path="/var/spool/cron/root"sed -i '/web_code.sh/d;/Monitoring.sh/d;/docker-log.sh/d' $Job_Pathsed -i '/Monitoring/d;/Check/d;/Clear/d' $Job_PathPackage=`rpm -qa|grep bridge-utils|wc -l`if [ $Package -ne 1 ];thenyum -y install bridge-utilsfi#BRIDGE_LIST=$( brctl show | cut -f 1 | grep br-)#ifconfig $BRIDGE_LIST down#brctl delbr $BRIDGE_LIST}Network(){Network=`ping -c 3 www.baidu.com >> $Installation_Logs 2>&1`if [ $? == 0 ];thenCront_JobsHave_NetworkESL_directory_migrationelseNo_Networkfi}Network
