python更新tomcat脚本
#!/usr/bin/env python
# _*_ coding:UTF-8 _*_
import commands
import time,os,sys
from download_bag import downloadbag
tomcat_home = '/usr/local/apache-tomcat-7.0.42/'
webapp_name = 'front'
update_dir = "/home/deployer/update/"
backup_home = '/home/deployer/backup_war'
uptime = time.strftime('%Y%m%d',time.localtime(time.time()))
backup_time = time.strftime('%Y%m%d_%H%M',time.localtime(time.time()))
#关闭tomcat
def shutdown():
print "准备关闭tomcat......"
status,output=commands.getstatusoutput('ps -ef|grep -i '+tomcat_home+'|wc -l')
if(int(output)>2): #判断是否有tomcat进程
print " 开始关闭tomcat"
status,output=commands.getstatusoutput(tomcat_home+'bin/shutdown.sh')
if(status!=0):
print output
sys.exit(1)
time.sleep(0.5) #关闭程序缓存时间
status,output=commands.getstatusoutput('ps -ef|grep -i '+tomcat_home+'|wc -l')
if(int(output)>2): #判断是否关闭成功
print " 强制关闭tomcat"
status,output=commands.getstatusoutput('kill -9 `ps -ef|grep -i '+tomcat_home+' |awk \'NR==1{print $2}\'`')
if(status!=0):
print output
sys.exit(1)
print " 已关闭tomcat"
return 0
print " tomcat未启动"
#备份tomcat
def backup():
print "开始备份......"
#判断今日备份目录是否存在,不存在则创建
if(not os.path.exists(backup_home+'/'+webapp_name+'/'+backup_time)):
status,output=commands.getstatusoutput('mkdir -p '+backup_home+'/'+webapp_name+'/'+backup_time)
if(status!=0):
print output
sys.exit(1)
# #备份当前版本,备份前先判断目录是否是空的
if(len(os.listdir(tomcat_home+'webapps/'))>0):
status,output=commands.getstatusoutput('mv '+tomcat_home+'webapps/* '+backup_home+'/'+webapp_name+'/'+backup_time)
if(status!=0):
print output
sys.exit(1)
print ' 备份成功'
from download_bag import downloadbag
#部署新程序
def update():
print "开始部署....."
#部署新war包到tomcat里
status,output=commands.getstatusoutput('cp -rf '+update_dir+uptime+'/'+webapp_name + '.war ' + tomcat_home+'webapps/')
if(status!=0):
print output
sys.exit(1)
#启动tomcat
status,output=commands.getstatusoutput(tomcat_home+'bin/startup.sh')
print output
if __name__ == '__main__':
if os.geteuid() == 0:
print '''
***************************************************************************
********** hi,hi,hi.......请注意 ***************
********* 不能用root账号,请使用deployer账号,现在正在退出' ***************
********* exit now ********
'''
sys.exit(1)
else:
downloadbag()
shutdown()
backup()
update()