def diskRegister(path=None): def createName(): str1 = "" chars = '1234567890ABCDEFabcdef' length = len(chars) - 1 random = Random() for i in range(10): str1 += chars[random.randint(0, 13)] return str1 id = createName() now = datetime.datetime.now() time = now.strftime("%Y:%m:%d %H:%M:%S") cf = ConfigParser.ConfigParser() cf.read("/git_csdn/kvm/src/global.conf") pool_name = cf.get("storage", "pool_name") a = str(path).split('/') name = a[-1] del a[0] del a[-1] path = "/" for i in a: path += i + "/" rg = [{ 'image': name, 'path': path, 'time': "'{}'".format(time), 'st': "'N'", 'UUID': "'{}'".format(id), }] pyHost = redisMgnt().control(type='select', Dict=['UUID', 'nodeIP'], table='host_nodeinfo', Where={'nodeSTATUS': "'0'"}) for i in [m[1] for m in pyHost]: obj = domain_opera.virConn(i).getStoragePoolObject(pool_name, "name") domain_opera.storage_edit(obj).refer() hl = [m[1] for m in pyHost] ho = hl[Random().randint(0, len(hl) - 1)] volObject = domain_opera.virConn(ho).getVolObj(rg[0]['path'] + rg[0]['image']) rg[0]['AllSize'] = "'{}'".format( domain_opera.vol_edit(volObject).size()["AllSize"]) rg[0]['UseSize'] = "'{}'".format( domain_opera.vol_edit(volObject).size()["UseSize"]) rg[0]['image'] = "'{}'".format(rg[0]['image']) rg[0]['path'] = "'{}'".format(rg[0]['path']) rcode = redisMgnt().control(type='insert', Dict=rg[0], table='disk_diskregister') if rcode == 200: return {'code': 200}
def diskRefer(**kwargs): try: conn = redisMgnt() imageName = kwargs["image"] inhost = [ x[0] for x in conn.control( type="select", Dict=['nodeIP'], table="host_nodeinfo") ] path = "{}{}".format( conn.control(type="select", Dict=['path'], table="disk_diskregister", Where={'image': '"{}"'.format(imageName)})[0][0], imageName) rn = Random().randint(0, len(inhost) - 1) obj = domain_opera.virConn(inhost[rn]).getVolObj(path) AllSize = domain_opera.vol_edit(obj).size()["AllSize"] UseSize = domain_opera.vol_edit(obj).size()["UseSize"] # print(AllSize) conn.control(type="update", Dict={ 'AllSize': '"{}"'.format(AllSize), 'UseSize': '"{}"'.format(UseSize) }, table="disk_diskregister", Where={'image': '"{}"'.format(imageName)}) conn.__exit__() return 200 except Exception, msg: print(msg)
def diskRefer(**kwargs): try: UUID = kwargs["image"] inhost = [ x[0] for x in redisMgnt().control( type="select", Dict=['nodeIP'], table="host_nodeinfo") ] f = redisMgnt().control(type="select", Dict=['path', 'image'], table="disk_diskregister", Where={'UUID': '"{}"'.format(UUID)}) path = f[0][0] + f[0][1] rn = Random().randint(0, len(inhost) - 1) obj = domain_opera.virConn(inhost[rn]).getVolObj(path) AllSize = domain_opera.vol_edit(obj).size()["AllSize"] UseSize = domain_opera.vol_edit(obj).size()["UseSize"] if AllSize * 1024 * 1024 * 1024 != kwargs['size']: return diskRefer(image=kwargs['image'], size=kwargs['size']) else: # print(AllSize) redisMgnt().control(type="update", Dict={ 'AllSize': '"{}"'.format(AllSize), 'UseSize': '"{}"'.format(UseSize) }, table="disk_diskregister", Where={'UUID': '"{}"'.format(UUID)}) return 200 except Exception, msg: return {'error': 451}
def domainEjectCdrom(UUID,ACTION,CDUUID=None): try: conn = redisMgnt() host = conn.control(type='select', Dict=['inHost'], table='domain_machineinfo', Where={'UUID': "'{}'".format(UUID)})[ 0][0] object = domain_opera.domain_edit(domain_opera.virConn(host).getDomainObject(str=UUID,type='uuid')) xml = object.dumpxml() disk = re.findall("(\<disk type='file' device='cdrom'\>)(.+?)(\<\/disk\>)", xml, re.S) m = "" for i in disk[0]: m += i diskXMLtoJson = xmltodict.parse(m) if ACTION == 'eject': diskXMLtoJson['disk']['source']['@file'] = "" elif ACTION == 'mount': path = conn.control(type='select',Dict=['image','path'],table='imageregister',Where={'UUID':"'{}'".format(CDUUID)})[0] str = path[1] + path[0] f = json.dumps(diskXMLtoJson) diskXMLtoJson = json.loads(f) diskXMLtoJson['disk']['source'] = {'@file':str} diskJsontoXML = xmltodict.unparse(diskXMLtoJson) object.deviceUpdate(diskJsontoXML) conn.__exit__() return {'code' : 200} except Exception,msg: print(msg) return {'code' : 451}
def storageUpdate(): try: conn = redisMgnt() nl = [ x[0] for x in conn.control(type='select',Dict=['nodeIP'],table='host_nodeinfo')] cf = ConfigParser.ConfigParser() cf.read("/git_csdn/kvm/conf/kvm.conf") n = cf.get("storage","pool_name") conn.control(type='delete',table="storageinfo") for i in nl: obj = domain_opera.virConn(str(i)).getStoragePoolObject(str=n,type='name') m = domain_opera.storage_edit(_obj=obj).info() f = {} f['UUID'] = domain_opera.storage_edit(_obj=obj).uuid() f['inHost'] = i f['t_Usage'] = m['Usage'] f['t_All'] = m['All'] f['t_Free'] = m['Free'] for k in f.keys(): f[k] = "'{}'".format(f[k]) conn.control(type='insert',Dict=f,table='storageinfo') conn.__exit__() print({'status': 'ok', 'task': 'refer storage info', 'module': 'domain_control-storageUpdate'}) return {'code' : 200} except Exception,msg: print(msg) return {'error' : 452}
def domain_crontrol_function(UUID=None,ACTION=None,Dict=None): try: UUID = str(UUID).strip("\'") ACTION = ACTION domainInhost = redisMgnt().control(type='select',Dict=['inHost'],table='domain_machineinfo',Where={'UUID' : "'{}'".format(UUID)})[0][0] domainObject=domain_opera.virConn(domainInhost).getDomainObject(str=UUID,type='uuid') if ACTION == 'start': if domain_opera.domain_edit(object=domainObject).start() == 200: redisMgnt().control(type='update',Dict={'rStatus' : "'1'"},table='domain_machineinfo',Where={'UUID':"'{}'".format(UUID)}) print {'type': 'domainCrontrol', 'status': 'ok', 'des': 'start ok', 'user': '******'.format(UUID),'module': 'api-domain_crontrol-domain_crontrol_function'} return {'code' : 200} else: print {'type': 'domainCrontrol', 'status': 'faild', 'des': 'start faild', 'user': '******'.format(UUID),'module': 'api-domain_crontrol-domain_crontrol_function'} return {'code' : 455} elif ACTION == 'destroy': if domain_opera.domain_edit(object=domainObject).destroy() == 200: redisMgnt().control(type='update', Dict={'rStatus': "'0'"}, table='domain_machineinfo', Where={'UUID': "'{}'".format(UUID)}) print {'type': 'domainCrontrol', 'status': 'ok', 'des': 'destroy ok', 'user': '******'.format(UUID),'module': 'api-domain_crontrol-domain_crontrol_function'} return {'code': 200} else: print {'type': 'domainCrontrol', 'status': 'faild', 'des': 'destroy faild', 'user': '******'.format(UUID),'module': 'api-domain_crontrol-domain_crontrol_function'} return {'code':455} elif ACTION == 'restart': if domain_opera.domain_edit(object=domainObject).reboot() == 200: redisMgnt().control(type='update', Dict={'rStatus': "'0'"}, table='domain_machineinfo', Where={'UUID': "'{}'".format(UUID)}) print {'type': 'domainCrontrol', 'status': 'ok', 'des': 'restart ok', 'user': '******'.format(UUID),'module': 'api-domain_crontrol-domain_crontrol_function'} return {'code': 200} else: print {'type': 'domainCrontrol', 'status': 'faild', 'des': 'restart faild', 'user': '******'.format(UUID),'module': 'api-domain_crontrol-domain_crontrol_function'} return {'code':455} elif ACTION == 'delete': if domain_opera.domain_edit(object=domainObject).undefine() == 200: redisMgnt().control(type="delete", table='domain_machineinfo', Where={'UUID': "'{}'".format(UUID)}) redisMgnt().control(type="delete", table='domain_checkinfo', Where={'UUID': "'{}'".format(UUID)}) redisMgnt().control(type="update", table='disk_diskregister', Dict={'st': "'N'", 'Dhost': "' '"}, Where={'Dhost': "'{}'".format(UUID)}) print {'type' : 'domainCrontrol','status':'ok','des' :'delete ok','user':'******'.format(UUID),'module':'api-domain_crontrol-domain_crontrol_function'} return {'code': 200} else: print {'type': 'domainCrontrol', 'status': 'faild', 'des': 'delete faild', 'user': '******'.format(UUID), 'module': 'api-domain_crontrol-domain_crontrol_function'} return {'code':455} elif ACTION == 'ejectCDROM': pass except Exception,msg: print(msg) return {'error' : 453 }
def createMachine(data): try: conn = redisMgnt() machineName = data['name'] inHost = data['host'] iso = data['sys'] beizhu = data['info'] memoryUse = int(data['memory']) * 1024 * 1024 cpuUse = data['cpu'] disk = data['disk'] network = data['network'] # create a new domain xml with open('/git_csdn/kvm/src/temple/newMachine.xml') as f: newXML = f.read() domainXML = xmltodict.parse(newXML) domainXML['domain']['name'] = machineName domainXML['domain']['memory']['#text'] = memoryUse domainXML['domain']['currentMemory']['#text'] = memoryUse domainXML['domain']['vcpu']['#text'] = cpuUse aliveDISK = conn.control(type="select", Dict=['path', 'image'], table='disk_diskregister', Where={'UUID': "'{}'".format(disk)}) dp = "{}{}".format(aliveDISK[0][0], aliveDISK[0][1]) domainXML['domain']['devices']['disk'][0]['source']['@file'] = dp aliveIMAGE = conn.control(type="select", Dict=['path', 'image'], table='imageregister', Where={'UUID': '"{}"'.format(iso)}) sp = "{}{}".format(aliveIMAGE[0][0], aliveIMAGE[0][1]) domainXML['domain']['devices']['disk'][1]['source']['@file'] = sp target = script.getNetworkTarget() domainXML['domain']['devices']['interface']['target']['@dev'] = target domainXML['domain']['devices']['interface']['source']['@network'] = network aliveVNCPort = script.vncPortGet() domainXML['domain']['devices']['graphics']['@port'] = aliveVNCPort domainXML['domain']['devices']['graphics']['@autoport'] = 'no' domainXML['domain']['devices']['graphics']['@keymap'] = 'en-us' bronXML = xmltodict.unparse(domainXML) # get install host if inHost != 'auto': k = conn.control(type="select", Dict=['nodeIP'], table='host_nodeinfo', Where={'UUID': "'{}'".format(inHost)}) host = k[0][0] if domain_opera.virConn(host).defineXML(bronXML) == 200: domain_control_refer(num='onlyHost',UUID=inHost) domain_control_monitor(num='only',UUID=redisMgnt().control(type='select',Dict=['UUID'],table='domain_machineinfo',Where={'vName':"'{}'".format(machineName)})[0][0]) script.vncConfig() script.nodeVncPort() return {'code' : 200} else: return {'error': 451} except Exception, msg: print msg
def diskDelete(**kwargs): try: UUID = kwargs["UUID"] inhost = [ x[0] for x in redisMgnt().control( type="select", Dict=['nodeIP'], table="host_nodeinfo") ] f = redisMgnt().control(type="select", Dict=['path', 'image'], table="disk_diskregister", Where={'UUID': '"{}"'.format(UUID)}) path = f[0][0] + f[0][1] rn = Random().randint(0, len(inhost) - 1) obj = domain_opera.virConn(inhost[rn]).getVolObj(path) domain_opera.vol_edit(obj).delete() redisMgnt().control(type="delete", table="disk_diskregister", Where={'UUID': '"{}"'.format(UUID)}) return 200 except Exception, msg: print(msg) return 202
def domain_control_refer(num=None,UUID=None): try: if num == 'all': hostList = [ x[0] for x in redisMgnt().control(type='select',Dict=['nodeIP'],table="host_nodeinfo") ] hostObj = [ domain_opera.virConn(x[0]) for x in redisMgnt().control(type='select',Dict=['nodeIP'],table="host_nodeinfo") ] iL = [] for d in range(len(hostList)): domainObjList = [ domain_opera.domain_edit(object=k) for k in hostObj[d].getAllDomains(flags=0)] inHost = hostList[d] for i in domainObjList: st = i.isActive() doc = i.dumpxml() xdc = xmltodict.parse(xml_input=doc) xobj = script.domainXMLjx(xml=xdc) diskList = xobj.diskInfo() grphList = xobj.graphicel() netList = xobj.interfaceList() confList = xobj.configInfo() nC = { 'UUID': "'{}'".format(i.UUIDString()), 'inHost': "'{}'".format(inHost), 'vName': "'{}'".format(i.name()), 'rStatus': "'{}'".format(st), 'MaxMemory': "'{}'".format((confList['MaxMemory'])), 'CurMemory': "'{}'".format(confList['CurMemory']), 'MaxCpu': "'{}'".format(confList['MaxCpu']), 'CurCpu': "'{}'".format(confList['CurCpu']), 'grapType': "'{}'".format(grphList['grapType']), 'grapPort': "'{}'".format(grphList['grapPort']), 'grapCType': "'{}'".format(grphList['grapCType']), 'disk': "'{}'".format(json.dumps(diskList)), 'network': "'{}'".format(json.dumps(netList)) } iL.append(nC) redisMgnt().control(type='delete',table='domain_machineinfo') for i in iL: redisMgnt().control(type="insert",Dict=i,table='domain_machineinfo') print {'task':"refer domain info",'status':'ok','module':'domain_control-domain_crontrol_refer'} return {"code" : 200} elif num == 'only': hostList = [x[0] for x in redisMgnt().control(type='select', Dict=['nodeIP'], table="host_nodeinfo")] LK = [] for k in range(len(hostList)): hostUUID = [ x.UUIDString() for x in domain_opera.virConn(hostList[k]).getAllDomains(flags=0)] hostObj = [domain_opera.domain_edit(object=x) for x in domain_opera.virConn(hostList[k]).getAllDomains(flags=0)] inHost = hostList[k] if hostObj: for i in hostUUID: if UUID in hostUUID: ind = hostUUID.index(UUID) OBJ = hostObj[ind] st = OBJ.isActive() doc = OBJ.dumpxml() xdc = xmltodict.parse(xml_input=doc) xobj = script.domainXMLjx(xml=xdc) diskList = xobj.diskInfo() grphList = xobj.graphicel() netList = xobj.interfaceList() confList = xobj.configInfo() nC = { 'UUID': "'{}'".format(OBJ.UUIDString()), 'inHost': "'{}'".format(inHost), 'vName': "'{}'".format(OBJ.name()), 'rStatus': "'{}'".format(st), 'MaxMemory': "'{}'".format((confList['MaxMemory'])), 'CurMemory': "'{}'".format(confList['CurMemory']), 'MaxCpu': "'{}'".format(confList['MaxCpu']), 'CurCpu': "'{}'".format(confList['CurCpu']), 'grapType': "'{}'".format(grphList['grapType']), 'grapPort': "'{}'".format(grphList['grapPort']), 'grapCType': "'{}'".format(grphList['grapCType']), 'disk': "'{}'".format(json.dumps(diskList)), 'network': "'{}'".format(json.dumps(netList)) } LK.append(nC) break else: continue for i in LK: redisMgnt().control(type='delete',table='domain_machineinfo',Where={'UUID':"'{}'".format(UUID)}) redisMgnt().control(type="insert",Dict=i,table='domain_machineinfo') return {"code" : 200} if num == 'onlyHost': #get the UUID -> ip inHost = redisMgnt().control(type='select',Dict=['nodeIP'],table='host_nodeinfo',Where={'UUID':"'{}'".format(UUID)})[0][0] #bron host object hostObj = [ domain_opera.domain_edit(object=x) for x in domain_opera.virConn(inHost).getAllDomains(flags=0)] if hostObj: domainObjectInfo = [] for domainObject in hostObj: st = domainObject.isActive() doc = domainObject.dumpxml() xdc = xmltodict.parse(xml_input=doc) xobj = script.domainXMLjx(xml=xdc) diskList = xobj.diskInfo() grphList = xobj.graphicel() netList = xobj.interfaceList() confList = xobj.configInfo() nC = { 'UUID': "'{}'".format(domainObject.UUIDString()), 'inHost': "'{}'".format(inHost), 'vName': "'{}'".format(domainObject.name()), 'rStatus': "'{}'".format(st), 'MaxMemory': "'{}'".format((confList['MaxMemory'])), 'CurMemory': "'{}'".format(confList['CurMemory']), 'MaxCpu': "'{}'".format(confList['MaxCpu']), 'CurCpu': "'{}'".format(confList['CurCpu']), 'grapType': "'{}'".format(grphList['grapType']), 'grapPort': "'{}'".format(grphList['grapPort']), 'grapCType': "'{}'".format(grphList['grapCType']), 'disk': "'{}'".format(json.dumps(diskList)), 'network': "'{}'".format(json.dumps(netList)) } domainObjectInfo.append(nC) for i in domainObjectInfo: redisMgnt().control(type='delete',table='domain_machineinfo',Where={'UUID':"{}".format(i['UUID'])}) redisMgnt().control(type='insert',table='domain_machineinfo',Dict=i) return {"code" : 200} except Exception,msg: print(msg) return {"error" : 454}
def domain_control_monitor(num=None,UUID=None): try: if num == 'all': #Generate Host object list hostObj = [domain_opera.virConn(x[0]) for x in redisMgnt().control(type='select', Dict=['nodeIP'], table="host_nodeinfo")] #start cycle domainMonitorInfo = [] for i in hostObj: for k in [ x for x in i.getAllDomains(flags=0)]: domainObj = domain_opera.domain_edit(object=k) if domainObj.isActive() == 0: tmp = { 'UUID' : "'{}'".format(domainObj.UUIDString()), 'cpuLV': "'None'", 'memLv': "'None'", 'rss': "'None'", 'vName': "'{}'".format(domainObj.name()), 'rStatus': "'None'", 'disk': "'None'", 'network': "'None'"} domainMonitorInfo.append(tmp) else: cl = domainObj.getcpuInfo() ml = domainObj.getmemoryInfo() di = redisMgnt().control(type='select',Dict=['network','disk'],table='domain_machineinfo',Where={'UUID':"'{}'".format(domainObj.UUIDString())}) f = json.loads(di[0][0])[0] nl = domainObj.getinterfaceInfo(gdict=[f[f.keys()[0]]['netTarget']]) diskL = json.loads(di[0][1]) diski = [] for i in diskL: if i[i.keys()[0]]['devType'] == 'disk': diski.append(i.keys()[0]) else: pass dl = domainObj.getdiskInfo(gdict=diski) dic = { 'cpuLV': "'%.2f%%'" % ( float(cl['system_time'] + float(cl['user_time'])) / cl['cpu_time'] / cl['cpuNr'] * 100), 'rStatus': "'running'", } if ml['virito'] == 0: dic['memLv'] = "'None'" dic['rss'] = "'{}'".format(ml['rss']) elif ml['virito'] == 1: dic['memLv'] = "'%.2f%%'" % ( (float(ml['available']) - float(ml['unused'])) / float(ml['available']) * 100) dic['rss'] = "'{}'".format(ml['rss']) dic['vName'] = "'{}'".format(domainObj.name()) dic['UUID'] = "'{}'".format(domainObj.UUIDString()) dic["network"] = "'{}'".format(json.dumps(nl)) dic["disk"] = "'{}'".format(json.dumps(dl)) domainMonitorInfo.append(dic) redisMgnt().control(type='delete',table='domain_checkinfo') for i in domainMonitorInfo: redisMgnt().control(type='insert',Dict=i,table="domain_checkinfo") return {'code' : '200'} elif num == 'only': hostList = [x[0] for x in redisMgnt().control(type='select', Dict=['nodeIP'], table="host_nodeinfo")] domainMonitorInfo = [] for k in range(len(hostList)): hostUUID = [x.UUIDString() for x in domain_opera.virConn(hostList[k]).getAllDomains(flags=0)] hostObj = [domain_opera.domain_edit(object=x) for x in domain_opera.virConn(hostList[k]).getAllDomains(flags=0)] if UUID in hostUUID: ind = hostUUID.index(UUID) domainObj = hostObj[ind] if domainObj.isActive() == 0: tmp = { 'UUID': "'{}'".format(domainObj.UUIDString()), 'cpuLV': "'None'", 'memLv': "'None'", 'rss': "'None'", 'vName': "'{}'".format(domainObj.name()), 'rStatus': "'None'", 'disk': "'None'", 'network': "'None'"} domainMonitorInfo.append(tmp) else: cl = domainObj.getcpuInfo() ml = domainObj.getmemoryInfo() di = redisMgnt().control(type='select', Dict=['network', 'disk'], table='domain_machineinfo', Where={'UUID': "'{}'".format(domainObj.UUIDString())}) f = json.loads(di[0][0])[0] nl = domainObj.getinterfaceInfo(gdict=[f[f.keys()[0]]['netTarget']]) diskL = json.loads(di[0][1]) diski = [] for i in diskL: if i[i.keys()[0]]['devType'] == 'disk': diski.append(i.keys()[0]) else: pass dl = domainObj.getdiskInfo(gdict=diski) dic = { 'cpuLV': "'%.2f%%'" % ( float(cl['system_time'] + float(cl['user_time'])) / cl['cpu_time'] / cl[ 'cpuNr'] * 100), 'rStatus': "'running'", } if ml['virito'] == 0: dic['memLv'] = "'None'" dic['rss'] = "'{}'".format(ml['rss']) elif ml['virito'] == 1: dic['memLv'] = "'%.2f%%'" % ( (float(ml['available']) - float(ml['unused'])) / float(ml['available']) * 100) dic['rss'] = "'{}'".format(ml['rss']) dic['vName'] = "'{}'".format(domainObj.name()) dic['UUID'] = "'{}'".format(domainObj.UUIDString()) dic["network"] = "'{}'".format(json.dumps(nl)) dic["disk"] = "'{}'".format(json.dumps(dl)) domainMonitorInfo.append(dic) redisMgnt().control(type='delete',table='domain_checkinfo',Where={'UUID':"'{}'".format(UUID)}) for i in domainMonitorInfo: redisMgnt().control(type='insert', Dict=i, table="domain_checkinfo") return {'code': '200'} except Exception,msg: print(msg) return {"error" : 454}