#!/usr/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="./log/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.sh 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 2>&1 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 if [ $? -ne 0 ];then exit fi 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" >>$Installation_Logs 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 >> $Installation_Logs 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 echo "#Check Web Code " >>/var/spool/cron/root echo "*/3 * * * * bash /usr/local/esl/scripts/web_code.sh" >>/var/spool/cron/root}No_Network(){ echo "=======================================================================================" echo -n -e '\033[1;32m There is no external network on the current machine, and the offline installation script is being executed \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 Running 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'}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 The migration is in progress. Please do not leave the terminal... \n\033[0m" echo -e -n "\033[1;32m Select system maximum disk migration. \n\033[0m" echo -e -n "\033[1;32m for example: \n\033[0m" echo -e -n "\033[1;32m input /data Migrate the default installation path of ESL and docker to the /data directory \n\033[0m" echo -e -n "\033[1;32m If the input is empty, the default installation location is /usr/local/. Exit the program \n\033[0m" echo -e -n "\033[1;32m /usr/local is the default installation location /usr/local/, exit the program \n\033[0m" echo -e -n "\033[1;32m ================================================================================================================\n\033[0m" read -t 50 -p "Please select the largest disk of the current system :" Storage_disk if [ -z $Storage_disk ];then echo "The value is empty" cd $Project_Path && ./docker-compose up -d --build >>$Installation_Logs 2>&1` exit 1 elif [ "$Storage_disk" = "$Default_Path" ];then echo "Install to default path $Default_Path" cd $Project_Path && ./docker-compose up -d --build >>$Installation_Logs 2>&1` exit 1 else echo "正在迁移esl目录至$Storage_disk..." cd $Project_Path && $Command >>$Installation_Logs 2>&1 if [ $? -ne 0 ];then exit 1 fi mv $Project_Path $Storage_disk if [ -d $Project_Path ];then rm -f $Project_Path fi ln -s $Storage_disk/esl $Project_Path && cd $Project_Path && ./docker-compose up -d --build >>$Installation_Logs 2>&1 echo -e -n "\033[1;32m ESL迁移到$Storage_disk 成功,请检查$Project_Path 目录是否为软连接目录..\n\033[0m" fi echo -e -n "\033[1;32m 正在迁移Docker默认安装路径到$Storage_disk 目录...\n\033[0m" cd $Project_Path && $Command >>$Installation_Logs 2>&1 systemctl stop docker.service mv /var/lib/docker $Storage_disk ln -s $Storage_disk/docker $Docker_Default_Path systemctl start docker.service echo "Docker安装目录已迁移到$Storage_disk 目录完成,请检查$Docker_Default_Path 是否为软连接目录..." echo "正在检查服务状态.." echo -n -e '\033[1;32m=========================================================================================================================================================================== \n\033[0m' docker ps echo -n -e '\033[1;32m=========================================================================================================================================================================== \n\033[0m'}TimeZone(){PKGVERSION="(GNU libc) "TZVERSION="2.17"REPORT_BUGS_TO="<http://www.gnu.org/software/libc/bugs.html>": ${AWK=awk}: ${TZDIR=/usr/share/zoneinfo}($AWK -v x=y 'BEGIN { exit 123 }') </dev/null >/dev/null 2>&1[ $? = 123 ] || { echo >&2 "$0: Sorry, your \`$AWK' program is not Posix compatible." exit 1}if [ "$1" = "--help" ]; then cat <<EOFUsage: tzselectSelect a time zone interactively.Report bugs to $REPORT_BUGS_TO.EOF exitelif [ "$1" = "--version" ]; then cat <<EOFtzselect $PKGVERSION$TZVERSIONEOF exitfi# Make sure the tables are readable.TZ_COUNTRY_TABLE=$TZDIR/iso3166.tabTZ_ZONE_TABLE=$TZDIR/zone.tabfor f in $TZ_COUNTRY_TABLE $TZ_ZONE_TABLEdo <$f || { echo >&2 "$0: time zone files are not set up correctly" exit 1 }donenewline=''IFS=$newline# Work around a bug in bash 1.14.7 and earlier, where $PS3 is sent to stdout.case $(echo 1 | (select x in x; do break; done) 2>/dev/null) in?*) PS3=esac# Begin the main loop. We come back here if the user wants to retry.while echo >&2 'Please identify a location' \ 'so that time zone rules can be set correctly.' continent= country= region= # Ask the user for continent or ocean. echo >&2 'Please select a continent or ocean.' select continent in \ Africa \ Americas \ Antarctica \ 'Arctic Ocean' \ Asia \ 'Atlantic Ocean' \ Australia \ Europe \ 'Indian Ocean' \ 'Pacific Ocean' \ 'none - I want to specify the time zone using the Posix TZ format.' do case $continent in '') echo >&2 'Please enter a number in range.';; ?*) case $continent in Americas) continent=America;; *' '*) continent=$(expr "$continent" : '\([^ ]*\)') esac break esac done case $continent in '') exit 1;; none) # Ask the user for a Posix TZ string. Check that it conforms. while echo >&2 'Please enter the desired value' \ 'of the TZ environment variable.' echo >&2 'For example, GST-10 is a zone named GST' \ 'that is 10 hours ahead (east) of UTC.' read TZ $AWK -v TZ="$TZ" 'BEGIN { tzname = "[^-+,0-9][^-+,0-9][^-+,0-9]+" time = "[0-2]?[0-9](:[0-5][0-9](:[0-5][0-9])?)?" offset = "[-+]?" time date = "(J?[0-9]+|M[0-9]+\.[0-9]+\.[0-9]+)" datetime = "," date "(/" time ")?" tzpattern = "^(:.*|" tzname offset "(" tzname \ "(" offset ")?(" datetime datetime ")?)?)$" if (TZ ~ tzpattern) exit 1 exit 0 }' do echo >&2 "\`$TZ' is not a conforming" \ 'Posix time zone string.' done TZ_for_date=$TZ;; *) # Get list of names of countries in the continent or ocean. countries=$($AWK -F'\t' \ -v continent="$continent" \ -v TZ_COUNTRY_TABLE="$TZ_COUNTRY_TABLE" \ ' /^#/ { next } $3 ~ ("^" continent "/") { if (!cc_seen[$1]++) cc_list[++ccs] = $1 } END { while (getline <TZ_COUNTRY_TABLE) { if ($0 !~ /^#/) cc_name[$1] = $2 } for (i = 1; i <= ccs; i++) { country = cc_list[i] if (cc_name[country]) { country = cc_name[country] } print country } } ' <$TZ_ZONE_TABLE | sort -f) # If there's more than one country, ask the user which one. case $countries in *"$newline"*) echo >&2 'Please select a country.' select country in $countries do case $country in '') echo >&2 'Please enter a number in range.';; ?*) break esac done case $country in '') exit 1 esac;; *) country=$countries esac # Get list of names of time zone rule regions in the country. regions=$($AWK -F'\t' \ -v country="$country" \ -v TZ_COUNTRY_TABLE="$TZ_COUNTRY_TABLE" \ ' BEGIN { cc = country while (getline <TZ_COUNTRY_TABLE) { if ($0 !~ /^#/ && country == $2) { cc = $1 break } } } $1 == cc { print $4 } ' <$TZ_ZONE_TABLE) # If there's more than one region, ask the user which one. case $regions in *"$newline"*) echo >&2 'Please select one of the following' \ 'time zone regions.' select region in $regions do case $region in '') echo >&2 'Please enter a number in range.';; ?*) break esac done case $region in '') exit 1 esac;; *) region=$regions esac # Determine TZ from country and region. TZ=$($AWK -F'\t' \ -v country="$country" \ -v region="$region" \ -v TZ_COUNTRY_TABLE="$TZ_COUNTRY_TABLE" \ ' BEGIN { cc = country while (getline <TZ_COUNTRY_TABLE) { if ($0 !~ /^#/ && country == $2) { cc = $1 break } } } $1 == cc && $4 == region { print $3 } ' <$TZ_ZONE_TABLE) # Make sure the corresponding zoneinfo file exists. TZ_for_date=$TZDIR/$TZ <$TZ_for_date || { echo >&2 "$0: time zone files are not set up correctly" exit 1 } esac # Use the proposed TZ to output the current date relative to UTC. # Loop until they agree in seconds. # Give up after 8 unsuccessful tries. extra_info= for i in 1 2 3 4 5 6 7 8 do TZdate=$(LANG=C TZ="$TZ_for_date" date) UTdate=$(LANG=C TZ=UTC0 date) TZsec=$(expr "$TZdate" : '.*:\([0-5][0-9]\)') UTsec=$(expr "$UTdate" : '.*:\([0-5][0-9]\)') case $TZsec in $UTsec) extra_info="Local time is now: $TZdate.Universal Time is now: $UTdate." break esac done # Output TZ info and ask the user to confirm. echo >&2 "" echo >&2 "The following information has been given:" echo >&2 "" case $country+$region in ?*+?*) echo >&2 " $country$newline $region";; ?*+) echo >&2 " $country";; +) echo >&2 " TZ='$TZ'" esac echo >&2 "" echo >&2 "Therefore TZ='$TZ' will be used.$extra_info" echo >&2 "Is the above information OK?" ok= select ok in Yes No do case $ok in '') echo >&2 'Please enter 1 for Yes, or 2 for No.';; ?*) break esac done case $ok in '') exit 1;; Yes) break esacdo :donecase $SHELL in*csh) file=.login line="setenv TZ '$TZ'";;*) file=.profile line="TZ='$TZ'; export TZ"esacecho >&2 "You can make this change permanent for yourself by appending the line $lineto the file '$file' in your home directory; then log out and log in again.Here is that TZ value again, this time on standard output so that youcan use the $0 command in shell scripts:" >> $Installation_Logssed '/Time/d' /usr/local/esl/.env -i echo "$TZ"echo "Time=$TZ" >> /usr/local/esl/.env}Cront_Jobs(){Job_Path="/var/spool/cron/root" sed -i '/web_code.sh/d;/Monitoring.sh/d;/docker-log.sh/d' $Job_Path sed -i '/Monitoring/d;/Check/d;/Clear/d' $Job_Path Package=`rpm -qa|grep bridge-utils|wc -l` if [ $Package -ne 1 ];then yum -y install bridge-utils fi 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 ];then TimeZone Cront_Jobs Have_Network ESL_directory_migration else No_Network fi}Network