def getDetails(vmName): try: vm = OvmVm() try: OvmHost()._getDomainIdByName(vmName) vmPath = OvmHost()._vmNameToPath(vmName) vifsFromConfig = False except NoVmFoundException, e: vmPath = OvmHost()._getVmPathFromPrimaryStorage(vmName) vifsFromConfig = True if not isdir(vmPath): # The case is, when vm starting was not completed at primaryStroageDownload or createVolume(e.g. mgmt server stop), the mgmt # server will keep vm state in staring, then a stop command will be sent. The stop command will delete bridges that vm attaches, # by retriving birdge info by OvmVm.getDetails(). In this case, the vm doesn't exists, so returns a fake object here. fakeDisk = OvmDisk() vm.rootDisk = fakeDisk else: if vifsFromConfig: vm.vifs.extend(vm._getVifsFromConfig(vmPath)) else: vm.vifs.extend(vm._getVifs(vmName)) safeSetAttr(vm, 'name', vmName) disks = successToMap( xen_get_vdisks(vmPath))['vdisks'].split(',') rootDisk = None #BUG: there is no way to get type of disk, assume all are "w" for d in disks: if vmName in d: rootDisk = OvmDisk() safeSetAttr(rootDisk, 'path', d) safeSetAttr(rootDisk, 'type', "w") continue disk = OvmDisk() safeSetAttr(disk, 'path', d) safeSetAttr(disk, 'type', "w") vm.disks.append(disk) if not rootDisk: raise Exception("Cannot find root disk for vm %s" % vmName) safeSetAttr(vm, 'rootDisk', rootDisk) vcpus = int(successToMap(xen_get_vcpus(vmPath))['vcpus']) safeSetAttr(vm, 'cpuNum', vcpus) memory = MtoBytes( int(successToMap(xen_get_memory(vmPath))['memory'])) safeSetAttr(vm, 'memory', memory) vmStatus = db_get_vm(vmPath) safeSetAttr(vm, 'powerState', vmStatus['status']) vmType = successToMap(xen_get_vm_type(vmPath))['type'].replace( 'hvm', 'HVM').replace('para', 'PV') safeSetAttr(vm, 'type', vmType) rs = fromOvmVm(vm) logger.info(OvmVm.getDetails, rs) return rs
def getAllVms(): def scanStoppedVmOnPrimaryStorage(vms): def isMyVmDirLink(path): return (islink(path) and exists(join(path, 'vm.cfg')) and ('-' in basename(path)) and (exists(join(path, makeOwnerFileName())))) mps = OvmStoragePool()._getAllMountPoints() for mountPoint in mps: runningPool = join(mountPoint, 'running_pool') if not exists(runningPool): logger.debug( OvmHost.getAllVms, "Primary storage %s not existing, skip it. this should be first getAllVms() called from Ovm resource configure" % runningPool) continue for dir in os.listdir(runningPool): vmDir = join(runningPool, dir) if not isMyVmDirLink(vmDir): logger.debug( OvmHost.getAllVms, "%s is not our vm directory, skip it" % vmDir) continue if vms.has_key(dir): logger.debug( OvmHost.getAllVms, "%s is already in running list, skip it" % dir) continue logger.debug( OvmHost.getAllVms, "Found a stopped vm %s on primary storage %s, report it to management server" % (dir, mountPoint)) vms[dir] = "DOWN" try: l = OvmHost()._getAllDomains() dct = {} host = OvmHost() for name, id in l: try: vmPath = host._getVmPathFromPrimaryStorage(name) vmStatus = db_get_vm(vmPath) dct[name] = vmStatus['status'] except Exception, e: logger.debug( OvmHost.getAllVms, "Cannot find link for %s on primary storage, treat it as Error" % name) dct[name] = 'ERROR' scanStoppedVmOnPrimaryStorage(dct) rs = toGson(dct) logger.info(OvmHost.getAllVms, rs) return rs
def getDetails(vmName): try: vm = OvmVm() try: OvmHost()._getDomainIdByName(vmName) vmPath = OvmHost()._vmNameToPath(vmName) vifsFromConfig = False except NoVmFoundException, e: vmPath = OvmHost()._getVmPathFromPrimaryStorage(vmName) vifsFromConfig = True if not isdir(vmPath): # The case is, when vm starting was not completed at primaryStroageDownload or createVolume(e.g. mgmt server stop), the mgmt # server will keep vm state in staring, then a stop command will be sent. The stop command will delete bridges that vm attaches, # by retriving birdge info by OvmVm.getDetails(). In this case, the vm doesn't exists, so returns a fake object here. fakeDisk = OvmDisk() vm.rootDisk = fakeDisk else: if vifsFromConfig: vm.vifs.extend(vm._getVifsFromConfig(vmPath)) else: vm.vifs.extend(vm._getVifs(vmName)) safeSetAttr(vm, 'name', vmName) disks = successToMap(xen_get_vdisks(vmPath))['vdisks'].split(',') rootDisk = None #BUG: there is no way to get type of disk, assume all are "w" for d in disks: if vmName in d: rootDisk = OvmDisk() safeSetAttr(rootDisk, 'path', d) safeSetAttr(rootDisk, 'type', "w") continue disk = OvmDisk() safeSetAttr(disk, 'path', d) safeSetAttr(disk, 'type', "w") vm.disks.append(disk) if not rootDisk: raise Exception("Cannot find root disk for vm %s"%vmName) safeSetAttr(vm, 'rootDisk', rootDisk) vcpus = int(successToMap(xen_get_vcpus(vmPath))['vcpus']) safeSetAttr(vm, 'cpuNum', vcpus) memory = MtoBytes(int(successToMap(xen_get_memory(vmPath))['memory'])) safeSetAttr(vm, 'memory', memory) vmStatus = db_get_vm(vmPath) safeSetAttr(vm, 'powerState', vmStatus['status']) vmType = successToMap(xen_get_vm_type(vmPath))['type'].replace('hvm', 'HVM').replace('para', 'PV') safeSetAttr(vm, 'type', vmType) rs = fromOvmVm(vm) logger.info(OvmVm.getDetails, rs) return rs
def getAllVms(): try: l = OvmHost()._getAllDomains() dct = {} host = OvmHost() for name, id in l: vmPath = host._getVmPathFromPrimaryStorage(name) vmStatus = db_get_vm(vmPath) dct[name] = vmStatus['status'] rs = toGson(dct) logger.info(OvmHost.getAllVms, rs) return rs except Exception, e: errmsg = fmt_err_msg(e) logger.error(OvmHost.getAllVms, errmsg) raise XmlRpcFault(toErrCode(OvmHost, OvmHost.getAllVms), errmsg)
def getAllVms(): def scanStoppedVmOnPrimaryStorage(vms): def isMyVmDirLink(path): return ( islink(path) and exists(join(path, "vm.cfg")) and ("-" in basename(path)) and (exists(join(path, makeOwnerFileName()))) ) mps = OvmStoragePool()._getAllMountPoints() for mountPoint in mps: runningPool = join(mountPoint, "running_pool") for dir in os.listdir(runningPool): vmDir = join(runningPool, dir) if not isMyVmDirLink(vmDir): logger.debug(OvmHost.getAllVms, "%s is not our vm directory, skip it" % vmDir) continue if vms.has_key(dir): logger.debug(OvmHost.getAllVms, "%s is already in running list, skip it" % dir) continue logger.debug( OvmHost.getAllVms, "Found a stopped vm %s on primary storage %s, report it to management server" % (dir, mountPoint), ) vms[dir] = "DOWN" try: l = OvmHost()._getAllDomains() dct = {} host = OvmHost() for name, id in l: vmPath = host._getVmPathFromPrimaryStorage(name) vmStatus = db_get_vm(vmPath) dct[name] = vmStatus["status"] scanStoppedVmOnPrimaryStorage(dct) rs = toGson(dct) logger.info(OvmHost.getAllVms, rs) return rs except Exception, e: errmsg = fmt_err_msg(e) logger.error(OvmHost.getAllVms, errmsg) raise XmlRpcFault(toErrCode(OvmHost, OvmHost.getAllVms), errmsg)
def getAllVms(): def scanStoppedVmOnPrimaryStorage(vms): def isMyVmDirLink(path): return (islink(path) and exists(join(path, 'vm.cfg')) and ('-' in basename(path)) and (exists(join(path, makeOwnerFileName())))) mps = OvmStoragePool()._getAllMountPoints() for mountPoint in mps: runningPool = join(mountPoint, 'running_pool') if not exists(runningPool): logger.debug(OvmHost.getAllVms, "Primary storage %s not existing, skip it. this should be first getAllVms() called from Ovm resource configure"%runningPool) continue for dir in os.listdir(runningPool): vmDir = join(runningPool, dir) if not isMyVmDirLink(vmDir): logger.debug(OvmHost.getAllVms, "%s is not our vm directory, skip it"%vmDir) continue if vms.has_key(dir): logger.debug(OvmHost.getAllVms, "%s is already in running list, skip it"%dir) continue logger.debug(OvmHost.getAllVms, "Found a stopped vm %s on primary storage %s, report it to management server" % (dir, mountPoint)) vms[dir] = "DOWN" try: l = OvmHost()._getAllDomains() dct = {} host = OvmHost() for name, id in l: try: vmPath = host._getVmPathFromPrimaryStorage(name) vmStatus = db_get_vm(vmPath) dct[name] = vmStatus['status'] except Exception, e: logger.debug(OvmHost.getAllVms, "Cannot find link for %s on primary storage, treat it as Error"%name) dct[name] = 'ERROR' scanStoppedVmOnPrimaryStorage(dct) rs = toGson(dct) logger.info(OvmHost.getAllVms, rs) return rs