To log when to create,start,stop and delete the container in container.py

This commit is contained in:
zhuyj17 2016-07-24 10:09:41 +08:00
parent 35af1df339
commit 381295a59d
4 changed files with 34 additions and 9 deletions

View File

@ -5,6 +5,7 @@ import imagemgr
from log import logger
import env
from lvmtool import sys_run, check_volume
from monitor import History_Manager
class Container(object):
def __init__(self, addr, etcdclient):
@ -20,9 +21,11 @@ class Container(object):
self.lxcpath = "/var/lib/lxc"
self.imgmgr = imagemgr.ImageMgr()
self.historymgr = History_Manager()
def create_container(self, lxc_name, username, setting, clustername, clusterid, containerid, hostname, ip, gateway, vlanid, image):
logger.info("create container %s of %s for %s" %(lxc_name, clustername, username))
self.historymgr.log(lxc_name,"Create")
try:
setting = json.loads(setting)
cpu = int(setting['cpu']) * 100000
@ -134,6 +137,7 @@ IP=%s
def delete_container(self, lxc_name):
logger.info ("delete container:%s" % lxc_name)
self.historymgr.log(lxc_name,"Delete")
if self.imgmgr.deleteFS(lxc_name):
logger.info("delete container %s success" % lxc_name)
return [True, "delete container success"]
@ -151,6 +155,7 @@ IP=%s
# start container, if running, restart it
def start_container(self, lxc_name):
logger.info ("start container:%s" % lxc_name)
self.historymgr.log(lxc_name,"Start")
#status = subprocess.call([self.libpath+"/lxc_control.sh", "start", lxc_name])
#if int(status) == 1:
# logger.error ("start container %s failed" % lxc_name)
@ -224,6 +229,7 @@ IP=%s
def stop_container(self, lxc_name):
logger.info ("stop container:%s" % lxc_name)
self.historymgr.log(lxc_name,"Stop")
#status = subprocess.call([self.libpath+"/lxc_control.sh", "stop", lxc_name])
[success, status] = self.container_status(lxc_name)
if not success:

View File

@ -877,7 +877,7 @@ if __name__ == '__main__':
if etcdclient.isdir("_lock")[0]:
etcdclient.deldir("_lock")
G_usermgr = userManager.userManager('root')
G_usermgr = userManager.userManager('root','unias1616')
if mode == "new":
G_usermgr.initUsage()
G_notificationmgr = notificationmgr.NotificationMgr()
@ -899,8 +899,7 @@ if __name__ == '__main__':
logger.info("imagemgr started")
master_collector = monitor.Master_Collector(G_nodemgr)
master_collector.start()
G_historymgr = monitor.History_Manager()
logger.info("historymgr started")
logger.info("master_collector started")
logger.info("startting to listen on: ")
masterip = env.getenv('MASTER_IP')

View File

@ -206,12 +206,11 @@ class History(db.Model):
billings = db.Column(db.Integer)
actionTime = db.Column(db.DateTime)
def __init__(self, vnode_name, isToStart, cputime, billings):
self.vnode = vnode_name
self.isToStart = isToStart
def __init__(self, action, cputime, billings):
self.action = action
self.cputime = cputime
self.billings = billings
self.actionTime = datetime.utcnow()
self.actionTime = datetime.now()
def __repr__(self):
return "{\"id\":\"%d\",\"vnode\":\"%s\",\"isToStart\":\"%r\",\"cputime\":\"%f\",\"billings\":\"%d\",\"actionTime\":\"%s\"}" % (self.id, self.vnode, self.isToStart, self.cputime, self.billings, self.actionTime)
return "{\"id\":\"%d\",\"vnode\":\"%s\",\"action\":\"%s\",\"cputime\":\"%f\",\"billings\":\"%d\",\"actionTime\":\"%s\"}" % (self.id, self.vnode, self.action, self.cputime, self.billings, self.actionTime.strftime("%Y-%m-%d %H:%M:%S"))

View File

@ -404,6 +404,8 @@ class Master_Collector(threading.Thread):
logger.warning(traceback.format_exc())
logger.warning(err)
time.sleep(2)
#logger.info(History.query.all())
#logger.info(VNode.query.all())
return
def stop(self):
@ -569,5 +571,24 @@ class History_Manager:
def getAll(self):
return History.query.all()
def log(self,vnode_name,action):
global monitor_vnodes
res = VNode.query.filter_by(name=vnode_name).first()
if res is None:
vnode = VNode(vnode_name)
vnode.histories = []
db.session.add(vnode)
db.session.commit()
vnode = VNode.query.get(vnode_name)
try:
owner = get_owner(vnode_name)
billings = int(monitor_vnodes[owner][vnode_name]['basic_info']['billings'])
cputime = float(monitor_vnodes[owner][vnode_name]['basic_info']['cpu_use']['val'])
except:
billings = 0
cputime = 0.0
history = History(action,cputime,billings)
vnode.histories.append(history)
db.session.add(history)
db.session.commit()