#!/bin/bash
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
clear
printf "
# Name: Downtown_Nodejs_Deploy_Script
#
# Purpose:Deploy tar.gz to the host
#
# Notes:
# Upload your tar.gz file to the path /data/source
#
# ./script_name Filename.postfix
# For Example:./New_UPDATE_nodejs_tar_update.sh buffetmealapi.tar.gz
#
# Return codes:
# 启动时间 启动程序
# : nodejs
########################################################
"
#避免出现TERM environment variable not set.
export TERM=linux
#check user
if [ $(id -u) != "" ]
then
echo "Error: You must be root to run this script!!!"
exit
fi
if [ -z "$1" ];then
echo "Usage:script.sh file.tar.gz"
exit
else
echo "执行文件名: $1"
fi
#Define
Project_Name_file=$ #buffetmealapi.tar.gz
Project_Name=${Project_Name_file%%.*} #buffetmealapi
INIT_SCRIPT_URL=/data/source
Config_URL=/data/config
Project_Name_Suffix=${Project_Name_file#*.} #tar.gz
Project_Root_Url=/data/app/openresty/nginx/conf/nginx.conf.d/vhost
Host_Domain=`hostname |cut -d. -f2-`
DNS_RESOLVER="192.168.20.20"
##工程目录
PROJECT_NODE_APP_PATH="/data/app/openresty/nginx/conf/nginx.conf.d/vhost/$Project_Name"
##源配置文件模板目录
PROJECT_NODE_APP_CONFIG_TEMPLATES_PATH="/data/app/openresty/nginx/conf/nginx.conf.d/vhost/$Project_Name/config-templates" #/data/app/eshine/storeapi/config-templates/
##目标配置文件目录
PROJECT_NODE_APP_CONFD_PATH="/data/app/confd/config/$Project_Name" #/data/app/confd/config/OSS
function_deploy_tar_gz()
{
source /etc/profile
echo -e "\033[31m Make Sure REMOVE THIS HOST FROM LVS---LVS---LVS,5 seconds to think? press ctrl+C to cancel \033[0m"
sleep
#创建备份目录
mkdir -p /data/backup/$(date +%Y%m%d%H%M)
#更新前备份
# if [ -d "$Project_Root_Url/$Project_Name" ];then
# mv $Project_Root_Url/$Project_Name /data/backup/$(date +%Y%m%d%H%M)/
# mv $Project_Root_Url/$Project_Name.conf /data/backup/$(date +%Y%m%d%H%M)/
# echo "###############################################################"
# echo -e "# $Project_Name backup complated! #"
# echo "###############################################################"
# else
# echo "###############################################################"
# echo -e "# Warning : $Project_Name does not exist! #"
# echo "###############################################################"
# fi
#解压新tar包
tar -zxf $INIT_SCRIPT_URL/$Project_Name_file -C $Project_Root_Url
#复制网站模板文件为配置文件
cd $Project_Root_Url
rm -rf ProjectModel.conf
wget http://downtown-weixin-wordpress.oss-cn-hangzhou.aliyuncs.com/downtown-source/common_files/ProjectModel.conf
cp -fr ProjectModel.conf $Project_Name.conf
#此部分用来定义工程中存在userconf/cache.conf配置文件的设置
if [ -f $Project_Name/config/nginx_customize_conf/cache.conf ];then
sed -i '/#####/,/####@/s/^/#@/g' $Project_Name.conf
sed -i '/####@/a\include \/data\/app\/openresty\/nginx\/conf\/nginx.conf.d\/vhost\/project\/config\/nginx_customize_conf\/cache.conf;' $Project_Name.conf
fi
#添加log日志参数,表示空格
sed -i '/####@/a\access_log \/data\/logs\/nginx\/project_access.log json ;' $Project_Name.conf
mkdir -p /data/logs/nginx
#差异化,主要用来临时解决i.eshine.cn域名问题
#正式环境downtown8.com;项目名称为i和www
if [ "$Host_Domain" = "downtown8.com" ];then
if [ "$Project_Name" = "i" -o "$Project_Name" = "www" -o "$Project_Name" = "ProLineFrontEnd" -o "$Project_Name" = "ProLineWeChat" -o "$Project_Name" = "eshopInventory" -o "$Project_Name" = "Dataviz" ];then
sed -i '1,$s/project.domain.com/'$Project_Name.eshine.cn'/g' $Project_Name.conf
sed -i '1,$s/project/'$Project_Name'/g' $Project_Name.conf
else
sed -i '1,$s/project.domain.com/'$Project_Name.$Host_Domain'/g' $Project_Name.conf
sed -i '1,$s/project/'$Project_Name'/g' $Project_Name.conf
fi
else
#通用匹配替换规则
sed -i '1,$s/project.domain.com/'$Project_Name.$Host_Domain'/g' $Project_Name.conf
sed -i '1,$s/project/'$Project_Name'/g' $Project_Name.conf
fi
#为保持对原有wechatmanage项目的兼容性,wechatmanage项目也转入wp
if [ "$Project_Name" = "wp" ];then
sed -i '/wp\./a server_name wechatmanage.'$Host_Domain';' wp.conf
fi
#替换项目配置文件
#rm -fr $Project_Name/config
#更新配置文件并启用confd监测
#先结束掉该项目的confd进程
ps -ef | grep confd/config/$Project_Name | grep -v grep | awk '{ print "kill -9 " $2}' | bash
echo "==============================="
echo "[confd] for $Project_Name killed"
echo "==============================="
#清理confd项目目录
rm -rf $PROJECT_NODE_APP_CONFD_PATH
mkdir -p $PROJECT_NODE_APP_CONFD_PATH/conf.d
mkdir -p $PROJECT_NODE_APP_CONFD_PATH/templates
ls $PROJECT_NODE_APP_CONFD_PATH
##复制配置文件到指定目录
find $PROJECT_NODE_APP_CONFIG_TEMPLATES_PATH -type f -name "*.*" -exec cp -fr {} $PROJECT_NODE_APP_CONFD_PATH/templates \;
echo ========templates copied=====================
ls $PROJECT_NODE_APP_CONFD_PATH/*/*
echo ========templates copied=====================
##生成模式化文件format_standard.txt
##格式为:文件名 文件路径
##清空format_standard.txt文件
echo "" > $PROJECT_NODE_APP_CONFD_PATH/format_standard.txt
##提取PROJECT_NODE_APP_CONFIG_TEMPLATES_PATH目录内的子目录作为变量,并提出所有文件名
PROJECT_NODE_APP_CONFIG_TEMPLATES_L2_LIST=`cd $PROJECT_NODE_APP_CONFIG_TEMPLATES_PATH ; find $PWD | xargs ls -dl | grep -v "^d" | awk 'BEGIN{FS="/"} {print $(NF-1)"/"$NF}'`
#i代表的是$PROJECT_NODE_APP_CONFIG_TEMPLATES_L2_LIST输出出来的所有文件的相对路径
echo ""
echo ""
echo ========config file need to be generate =====================
echo "${PROJECT_NODE_APP_CONFIG_TEMPLATES_L2_LIST}"
echo ========config file need to be generate =====================
echo ""
echo ""
cd $PROJECT_NODE_APP_CONFD_PATH
printf "
########################################################
# 前端项目如果是用Ionic编译的,如果内部结构为
# 项目名--
# --www
# --config
#必须使用带有Confd_www的ci任务执行
#与普通前端项目的部署差异为 $PROJECT_NODE_APP_PATH/www/$i里加入了一个www目录
########################################################
"
echo "####################Current Path 1#######################"
pwd
echo "####################Current Path 1#######################"
for i in $PROJECT_NODE_APP_CONFIG_TEMPLATES_L2_LIST
do
if [ -f $PROJECT_NODE_APP_PATH/$i ];then
find $PROJECT_NODE_APP_PATH/$i -type f -name "*.*" |awk 'BEGIN{FS="/"} {printf("%30s\t%s\n",$NF,$0)}' >> $PROJECT_NODE_APP_CONFD_PATH/format_standard.txt
fi
done
for i in $PROJECT_NODE_APP_CONFIG_TEMPLATES_L2_LIST
do
if [ -f $PROJECT_NODE_APP_PATH/config/$i ];then
find $PROJECT_NODE_APP_PATH/config/$i -type f -name "*.*" |awk 'BEGIN{FS="/"} {printf("%30s\t%s\n",$NF,$0)}' >> $PROJECT_NODE_APP_CONFD_PATH/format_standard.txt
fi
done
for i in $PROJECT_NODE_APP_CONFIG_TEMPLATES_L2_LIST
do
if [ -f $PROJECT_NODE_APP_PATH/www/$i ];then
find $PROJECT_NODE_APP_PATH/www/$i -type f -name "*.*" |awk 'BEGIN{FS="/"} {printf("%30s\t%s\n",$NF,$0)}' >> $PROJECT_NODE_APP_CONFD_PATH/format_standard.txt
fi
done
##遍历格式化文件并生成模板文件
cd $PROJECT_NODE_APP_CONFD_PATH
echo "####################Current Path 2#######################"
pwd
echo "####################Current Path 2#######################"
cat format_standard.txt |awk 'NR>1' |while read F_CONFIG_NAME F_CONFIG_PATH
do
##生成模板文件
cat << EOF > conf.d/$F_CONFIG_NAME.toml
[template]
#模板文件,基于它进行修改
src = "$F_CONFIG_NAME"
#目标文件地址
dest = "$F_CONFIG_PATH"
#keys是在etcd上订阅消息的前缀
keys = [
"",
]
#更新配置后的命令
reload_cmd = "/data/app/openresty/restart-app.sh reload"
EOF
done
#显示该项目的confd进程数
ps -ef | grep confd/config/$Project_Name | grep -v grep | wc -l
export ETCDCTL_API=2
nohup /data/app/confd/bin/confd -config-file /data/app/confd/confd.config -confdir /data/app/confd/config/$Project_Name/ -username root -password Downtown2013 -basic-auth true > /data/app/confd/config/$Project_Name/confd_$Project_Name.log 2>&1 &
echo "==============================="
echo "[confd] for $Project_Name Started"
echo "==============================="
echo "==============================="
echo "sleep 15 secs"
echo "==============================="
sleep 15
/data/app/openresty/restart-app.sh reload
}
#######Deploy War Done#########
#Decide which script to be run by Project_Name_Suffix
case $Project_Name_Suffix in
tar.gz)
function_deploy_tar_gz
;;
*)
echo ".tar.gz allowed only"
esac
exit 0