# -*- coding: utf-8 -*-
"""
This shell is base on tomcat env
"""
import os
import time
import commands
import MySQLdb
#assigned
APP_ID = "123456789987456321ABCD"
APP_NAME = ""
BEFORE_SHELL = ""
AFTER_SHELL = ""
PKG_URL = ""
PUB_CONTAINER = ""
PUB_DIR = ""
PUB_PKG = ""
SHUTDOWN_WAIT = 5
EXTRACTED_WAIT = 5
#execute command
def execute(command):
result= commands.getstatusoutput(command)
code = result[0]
msg= result[1]
if code == 0:
print "---"+time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) + "--- execute success with message:\n"+msg
return msg
conn=MySQLdb.connect(host='localhost',user='root',passwd='123456',db='test',port=3306)
cursor=conn.cursor()
values=[APP_ID,1]
sql = ("SELECT "
+" ID, "
+" TASK_ID, "
+" APP_ID, "
+" APP_NAME, "
+" PKG_URL, "
+" PUB_CONTAINER, "
+" PUB_DIR, "
+" PUB_PKG, "
+" BEFORE_SHELL, "
+" AFTER_SHELL, "
+" SHUTDOWN_WAIT, "
+" EXTRACTED_WAIT,"
+" STATUS, "
+" CREATE_TIME, "
+" CREATE_BY, "
+" UPDATE_TIME, "
+" UPDATE_BY FROM DEPLOY_QUEUE"
+" WHERE APP_ID = %s AND STATUS = %s ")
try:
cursor.execute(sql,values)
result = cursor.fetchone()
APP_NAME = result[3]
PKG_URL = result[4]
PUB_CONTAINER = result[5]
PUB_DIR = result[6]
PUB_PKG = result[7]
BEFORE_SHELL = result[8]
AFTER_SHELL = result[9]
SHUTDOWN_WAIT = result[10]
EXTRACTED_WAIT = result[11]
print "-------------get package start--------------"
if PKG_URL != "":
execute(
"rm " + PUB_DIR + PUB_PKG + " -rf "
+ " && wget -c " + PKG_URL + " -O " + PUB_DIR + PUB_PKG
)
print "-------------get package end--------------"
print "-------------before shell start--------------"
if BEFORE_SHELL == "":
execute(
"cd "+PUB_CONTAINER+"bin/"
+ " && ./shutdown.sh"
)
else:
execute(BEFORE_SHELL)
print "-------------before shell end--------------"
#wait shutdown
time.sleep(SHUTDOWN_WAIT)
print "-------------before check begin--------------"
msg = execute("ps -aux | grep java | awk '/usr\/local\/tomcat*/{print $2}'")
if msg != '' :
execute("kill -9 "+msg)
print "-------------before check end--------------"
print "-------------backup app begin--------------"
if os.path.exists(PUB_DIR + APP_NAME):
execute(
"cd "+PUB_DIR
+ " && tar -zcf "+ PUB_DIR + APP_NAME+"_bak_$(date +'%Y%m%d%H%M%S').tar.gz "+APP_NAME
+ " && " + "cd "+PUB_DIR
+ " && rm -rf " + APP_NAME
)
print "-------------backup app end--------------"
print "-------------extracted package begin--------------"
if os.path.exists(PUB_DIR + PUB_PKG):
execute(
"cd "+PUB_DIR
+ " && tar -zxf "+ PUB_DIR +PUB_PKG
)
print "-------------extracted package end--------------"
#wait unzip
time.sleep(EXTRACTED_WAIT)
print "-------------after shell start--------------"
if AFTER_SHELL == "":
execute(
"cd "+PUB_CONTAINER+"bin/"
+ " && ./startup.sh"
)
else:
execute(AFTER_SHELL)
print "-------------after shell end--------------"
except MySQLdb.Error,e:
print "Mysql Error %d: %s" % (e.args[0], e.args[1])
finally:
cursor.close()
conn.close()