#!/bin/bash
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
clear
printf "
# Name: Downtown_Shell_Deploy_Script
#
# Purpose:Deploy tar.gz to the host
#
# Notes:
# 1 Upload your tar.gz file to the path /data/source
#
# 2 ./script_name Filename.postfix
# For Example:./New_UPDATE_nodejs_tar_update.sh buffetmealapi.tar.gz
#
# Return codes:
# 启动时间 启动程序
# 14:26 nodejs
########################################################
"
#check user
if [ $(id -u) != "0" ]
then
echo "Error: You must be root to run this script!!!"
exit 1
fi
if [ -z "$1" ];then
echo "Usage:script.sh file.tar.gz"
exit
else
echo "执行文件名: $1"
fi
#Define
Project_Name_file=$1 #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 2
#停止shell应用
ps -ef | grep /data/app/eshine/$Project_Name | grep -v grep | awk '{ print "kill -9 " $2}' | bash
sleep 3
#创建备份目录
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/scripts/restart-${Project_Name}_guard.sh"
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 "==============================="
#启动app
chmod 755 /data/app/eshine/$Project_Name/$Project_Name
chmod 755 /data/app/eshine/$Project_Name/*.py
cd /data/app/eshine/$Project_Name
pwd
ps -ef | grep /data/app/eshine/$Project_Name | grep -v grep | awk '{ print "kill -9 " $2}' | bash
sleep 15
nohup /data/app/eshine/$Project_Name/$Project_Name >>/data/logs/$Project_Name.log 2>&1 &
}
#######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
echo ======计划任务========
crontab_add(){
#根据系统定义crontab路径
SYSTEM_OS=`lsb_release -a |grep Description |awk -F : '{print $2}' |sed 's/^[ \t]*//g'`
echo ""
echo "=== SYSTEM_OS is $SYSTEM_OS===="
echo ""
if [[ $SYSTEM_OS =~ Ubuntu ]];then
CRONTAB_FILE_FOR_ROOT="/var/spool/cron/crontabs/root"
echo "====Crontab Path is $CRONTAB_FILE_FOR_ROOT"
else
CRONTAB_FILE_FOR_ROOT="/var/spool/cron/root"
echo "====Crontab Path is $CRONTAB_FILE_FOR_ROOT"
fi
#添加新cron
cd /data/app/eshine/$Project_Name
if [ -f crontab/time.txt ];then
sed -i '/python/d' $CRONTAB_FILE_FOR_ROOT
sed -i /$Project_Name/d $CRONTAB_FILE_FOR_ROOT
cat ./crontab/time.txt |egrep -v "^#|^$" >> $CRONTAB_FILE_FOR_ROOT
sed -i '/python/ s/python/cd \/data\/app\/eshine\/MenuRank; python/' $CRONTAB_FILE_FOR_ROOT
fi
#立即运行的任务
if [ -f crontab/once.txt ];then
chmod 755 crontab/once.txt
./crontab/once.txt
fi
#查看定时任务状态
echo "################ 系统计划任务列表Start ################"
cat $CRONTAB_FILE_FOR_ROOT
echo "################ 系统计划任务列表End ################"
}
Crontab_dir=/data/app/eshine/$Project_Name/crontab
if [ ! -d "$Crontab_dir" ]; then
echo "#######没有crontab目录#######"
else
main(){
crontab_add
}
main
fi
exit 0
有疑问加站长微信联系(非本文作者)