|
#!/bin/sh
|
|
# reactive_service.sh
|
|
# This script for reactive this projects.
|
|
# @version 170327:3
|
|
# @author zhangxuhong <zhangxuhong@xitu.io>
|
|
#
|
|
|
|
# [config]
|
|
echo "-[config]-"
|
|
|
|
# ----------------------------[manual config here]------------------------------
|
|
|
|
REPO_SRC=$1
|
|
BUILD_TARGET=$2
|
|
DEPLOY_SRC=$3
|
|
DEPLOY_LOG_SRC=$4
|
|
RELEASE_ENV=$5
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
# 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[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
|
|
echo "env ok";
|
|
elif [ "$RELEASE_ENV" == "beta" ]; then
|
|
echo "env ok";
|
|
elif [ "$RELEASE_ENV" == "online" ]; then
|
|
echo "env ok";
|
|
else
|
|
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 ""
|
|
sleep 1
|
|
|
|
|
|
# configure log
|
|
if [ ! -d "${DEPLOY_LOG_SRC}/${BUILD_TARGET}" ]; then mkdir "${DEPLOY_LOG_SRC}/${BUILD_TARGET}"; fi
|
|
if [ ! -L "${FINAL_DEPLOY_SRC}/logs" ]; then ln -s "${DEPLOY_LOG_SRC}/${BUILD_TARGET}" "${FINAL_DEPLOY_SRC}/logs"; fi
|
|
|
|
# remove old deploy signal
|
|
if [ -f "${DEPLOY_ERROR_SIGNAL_FILE}" ]; then rm -f "${DEPLOY_ERROR_SIGNAL_FILE}"; fi
|
|
if [ -f "${DEPLOY_SUCCESS_SIGNAL_FILE}" ]; then rm -f "${DEPLOY_SUCCESS_SIGNAL_FILE}"; fi
|
|
|
|
# deploy start
|
|
echo "" | tee -a $DEPLOY_LOG;
|
|
echo -e "`date "+%Y-%m-%d %H:%M:%S"` \c" | tee -a $DEPLOY_LOG;
|
|
echo "deploy start" | tee -a $DEPLOY_LOG;
|
|
|
|
# configure service
|
|
if [ ! -L "/etc/systemd/system/${SERVICE_NAME}" ]; then
|
|
cp "${FINAL_DEPLOY_SRC}/config/systemctl/${SERVICE_NAME}" "/etc/systemd/system/${SERVICE_NAME}";
|
|
else
|
|
rm -f "/etc/systemd/system/${SERVICE_NAME}";
|
|
cp "${FINAL_DEPLOY_SRC}/config/systemctl/${SERVICE_NAME}" "/etc/systemd/system/${SERVICE_NAME}";
|
|
fi
|
|
|
|
|
|
# configure nginx
|
|
PUBLIC_NGINX_CONF="${FINAL_DEPLOY_SRC}/config/nginx/${BUILD_TARGET}.public.conf"
|
|
PRIVATE_NGINX_CONF="${FINAL_DEPLOY_SRC}/config/nginx/${BUILD_TARGET}.private.conf"
|
|
|
|
if [ -f "${PUBLIC_NGINX_CONF}" ]; then
|
|
ln -s "${PUBLIC_NGINX_CONF}" $NGINX_CONFIG_LOCATION;
|
|
fi
|
|
if [ -f "${PRIVATE_NGINX_CONF}" ]; then
|
|
ln -s "${PRIVATE_NGINX_CONF}" $NGINX_CONFIG_LOCATION;
|
|
fi
|
|
|
|
# test nginx config
|
|
$NGINX_BIN_LOCATION -t 2> "${FINAL_DEPLOY_SRC}/logs/nginx_test.log";
|
|
isNginxOk=`grep 'test is successful' "${FINAL_DEPLOY_SRC}/logs/nginx_test.log" | wc -l `;
|
|
if [ "$isNginxOk" != "1" ]; then
|
|
touch $DEPLOY_ERROR_SIGNAL_FILE;
|
|
echo -e "`date "+%Y-%m-%d %H:%M:%S"` \c" | tee -a $DEPLOY_LOG;
|
|
echo "[ERROR!] deploy error with nginx config error, nginx config error is : " | tee -a $DEPLOY_LOG;
|
|
cat "${FINAL_DEPLOY_SRC}/logs/nginx_test.log" | tee -a $DEPLOY_LOG;
|
|
exit 1;
|
|
else
|
|
echo -e "`date "+%Y-%m-%d %H:%M:%S"` \c" | tee -a $DEPLOY_LOG;
|
|
echo "nginx config is ok:" | tee -a $DEPLOY_LOG;
|
|
cat "${FINAL_DEPLOY_SRC}/logs/nginx_test.log" | tee -a $DEPLOY_LOG;
|
|
fi
|
|
|
|
# stop nginx
|
|
$NGINX_BIN_LOCATION -s stop
|
|
|
|
# check nginx is stoped
|
|
sleep 20;
|
|
nginxNum=`ps auxw | grep --color 'nginx' | grep -v grep | wc -l`
|
|
if [ "$nginxNum" != "0" ]; then
|
|
touch $DEPLOY_ERROR_SIGNAL_FILE;
|
|
echo -e "`date "+%Y-%m-%d %H:%M:%S"` \c" | tee -a $DEPLOY_LOG;
|
|
echo "[ERROR!] deploy error with nginx stop failed, nginx process num is : ${nginxNum}" | tee -a $DEPLOY_LOG;
|
|
exit 1;
|
|
else
|
|
echo -e "`date "+%Y-%m-%d %H:%M:%S"` \c" | tee -a $DEPLOY_LOG;
|
|
echo "nginx stopped" | tee -a $DEPLOY_LOG;
|
|
fi
|
|
|
|
# restart service
|
|
/usr/bin/systemctl daemon-reload;
|
|
/usr/bin/systemctl enable ${SERVICE_NAME};
|
|
/usr/bin/systemctl stop ${BUILD_TARGET}; # use start-stop for first time deploy
|
|
/usr/bin/systemctl start ${BUILD_TARGET};
|
|
|
|
# check service
|
|
sleep 2;
|
|
/usr/bin/systemctl status "${BUILD_TARGET}" > "${FINAL_DEPLOY_SRC}/logs/service_status.log";
|
|
serviceStatus=`grep "Active: active" "${FINAL_DEPLOY_SRC}/logs/service_status.log" | wc -l`;
|
|
if [ "$serviceStatus" == "0" ]; then
|
|
touch $DEPLOY_ERROR_SIGNAL_FILE;
|
|
echo -e "`date "+%Y-%m-%d %H:%M:%S"` \c" | tee -a $DEPLOY_LOG;
|
|
echo "[ERROR!] deploy error with service start failed, systemctl status :" | tee -a $DEPLOY_LOG;
|
|
cat "${FINAL_DEPLOY_SRC}/logs/service_status.log" | tee -a $DEPLOY_LOG;
|
|
# restart nginx
|
|
$NGINX_BIN_LOCATION;
|
|
# check nginx num
|
|
nginxNum=`ps auxw | grep --color 'nginx' | grep -v grep | wc -l`
|
|
sleep 5;
|
|
if [ "$nginxNum" == "0" ]; then
|
|
touch $DEPLOY_ERROR_SIGNAL_FILE;
|
|
echo -e "`date "+%Y-%m-%d %H:%M:%S"` \c" | tee -a $DEPLOY_LOG;
|
|
echo "[ERROR!] deploy error with nginx start failed, nginx process num is : ${nginxNum}" | tee -a $DEPLOY_LOG;
|
|
exit 1;
|
|
else
|
|
echo -e "`date "+%Y-%m-%d %H:%M:%S"` \c" | tee -a $DEPLOY_LOG;
|
|
echo "nginx started" | tee -a $DEPLOY_LOG;
|
|
fi
|
|
exit 1;
|
|
else
|
|
echo -e "`date "+%Y-%m-%d %H:%M:%S"` \c" | tee -a $DEPLOY_LOG;
|
|
echo "service started" | tee -a $DEPLOY_LOG;
|
|
cat "${FINAL_DEPLOY_SRC}/logs/service_status.log" | tee -a $DEPLOY_LOG;
|
|
fi
|
|
|
|
# start nginx
|
|
$NGINX_BIN_LOCATION
|
|
|
|
# check nginx num
|
|
nginxNum=`ps auxw | grep --color 'nginx' | grep -v grep | wc -l`;
|
|
sleep 5;
|
|
if [ "$nginxNum" == "0" ]; then
|
|
touch $DEPLOY_ERROR_SIGNAL_FILE;
|
|
echo -e "`date "+%Y-%m-%d %H:%M:%S"` \c" | tee -a $DEPLOY_LOG;
|
|
echo "[ERROR!] deploy error with nginx start failed, nginx process num is : ${nginxNum}" | tee -a $DEPLOY_LOG;
|
|
exit 1;
|
|
else
|
|
echo -e "`date "+%Y-%m-%d %H:%M:%S"` \c" | tee -a $DEPLOY_LOG;
|
|
echo "nginx started" | tee -a $DEPLOY_LOG;
|
|
fi
|
|
|
|
# deploy success
|
|
echo "success" > $DEPLOY_SUCCESS_SIGNAL_FILE;
|
|
echo -e "`date "+%Y-%m-%d %H:%M:%S"` \c" | tee -a $DEPLOY_LOG;
|
|
echo "[OK] deploy success." | tee -a $DEPLOY_LOG;
|
|
exit 0;
|