这脚本被我写到多少文件中,分成几个脚本文件,相互关联,目前该脚本在生产环境中以开始使用,可以用于多个项目,是基于多节点的基础编写的。
还有一个密码文件,我就不发出来了,这个密码文件是sshpass命令使用的,也可以用密钥,但是我这边选择用了密码文件
- 主脚本
- 变量赋值脚本
- 启动脚本
- 项目配置文件
- 远端启动脚本
主脚本(deploy.sh)
#!/bin/bash
if [ ! -n "$1" ];then
echo -e "\033[34m ./deploy.sh 项目(user|coupon) 分支(master|dev) mvn(compile|install) \033[0m"
exit
fi
source /deploy/conf/deploy.conf
echo ' ' > /deploy/.oa
source /deploy/oa.sh
sleep 1
newq=$(cat /deploy/.oa)
if [ $newq == y ];then
echo -e "\033[32m 已获取到项目名称:$project_item \033[0m"
elif [ $newq == n ];then
echo -e "\033[31m 项目名不对 \033[0m"
exit
else
echo -e "\033[31m 脚本出错 \033[0m"
exit
fi
echo "当前的分支名:$2"
cd /deploy/git/
echo "跳转到"$PWD"目录"
#if [ ! -d $project_item/ ];then
rm -rf $project_src
git clone -b $2 $project_git
#else
# cd $project_src
# git fetch $project_git $2
#fi
if [ $? == 0 ];then
echo -e "\033[32m $project_item项目拉取成功 \033[0m"
cd $project_src
echo "跳转到"$PWD"目录"
mvn clean $3 -Dmaven.test.skip=true
if [ $? == 0 ];then
echo -e "\033[32m $project_item项目mvn成功 \033[0m"
else
echo -e "\033[31m $project_item项目mvn失败 \033[0m"
exit
fi
else
echo -e "\033[31m $project_item项目拉取失败 \033[0m"
exit
fi
if [ $project_compile == n ];then
exit
fi
if [ $3 == install ];then
for i in $project_ip
do
sshpass -f $password scp -r $project_jar $i:$path
done
if [ $? == 0 ];then
echo -e "\033[32m jar架包传输成功 \033[0m"
else
echo -e "\033[31m jar架包传输失败 \033[0m"
exit
fi
fi
if [ $3 == install ];then
if [ $project_change_nginx == n ];then
s=0
for y in $project_ip
do
array1=(${project_ipd//,/ })
for u in ${array[s]}
do
echo -e "\033[34m 当前节点IP:$u \033[0m"
break
done
s=$(($s+1))
echo ' ' > /deploy/.news
echo ' ' > /deploy/.newe
sshpass -f $password ssh $y "echo '$project_item' > $pate"
sshpass -f $password ssh $y "echo '$project_port' > $pats"
sshpass -f $password ssh $y "echo '$project_change_nginx' > $patw"
sshpass -f $password ssh $y "/sh/.startue.sh"
source /deploy/start.sh
sleep 3
newe=$(cat /deploy/.start)
if [ "$newe" == "yes" ];then
echo -e "\033[32m 启动成功 \033[0m"
if [ $s == $project_node ];then
exit
fi
elif [ "$newe" == "no" ];then
echo -e "\033[31m 启动失败 \033[0m"
exit
else
echo -e "\033[31m start.sh脚本文件出错,检测不到正确的返回值 \033[0m"
exit
fi
done
elif [ $project_change_nginx == y ];then
s=0
for y in $project_ip
do
array1=(${project_ipd//,/ })
echo $array1
for u in ${array1[s]}
do
echo -e "\033[34m 当前节点IP:$u \033[0m"
done
s=$(($s+1))
sshpass -f $password ssh $project_nginx_ip "sed -i 's/server $u\;/\#server $u\;/' $project_nginx_path"
if [ $? == 0 ];then
echo -e "\033[32m 注释nginx配置成功 \033[0m"
sshpass -f $password ssh -t $project_nginx_ip "sudo nginx -t"
sleep 1
sshpass -f $password ssh -t $project_nginx_ip "sudo nginx -s reload"
if [ $? == 0 ];then
echo -e "\033[32m 重启nginx配置成功 \033[0m"
sshpass -f $password ssh $y "echo '$project_item' > $pate"
sshpass -f $password ssh $y "echo '$project_port' > $pats"
sshpass -f $password ssh $y "echo '$project_change_nginx' > $patw"
echo ' ' > /deploy/.news
echo ' ' > /deploy/.newe
sshpass -f $password ssh $y "/sh/.startue.sh"
source /deploy/start.sh
sleep 3
newe=$(cat /deploy/.start)
if [ "$newe" == "yes" ];then
echo -e "\033[32m 启动以成功,恢复nginx配置 \033[0m"
sshpass -f $password ssh $project_nginx_ip "sed -i 's/\#server $u\;/server $u\;/' $project_nginx_path"
if [ $? == 0 ];then
echo -e "\033[32m 注释nginx配置成功 \033[0m"
sshpass -f $password ssh -t $project_nginx_ip "sudo nginx -t"
sleep 1
sshpass -f $password ssh -t $project_nginx_ip "sudo nginx -s reload"
#exit
else
echo -e "\033[31m 注释nginx配置出错 \033[0m"
exit
fi
elif [ "$newe" == "no" ];then
echo -e "\033[31m 启动失败 \033[0m"
exit
else
echo -e "\033[31m start.sh脚本文件出错,检测不到正确的返回值 \033[0m"
exit
fi
else
echo -e "\033[31m 重启nginx配置出错 \033[0m"
exit
fi
else
echo -e "\033[31m 注释nginx配置出错 \033[0m"
exit
fi
done
fi
fi
变量赋值脚本(oa.sh)
#!/bin/bash
source /deploy/conf/"$1".conf
project=$(ls /deploy/conf/ | grep .conf | sed -n "$pa"p | cut -d '.' -f 1,1)
for i in $project
do
if [ $i == $1 ];then
echo "y" > /deploy/.oa
break
else
echo "n" > /deploy/.oa
fi
done
avatar_ip=($1"_ip")
avatar_git=($1"_git")
avatar_port=($1"_port")
avatar_jar=($1"_jar")
avatar_src=($1"_src")
avatar_node=($1"_node")
avatar_item=($1"_item")
avatar_nginx=($1"_nginx")
avatar_nginx_path=($1"_nginx_path")
avatar_nginx_conf=($1"_nginx_conf")
avatar_nginx_ip=($1"_nginx_ip")
avatar_compile=($1"_compile")
avatar_change_nginx=($1"_change_nginx")
avatar_ipd=($1"_ipd")
avatar_node=($1"_node")
project_ip=$(eval echo '$'"${avatar_ip}")
project_git=$(eval echo '$'"${avatar_git}")
project_port=$(eval echo '$'"${avatar_port}")
project_jar=$(eval echo '$'"${avatar_jar}")
project_src=$(eval echo '$'"${avatar_src}")
project_node=$(eval echo '$'"${avatar_node}")
project_item=$(eval echo '$'"${avatar_item}")
project_nginx=$(eval echo '$'"${avatar_nginx}")
project_nginx_path=$(eval echo '$'"${avatar_nginx_path}")
project_nginx_conf=$(eval echo '$'"${avatar_nginx_conf}")
project_nginx_ip=$(eval echo '$'"${avatar_nginx_ip}")
project_compile=$(eval echo '$'"${avatar_compile}")
project_change_nginx=$(eval echo '$'"${avatar_change_nginx}")
project_ipd=$(eval echo '$'"${avatar_ipd}")
project_node=$(eval echo '$'"${avatar_node}")
启动脚本(start.sh)
#!/bin/bash
state=$(cat /deploy/.news)
w=0
while [ $w -le 230 ]
do
state=$(cat /deploy/.news)
if [ $state == $teme ]
then
echo -e "\033[32m 项目启动成功 \033[0m"
echo "yes" > /deploy/.start
break
elif [ $state == $teme-1 ]
then
echo -e "\033[31m 运行脚本,没有发现项目存活,尝试启动项目 \033[0m"
echo -e "\033[31m 项目启动失败 \033[0m"
echo "no" > /deploy/.start
exit
elif [ $state == $teme-2 ]
then
echo -e "\033[31m 在规定的时间内,项目端口依然存在 \033[0m"
echo -e "\033[35m 进入关闭项目序列 \033[0m"
sleep 5
continue
elif [ $state == $teme-3 ]
then
echo -e "\033[31m 在规定的时间内,项目没有成功关闭 \033[31m"
echo -e "\033[35m 进入强制关闭项目序列 \033[0m"
sleep 5
continue
elif [ $state == $teme-4 ]
then
echo -e "\033[31m 项目启动出错,没有检测到日志端口 \033[0m"
echo "no" > /deploy/.start
exit
elif [ $state == $teme-5 ]
then
echo -e "\033[31m 在规定的时间内,项目强制关闭失败 \033[0m"
echo "no" > /deploy/.start
exit
elif [ $state == $teme-6 ]
then
echo -e "\033[31m 项目PID多个,请检查 \033[0m"
echo "no" > /deploy/.start
exit
elif [ $w == 229 ]
then
echo -e "\033[31m 超过四分钟,接收不到返回值,脚本出错 \033[0m"
echo "no" > /deploy/.start
exit
fi
sleep 1
w=$(($w+1))
done
项目配置文件
项目配置文件分为全局配置文件跟项目配置文件二种
全局配置文件只有一个文件
项目配置文件按不同项目拥有不同的项目配置文件
下面就只发全局跟项目各一个
全局配置文件(deploy.conf)
#!/bin/bash
############################### 全局变量 ##############################################
path=/qinqukeji/microservice/stg_app/
PWD=$(pwd)
password=/deploy/.password
teme=$(date +%Y%m%d)
tama=$(date "+%Y/%m/%d(%H:%M:%S)")
pate=/sh/.news
pats=/sh/.port
patw=/sh/.porw
step=10
项目配置文件(gateway.conf)
#!/bin/bash
gateway_git="git@192.168.1.64:qinqu/qinqu-gateway.git"
gateway_ip="qqkj@192.168.0.102 qqkj@192.168.0.103"
gateway_port="2088"
gateway_compile=y
gateway_node=2
gateway_src="/deploy/git/qinqu-gateway/"
gateway_jar="/deploy/git/qinqu-gateway/qinqu-gateway-service/target/*.jar"
gateway_item=qinqu-gateway
gateway_change_nginx=y
gateway_nginx_ip=qqkj@192.168.0.8
gateway_ipd="192.168.0.102:2088,192.168.0.103:2088"
gateway_nginx_path=/usr/local/nginx/conf.d/microservice.qinqukeji.com.conf
gateway_nginx_conf=microservice.qinqukeji.com.conf
远端启动脚本(.startue.sh)
#!/bin/bash
item=$(cat /sh/.news)
itwm="JVM running for"
itam=$(cat /sh/.port)
project=$(ps -ef | grep "$item" | grep -v grep | awk '{print $2}')
projectss=$(ps -ef | grep "$item" | grep -v grep | awk '{print $2}')
ssh=qqkj@192.168.0.2
password=/sh/.password
teme=$(date +%Y%m%d)
pate=/deploy/.news
nginx_ip=192.168.0.8
project_nginx=$(cat /sh/.porw)
if [ -n "$project" ];then
echo "重启"
if [ $project_nginx == y ];then
i=0
while [ $i -le 30 ]
do
netstat -anlp | grep $project | grep "$nginx_ip"
if [ ! $? = 0 ]
then
break
elif [ $i == 29 ]
then
break
sshpass -f $password ssh $ssh "echo '$teme-2' > $pate"
fi
sleep 1
i=$(($i+1))
done
fi
project=$(ps -ef | grep "$item" | grep -v grep | awk '{print $2}')
echo $project
kill $project
o=0
while [ $o -le 30 ]
do
project1=$(ps -ef | grep "$item" | grep -v grep | awk '{print $2}')
if [ -z $project1 ]
then
cd /qinqukeji/microservice/stg_app/
/qinqukeji/microservice/stg_app/springBoot.sh start $item*
sleep 10
w=0
while [ $w -le 6 ]
do
tail /qinqukeji/microservice/stg_app/log/$item*.log | grep "$itwm"
if [ $? == 0 ]
then
echo "项目以启动成功"
sshpass -f $password ssh $ssh "echo '$teme' > $pate"
exit
elif [ $w == 5 ]
then
sshpass -f $password ssh $ssh "echo '$teme-4' > $pate"
tail -40 /qinqukeji/microservice/stg_app/log/$item*.log
echo "启动超时"
exit
fi
sleep 10
w=$(($w+1))
done
elif [ $o == 29 ]
then
break
sshpass -f $password ssh $ssh "echo '$teme-3' > $pate"
fi
sleep 1
o=$(($o+1))
done
project=$(ps -ef | grep "$item" | grep -v grep | awk '{print $2}')
echo $project
kill -9 $project
p=0
while [ $p -le 30 ]
do
project1=$(ps -ef | grep "$item" | grep -v grep | awk '{print $2}')
if [ -z $project1 ]
then
cd /qinqukeji/microservice/stg_app/
/qinqukeji/microservice/stg_app/springBoot.sh start $item*
sleep 10
k=0
while [ $k -le 6 ]
do
tail /qinqukeji/microservice/stg_app/log/$item*.log | grep "$itwm"
if [ $? == 0 ]
then
echo "项目以启动成功"
sshpass -f $password ssh $ssh "echo '$teme' > $pate"
exit
elif [ $k == 5 ]
then
sshpass -f $password ssh $ssh "echo '$teme-4' > $pate"
tail -40 /qinqukeji/microservice/stg_app/log/$item*.log
echo "启动超时"
exit
fi
sleep 10
k=$(($k+1))
done
elif [ $p == 29 ]
then
exit
sshpass -f $password ssh $ssh "echo '$teme-5' > $pate"
fi
sleep 1
p=$(($p+1))
done
else
echo "启动"
cd /qinqukeji/microservice/stg_app/
/qinqukeji/microservice/stg_app/springBoot.sh start $item*.jar
sleep 10
q=0
while [ $q -le 6 ]
do
tail /qinqukeji/microservice/stg_app/log/$item*.log | grep "$itwm"
if [ $? == 0 ]
then
echo "项目以启动成功"
sshpass -f $password ssh $ssh "echo '$teme' > $pate"
exit
elif [ $q == 5 ]
then
sshpass -f $password ssh $ssh "echo '$teme-1' > $pate"
tail -40 /qinqukeji/microservice/stg_app/log/$item*.log
echo "启动超时"
exit
fi
sleep 10
q=$(($q+1))
done
fi
转载请注明原文链接:shell脚本--自动部署发布脚本
共有 0 条评论