1. #!/usr/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="./log/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 2>&1
    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" >>$Installation_Logs
    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 >> $Installation_Logs 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 There is no external network on the current machine, and the offline installation script is being executed \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 Running 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 The migration is in progress. Please do not leave the terminal... \n\033[0m"
    119. echo -e -n "\033[1;32m Select system maximum disk migration. \n\033[0m"
    120. echo -e -n "\033[1;32m for example: \n\033[0m"
    121. echo -e -n "\033[1;32m input /data Migrate the default installation path of ESL and docker to the /data directory \n\033[0m"
    122. echo -e -n "\033[1;32m If the input is empty, the default installation location is /usr/local/. Exit the program \n\033[0m"
    123. echo -e -n "\033[1;32m /usr/local is the default installation location /usr/local/, exit the program \n\033[0m"
    124. echo -e -n "\033[1;32m ================================================================================================================\n\033[0m"
    125. read -t 50 -p "Please select the largest disk of the current system :" 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. TimeZone(){
    160. PKGVERSION="(GNU libc) "
    161. TZVERSION="2.17"
    162. REPORT_BUGS_TO="<http://www.gnu.org/software/libc/bugs.html>"
    163. : ${AWK=awk}
    164. : ${TZDIR=/usr/share/zoneinfo}
    165. ($AWK -v x=y 'BEGIN { exit 123 }') </dev/null >/dev/null 2>&1
    166. [ $? = 123 ] || {
    167. echo >&2 "$0: Sorry, your \`$AWK' program is not Posix compatible."
    168. exit 1
    169. }
    170. if [ "$1" = "--help" ]; then
    171. cat <<EOF
    172. Usage: tzselect
    173. Select a time zone interactively.
    174. Report bugs to $REPORT_BUGS_TO.
    175. EOF
    176. exit
    177. elif [ "$1" = "--version" ]; then
    178. cat <<EOF
    179. tzselect $PKGVERSION$TZVERSION
    180. EOF
    181. exit
    182. fi
    183. # Make sure the tables are readable.
    184. TZ_COUNTRY_TABLE=$TZDIR/iso3166.tab
    185. TZ_ZONE_TABLE=$TZDIR/zone.tab
    186. for f in $TZ_COUNTRY_TABLE $TZ_ZONE_TABLE
    187. do
    188. <$f || {
    189. echo >&2 "$0: time zone files are not set up correctly"
    190. exit 1
    191. }
    192. done
    193. newline='
    194. '
    195. IFS=$newline
    196. # Work around a bug in bash 1.14.7 and earlier, where $PS3 is sent to stdout.
    197. case $(echo 1 | (select x in x; do break; done) 2>/dev/null) in
    198. ?*) PS3=
    199. esac
    200. # Begin the main loop. We come back here if the user wants to retry.
    201. while
    202. echo >&2 'Please identify a location' \
    203. 'so that time zone rules can be set correctly.'
    204. continent=
    205. country=
    206. region=
    207. # Ask the user for continent or ocean.
    208. echo >&2 'Please select a continent or ocean.'
    209. select continent in \
    210. Africa \
    211. Americas \
    212. Antarctica \
    213. 'Arctic Ocean' \
    214. Asia \
    215. 'Atlantic Ocean' \
    216. Australia \
    217. Europe \
    218. 'Indian Ocean' \
    219. 'Pacific Ocean' \
    220. 'none - I want to specify the time zone using the Posix TZ format.'
    221. do
    222. case $continent in
    223. '')
    224. echo >&2 'Please enter a number in range.';;
    225. ?*)
    226. case $continent in
    227. Americas) continent=America;;
    228. *' '*) continent=$(expr "$continent" : '\([^ ]*\)')
    229. esac
    230. break
    231. esac
    232. done
    233. case $continent in
    234. '')
    235. exit 1;;
    236. none)
    237. # Ask the user for a Posix TZ string. Check that it conforms.
    238. while
    239. echo >&2 'Please enter the desired value' \
    240. 'of the TZ environment variable.'
    241. echo >&2 'For example, GST-10 is a zone named GST' \
    242. 'that is 10 hours ahead (east) of UTC.'
    243. read TZ
    244. $AWK -v TZ="$TZ" 'BEGIN {
    245. tzname = "[^-+,0-9][^-+,0-9][^-+,0-9]+"
    246. time = "[0-2]?[0-9](:[0-5][0-9](:[0-5][0-9])?)?"
    247. offset = "[-+]?" time
    248. date = "(J?[0-9]+|M[0-9]+\.[0-9]+\.[0-9]+)"
    249. datetime = "," date "(/" time ")?"
    250. tzpattern = "^(:.*|" tzname offset "(" tzname \
    251. "(" offset ")?(" datetime datetime ")?)?)$"
    252. if (TZ ~ tzpattern) exit 1
    253. exit 0
    254. }'
    255. do
    256. echo >&2 "\`$TZ' is not a conforming" \
    257. 'Posix time zone string.'
    258. done
    259. TZ_for_date=$TZ;;
    260. *)
    261. # Get list of names of countries in the continent or ocean.
    262. countries=$($AWK -F'\t' \
    263. -v continent="$continent" \
    264. -v TZ_COUNTRY_TABLE="$TZ_COUNTRY_TABLE" \
    265. '
    266. /^#/ { next }
    267. $3 ~ ("^" continent "/") {
    268. if (!cc_seen[$1]++) cc_list[++ccs] = $1
    269. }
    270. END {
    271. while (getline <TZ_COUNTRY_TABLE) {
    272. if ($0 !~ /^#/) cc_name[$1] = $2
    273. }
    274. for (i = 1; i <= ccs; i++) {
    275. country = cc_list[i]
    276. if (cc_name[country]) {
    277. country = cc_name[country]
    278. }
    279. print country
    280. }
    281. }
    282. ' <$TZ_ZONE_TABLE | sort -f)
    283. # If there's more than one country, ask the user which one.
    284. case $countries in
    285. *"$newline"*)
    286. echo >&2 'Please select a country.'
    287. select country in $countries
    288. do
    289. case $country in
    290. '') echo >&2 'Please enter a number in range.';;
    291. ?*) break
    292. esac
    293. done
    294. case $country in
    295. '') exit 1
    296. esac;;
    297. *)
    298. country=$countries
    299. esac
    300. # Get list of names of time zone rule regions in the country.
    301. regions=$($AWK -F'\t' \
    302. -v country="$country" \
    303. -v TZ_COUNTRY_TABLE="$TZ_COUNTRY_TABLE" \
    304. '
    305. BEGIN {
    306. cc = country
    307. while (getline <TZ_COUNTRY_TABLE) {
    308. if ($0 !~ /^#/ && country == $2) {
    309. cc = $1
    310. break
    311. }
    312. }
    313. }
    314. $1 == cc { print $4 }
    315. ' <$TZ_ZONE_TABLE)
    316. # If there's more than one region, ask the user which one.
    317. case $regions in
    318. *"$newline"*)
    319. echo >&2 'Please select one of the following' \
    320. 'time zone regions.'
    321. select region in $regions
    322. do
    323. case $region in
    324. '') echo >&2 'Please enter a number in range.';;
    325. ?*) break
    326. esac
    327. done
    328. case $region in
    329. '') exit 1
    330. esac;;
    331. *)
    332. region=$regions
    333. esac
    334. # Determine TZ from country and region.
    335. TZ=$($AWK -F'\t' \
    336. -v country="$country" \
    337. -v region="$region" \
    338. -v TZ_COUNTRY_TABLE="$TZ_COUNTRY_TABLE" \
    339. '
    340. BEGIN {
    341. cc = country
    342. while (getline <TZ_COUNTRY_TABLE) {
    343. if ($0 !~ /^#/ && country == $2) {
    344. cc = $1
    345. break
    346. }
    347. }
    348. }
    349. $1 == cc && $4 == region { print $3 }
    350. ' <$TZ_ZONE_TABLE)
    351. # Make sure the corresponding zoneinfo file exists.
    352. TZ_for_date=$TZDIR/$TZ
    353. <$TZ_for_date || {
    354. echo >&2 "$0: time zone files are not set up correctly"
    355. exit 1
    356. }
    357. esac
    358. # Use the proposed TZ to output the current date relative to UTC.
    359. # Loop until they agree in seconds.
    360. # Give up after 8 unsuccessful tries.
    361. extra_info=
    362. for i in 1 2 3 4 5 6 7 8
    363. do
    364. TZdate=$(LANG=C TZ="$TZ_for_date" date)
    365. UTdate=$(LANG=C TZ=UTC0 date)
    366. TZsec=$(expr "$TZdate" : '.*:\([0-5][0-9]\)')
    367. UTsec=$(expr "$UTdate" : '.*:\([0-5][0-9]\)')
    368. case $TZsec in
    369. $UTsec)
    370. extra_info="
    371. Local time is now: $TZdate.
    372. Universal Time is now: $UTdate."
    373. break
    374. esac
    375. done
    376. # Output TZ info and ask the user to confirm.
    377. echo >&2 ""
    378. echo >&2 "The following information has been given:"
    379. echo >&2 ""
    380. case $country+$region in
    381. ?*+?*) echo >&2 " $country$newline $region";;
    382. ?*+) echo >&2 " $country";;
    383. +) echo >&2 " TZ='$TZ'"
    384. esac
    385. echo >&2 ""
    386. echo >&2 "Therefore TZ='$TZ' will be used.$extra_info"
    387. echo >&2 "Is the above information OK?"
    388. ok=
    389. select ok in Yes No
    390. do
    391. case $ok in
    392. '') echo >&2 'Please enter 1 for Yes, or 2 for No.';;
    393. ?*) break
    394. esac
    395. done
    396. case $ok in
    397. '') exit 1;;
    398. Yes) break
    399. esac
    400. do :
    401. done
    402. case $SHELL in
    403. *csh) file=.login line="setenv TZ '$TZ'";;
    404. *) file=.profile line="TZ='$TZ'; export TZ"
    405. esac
    406. echo >&2 "
    407. You can make this change permanent for yourself by appending the line
    408. $line
    409. to the file '$file' in your home directory; then log out and log in again.
    410. Here is that TZ value again, this time on standard output so that you
    411. can use the $0 command in shell scripts:" >> $Installation_Logs
    412. sed '/Time/d' /usr/local/esl/.env -i
    413. echo "$TZ"
    414. echo "Time=$TZ" >> /usr/local/esl/.env
    415. }
    416. Cront_Jobs(){
    417. Job_Path="/var/spool/cron/root"
    418. sed -i '/web_code.sh/d;/Monitoring.sh/d;/docker-log.sh/d' $Job_Path
    419. sed -i '/Monitoring/d;/Check/d;/Clear/d' $Job_Path
    420. Package=`rpm -qa|grep bridge-utils|wc -l`
    421. if [ $Package -ne 1 ];then
    422. yum -y install bridge-utils
    423. fi
    424. BRIDGE_LIST=$( brctl show | cut -f 1 | grep br-)
    425. ifconfig $BRIDGE_LIST down
    426. brctl delbr $BRIDGE_LIST
    427. }
    428. Network(){
    429. Network=`ping -c 3 www.baidu.com >> $Installation_Logs 2>&1`
    430. if [ $? == 0 ];then
    431. TimeZone
    432. Cront_Jobs
    433. Have_Network
    434. ESL_directory_migration
    435. else
    436. No_Network
    437. fi
    438. }
    439. Network