1.一键部署本地tomcat 脚本
实现的功能:停止tomcat,然后把之前的war包加上时间重命名,删除以前的ROOT目录,放入新的war包,启动tomcat,只保留最近6个带时间的war包。显示启动日志。
实现方式: ./autodeploy.sh ROOT.war
#!/bin/bash
now=`date +%Y%m%d%H%M%S`
tomcat_path=/home/root/tomcat8-001
# 参数校验
deploywar=$1
if [ -e "$deploywar" ]; then
echo -e "\033[34m war archive: $deploywar \033[0m"
else
echo -e "\033[31m war archive '$deploywar' not exists \033[0m"
exit -1
fi
echo "tomcat home: $tomcat_path"
#echo "backup path: $backup_path"
echo 'try to stop tomcat...'
pid=`ps aux|grep "java"|grep "$tomcat_path"|awk '{printf $2}'`
if [ -n $pid ]; then
echo "tomcat pid: $pid";
kill -9 $pid;
fi
echo 'stop tomcat finished...'
echo 'backup old archive...'
if [ -f "$tomcat_path/webapps/$deploywar" ]; then
mv -v "$tomcat_path/webapps/$deploywar" "$tomcat_path/webapps/$deploywar$now";
fi
echo 'delete old archive...'
rm -rf $tomcat_path/webapps/ROOT
echo 'delete old archive work cache...'
mv -v "$deploywar" "$tomcat_path/webapps/$deploywar"
echo -e "\033[32m"
echo 'startup tomcat...'
sh $tomcat_path/bin/startup.sh
cd $tomcat_path/webapps
ls | grep 'ROOT.war2016*' | sort | head -n -6 | xargs rm
tail -10f $tomcat_path/logs/catalina.out
2.部署远程服务器war包
这里调用了expect,需要安装一下。
实现的功能:连接到远程服务器,执行上面的autodeploy.sh 脚本
实现的方式:./scp.sh ROOT.war
#!/usr/bin/expect
set file [lindex $argv 0]
spawn scp -P 22 $file horder@192.168.1.1:/home/root/
expect {
"yes/no" {send "yes\r"; exp_continue}
"*assword" {send "password\r"}
}
spawn ssh -p 22 horder@192.168.1.1
expect {
"yes/no" {send "yes\r"; exp_continue}
"*assword" {send "password\r"}
}
expect "$"
send "./autodeploy.sh ROOT.war\r"
expect "$"
send "exit\r"
expect eof
3.最后,调用上面两个脚本,实现人机交互的方式。
#!/bin/bash
OPTIONS="前台 后台 远程前台"
select opt in $OPTIONS;do
if [ "$opt" = "前台" ];then
./autodeploy-rights.sh ROOT.war
elif [ "$opt" = "后台" ];then
./autodeploy-weiquan.sh ROOT.war
elif [ "$opt" = "远程前台" ];then
./scp.sh ROOT.war
rm ROOT.war
else
echo bad option
fi
done
这样,直接使用 ./select.sh 就可以选择操作具体的服务器,从而实现多服务器war包一键部署。