python自动运维管理

# -*- 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()





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值