/TT/setup.sh

https://gitlab.com/lisit1003/TTAutoDeploy · Shell · 616 lines · 519 code · 63 blank · 34 comment · 61 complexity · 05b75e2808b5c518a9c309584531c280 MD5 · raw file

  1. #!/bin/bash
  2. # this is a setup scripts for TeamTalk
  3. # author: luoning
  4. # date: 09/05/2014
  5. # setup TeamTalk
  6. REDIS=redis
  7. MYSQL=percona
  8. NGINX_PHP=nginx_php
  9. NGINX=nginx
  10. PHP=php
  11. JDK=jdk
  12. IM_WEB=im_web
  13. IM_SERVER=im_server
  14. IM_DB_PROXY=im_db_proxy
  15. CUR_DIR=
  16. SETUP_PROGRESS=setup.progress
  17. REDIS_SETUP_BEGIN=0
  18. REDIS_SETUP_SUCCESS=0
  19. MYSQL_SETUP_BEGIN=0
  20. MYSQL_SETUP_SUCCESS=0
  21. NGINX_SETUP_BEGIN=0
  22. NGINX_SETUP_SUCCESS=0
  23. PHP_SETUP_BEGIN=0
  24. PHP_SETUP_SUCCESS=0
  25. JDK_SETUP_BEGIN=0
  26. JDK_SETUP_SUCCESS=0
  27. IM_WEB_SETUP_BEGIN=0
  28. IM_WEB_SETUP_SUCCESS=0
  29. IM_SERVER_SETUP_BEGIN=0
  30. IM_SERVER_SETUP_SUCCESS=0
  31. IM_DB_PROXY_SETUP_BEGIN=0
  32. IM_DB_PROXY_SETUP_SUCCESS=0
  33. # Check if user is root
  34. check_user() {
  35. if [ $(id -u) != "0" ]; then
  36. echo "Error: You must be root to run this script, please use root to install im"
  37. exit 1
  38. fi
  39. }
  40. check_os() {
  41. OS_VERSION=$(cat /etc/issue)
  42. OS_BIT=$(getconf LONG_BIT)
  43. #echo "$OS_VERSION, $OS_BIT bit..."
  44. if [[ $OS_VERSION =~ "CentOS" ]]; then
  45. if [ $OS_BIT == 64 ]; then
  46. return 0
  47. else
  48. echo "Error: OS must be CentOS 64bit to run this script."
  49. exit 1
  50. fi
  51. else
  52. echo "Error: OS must be CentOS 64bit to run this script."
  53. exit 1
  54. fi
  55. }
  56. print_hello() {
  57. echo "========================================================================="
  58. echo "TeamTalk V1.0 for CentOS Linux Server, Written by Luoning"
  59. echo "========================================================================="
  60. echo "A tool to auto-compile & install TeamTalk on Linux "
  61. echo ""
  62. echo "For more information please visit https://github.com/mogutt/TTAutoDeploy/"
  63. echo "========================================================================="
  64. }
  65. get_cur_dir() {
  66. # Get the fully qualified path to the script
  67. case $0 in
  68. /*)
  69. SCRIPT="$0"
  70. ;;
  71. *)
  72. PWD_DIR=$(pwd);
  73. SCRIPT="${PWD_DIR}/$0"
  74. ;;
  75. esac
  76. # Resolve the true real path without any sym links.
  77. CHANGED=true
  78. while [ "X$CHANGED" != "X" ]
  79. do
  80. # Change spaces to ":" so the tokens can be parsed.
  81. SAFESCRIPT=`echo $SCRIPT | sed -e 's; ;:;g'`
  82. # Get the real path to this script, resolving any symbolic links
  83. TOKENS=`echo $SAFESCRIPT | sed -e 's;/; ;g'`
  84. REALPATH=
  85. for C in $TOKENS; do
  86. # Change any ":" in the token back to a space.
  87. C=`echo $C | sed -e 's;:; ;g'`
  88. REALPATH="$REALPATH/$C"
  89. # If REALPATH is a sym link, resolve it. Loop for nested links.
  90. while [ -h "$REALPATH" ] ; do
  91. LS="`ls -ld "$REALPATH"`"
  92. LINK="`expr "$LS" : '.*-> \(.*\)$'`"
  93. if expr "$LINK" : '/.*' > /dev/null; then
  94. # LINK is absolute.
  95. REALPATH="$LINK"
  96. else
  97. # LINK is relative.
  98. REALPATH="`dirname "$REALPATH"`""/$LINK"
  99. fi
  100. done
  101. done
  102. if [ "$REALPATH" = "$SCRIPT" ]
  103. then
  104. CHANGED=""
  105. else
  106. SCRIPT="$REALPATH"
  107. fi
  108. done
  109. # Change the current directory to the location of the script
  110. CUR_DIR=$(dirname "${REALPATH}")
  111. }
  112. setup_begin() {
  113. # example: redis start
  114. echo "$1 start" >> $CUR_DIR/$SETUP_PROGRESS
  115. }
  116. setup_success() {
  117. # example: redis success
  118. echo "$1 success" >> $CUR_DIR/$SETUP_PROGRESS
  119. }
  120. get_setup_process() {
  121. if [ -f $CUR_DIR/$SETUP_PROGRESS ]; then
  122. while read line
  123. do
  124. case $line in
  125. "$REDIS start")
  126. REDIS_SETUP_BEGIN=1
  127. ;;
  128. "$REDIS success")
  129. REDIS_SETUP_SUCCESS=1
  130. ;;
  131. "$MYSQL start")
  132. MYSQL_SETUP_BEGIN=1
  133. ;;
  134. "$MYSQL success")
  135. MYSQL_SETUP_SUCCESS=1
  136. ;;
  137. "$NGINX start")
  138. NGINX_SETUP_BEGIN=1
  139. ;;
  140. "$NGINX success")
  141. NGINX_SETUP_SUCCESS=1
  142. ;;
  143. "$PHP start")
  144. PHP_SETUP_BEGIN=1
  145. ;;
  146. "$PHP success")
  147. PHP_SETUP_SUCCESS=1
  148. ;;
  149. "$JDK start")
  150. JDK_SETUP_BEGIN=1
  151. ;;
  152. "$JDK success")
  153. JDK_SETUP_SUCCESS=1
  154. ;;
  155. "$IM_WEB start")
  156. IM_WEB_SETUP_BEGIN=1
  157. ;;
  158. "$IM_WEB success")
  159. IM_WEB_SETUP_SUCCESS=1
  160. ;;
  161. "$IM_SERVER start")
  162. IM_SERVER_SETUP_BEGIN=1
  163. ;;
  164. "$IM_SERVER success")
  165. IM_SERVER_SETUP_SUCCESS=1
  166. ;;
  167. "$IM_DB_PROXY start")
  168. IM_DB_PROXY_SETUP_BEGIN=1
  169. ;;
  170. "$IM_DB_PROXY success")
  171. IM_DB_PROXY_SETUP_SUCCESS=1
  172. ;;
  173. *)
  174. echo "unknown setup progress: $line "
  175. ;;
  176. esac
  177. done < $CUR_DIR/$SETUP_PROGRESS
  178. fi
  179. }
  180. check_redis() {
  181. cd $REDIS
  182. chmod +x setup.sh
  183. ./setup.sh check
  184. if [ $? -eq 0 ]; then
  185. cd $CUR_DIR
  186. else
  187. return 1
  188. fi
  189. }
  190. build_redis() {
  191. cd $REDIS
  192. chmod +x setup.sh
  193. setup_begin $REDIS
  194. ./setup.sh install
  195. if [ $? -eq 0 ]; then
  196. setup_success $REDIS
  197. cd $CUR_DIR
  198. else
  199. return 1
  200. fi
  201. }
  202. check_percona() {
  203. cd $MYSQL
  204. chmod +x setup.sh
  205. ./setup.sh check
  206. if [ $? -eq 0 ]; then
  207. cd $CUR_DIR
  208. else
  209. return 1
  210. fi
  211. }
  212. build_percona() {
  213. cd $MYSQL
  214. chmod +x setup.sh
  215. setup_begin $MYSQL
  216. ./setup.sh install
  217. if [ $? -eq 0 ]; then
  218. setup_success $MYSQL
  219. cd $CUR_DIR
  220. else
  221. return 1
  222. fi
  223. }
  224. check_nginx() {
  225. cd $NGINX_PHP
  226. cd $NGINX
  227. chmod +x setup.sh
  228. ./setup.sh check
  229. if [ $? -eq 0 ]; then
  230. cd $CUR_DIR
  231. else
  232. return 1
  233. fi
  234. }
  235. check_php() {
  236. cd $NGINX_PHP
  237. cd $PHP
  238. chmod +x setup.sh
  239. ./setup.sh check
  240. if [ $? -eq 0 ]; then
  241. cd $CUR_DIR
  242. else
  243. return 1
  244. fi
  245. }
  246. build_nginx() {
  247. cd $NGINX_PHP
  248. cd $NGINX
  249. chmod +x setup.sh
  250. setup_begin $NGINX
  251. ./setup.sh install
  252. if [ $? -eq 0 ]; then
  253. setup_success $NGINX
  254. cd $CUR_DIR
  255. else
  256. return 1
  257. fi
  258. }
  259. build_php() {
  260. cd $NGINX_PHP
  261. cd $PHP
  262. chmod +x setup.sh
  263. setup_begin $PHP
  264. ./setup.sh install
  265. if [ $? -eq 0 ]; then
  266. setup_success $PHP
  267. cd $CUR_DIR
  268. else
  269. return 1
  270. fi
  271. }
  272. check_jdk() {
  273. cd $JDK
  274. chmod +x setup.sh
  275. ./setup.sh check
  276. if [ $? -eq 0 ]; then
  277. cd $CUR_DIR
  278. else
  279. return 1
  280. fi
  281. }
  282. build_jdk() {
  283. cd $JDK
  284. chmod +x setup.sh
  285. setup_begin $JDK
  286. ./setup.sh install
  287. if [ $? -eq 0 ]; then
  288. setup_success $JDK
  289. cd $CUR_DIR
  290. else
  291. return 1
  292. fi
  293. }
  294. check_im_web() {
  295. cd $IM_WEB
  296. chmod +x setup.sh
  297. ./setup.sh check
  298. if [ $? -eq 0 ]; then
  299. cd $CUR_DIR
  300. else
  301. return 1
  302. fi
  303. }
  304. build_im_web() {
  305. cd $IM_WEB
  306. chmod +x setup.sh
  307. setup_begin $IM_WEB
  308. ./setup.sh install
  309. if [ $? -eq 0 ]; then
  310. setup_success $IM_WEB
  311. cd $CUR_DIR
  312. else
  313. return 1
  314. fi
  315. }
  316. check_im_db_proxy() {
  317. cd $IM_DB_PROXY
  318. chmod +x setup.sh
  319. ./setup.sh check
  320. if [ $? -eq 0 ]; then
  321. cd $CUR_DIR
  322. else
  323. return 1
  324. fi
  325. }
  326. build_im_db_proxy() {
  327. cd $IM_DB_PROXY
  328. chmod +x setup.sh
  329. setup_begin $IM_DB_PROXY
  330. ./setup.sh install
  331. if [ $? -eq 0 ]; then
  332. setup_success $IM_DB_PROXY
  333. cd $CUR_DIR
  334. else
  335. return 1
  336. fi
  337. }
  338. check_im_server() {
  339. cd $IM_SERVER
  340. chmod +x setup.sh
  341. ./setup.sh check
  342. if [ $? -eq 0 ]; then
  343. cd $CUR_DIR
  344. else
  345. return 1
  346. fi
  347. }
  348. build_im_server() {
  349. cd $IM_SERVER
  350. chmod +x setup.sh
  351. setup_begin $IM_SERVER
  352. ./setup.sh install
  353. if [ $? -eq 0 ]; then
  354. setup_success $IM_SERVER
  355. cd $CUR_DIR
  356. else
  357. return 1
  358. fi
  359. }
  360. check_install() {
  361. local MODULE=$1
  362. local MODULE_SETUP_BEGIN=$2
  363. local MODULE_SETUP_SUCCESS=$3
  364. if [ $MODULE_SETUP_BEGIN = 1 ] && [ $MODULE_SETUP_SUCCESS = 1 ]; then
  365. echo "$MODULE has installed, skip check..."
  366. return 2
  367. else
  368. if [ $MODULE_SETUP_BEGIN = 1 ] && [ $MODULE_SETUP_SUCCESS = 0 ]; then
  369. echo "Warning: $MODULE has installed before, but failed by some reason, check/build again?(Y/N)"
  370. while read input
  371. do
  372. if [ $input = "Y" ] || [ $input = "y" ]; then
  373. return 0
  374. elif [ $input = "N" ] || [ $input = "n" ]; then
  375. return 1
  376. else
  377. echo "unknown input, try again please, check/build again?(Y/N)."
  378. continue
  379. fi
  380. done
  381. fi
  382. fi
  383. return 0
  384. }
  385. check_env() {
  386. local MODULE=$1
  387. echo "start to check $MODULE..."
  388. check_$MODULE
  389. if [ $? -eq 0 ]; then
  390. echo "check $MODULE successed."
  391. else
  392. echo "Error: check $MODULE failed, stop install."
  393. exit 1
  394. fi
  395. }
  396. check_module() {
  397. local MODULE=$1
  398. local MODULE_SETUP_BEGIN=$2
  399. local MODULE_SETUP_SUCCESS=$3
  400. check_install $MODULE $MODULE_SETUP_BEGIN $MODULE_SETUP_SUCCESS
  401. RET=$?
  402. if [ $RET -eq 0 ]; then
  403. check_env $MODULE
  404. elif [ $RET -eq 1 ]; then
  405. return 1
  406. fi
  407. return 0
  408. }
  409. check_all() {
  410. get_setup_process
  411. #redis
  412. check_module $REDIS $REDIS_SETUP_BEGIN $REDIS_SETUP_SUCCESS
  413. if [ $? -eq 1 ]; then
  414. exit 1
  415. fi
  416. #mysql
  417. check_module $MYSQL $MYSQL_SETUP_BEGIN $MYSQL_SETUP_SUCCESS
  418. if [ $? -eq 1 ]; then
  419. exit 1
  420. fi
  421. #nginx
  422. check_module $NGINX $NGINX_SETUP_BEGIN $NGINX_SETUP_SUCCESS
  423. if [ $? -eq 1 ]; then
  424. exit 1
  425. fi
  426. #php
  427. check_module $PHP $PHP_SETUP_BEGIN $PHP_SETUP_SUCCESS
  428. if [ $? -eq 1 ]; then
  429. exit 1
  430. fi
  431. #jdk
  432. check_module $JDK $JDK_SETUP_BEGIN $JDK_SETUP_SUCCESS
  433. if [ $? -eq 1 ]; then
  434. exit 1
  435. fi
  436. #im_web
  437. check_module $IM_WEB $IM_WEB_SETUP_BEGIN $IM_WEB_SETUP_SUCCESS
  438. if [ $? -eq 1 ]; then
  439. exit 1
  440. fi
  441. #im_server
  442. check_module $IM_SERVER $IM_SERVER_SETUP_BEGIN $IM_SERVER_SETUP_SUCCESS
  443. if [ $? -eq 1 ]; then
  444. exit 1
  445. fi
  446. #im_db_proxy
  447. check_module $IM_DB_PROXY $IM_DB_PROXY_SETUP_BEGIN $IM_DB_PROXY_SETUP_SUCCESS
  448. if [ $? -eq 1 ]; then
  449. exit 1
  450. fi
  451. echo "Check TeamTalk successed, and you can install TeamTalk now."
  452. }
  453. clean_yum() {
  454. YUM_PID=/var/run/yum.pid
  455. if [ -f "$YUM_PID" ]; then
  456. set -x
  457. rm -f YUM_PID
  458. killall yum
  459. set +x
  460. fi
  461. }
  462. build_module() {
  463. local MODULE=$1
  464. local MODULE_SETUP_BEGIN=$2
  465. local MODULE_SETUP_SUCCESS=$3
  466. if [ $MODULE_SETUP_BEGIN = 1 ] && [ $MODULE_SETUP_SUCCESS = 1 ]; then
  467. echo "$MODULE has installed, skip build..."
  468. else
  469. echo "start to build $MODULE..."
  470. build_$MODULE
  471. if [ $? -eq 0 ]; then
  472. echo "build $MODULE successed."
  473. else
  474. echo "Error: build $MODULE failed, stop install."
  475. return 1
  476. fi
  477. fi
  478. }
  479. build_all() {
  480. clean_yum
  481. yum -y install yum
  482. if [ $? -eq 0 ]; then
  483. echo "update yum successed."
  484. else
  485. echo "update yum failed."
  486. exit 1
  487. fi
  488. #redis
  489. build_module $REDIS $REDIS_SETUP_BEGIN $REDIS_SETUP_SUCCESS
  490. if [ $? -eq 1 ]; then
  491. exit 1
  492. fi
  493. #mysql
  494. build_module $MYSQL $MYSQL_SETUP_BEGIN $MYSQL_SETUP_SUCCESS
  495. if [ $? -eq 1 ]; then
  496. exit 1
  497. fi
  498. #nginx
  499. build_module $NGINX $NGINX_SETUP_BEGIN $NGINX_SETUP_SUCCESS
  500. if [ $? -eq 1 ]; then
  501. exit 1
  502. fi
  503. #php
  504. build_module $PHP $PHP_SETUP_BEGIN $PHP_SETUP_SUCCESS
  505. if [ $? -eq 1 ]; then
  506. exit 1
  507. fi
  508. #jdk
  509. build_module $JDK $JDK_SETUP_BEGIN $JDK_SETUP_SUCCESS
  510. if [ $? -eq 1 ]; then
  511. exit 1
  512. fi
  513. #im_web
  514. build_module $IM_WEB $IM_WEB_SETUP_BEGIN $IM_WEB_SETUP_SUCCESS
  515. if [ $? -eq 1 ]; then
  516. exit 1
  517. fi
  518. #im_server
  519. build_module $IM_SERVER $IM_SERVER_SETUP_BEGIN $IM_SERVER_SETUP_SUCCESS
  520. if [ $? -eq 1 ]; then
  521. exit 1
  522. fi
  523. #im_db_proxy
  524. build_module $IM_DB_PROXY $IM_DB_PROXY_SETUP_BEGIN $IM_DB_PROXY_SETUP_SUCCESS
  525. if [ $? -eq 1 ]; then
  526. exit 1
  527. fi
  528. }
  529. print_help() {
  530. echo "Usage: "
  531. echo " $0 check --- check environment"
  532. echo " $0 install --- check & run scripts to install"
  533. }
  534. case $1 in
  535. check)
  536. print_hello
  537. check_user
  538. check_os
  539. get_cur_dir
  540. check_all
  541. ;;
  542. install)
  543. print_hello
  544. check_user
  545. check_os
  546. get_cur_dir
  547. check_all
  548. build_all
  549. ;;
  550. *)
  551. print_help
  552. ;;
  553. esac