tomcat站点更新脚本,欢迎吐槽
#!/usr/local/bin/python3
#coding:utf-8
import os, sys, subprocess, time, shutil
#get tommcat pid
tomcat_pid = ((subprocess.Popen("lsof -i :8080 |grep root |awk '{print $2}'", shell=True, stdout=subprocess.PIPE)).stdout.read()).decode()
#关闭tomcat
print("Tomcat will shutdown after 6s, u can enter Ctrl + c interrupt it ! ")
for i in range(3):
print("." ,end = "")
sys.stdout.flush()
time.sleep(1)
print()
if len(tomcat_pid) == 0:
print("> tomcat already shutdown!")
else:
subprocess.Popen("/var/tomcat/bin/shutdown.sh > /dev/null 2>&1", shell=True, stdout=subprocess.PIPE)
for i in range(3):
print("." ,end = "")
sys.stdout.flush()
time.sleep(1)
if len(tomcat_pid) == 0:
pass
else :
subprocess.Popen("kill -9 " + tomcat_pid, shell=True, stdout=subprocess.PIPE)
print("
> Tomcat close the failure, kill the pid %s" % tomcat_pid)
#clean tomcat cache
print("
> clean tomcat webapp cache...")
subprocess.Popen("rm -rf /var/tomcat/temp/*", shell=True, stdout=subprocess.PIPE)
subprocess.Popen("rm -rf /var/tomcat/work/Catalina/localhost/*", shell=True, stdout=subprocess.PIPE)
print("
> clean tomcat webapp cache completed !!!")
#备份旧站点
print("
--------Begin to backup webapps---------
")
try:
p = subprocess.Popen("Time=`date +%m%d%H%M` && tar czf /home/webbak/webapps$Time.tar.gz /var/tomcat/webapps", shell=True, stdout=subprocess.PIPE)
p.stdout.read()
except Exception as e:
print(e)
print("
> Backup completed,Start to update the program...")
for i in range(3):
print("." ,end = "")
sys.stdout.flush()
time.sleep(1)
def Delete_old_site(sitefile):
update_file = '/home/sourcedir/' + sitefile + '.war' #升级版本上传目录
webapps_file = '/var/tomcat/webapps/' + sitefile + '.war'
webapps_dir = '/var/tomcat/webapps/' + sitefile
os.remove(webapps_file)
shutil.rmtree(webapps_dir)
if os.path.exists(webapps_file) or os.path.exists(webapps_dir):
print(u"旧站点文件删除失败,请手动删除...")
sys.exit(0)
else:
if os.path.exists(update_file):
shutil.copyfile(update_file, webapps_file)
else:
sys.exit(0)
#定义站点站点相关目录及文件位置
site_file = sys.argv
try:
sitefile = site_file[1]
Delete_old_site(sitefile)
except Exception as e:
print(u"warning:脚本最多指定三个参数,最少指定一个参数!!!")
sys.exit(0)
try:
sitefile = site_file[2]
Delete_old_site(sitefile)
except Exception as e:
pass
try:
sitefile = site_file[3]
Delete_old_site(sitefile)
except Exception as e:
pass
print("
> Update completed,start tomcat...")
subprocess.Popen("/var/tomcat/bin/startup.sh", shell=True, stdout=subprocess.PIPE)
for i in range(3):
print("." ,end = "")
sys.stdout.flush()
time.sleep(1)
tomcat_pid2 = ((subprocess.Popen("lsof -i :8080 |grep root |awk '{print $2}'", shell=True, stdout=subprocess.PIPE)).stdout.read()).decode()
if len(tomcat_pid2) == 0:
print("
> Tomcat has not start, Please check u program!")
else:
print("
> Tomcat have already started !")
END!