Go框架demo
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
6.2 KiB

3 years ago
  1. #!/bin/sh
  2. # reactive_service.sh
  3. # This script for reactive this projects.
  4. # @version 170327:3
  5. # @author zhangxuhong <zhangxuhong@xitu.io>
  6. #
  7. # [config]
  8. echo "-[config]-"
  9. # ----------------------------[manual config here]------------------------------
  10. REPO_SRC=$1
  11. BUILD_TARGET=$2
  12. DEPLOY_SRC=$3
  13. DEPLOY_LOG_SRC=$4
  14. RELEASE_ENV=$5
  15. # ------------------------------------------------------------------------------
  16. # program system service name for systemctl
  17. SERVICE_NAME="${BUILD_TARGET}.service"
  18. # final deploy src
  19. FINAL_DEPLOY_SRC="${DEPLOY_SRC}/${BUILD_TARGET}"
  20. # nginx config
  21. NGINX_BIN_LOCATION="/data/apps/nginx/sbin/nginx"
  22. NGINX_CONFIG_LOCATION="/data/apps/nginx/conf/vhost/"
  23. # deploy target machine error handler config
  24. DEPLOY_ERROR_SIGNAL_FILE="${FINAL_DEPLOY_SRC}/logs/deploy_error.s"
  25. DEPLOY_SUCCESS_SIGNAL_FILE="${FINAL_DEPLOY_SRC}/logs/deploy_success.s"
  26. DEPLOY_LOG="${FINAL_DEPLOY_SRC}/logs/deploy.log"
  27. # print config
  28. echo -e "\033[34mREPO_SRC set to: ${REPO_SRC}\033[0m"
  29. echo -e "\033[34mBUILD_TARGET set to: ${BUILD_TARGET}\033[0m"
  30. echo -e "\033[34mPROJECT WILL DEPLOY TO: ${FINAL_DEPLOY_SRC}\033[0m"
  31. echo -e "\033[34mSERVICE_NAME set to: ${SERVICE_NAME}\033[0m"
  32. echo ""
  33. sleep 2
  34. # [check input]
  35. if [ "$RELEASE_ENV" == "dev" ]; then
  36. echo "env ok";
  37. elif [ "$RELEASE_ENV" == "beta" ]; then
  38. echo "env ok";
  39. elif [ "$RELEASE_ENV" == "online" ]; then
  40. echo "env ok";
  41. else
  42. echo "please input an avaliable deploy environment [dev/beta/online]";
  43. echo "example: ./deploy.sh beta";
  44. exit 1;
  45. fi
  46. echo -e "\033[34mRELEASE_ENV set to: ${RELEASE_ENV} \033[0m"
  47. echo ""
  48. sleep 1
  49. # configure log
  50. if [ ! -d "${DEPLOY_LOG_SRC}/${BUILD_TARGET}" ]; then mkdir "${DEPLOY_LOG_SRC}/${BUILD_TARGET}"; fi
  51. if [ ! -L "${FINAL_DEPLOY_SRC}/logs" ]; then ln -s "${DEPLOY_LOG_SRC}/${BUILD_TARGET}" "${FINAL_DEPLOY_SRC}/logs"; fi
  52. # remove old deploy signal
  53. if [ -f "${DEPLOY_ERROR_SIGNAL_FILE}" ]; then rm -f "${DEPLOY_ERROR_SIGNAL_FILE}"; fi
  54. if [ -f "${DEPLOY_SUCCESS_SIGNAL_FILE}" ]; then rm -f "${DEPLOY_SUCCESS_SIGNAL_FILE}"; fi
  55. # deploy start
  56. echo "" | tee -a $DEPLOY_LOG;
  57. echo -e "`date "+%Y-%m-%d %H:%M:%S"` \c" | tee -a $DEPLOY_LOG;
  58. echo "deploy start" | tee -a $DEPLOY_LOG;
  59. # configure service
  60. if [ ! -L "/etc/systemd/system/${SERVICE_NAME}" ]; then
  61. cp "${FINAL_DEPLOY_SRC}/config/systemctl/${SERVICE_NAME}" "/etc/systemd/system/${SERVICE_NAME}";
  62. else
  63. rm -f "/etc/systemd/system/${SERVICE_NAME}";
  64. cp "${FINAL_DEPLOY_SRC}/config/systemctl/${SERVICE_NAME}" "/etc/systemd/system/${SERVICE_NAME}";
  65. fi
  66. # configure nginx
  67. PUBLIC_NGINX_CONF="${FINAL_DEPLOY_SRC}/config/nginx/${BUILD_TARGET}.public.conf"
  68. PRIVATE_NGINX_CONF="${FINAL_DEPLOY_SRC}/config/nginx/${BUILD_TARGET}.private.conf"
  69. if [ -f "${PUBLIC_NGINX_CONF}" ]; then
  70. ln -s "${PUBLIC_NGINX_CONF}" $NGINX_CONFIG_LOCATION;
  71. fi
  72. if [ -f "${PRIVATE_NGINX_CONF}" ]; then
  73. ln -s "${PRIVATE_NGINX_CONF}" $NGINX_CONFIG_LOCATION;
  74. fi
  75. # test nginx config
  76. $NGINX_BIN_LOCATION -t 2> "${FINAL_DEPLOY_SRC}/logs/nginx_test.log";
  77. isNginxOk=`grep 'test is successful' "${FINAL_DEPLOY_SRC}/logs/nginx_test.log" | wc -l `;
  78. if [ "$isNginxOk" != "1" ]; then
  79. touch $DEPLOY_ERROR_SIGNAL_FILE;
  80. echo -e "`date "+%Y-%m-%d %H:%M:%S"` \c" | tee -a $DEPLOY_LOG;
  81. echo "[ERROR!] deploy error with nginx config error, nginx config error is : " | tee -a $DEPLOY_LOG;
  82. cat "${FINAL_DEPLOY_SRC}/logs/nginx_test.log" | tee -a $DEPLOY_LOG;
  83. exit 1;
  84. else
  85. echo -e "`date "+%Y-%m-%d %H:%M:%S"` \c" | tee -a $DEPLOY_LOG;
  86. echo "nginx config is ok:" | tee -a $DEPLOY_LOG;
  87. cat "${FINAL_DEPLOY_SRC}/logs/nginx_test.log" | tee -a $DEPLOY_LOG;
  88. fi
  89. # stop nginx
  90. $NGINX_BIN_LOCATION -s stop
  91. # check nginx is stoped
  92. sleep 20;
  93. nginxNum=`ps auxw | grep --color 'nginx' | grep -v grep | wc -l`
  94. if [ "$nginxNum" != "0" ]; then
  95. touch $DEPLOY_ERROR_SIGNAL_FILE;
  96. echo -e "`date "+%Y-%m-%d %H:%M:%S"` \c" | tee -a $DEPLOY_LOG;
  97. echo "[ERROR!] deploy error with nginx stop failed, nginx process num is : ${nginxNum}" | tee -a $DEPLOY_LOG;
  98. exit 1;
  99. else
  100. echo -e "`date "+%Y-%m-%d %H:%M:%S"` \c" | tee -a $DEPLOY_LOG;
  101. echo "nginx stopped" | tee -a $DEPLOY_LOG;
  102. fi
  103. # restart service
  104. /usr/bin/systemctl daemon-reload;
  105. /usr/bin/systemctl enable ${SERVICE_NAME};
  106. /usr/bin/systemctl stop ${BUILD_TARGET}; # use start-stop for first time deploy
  107. /usr/bin/systemctl start ${BUILD_TARGET};
  108. # check service
  109. sleep 2;
  110. /usr/bin/systemctl status "${BUILD_TARGET}" > "${FINAL_DEPLOY_SRC}/logs/service_status.log";
  111. serviceStatus=`grep "Active: active" "${FINAL_DEPLOY_SRC}/logs/service_status.log" | wc -l`;
  112. if [ "$serviceStatus" == "0" ]; then
  113. touch $DEPLOY_ERROR_SIGNAL_FILE;
  114. echo -e "`date "+%Y-%m-%d %H:%M:%S"` \c" | tee -a $DEPLOY_LOG;
  115. echo "[ERROR!] deploy error with service start failed, systemctl status :" | tee -a $DEPLOY_LOG;
  116. cat "${FINAL_DEPLOY_SRC}/logs/service_status.log" | tee -a $DEPLOY_LOG;
  117. # restart nginx
  118. $NGINX_BIN_LOCATION;
  119. # check nginx num
  120. nginxNum=`ps auxw | grep --color 'nginx' | grep -v grep | wc -l`
  121. sleep 5;
  122. if [ "$nginxNum" == "0" ]; then
  123. touch $DEPLOY_ERROR_SIGNAL_FILE;
  124. echo -e "`date "+%Y-%m-%d %H:%M:%S"` \c" | tee -a $DEPLOY_LOG;
  125. echo "[ERROR!] deploy error with nginx start failed, nginx process num is : ${nginxNum}" | tee -a $DEPLOY_LOG;
  126. exit 1;
  127. else
  128. echo -e "`date "+%Y-%m-%d %H:%M:%S"` \c" | tee -a $DEPLOY_LOG;
  129. echo "nginx started" | tee -a $DEPLOY_LOG;
  130. fi
  131. exit 1;
  132. else
  133. echo -e "`date "+%Y-%m-%d %H:%M:%S"` \c" | tee -a $DEPLOY_LOG;
  134. echo "service started" | tee -a $DEPLOY_LOG;
  135. cat "${FINAL_DEPLOY_SRC}/logs/service_status.log" | tee -a $DEPLOY_LOG;
  136. fi
  137. # start nginx
  138. $NGINX_BIN_LOCATION
  139. # check nginx num
  140. nginxNum=`ps auxw | grep --color 'nginx' | grep -v grep | wc -l`;
  141. sleep 5;
  142. if [ "$nginxNum" == "0" ]; then
  143. touch $DEPLOY_ERROR_SIGNAL_FILE;
  144. echo -e "`date "+%Y-%m-%d %H:%M:%S"` \c" | tee -a $DEPLOY_LOG;
  145. echo "[ERROR!] deploy error with nginx start failed, nginx process num is : ${nginxNum}" | tee -a $DEPLOY_LOG;
  146. exit 1;
  147. else
  148. echo -e "`date "+%Y-%m-%d %H:%M:%S"` \c" | tee -a $DEPLOY_LOG;
  149. echo "nginx started" | tee -a $DEPLOY_LOG;
  150. fi
  151. # deploy success
  152. echo "success" > $DEPLOY_SUCCESS_SIGNAL_FILE;
  153. echo -e "`date "+%Y-%m-%d %H:%M:%S"` \c" | tee -a $DEPLOY_LOG;
  154. echo "[OK] deploy success." | tee -a $DEPLOY_LOG;
  155. exit 0;