|
#!/bin/sh
|
|
# deploy.sh
|
|
# This script deploy repo to target env.
|
|
# @version 170322:2
|
|
# @author zhangxuhong <zhangxuhong@xitu.io>
|
|
#
|
|
|
|
# [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 <<DEPLOY_INIT_SCRIPTS_1
|
|
if [ ! -d $FINAL_DEPLOY_SRC ]; then mkdir $FINAL_DEPLOY_SRC; fi
|
|
if [ -f ${FINAL_DEPLOY_SRC}/${BUILD_TARGET}.tar.gz ]; then
|
|
rm -f ${FINAL_DEPLOY_SRC}/${BUILD_TARGET}.tar.gz;
|
|
fi
|
|
DEPLOY_INIT_SCRIPTS_1
|
|
scp "${RELEASE_TEMP_DIR}/${BUILD_TARGET}.tar.gz" "${releaseLocation}:${FINAL_DEPLOY_SRC}/";
|
|
ssh -T $releaseLocation <<DEPLOY_INIT_SCRIPTS_2
|
|
cd "${FINAL_DEPLOY_SRC}/";
|
|
tar -zxvf "./${BUILD_TARGET}.tar.gz";
|
|
DEPLOY_INIT_SCRIPTS_2
|
|
|
|
done
|
|
|
|
echo "done."
|
|
echo ""
|
|
|
|
echo "-[configure & reactive service]-"
|
|
i=0
|
|
for releaseLocation in ${RELEASE_ENV_LOCATION[*]};
|
|
do
|
|
# sleep for online lvs reconnect nginx
|
|
if [ "$i" != "0" ]; then
|
|
echo "sleep 120 seconds for online lvs reconnect nginx.";
|
|
sleep 120;
|
|
fi
|
|
# deploy start
|
|
echo -e "\033[34mconfigure ${releaseLocation} \033[0m"
|
|
ssh -T $releaseLocation <<DEPLOY_SCRIPTS_1
|
|
# exec reactive scripts
|
|
chmod +x "${FINAL_DEPLOY_SRC}/scripts/ci/reactive_service.sh";
|
|
bash "${FINAL_DEPLOY_SRC}/scripts/ci/reactive_service.sh" ${REPO_SRC} ${BUILD_TARGET} ${DEPLOY_SRC} ${DEPLOY_LOG_SRC} ${RELEASE_ENV};
|
|
DEPLOY_SCRIPTS_1
|
|
|
|
#check deploy result
|
|
isDeploySuccess=""
|
|
isDeploySuccess=`ssh -T $releaseLocation <<DEPLOY_SCRIPTS_2
|
|
cat $DEPLOY_SUCCESS_SIGNAL_FILE
|
|
DEPLOY_SCRIPTS_2`
|
|
if [ "${isDeploySuccess}" != "success" ]; then
|
|
echo -e "\033[43m[ERROR!] deploy failed detected. Failed at : ${releaseLocation}\033[0m";
|
|
echo "please check release log at : ${releaseLocation}${DEPLOY_LOG}";
|
|
echo "script exit.";
|
|
exit 1;
|
|
else
|
|
echo -e "\033[32m[OK] deploy success. deploy at : ${releaseLocation}\033[0m";
|
|
fi
|
|
# counter
|
|
i=$(expr "$i" + "1");
|
|
done
|
|
|
|
echo "done.";
|
|
|
|
exit 0;
|
|
|
|
|
|
|
|
|