shell脚本--自动部署发布脚本新版本

 

这脚本被我写到多少文件中,分成几个脚本文件,相互关联,目前该脚本在生产环境中以开始使用,可以用于多个项目,是基于多节点的基础编写的。

还有一个密码文件,我就不发出来了,这个密码文件是sshpass命令使用的,也可以用密钥,但是我这边选择用了密码文件

  1. 主脚本
  2. 变量赋值脚本
  3. 启动脚本
  4. 项目配置文件
  5. 远端启动脚本
  6. 远端监控启动脚本

新版本新加了:多项目部署功能,zabbix监控的检测,一个项目中有多个独立项目发布功能。 

 

 

主脚本(deploy.sh)

#!/bin/bash

if [ ! -n "$1" ];then
	echo -e "\033[34m ./deploy.sh 项目(user|coupon) 分支(master|dev) mvn(compile|install) \033[0m"
	echo -e "\033[34m 多项目部署方式:\"user coupon\" \033[0m"
	exit
fi

user=$(whoami)
if [ $user != "root" ];then
	echo -e "\033[34m 请以“root”身份运行 \033[0m"
	exit
fi

for pro in $1
do
	source /deploy/conf/deploy.conf
	su qqkj -c "cat /dev/null > /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"目录"
	backups=$(cat /deploy/git/.$pro)
	if [[ "$backups" == "yes" ]];then
        	if [[ $project_compile == "y" ]];then
                	for i in $project_ip
                	do
				for o in $project_jar
				do
        	                	sshpass -f $password scp -r $o $i:"$patp"/"$project_item"."$tama".jar
                	        	if [ $? == 0 ];then
                        	        	echo -e "\033[32m $project_item项目备份成功 \033[0m"
                        		else
                                		echo -e "\033[31m $project_item项目备份失败 \033[0m"
	                                	exit
        	                	fi
				done
	                done
        	fi
	fi
	cat /dev/null > /deploy/git/.$pro
	#if [ ! -d $project_item/ ];then
	rm -rf $project_src
	su qqkj -c "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"目录"
	        su qqkj -c "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 == y ];then
		for i in $project_ip
		do
			for o in $project_jar
			do
				sshpass -f $password scp -r $o $i:$path
		        	if [ $? == 0 ];then
					echo -e "\033[32m jar架包传输成功 \033[0m"
					echo "yes" > /deploy/git/.$pro
		        	else
        			        echo -e "\033[31m jar架包传输失败 \033[0m"
					exit
			        fi
			done
		done
	fi

	if [ $project_compile == y ];then
		s=0
		we=1
		for y in $project_ip
		do
			en=${y#*@}
			echo -e "\033[35m ============================================================================================================ \033[0m"
			echo -e "\033[35m ======================================= 当前节点IP:$en ========================================== \033[0m"
			echo -e "\033[35m ============================================================================================================ \033[0m"
                	f=0
                	t=0
			avatar_nginx_ipd=($pro"_nginx_ipd"$we)
			project_nginx_ipd=$(eval echo '$'"${avatar_nginx_ipd}")
			if [[ $project_nginx_node == "y" ]];then
				s=0
			fi
			we=$(($we+1))
			for n in $project_nodelist
			do
				array2=(${project_item//,/ })
                                for e in ${array2[$f]}
                                do
					echo -e "\033[35m ======================================= 当前项目名:$e ======================================== \033[0m"
                                done
				if [ $n == 1 ];then
					nginxd=0
					if [[ $project_nginx_node == y ]];then
						array1=(${project_nginx_ipd//,/ })
					else
						array1=(${project_ipd//,/ })
					fi
					for u in ${array1[$s]}
					do
						echo -e "\033[34m 当前节点IP:$u \033[0m"
						echo -e "\033[34m 修改nginx配置...... \033[0m"
						sshpass -f $password ssh -t $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"
								nginxd=nginx
							else
								echo -e "\033[31m 重启nginx配置失败 \033[0m"
								exit
							fi
						else
							echo -e "\033[31m 注释nginx配置失败 \033[0m"
							exit
						fi
					done
				fi
				cat /dev/null > /deploy/.news
				cat /dev/null > /deploy/.newe
	 			cat /dev/null > /deploy/.zabbix
				cat /dev/null > /deploy/.zabbix_ip
				sshpass -f $password ssh $y "echo '$e' > $pate"
				f=$(($f+1))
	 			array3=(${project_change_nginx//,/ })
                                for o in ${array3[$t]}
                                do
                                        sshpass -f $password ssh $y "echo '$o' > $patw"
                                done
                                t=$(($t+1))
				sshpass -f $password ssh $y "/sh/.startue.sh"
				sleep 1
				source /deploy/start.sh
				sleep 3
				newe=$(cat /deploy/.start)
				if [ "$newe" == "yes" ];then
					echo -e "\033[32m 部署启动成功 \033[0m"
					if [ $n == 1 ];then
						echo -e "\033[34m 恢复nginx配置...... \033[0m"
						echo -e "\033[32m nginx取消注释中..... \033[0m"
                                                sshpass -f $password ssh -t $project_nginx_ip "sed -i 's/\#server $u\;/server $u\;/' $project_nginx_path"
                                                if [ $? == 0 ];then
                                                        echo -e "\033[32m nginx取消注释成功\033[0m"
							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"
                                                        else
                                                                echo -e "\033[31m 重启nginx配置失败 \033[0m"
                                                                exit
                                                        fi
                                                else
                                                        echo -e "\033[31m 注释nginx配置失败 \033[0m"
                                                        exit
                                                fi
					fi
				elif [ "$newe" == "no" ];then
					echo -e "\033[31m 部署启动失败 \033[0m"
					exit
				else
					echo -e "\033[31m start.sh脚本文件出错,检测不到正确的返回值 \033[0m"
					exit
				fi
			done
			s=$(($s+1))
		done
	fi
done
chown qqkj:qqkj -R /deploy/

 

 

 

变量赋值脚本(oa.sh)

#!/bin/bash

#source /deploy/conf/"$pro".conf

project=$(ls /deploy/conf/ | grep .conf | sed -n "$pa"p | cut -d '.' -f 1,1)
for i in $project
do
        if [ $i == $pro ];then
		source /deploy/conf/"$pro".conf
		echo "y" > /deploy/.oa
		break
	else
		echo "n" > /deploy/.oa
        fi
done
avatar_ip=($pro"_ip")
avatar_git=($pro"_git")
avatar_port=($pro"_port")
avatar_jar=($pro"_jar")
avatar_src=($pro"_src")
avatar_node=($pro"_node")
avatar_item=($pro"_item")
avatar_nginx=($pro"_nginx")
avatar_nginx_path=($pro"_nginx_path")
avatar_nginx_conf=($pro"_nginx_conf")
avatar_nginx_ip=($pro"_nginx_ip")
avatar_compile=($pro"_compile")
avatar_change_nginx=($pro"_change_nginx")
avatar_ipd=($pro"_ipd")
avatar_node=($pro"_node")
avatar_nodelist=($pro"_nodelist")
avatar_nginx_ipd1=($pro"_nginx_ipd1")
avatar_nginx_ipd2=($pro"_nginx_ipd2")
avatar_nginx_node=($pro"_nginx_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}")
project_nodelist=$(eval echo '$'"${avatar_nodelist}")
project_nginx_ipd1=$(eval echo '$'"${avatar_nginx_ipd1}")
project_nginx_ipd2=$(eval echo '$'"${avatar_nginx_ipd2}")
project_nginx_node=$(eval echo '$'"${avatar_nginx_node}")

 

 

启动脚本(start.sh)

#!/bin/bash

state=$(cat /deploy/.news)
w=0
while [ $w -le 230 ]
do
	state=$(cat /deploy/.news)
	zabbix=$(cat /deploy/.zabbix)
	ip=$(cat /deploy/.zabbix_ip)
	if [ $state == $teme ]
        then
        	echo -e "\033[32m 项目启动成功 \033[0m"
                su qqkj -c "echo 'yes' > /deploy/.start"
                break
        elif [ $state == $teme-1 ]
        then
                echo -e "\033[31m 运行脚本,没有发现项目存活,尝试启动项目 \033[0m"
                echo -e "\033[31m 项目启动失败 \033[0m"
		su qqkj -c "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"
		su qqkj -c "echo 'no' > /deploy/.start"
                exit
        elif [ $state == $teme-5 ]
        then
                echo -e "\033[31m 在规定的时间内,项目强制关闭失败 \033[0m"
		su qqkj -c "echo 'no' > /deploy/.start"
        	exit
	elif [ $state == $teme-6 ]
        then
                echo -e "\033[31m 项目PID多个,请检查 \033[0m"
                su qqkj -c "echo 'no' > /deploy/.start"
                exit
	elif [ $zabbix == 1 ]
	then
		ssh root@192.168.1.66 "ansible $ip -m shell -a 'bash /sh/.zabbix.sh'"
		sleep 5
		su qqkj -c "echo ' ' > /deploy/.zabbix"
		continue
	elif [ $w == 229 ]
        then
                echo -e "\033[31m 超过四分钟,接收不到返回值,脚本出错 \033[0m"
		su qqkj -c "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
patp=/qinqukeji/microservice/stg_app/backups
teme=$(date +%Y%m%d)
tama=$(date "+%Y-%m-%d.%H:%M:%S")
pate=/sh/.news
pats=/sh/.port
patw=/sh/.porw
step=10

 

 

项目配置文件(xxljob.conf)

#!/bin/bash

xxljob_git="https://gitee.com/piano_interest_91/xxl-job.git"
xxljob_ip="qqkj@172.30.30.3 qqkj@172.30.30.5"
xxljob_port="8080"
xxljob_compile=y
xxljob_node=2
xxljob_src="/deploy/git/xxl-job/"
xxljob_jar="/deploy/git/xxl-job/xxl-job-admin/target/*.jar /deploy/git/xxl-job/xxl-job-executor-samples/xxl-job-executor-sample-springboot/target/*.jar"
xxljob_item="xxl-job-admin,xxl-job-executor-sample-springboot"
xxljob_change_nginx="y,n"
xxljob_nginx_ip=qqkj@172.30.30.1
xxljob_ipd="172.30.30.3:8080,172.30.30.5:8080"
xxljob_nginx_path=/etc/nginx/conf.d/stg-erp.91qinqu.com.conf
xxljob_nginx_conf=stg-erp.91qinqu.com.conf
xxljob_nodelist="1 0"
xxljob_nginx_ipd1="172.30.30.3:8080"
xxljob_nginx_ipd2="172.30.30.5:8080"
xxljob_nginx_node=y

 

 

 

远端启动脚本(.startue.sh)

#!/bin/bash

item=$(cat /sh/.news)
itwm="JVM running for"
itam=$(cat /sh/.port)
project=$(ps -ef | grep "$item".jar | grep -v grep | awk '{print $2}')
projectss=$(ps -ef | grep "$item".jar | grep -v grep | awk '{print $2}')
gain_ip=$( ip addr | grep inet | grep -v 127.0.0.1 | grep -v inet6 | awk '{print $2}' | tr -d "addr:" )
ipd=$(echo ${gain_ip: 10})
ip=$(echo ${ipd%/*})
ssh=qqkj@192.168.0.2
password=/sh/.password
teme=$(date +%Y%m%d)
pate=/deploy/.news
zabbix=/deploy/.zabbix
zabbix_ip=/deploy/.zabbix_ip
nginx_ip=192.168.0.8
path=backups/
project_nginx=$(cat /sh/.porw)
echo '' > /sh/.zabbix

cd /qinqukeji/microservice/stg_app/
quantity=$(ls "$path" | wc -l)
while [ $quantity -gt 10 ]
do
        que=$(ls "$path" | sed -n 1p)
        rm -rf "$path""$que"
        quantity=$(ls "$path" | wc -l)
done

if [ -n "$project" ];then
	echo -e "\033[32m 重启 \033[0m"
	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".jar | grep -v grep | awk '{print $2}')
	echo -e "\033[32m PID:$project \033[0m"
	kill $project
	o=0
	while [ $o -le 30 ]
	do
		project1=$(ps -ef | grep "$item".jar | 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 -100 /qinqukeji/microservice/stg_app/log/$item*.log | grep "$itwm"
				if [ $? == 0 ]
				then
					netstat -anlp | grep 10050 > /dev/null
					if [ $? == 0 ];then
						echo -e "\033[32m 监控正在运行中 \033[0m"
					else
						echo -e "\033[32m 正在启动监控服务,请等待... \033[0m"
						sshpass -f $password ssh $ssh "echo '1' > $zabbix"
						sshpass -f $password ssh $ssh "echo $ip > $zabbix_ip"
						sleep 3
						g=0
						while [ $g -le 31 ]
						do
							netstat -anlp | grep 10050 > /dev/null
							zb=$(cat /sh/.zabbix)
							if [ $? == 0 ];then
								echo -e "\033[32m 监控服务启动成功 \033[0m"
								break
							elif [ $zb == yes ];then
								echo -e "\033[32m 监控服务启动成功,监测不到端口。 \033[0m"
								break
							elif [ $zb == no ];then
								echo -e "\033[31m 监控服务启动失败 \033[0m"
								break
							elif [ $g == 30 ];then
								echo -e "\033[31m 监控服务启动失败 \033[0m"
								break
							fi
							sleep 1
							g=$(($g+1))
						done
					fi
					echo -e "\033[32m 项目以启动成功 \033[0m"
					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 -e "\033[31m 启动超时 \033[0m"
					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 -e "\033[32m PID:$project \033[0m "
	kill -9 $project
	p=0
	while [ $p -le 30 ]
        do
                project1=$(ps -ef | grep "$item".jar | 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 -100 /qinqukeji/microservice/stg_app/log/$item*.log | grep "$itwm"
                                if [ $? == 0 ]
                                then
					netstat -anlp | grep 10050 > /dev/null
                                        if [ $? == 0 ];then
                                                echo -e "\033[32m 监控正在运行中 \033[0m"
                                        else
                                                echo -e "\033[32m 正在启动监控服务,请等待... \033[0m"
                                                sshpass -f $password ssh $ssh "echo '1' > $zabbix"
                                                sshpass -f $password ssh $ssh "echo $ip > $zabbix_ip"
                                                sleep 3
                                                g=0
                                                while [ $g -le 31 ]
                                                do
                                                        netstat -anlp | grep 10050 > /dev/null
                                                        zb=$(cat /sh/.zabbix)
                                                        if [ $? == 0 ];then
                                                                echo -e "\033[32m 监控服务启动成功 \033[0m"
                                                                break
                                                        elif [ $zb == yes ];then
                                                                echo -e "\033[32m 监控服务启动成功,监测不到端口。 \033[0m"
                                                                break
                                                        elif [ $zb == no ];then
                                                                echo -e "\033[31m 监控服务启动失败 \033[0m"
                                                                break
                                                        elif [ $g == 30 ];then
                                                                echo -e "\033[31m 监控服务启动失败 \033[0m"
                                                                break
                                                        fi
                                                        sleep 1
                                                        g=$(($g+1))
                                                done
                                        fi
                                        echo -e "\033[32m 项目以启动成功 \033[0m"
                                        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 -e "\033[31m 启动超时 \033[0m"
                                        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 -e "\033[32m 启动 \033[0m"
	cd /qinqukeji/microservice/stg_app/
	/qinqukeji/microservice/stg_app/springBoot.sh start $item*.jar
        sleep 10
        q=0
        while [ $q -le 6 ]
        do
	        tail -100 /qinqukeji/microservice/stg_app/log/$item*.log | grep "$itwm"
                if [ $? == 0 ]
                then
			netstat -anlp | grep 10050 > /dev/null
                        if [ $? == 0 ];then
	                        echo -e "\033[32m 监控正在运行中 \033[0m"
                        else
                                sshpass -f $password ssh $ssh "echo '1' > $zabbix"
                                sshpass -f $password ssh $ssh "echo $ip > $zabbix_ip"
				echo -e "\033[32m 正在启动监控服务,请等待... \033[0m"
                                sleep 3
                                g=0
                                while [ $g -le 31 ]
                                do
	                                netstat -anlp | grep 10050 > /dev/null
                                        zb=$(cat /sh/.zabbix)
                                        if [ $? == 0 ];then
 	                                       echo -e "\033[32m 监控服务启动成功 \033[0m"
                                               break
                                        elif [ $zb == yes ];then
                                               echo -e "\033[32m 监控服务启动成功,监测不到端口。 \033[0m"
                                               break
                                        elif [ $zb == no ];then
                                               echo -e "\033[31m 监控服务启动失败 \033[0m"
                                               break
                                        elif [ $g == 30 ];then
                                               echo -e "\033[31m 监控服务启动失败 \033[0m"
                                               break
                                        fi
                                        sleep 1
                                        g=$(($g+1))
                                done
                        fi
  	              	echo -e "\033[32m 项目以启动成功 \033[0m"
                	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 -e "\033[31m 启动超时 \033[0m"
                      	exit
                fi
                sleep 10
                q=$(($q+1))
        done
fi

 

 

 

远端监控启动脚本(.startue.sh)

#!/bin/bash

netstat -anlp | grep 10050 > /dev/null
if [ $? == 0 ];then
	echo "监控以启动"
	echo 'yes' > /sh/.zabbix
else
        if [ ! -d "/var/run/zabbix" ];then
        	mkdir /var/run/zabbix > /dev/null
        fi
        chmod zabbix:zabbix -R /var/run/zabbix/ > /dev/null
        sleep 1
        /usr/sbin/zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf > /dev/null
        sleep 1
        netstat -anlp | grep 10050 > /dev/null
        if [ $? == 0 ];then
        	echo "监控以启动"
		echo 'yes' > /sh/.zabbix
        else
                echo "监控未启动"
		echo 'no' > /sh/.zabbix
        fi
fi

 

为什么新版本要这么转圈子,因为我们的项目都是有一个专门的用户控制的,zabbix是root控制的,也因为现在机器多,也懒得改监控服务,就把脚本写了多点

这脚本肯定还有很多不足之处,欢迎大家前来指点指点

 

 

 

转载请注明原文链接:shell脚本--自动部署发布脚本新版本

发表评论:

共有 0 条评论

 Top