脚本功能:公网或内网一键安装、钉钉告警

    1. #!/bin/bash
    2. # Debian/Ubuntu Linux systems. (May 10, 2019)
    3. # (GNU/General Public License version 2.0)
    4. # For Centos 7 and up, Linux lzone_zkong_cluster_01 3.10.0-862.el7.x86_64.
    5. # ...And away we go!
    6. source /etc/profile
    7. source ~/.bash_profile
    8. current_directory=$(pwd)
    9. Installation_Logs="esl-install.log"
    10. Kernel_conf=/etc/sysctl.conf
    11. Iptables=net.bridge.bridge-nf-call-iptables=1
    12. Ip6tables=net.bridge.bridge-nf-call-ip6tables=1
    13. Ip_forward=net.ipv4.ip_forward=1
    14. Log_time=`date +'%F %H:%M:%S'`
    15. Have_Network(){
    16. echo -n -e "\033[1;32m $Log_time Start install \n\033[0m"
    17. source ./scripts/Project_name.sh
    18. cp -n docker-compose /usr/local/bin/
    19. if [ ! -f "/usr/bin/dockerd" ];then
    20. if [ ! -f "/etc/yum.repos.d/docker-ce.repo" ];then
    21. cp docker-ce.repo /etc/yum.repos.d/
    22. fi
    23. fi
    24. echo -n -e "\033[1;32m $Log_time Install Docker \n\033[0m" > $Installation_Logs
    25. yum -y install docker-ce-19.03.4-3.el7 > $Installation_Logs 2>&1
    26. result=$?
    27. if [ $result == "0" ];then
    28. systemctl daemon-reload && systemctl start docker
    29. systemctl enable docker.service >> $Installation_Logs
    30. systemctl status docker >> $Installation_Logs && docker -v >> $Installation_Logs
    31. else
    32. echo 'Please make sure your network can access the public network and reinstall'
    33. exit
    34. fi
    35. echo -n -e "\033[1;32m $Log_time Modify kernel parameters \n\033[0m" >> $Installation_Logs
    36. echo $Iptables >> $Kernel_conf
    37. echo $Ip6tables >> $Kernel_conf
    38. echo $Ip_forward >> /etc/sysctl.conf
    39. sysctl -p >> $Installation_Logs 2>&1
    40. echo -n -e "\033[1;32m $Log_time Load mirror \n\033[0m" >> $Installation_Logs
    41. for image in `ls docker-images`
    42. do
    43. docker load -i docker-images/$image >> $Installation_Logs 2>&1
    44. done
    45. if [ $? -ne 0 ];then
    46. exit
    47. fi
    48. sed -i "s@/usr/local/esl@$current_directory@" docker-compose.yaml
    49. echo -n -e "\033[1;32m $Log_time Start Docker-compose.. \n\033[0m" >> $Installation_Logs
    50. docker-compose up -d >> $Installation_Logs 2>&1
    51. if [ $? -eq 0 ];then
    52. echo "The installation is complete"
    53. fi
    54. echo -n -e "\033[1;32m $Log_time Stop firewalld.. \n\033[0m" >> $Installation_Logs
    55. systemctl status firewalld | grep "running" >> $Installation_Logs 2>&1
    56. if [ $? -eq 0 ];then
    57. systemctl stop firewalld >> $Installation_Logs 2>&1
    58. systemctl disable firewalld >> $Installation_Logs 2>&1
    59. fi
    60. echo -n -e "\033[1;32m $Log_time Disabled Selinux \n\033[0m" >> $Installation_Logs
    61. if [ `getenforce` != "Disabled" ];then
    62. sed -i 's/SELINUX=enabled/SELINUX=disabled/g' /etc/selinux/config >> $Installation_Logs 2>&1
    63. setenforce 0 >> $Installation_Logs 2>&1
    64. fi
    65. echo "$Log_time Installing qrencode" >> Installation_Logs 2>&1
    66. #yum -y install qrencode >> esl-install.log 2>&1
    67. echo Configure monitoring alarm >> esl-install.log 2>&1
    68. echo "#Monitoring alarm - CPU, memory, web, disk" >>/var/spool/cron/root
    69. echo "* * * * * bash /usr/local/esl/scripts/Monitoring.sh" >>/var/spool/cron/root
    70. echo "#Clear docker log every Sunday" >>/var/spool/cron/root
    71. echo "0 0 * * 0 /bin/bash /usr/local/esl/scripts/docker-log.sh" >>/var/spool/cron/root
    72. echo "#Check Web Code " >>/var/spool/cron/root
    73. echo "*/3 * * * * bash /usr/local/esl/scripts/web_code.sh" >>/var/spool/cron/root
    74. }
    75. No_Network(){
    76. echo "======================================================================================="
    77. echo -n -e '\033[1;32m当前机器无外网,正在执行离线安装脚本 \n\033[0m'
    78. sh /usr/local/esl/scripts/docker-install-and-uninstall.sh
    79. echo -n -e "\033[1;32m $Log_time Modify kernel parameters \n\033[0m" >> $Installation_Logs
    80. echo $Iptables >> $Kernel_conf
    81. echo $Ip6tables >> $Kernel_conf
    82. echo $Ip_forward >> /etc/sysctl.conf
    83. sysctl -p >> $Installation_Logs 2>&1
    84. echo -n -e "\033[1;32m $Log_time Load mirror \n\033[0m" >> $Installation_Logs
    85. for image in `ls docker-images`
    86. do
    87. docker load -i docker-images/$image >> $Installation_Logs 2>&1
    88. done
    89. sed -i "s@/usr/local/esl@$current_directory@" docker-compose.yaml
    90. echo -n -e "\033[1;32m $Log_time Start Docker-compose.. \n\033[0m" >> $Installation_Logs
    91. docker-compose up -d >> $Installation_Logs 2>&1
    92. if [ $? -eq 0 ];then
    93. echo "The installation is complete"
    94. fi
    95. echo -n -e "\033[1;32m $Log_time Stop firewalld.. \n\033[0m" >> $Installation_Logs
    96. systemctl status firewalld | grep "running" >> $Installation_Logs 2>&1
    97. if [ $? -eq 0 ];then
    98. systemctl stop firewalld >> $Installation_Logs 2>&1
    99. systemctl disable firewalld >> $Installation_Logs 2>&1
    100. fi
    101. echo -n -e "\033[1;32m $Log_time Disabled Selinux \n\033[0m" >> $Installation_Logs
    102. if [ `getenforce` != "Disabled" ];then
    103. sed -i 's/SELINUX=enabled/SELINUX=disabled/g' /etc/selinux/config >> $Installation_Logs 2>&1
    104. setenforce 0 >> $Installation_Logs 2>&1
    105. fi
    106. echo -n -e '\033[1;32m正在执行docker-compose up -d --build \n\033[0m'
    107. docker-compose up -d --build >> $Installation_Logs 2>&1
    108. echo -n -e '\033[1;32m=========================================================================================================================================================================== \n\033[0m'
    109. docker ps
    110. echo -n -e '\033[1;32m=========================================================================================================================================================================== \n\033[0m'
    111. }
    112. ESL_directory_migration() {
    113. Project_Path="/usr/local/esl"
    114. Default_Path="/usr/local"
    115. Docker_Default_Path="/var/lib/docker"
    116. Command=`./docker-compose stop >>$Installation_Logs 2>&1`
    117. echo -e -n "\033[1;32m ===============================================================\n\033[0m"
    118. echo -e -n "\033[1;32m 当前正在执行迁移动作需要交互, 请勿离开当前终端... \n\033[0m"
    119. echo -e -n "\033[1;32m 选择安装最大系统盘. \n\033[0m"
    120. echo -e -n "\033[1;32m 例如: \n\033[0m"
    121. echo -e -n "\033[1;32m 输入/data 迁移ESL和Docker默认安装路径到/data目录 \n\033[0m"
    122. echo -e -n "\033[1;32m 输入为空 默认安装位置/usr/local/,退出程序. \n\033[0m"
    123. echo -e -n "\033[1;32m /usr/local 默认安装位置/usr/local/,退出程序. \n\033[0m"
    124. echo -e -n "\033[1;32m ===============================================================\n\033[0m"
    125. read -t 5 -p "请选择安装到系统最大盘 :" Storage_disk
    126. if [ -z $Storage_disk ];then
    127. echo "The value is empty"
    128. cd $Project_Path && ./docker-compose up -d --build >>$Installation_Logs 2>&1`
    129. exit 1
    130. elif [ "$Storage_disk" = "$Default_Path" ];then
    131. echo "Install to default path $Default_Path"
    132. cd $Project_Path && ./docker-compose up -d --build >>$Installation_Logs 2>&1`
    133. exit 1
    134. else
    135. echo "正在迁移esl目录至$Storage_disk..."
    136. cd $Project_Path && $Command >>$Installation_Logs 2>&1
    137. if [ $? -ne 0 ];then
    138. exit 1
    139. fi
    140. mv $Project_Path $Storage_disk
    141. if [ -d $Project_Path ];then
    142. rm -f $Project_Path
    143. fi
    144. ln -s $Storage_disk/esl $Project_Path && cd $Project_Path && ./docker-compose up -d --build >>$Installation_Logs 2>&1
    145. echo -e -n "\033[1;32m ESL迁移到$Storage_disk 成功,请检查$Project_Path 目录是否为软连接目录..\n\033[0m"
    146. fi
    147. echo -e -n "\033[1;32m 正在迁移Docker默认安装路径到$Storage_disk 目录...\n\033[0m"
    148. cd $Project_Path && $Command >>$Installation_Logs 2>&1
    149. systemctl stop docker.service
    150. mv /var/lib/docker $Storage_disk
    151. ln -s $Storage_disk/docker $Docker_Default_Path
    152. systemctl start docker.service
    153. echo "Docker安装目录已迁移到$Storage_disk 目录完成,请检查$Docker_Default_Path 是否为软连接目录..."
    154. echo "正在检查服务状态.."
    155. echo -n -e '\033[1;32m=========================================================================================================================================================================== \n\033[0m'
    156. docker ps
    157. echo -n -e '\033[1;32m=========================================================================================================================================================================== \n\033[0m'
    158. }
    159. Cront_Jobs(){
    160. Job_Path="/var/spool/cron/root"
    161. sed -i '/web_code.sh/d;/Monitoring.sh/d;/docker-log.sh/d' $Job_Path
    162. sed -i '/Monitoring/d;/Check/d;/Clear/d' $Job_Path
    163. Package=`rpm -qa|grep bridge-utils|wc -l`
    164. if [ $Package -ne 1 ];then
    165. yum -y install bridge-utils
    166. fi
    167. #BRIDGE_LIST=$( brctl show | cut -f 1 | grep br-)
    168. #ifconfig $BRIDGE_LIST down
    169. #brctl delbr $BRIDGE_LIST
    170. }
    171. Network(){
    172. Network=`ping -c 3 www.baidu.com >> $Installation_Logs 2>&1`
    173. if [ $? == 0 ];then
    174. Cront_Jobs
    175. Have_Network
    176. ESL_directory_migration
    177. else
    178. No_Network
    179. fi
    180. }
    181. Network