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 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 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