天天看点

shell脚本发布war(tomcat)

1.deploy.sh(部署脚本)

#!/bin/bash

git_branch=dev-log4j
readonly git_branch

project_location=/home/springboot-demo
readonly project_location

war_path=$project_location"/target"
readonly war_path

war_name=springboot-demo.war
readonly war_name

war_dir=springboot-demo
readonly war_dir

tomcat_path=/usr/local/tomcat8
readonly tomcat_path

tomcat_name=tomcat8
readonly tomcat_name

war_bak_path=/home/warBak
readonly war_bak_path

updateCode() {
	echo -e "\e[1;32m"
	echo " ---------- updateCode start ---------- "
	cd $project_location
	git checkout $git_branch
	git pull origin $git_branch
	echo " ---------- updateCode end ------------ "
	echo -e "\e[0m"
}
updateCode

packageWar() {
	echo -e "\e[1;32m --------- packageWar start ----------- \e[0m"
	cd $project_location
	mvn clean package
	echo -e "\e[1;32m --------- packageWar end ----------- \e[0m"
} 
packageWar

checkWarExist() {
	echo -e "\e[1;32m"
	echo " --------- checkWarExist start ----------- "
	cd $war_path
	if [ $(find ./ -maxdepth 1 -name $war_name | wc -l) -ne 1 ]
	then
		echo " war not exist "
		exit 1
	else
		echo " war generate success "
	fi
	echo " --------- checkWarExist end ----------- "
	echo -e "\e[0m"
}
checkWarExist

stopTomcat() {
	echo -e "\e[1;32m"
	echo " --------- stopTomcat start ----------- "
	cd $tomcat_path
	if [ $? -ne 0 ]
	then
		echo "please check tomcat path is right"
		exit 1
	fi
	tomat_pid=$(ps -ef | grep tomcat | grep -w $tomcat_name | grep -v 'grep' | awk '{print $2}')
	if [ -z "$tomat_pid" ]
	then
		echo "$tomcat_name process has closed"
	else
		kill -9 $tomat_pid
		echo "$tomcat_name process has closed"
	fi
	echo " --------- stopTomcat end ----------- "
	echo -e "\e[0m"
}
stopTomcat

bakWar() {
	echo -e "\e[1;32m"
	echo " --------- bakWar start ----------- "
	if [ -f $war_bak_path ]
	then
		echo "$war_bak_path is file, delete it"
		rm -rf $war_bak_path
	fi
	if [ -d $war_bak_path ]
	then
		echo "$war_bak_path has exist"
	else
		mkdir $war_bak_path
	fi
	cd $tomcat_path
	if [ $(find ./webapps -maxdepth 1 -name $war_name | wc -l) -ne 1 ]
	then
		echo "$war_name not exist, not need bak"
	else
		cp ./webapps/"$war_name" $war_bak_path/"$war_name".`date +%Y%m%d_%H%M%S`
	fi
	echo " --------- bakWar end ----------- "
	echo -e "\e[0m"
}
bakWar

delWar() {
	echo -e "\e[1;32m"
	echo " --------- delWar start ----------- "
	rm -rf $tomcat_path"/webapps/$war_name"
	rm -rf $tomcat_path"/webapps/$war_dir"
	echo " clean success "
	echo " --------- delWar end ----------- "
	echo -e "\e[0m"
}
delWar

startTomcat() {
	echo -e "\e[1;32m"
	echo " --------- startTomcat start ----------- "
	cp $war_path/$war_name $tomcat_path"/webapps/"
	cd $tomcat_path
	./bin/startup.sh
	echo " --------- startTomcat end ----------- "
	echo -e "\e[0m"
}
startTomcat
           

2.rollback.sh(回滚脚本)

#!/bin/bash

rollback_war=$1
readonly rollback_war

tomcat_path=/usr/local/tomcat8
readonly tomcat_path

tomcat_name=tomcat8
readonly tomcat_name

war_name=springboot-demo.war
readonly war_name

war_dir=springboot-demo
readonly war_dir

checkWarExist() {
	echo -e "\e[1;32m"
	echo " --------- checkWarExist start ----------- "
	if [ -z "$rollback_war" ]
	then
		echo " please input right rollback war path"
		exit 1
	fi
	if [ -f $rollback_war ]
	then
		echo " war exist "
	else
		echo " war not exist "
		exit 1
	fi
	echo " --------- checkWarExist end ----------- "
	echo -e "\e[0m"
}
checkWarExist

stopTomcat() {
	echo -e "\e[1;32m"
	echo " --------- stopTomcat start ----------- "
	cd $tomcat_path
	if [ $? -ne 0 ]
	then
		echo "please check tomcat path is right"
		exit 1
	fi
	tomat_pid=$(ps -ef | grep tomcat | grep -w $tomcat_name | grep -v 'grep' | awk '{print $2}')
	if [ -z "$tomat_pid" ]
	then
		echo "$tomcat_name process has closed"
	else
		kill -9 $tomat_pid
		echo "$tomcat_name process has closed"
	fi
	echo " --------- stopTomcat end ----------- "
	echo -e "\e[0m"
}
stopTomcat

delWar() {
	echo -e "\e[1;32m"
	echo " --------- delWar start ----------- "
	rm -rf $tomcat_path"/webapps/$war_name"
	rm -rf $tomcat_path"/webapps/$war_dir"
	echo " clean success "
	echo " --------- delWar end ----------- "
	echo -e "\e[0m"
}
delWar

startTomcat() {
	echo -e "\e[1;32m"
	echo " --------- startTomcat start ----------- "
	cp $rollback_war $tomcat_path"/webapps/$war_name"
	cd $tomcat_path
	./bin/startup.sh
	echo " --------- startTomcat end ----------- "
	echo -e "\e[0m"
}
startTomcat