项目发布脚本-nodejs

#!/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
########################################################
" #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_NODE_APP_PATH="/data/app/eshine/$Project_Name"
##源配置文件模板目录
PROJECT_NODE_APP_CONFIG_TEMPLATES_PATH="/data/app/eshine/$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/storeapi 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
#停止nodejs应用
PID=`pm2 list |grep $Project_Name |awk '{print $10}'`
if [ "$PID" == "online" ];then
pm2 stop $Project_Name
pm2 delete $Project_Name
fi
sleep
#创建备份目录
mkdir -p /data/backup/$(date +%Y%m%d%H%M)
#创建app工作目录
mkdir -p /data/app/eshine
#更新前备份
# if [ -d "/data/app/eshine/$Project_Name" ];then
# mv /data/app/eshine/$Project_Name /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
#解压新app包
cd /data/source
tar -zxf $Project_Name_file -C ./
#部署应用
rm -rf /data/app/eshine/$Project_Name
mv $Project_Name /data/app/eshine/ #更新配置文件并启用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 ========config file need to be generate =====================
echo "${PROJECT_NODE_APP_CONFIG_TEMPLATES_L2_LIST}"
echo ========config file need to be generate ===================== cd $PROJECT_NODE_APP_CONFD_PATH 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 echo "################# format_standard.txt############"
cat $PROJECT_NODE_APP_CONFD_PATH/format_standard.txt
echo "################# format_standard.txt############" ##遍历格式化文件并生成模板文件
cd $PROJECT_NODE_APP_CONFD_PATH
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/node/bin/pm2 restart $Project_Name"
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 "===============================" #创建日志目录
mkdir -p /data/logs/pm2/$Project_Name #启动app
cd /data/app/eshine/$Project_Name echo "==============================="
echo "sleep 15 secs"
echo "===============================" sleep 15
if [ -f "pm2.json" ];then
pm2 start pm2.json
echo "=== pm2 start pm2.json ==="
else
pm2 start server/server.js --log /data/logs/pm2/$Project_Name/$Project_Name.log --name="$Project_Name"
echo "=== pm2 start server/server.js ==="
fi
} #######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
上一篇:项目发布脚本-go


下一篇:Jenkins+GitHub 项目环境搭建和发布脚本(二)