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.

181 lines
5.3 KiB

3 years ago
  1. #!/bin/sh
  2. # deploy.sh
  3. # This script deploy repo to target env.
  4. # @version 170322:2
  5. # @author zhangxuhong <zhangxuhong@xitu.io>
  6. #
  7. # [start deploy]
  8. echo "-[start deploy]-"
  9. # ----------------------------[manual config here]------------------------------
  10. # REPO_SRC=/data/repo/collection_set_api
  11. # BUILD_TARGET=collection_set_api
  12. # DEPLOY_SRC=/data/repo
  13. # DEPLOY_LOG_SRC=/data/logs
  14. # RELEASE_ENV=$1
  15. #
  16. # DEV_ENV_LOCATION=(
  17. # "root@192.168.0.203"
  18. # )
  19. # BETA_ENV_LOCATION=(
  20. # "root@192.168.0.204"
  21. # )
  22. # ONLINE_ENV_LOCATION=(
  23. # "root@192.168.100.32" # microservice03v
  24. # "root@192.168.100.28" # microservice04v
  25. # )
  26. # ------------------------------------------------------------------------------
  27. # final release location
  28. RELEASE_ENV_LOCATION=""
  29. # release file temp storage location
  30. RELEASE_TEMP_DIR=/data/repo/release
  31. # program system service name for systemctl
  32. SERVICE_NAME="${BUILD_TARGET}.service"
  33. # final deploy src
  34. FINAL_DEPLOY_SRC="${DEPLOY_SRC}/${BUILD_TARGET}"
  35. # nginx config
  36. NGINX_BIN_LOCATION="/data/apps/nginx/sbin/nginx"
  37. NGINX_CONFIG_LOCATION="/data/apps/nginx/conf/vhost/"
  38. # deploy target machine error handler config
  39. DEPLOY_ERROR_SIGNAL_FILE="${FINAL_DEPLOY_SRC}/logs/deploy_error.s"
  40. DEPLOY_SUCCESS_SIGNAL_FILE="${FINAL_DEPLOY_SRC}/logs/deploy_success.s"
  41. DEPLOY_LOG="${FINAL_DEPLOY_SRC}/logs/deploy.log"
  42. # print config
  43. echo -e "\033[34mREPO_SRC set to: ${REPO_SRC}\033[0m"
  44. echo -e "\033[34mBUILD_TARGET set to: ${BUILD_TARGET}\033[0m"
  45. echo -e "\033[34mRELEASE_TEMP_DIR set to: ${RELEASE_TEMP_DIR}\033[0m"
  46. echo -e "\033[34mPROJECT WILL DEPLOY TO: ${FINAL_DEPLOY_SRC}\033[0m"
  47. echo -e "\033[34mSERVICE_NAME set to: ${SERVICE_NAME}\033[0m"
  48. echo ""
  49. sleep 2
  50. # [check input]
  51. if [ "$RELEASE_ENV" == "dev" ]; then
  52. RELEASE_ENV_LOCATION=${DEV_ENV_LOCATION[*]}
  53. elif [ "$RELEASE_ENV" == "beta" ]; then
  54. RELEASE_ENV_LOCATION=${BETA_ENV_LOCATION[*]}
  55. elif [ "$RELEASE_ENV" == "online" ]; then
  56. RELEASE_ENV_LOCATION=${ONLINE_ENV_LOCATION[*]}
  57. else
  58. RELEASE_ENV_LOCATION=""
  59. fi
  60. if [ "$RELEASE_ENV_LOCATION" == "" ]; then
  61. echo "please input an avaliable deploy environment [dev/beta/online]";
  62. echo "example: ./deploy.sh beta";
  63. exit 1;
  64. fi
  65. echo -e "\033[34mRELEASE_ENV set to: ${RELEASE_ENV} \033[0m"
  66. echo -e "\033[34mRELEASE_ENV_LOCATION set to: \033[0m"
  67. for releaseLocation in ${RELEASE_ENV_LOCATION[*]};
  68. do
  69. echo -e "\033[34m ${releaseLocation} \033[0m"
  70. done
  71. echo ""
  72. sleep 1
  73. # [check old release file]
  74. echo "-[check old release target]-"
  75. if [ ! -d $RELEASE_TEMP_DIR ]; then mkdir $RELEASE_TEMP_DIR; fi
  76. if [ -f "${RELEASE_TEMP_DIR}/${BUILD_TARGET}.tar.gz" ]; then
  77. echo -e "\033[43m[WARRING!] release target alerady exists, will remove it. \033[0m";
  78. ls -alh "${RELEASE_TEMP_DIR}/${BUILD_TARGET}.tar.gz";
  79. oldShaSum=`shasum "${RELEASE_TEMP_DIR}/${BUILD_TARGET}.tar.gz" | awk -F' ' '{print $1}'`;
  80. echo "target sha: ${oldShaSum}";
  81. rm -f "${RELEASE_TEMP_DIR}/${BUILD_TARGET}.tar.gz";
  82. echo "old build target removed.";
  83. fi
  84. echo ""
  85. sleep 1
  86. # [compress new release file]
  87. echo "-[compress new release file]-"
  88. cd $REPO_SRC;
  89. tar -zcf "${RELEASE_TEMP_DIR}/${BUILD_TARGET}.tar.gz" "./"
  90. ls -alh "${RELEASE_TEMP_DIR}/${BUILD_TARGET}.tar.gz";
  91. newShaSum=`shasum "${RELEASE_TEMP_DIR}/${BUILD_TARGET}.tar.gz" | awk -F' ' '{print $1}'`;
  92. echo "target sha: ${newShaSum}";
  93. if [ "$oldShaSum" == "$newShaSum" ];then
  94. echo -e "\033[43m[WARRING!] old release target shasum is equals to new release target. \033[0m"
  95. fi
  96. echo ""
  97. sleep 1
  98. # [deploy]
  99. echo "-[deploy]-"
  100. echo "- coyp to target machine"
  101. for releaseLocation in ${RELEASE_ENV_LOCATION[*]};
  102. do
  103. echo -e "\033[34m deploy to ${releaseLocation} \033[0m";
  104. ssh -T $releaseLocation <<DEPLOY_INIT_SCRIPTS_1
  105. if [ ! -d $FINAL_DEPLOY_SRC ]; then mkdir $FINAL_DEPLOY_SRC; fi
  106. if [ -f ${FINAL_DEPLOY_SRC}/${BUILD_TARGET}.tar.gz ]; then
  107. rm -f ${FINAL_DEPLOY_SRC}/${BUILD_TARGET}.tar.gz;
  108. fi
  109. DEPLOY_INIT_SCRIPTS_1
  110. scp "${RELEASE_TEMP_DIR}/${BUILD_TARGET}.tar.gz" "${releaseLocation}:${FINAL_DEPLOY_SRC}/";
  111. ssh -T $releaseLocation <<DEPLOY_INIT_SCRIPTS_2
  112. cd "${FINAL_DEPLOY_SRC}/";
  113. tar -zxvf "./${BUILD_TARGET}.tar.gz";
  114. DEPLOY_INIT_SCRIPTS_2
  115. done
  116. echo "done."
  117. echo ""
  118. echo "-[configure & reactive service]-"
  119. i=0
  120. for releaseLocation in ${RELEASE_ENV_LOCATION[*]};
  121. do
  122. # sleep for online lvs reconnect nginx
  123. if [ "$i" != "0" ]; then
  124. echo "sleep 120 seconds for online lvs reconnect nginx.";
  125. sleep 120;
  126. fi
  127. # deploy start
  128. echo -e "\033[34mconfigure ${releaseLocation} \033[0m"
  129. ssh -T $releaseLocation <<DEPLOY_SCRIPTS_1
  130. # exec reactive scripts
  131. chmod +x "${FINAL_DEPLOY_SRC}/scripts/ci/reactive_service.sh";
  132. bash "${FINAL_DEPLOY_SRC}/scripts/ci/reactive_service.sh" ${REPO_SRC} ${BUILD_TARGET} ${DEPLOY_SRC} ${DEPLOY_LOG_SRC} ${RELEASE_ENV};
  133. DEPLOY_SCRIPTS_1
  134. #check deploy result
  135. isDeploySuccess=""
  136. isDeploySuccess=`ssh -T $releaseLocation <<DEPLOY_SCRIPTS_2
  137. cat $DEPLOY_SUCCESS_SIGNAL_FILE
  138. DEPLOY_SCRIPTS_2`
  139. if [ "${isDeploySuccess}" != "success" ]; then
  140. echo -e "\033[43m[ERROR!] deploy failed detected. Failed at : ${releaseLocation}\033[0m";
  141. echo "please check release log at : ${releaseLocation}${DEPLOY_LOG}";
  142. echo "script exit.";
  143. exit 1;
  144. else
  145. echo -e "\033[32m[OK] deploy success. deploy at : ${releaseLocation}\033[0m";
  146. fi
  147. # counter
  148. i=$(expr "$i" + "1");
  149. done
  150. echo "done.";
  151. exit 0;