#!/bin/sh # deploy.sh # This script deploy repo to target env. # @version 170322:2 # @author zhangxuhong # # [start deploy] echo "-[start deploy]-" # ----------------------------[manual config here]------------------------------ # REPO_SRC=/data/repo/collection_set_api # BUILD_TARGET=collection_set_api # DEPLOY_SRC=/data/repo # DEPLOY_LOG_SRC=/data/logs # RELEASE_ENV=$1 # # DEV_ENV_LOCATION=( # "root@192.168.0.203" # ) # BETA_ENV_LOCATION=( # "root@192.168.0.204" # ) # ONLINE_ENV_LOCATION=( # "root@192.168.100.32" # microservice03v # "root@192.168.100.28" # microservice04v # ) # ------------------------------------------------------------------------------ # final release location RELEASE_ENV_LOCATION="" # release file temp storage location RELEASE_TEMP_DIR=/data/repo/release # program system service name for systemctl SERVICE_NAME="${BUILD_TARGET}.service" # final deploy src FINAL_DEPLOY_SRC="${DEPLOY_SRC}/${BUILD_TARGET}" # nginx config NGINX_BIN_LOCATION="/data/apps/nginx/sbin/nginx" NGINX_CONFIG_LOCATION="/data/apps/nginx/conf/vhost/" # deploy target machine error handler config DEPLOY_ERROR_SIGNAL_FILE="${FINAL_DEPLOY_SRC}/logs/deploy_error.s" DEPLOY_SUCCESS_SIGNAL_FILE="${FINAL_DEPLOY_SRC}/logs/deploy_success.s" DEPLOY_LOG="${FINAL_DEPLOY_SRC}/logs/deploy.log" # print config echo -e "\033[34mREPO_SRC set to: ${REPO_SRC}\033[0m" echo -e "\033[34mBUILD_TARGET set to: ${BUILD_TARGET}\033[0m" echo -e "\033[34mRELEASE_TEMP_DIR set to: ${RELEASE_TEMP_DIR}\033[0m" echo -e "\033[34mPROJECT WILL DEPLOY TO: ${FINAL_DEPLOY_SRC}\033[0m" echo -e "\033[34mSERVICE_NAME set to: ${SERVICE_NAME}\033[0m" echo "" sleep 2 # [check input] if [ "$RELEASE_ENV" == "dev" ]; then RELEASE_ENV_LOCATION=${DEV_ENV_LOCATION[*]} elif [ "$RELEASE_ENV" == "beta" ]; then RELEASE_ENV_LOCATION=${BETA_ENV_LOCATION[*]} elif [ "$RELEASE_ENV" == "online" ]; then RELEASE_ENV_LOCATION=${ONLINE_ENV_LOCATION[*]} else RELEASE_ENV_LOCATION="" fi if [ "$RELEASE_ENV_LOCATION" == "" ]; then echo "please input an avaliable deploy environment [dev/beta/online]"; echo "example: ./deploy.sh beta"; exit 1; fi echo -e "\033[34mRELEASE_ENV set to: ${RELEASE_ENV} \033[0m" echo -e "\033[34mRELEASE_ENV_LOCATION set to: \033[0m" for releaseLocation in ${RELEASE_ENV_LOCATION[*]}; do echo -e "\033[34m ${releaseLocation} \033[0m" done echo "" sleep 1 # [check old release file] echo "-[check old release target]-" if [ ! -d $RELEASE_TEMP_DIR ]; then mkdir $RELEASE_TEMP_DIR; fi if [ -f "${RELEASE_TEMP_DIR}/${BUILD_TARGET}.tar.gz" ]; then echo -e "\033[43m[WARRING!] release target alerady exists, will remove it. \033[0m"; ls -alh "${RELEASE_TEMP_DIR}/${BUILD_TARGET}.tar.gz"; oldShaSum=`shasum "${RELEASE_TEMP_DIR}/${BUILD_TARGET}.tar.gz" | awk -F' ' '{print $1}'`; echo "target sha: ${oldShaSum}"; rm -f "${RELEASE_TEMP_DIR}/${BUILD_TARGET}.tar.gz"; echo "old build target removed."; fi echo "" sleep 1 # [compress new release file] echo "-[compress new release file]-" cd $REPO_SRC; tar -zcf "${RELEASE_TEMP_DIR}/${BUILD_TARGET}.tar.gz" "./" ls -alh "${RELEASE_TEMP_DIR}/${BUILD_TARGET}.tar.gz"; newShaSum=`shasum "${RELEASE_TEMP_DIR}/${BUILD_TARGET}.tar.gz" | awk -F' ' '{print $1}'`; echo "target sha: ${newShaSum}"; if [ "$oldShaSum" == "$newShaSum" ];then echo -e "\033[43m[WARRING!] old release target shasum is equals to new release target. \033[0m" fi echo "" sleep 1 # [deploy] echo "-[deploy]-" echo "- coyp to target machine" for releaseLocation in ${RELEASE_ENV_LOCATION[*]}; do echo -e "\033[34m deploy to ${releaseLocation} \033[0m"; ssh -T $releaseLocation <