本文整理汇总了Python中ovs.extensions.hypervisor.factory.Factory.get方法的典型用法代码示例。如果您正苦于以下问题:Python Factory.get方法的具体用法?Python Factory.get怎么用?Python Factory.get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ovs.extensions.hypervisor.factory.Factory
的用法示例。
在下文中一共展示了Factory.get方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: sync_with_hypervisor
# 需要导入模块: from ovs.extensions.hypervisor.factory import Factory [as 别名]
# 或者: from ovs.extensions.hypervisor.factory.Factory import get [as 别名]
def sync_with_hypervisor(vmachineguid, storagedriver_id=None):
"""
Updates a given vmachine with data retrieved from a given pmachine
:param vmachineguid: Guid of the virtual machine
:param storagedriver_id: Storage Driver hosting the vmachine
"""
try:
vmachine = VMachine(vmachineguid)
except Exception as ex:
VMachineController._logger.info('Cannot get VMachine object: {0}'.format(str(ex)))
raise
vm_object = None
if vmachine.pmachine.mgmtcenter and storagedriver_id is not None and vmachine.devicename is not None:
try:
mgmt_center = Factory.get_mgmtcenter(vmachine.pmachine)
storagedriver = StorageDriverList.get_by_storagedriver_id(storagedriver_id)
VMachineController._logger.info('Syncing vMachine (name {0}) with Management center {1}'.format(vmachine.name, vmachine.pmachine.mgmtcenter.name))
vm_object = mgmt_center.get_vm_agnostic_object(devicename=vmachine.devicename,
ip=storagedriver.storage_ip,
mountpoint=storagedriver.mountpoint)
except Exception as ex:
VMachineController._logger.info('Error while fetching vMachine info from management center: {0}'.format(str(ex)))
if vm_object is None and storagedriver_id is None and vmachine.hypervisor_id is not None and vmachine.pmachine is not None:
try:
# Only the vmachine was received, so base the sync on hypervisor id and pmachine
hypervisor = Factory.get(vmachine.pmachine)
VMachineController._logger.info('Syncing vMachine (name {0})'.format(vmachine.name))
vm_object = hypervisor.get_vm_agnostic_object(vmid=vmachine.hypervisor_id)
except Exception as ex:
VMachineController._logger.info('Error while fetching vMachine info from hypervisor: {0}'.format(str(ex)))
if vm_object is None and storagedriver_id is not None and vmachine.devicename is not None:
try:
# Storage Driver id was given, using the devicename instead (to allow hypervisor id updates
# which can be caused by re-adding a vm to the inventory)
pmachine = PMachineList.get_by_storagedriver_id(storagedriver_id)
storagedriver = StorageDriverList.get_by_storagedriver_id(storagedriver_id)
hypervisor = Factory.get(pmachine)
if not hypervisor.file_exists(storagedriver, hypervisor.clean_vmachine_filename(vmachine.devicename)):
return
vmachine.pmachine = pmachine
vmachine.save()
VMachineController._logger.info('Syncing vMachine (device {0}, ip {1}, mountpoint {2})'.format(vmachine.devicename,
storagedriver.storage_ip,
storagedriver.mountpoint))
vm_object = hypervisor.get_vm_object_by_devicename(devicename=vmachine.devicename,
ip=storagedriver.storage_ip,
mountpoint=storagedriver.mountpoint)
except Exception as ex:
VMachineController._logger.info('Error while fetching vMachine info from hypervisor using devicename: {0}'.format(str(ex)))
if vm_object is None:
message = 'Not enough information to sync vmachine'
VMachineController._logger.info('Error: {0}'.format(message))
raise RuntimeError(message)
VMachineController.update_vmachine_config(vmachine, vm_object)
示例2: create_new
# 需要导入模块: from ovs.extensions.hypervisor.factory import Factory [as 别名]
# 或者: from ovs.extensions.hypervisor.factory.Factory import get [as 别名]
def create_new(diskname, size, storagedriver_guid):
"""
Create a new vdisk/volume using filesystem calls
:param diskname: name of the disk
:param size: size of the disk (GB)
:param storagedriver_guid: guid of the Storagedriver
:return: guid of the new disk
"""
logger.info('Creating new empty disk {0} of {1} GB'.format(diskname, size))
storagedriver = StorageDriver(storagedriver_guid)
vp_mountpoint = storagedriver.mountpoint
hypervisor = Factory.get(storagedriver.storagerouter.pmachine)
disk_path = hypervisor.clean_backing_disk_filename(hypervisor.get_disk_path(None, diskname))
location = os.path.join(vp_mountpoint, disk_path)
VDiskController.create_volume(location, size)
backoff = 1
timeout = 30 # seconds
start = time.time()
while time.time() < start + timeout:
vdisk = VDiskList.get_by_devicename_and_vpool(disk_path, storagedriver.vpool)
if vdisk is None:
logger.debug('Waiting for disk to be picked up by voldrv')
time.sleep(backoff)
backoff += 1
else:
return vdisk.guid
raise RuntimeError('Disk {0} was not created in {1} seconds.'.format(diskname, timeout))
示例3: delete_from_voldrv
# 需要导入模块: from ovs.extensions.hypervisor.factory import Factory [as 别名]
# 或者: from ovs.extensions.hypervisor.factory.Factory import get [as 别名]
def delete_from_voldrv(volumename, storagedriver_id):
"""
Delete a disk
Triggered by volumedriver messages on the queue
@param volumename: volume id of the disk
"""
_ = storagedriver_id # For logging purposes
disk = VDiskList.get_vdisk_by_volume_id(volumename)
if disk is not None:
mutex = VolatileMutex('{}_{}'.format(volumename, disk.devicename))
try:
mutex.acquire(wait=20)
pmachine = None
try:
pmachine = PMachineList.get_by_storagedriver_id(disk.storagedriver_id)
except RuntimeError as ex:
if 'could not be found' not in str(ex):
raise
# else: pmachine can't be loaded, because the volumedriver doesn't know about it anymore
if pmachine is not None:
limit = 5
hypervisor = Factory.get(pmachine)
exists = hypervisor.file_exists(disk.vpool, disk.devicename)
while limit > 0 and exists is True:
time.sleep(1)
exists = hypervisor.file_exists(disk.vpool, disk.devicename)
limit -= 1
if exists is True:
logger.info('Disk {0} still exists, ignoring delete'.format(disk.devicename))
return
logger.info('Delete disk {}'.format(disk.name))
disk.delete()
finally:
mutex.release()
示例4: resize_from_voldrv
# 需要导入模块: from ovs.extensions.hypervisor.factory import Factory [as 别名]
# 或者: from ovs.extensions.hypervisor.factory.Factory import get [as 别名]
def resize_from_voldrv(volumename, volumesize, volumepath, storagedriver_id):
"""
Resize a disk
Triggered by volumedriver messages on the queue
@param volumepath: path on hypervisor to the volume
@param volumename: volume id of the disk
@param volumesize: size of the volume
"""
pmachine = PMachineList.get_by_storagedriver_id(storagedriver_id)
storagedriver = StorageDriverList.get_by_storagedriver_id(storagedriver_id)
hypervisor = Factory.get(pmachine)
volumepath = hypervisor.clean_backing_disk_filename(volumepath)
mutex = VolatileMutex('{}_{}'.format(volumename, volumepath))
try:
mutex.acquire(wait=30)
disk = VDiskList.get_vdisk_by_volume_id(volumename)
if disk is None:
disk = VDiskList.get_by_devicename_and_vpool(volumepath, storagedriver.vpool)
if disk is None:
disk = VDisk()
finally:
mutex.release()
disk.devicename = volumepath
disk.volume_id = volumename
disk.size = volumesize
disk.vpool = storagedriver.vpool
disk.save()
VDiskController.sync_with_mgmtcenter(disk, pmachine, storagedriver)
MDSServiceController.ensure_safety(disk)
示例5: clone
# 需要导入模块: from ovs.extensions.hypervisor.factory import Factory [as 别名]
# 或者: from ovs.extensions.hypervisor.factory.Factory import get [as 别名]
def clone(diskguid, snapshotid, devicename, pmachineguid, machinename, machineguid=None):
"""
Clone a disk
"""
pmachine = PMachine(pmachineguid)
hypervisor = Factory.get(pmachine)
description = '{} {}'.format(machinename, devicename)
properties_to_clone = ['description', 'size', 'type', 'retentionpolicyguid',
'snapshotpolicyguid', 'autobackup']
vdisk = VDisk(diskguid)
location = hypervisor.get_backing_disk_path(machinename, devicename)
new_vdisk = VDisk()
new_vdisk.copy(vdisk, include=properties_to_clone)
new_vdisk.parent_vdisk = vdisk
new_vdisk.name = '{0}-clone'.format(vdisk.name)
new_vdisk.description = description
new_vdisk.devicename = hypervisor.clean_backing_disk_filename(location)
new_vdisk.parentsnapshot = snapshotid
new_vdisk.vmachine = VMachine(machineguid) if machineguid else vdisk.vmachine
new_vdisk.vpool = vdisk.vpool
new_vdisk.save()
try:
storagedriver = StorageDriverList.get_by_storagedriver_id(vdisk.storagedriver_id)
if storagedriver is None:
raise RuntimeError('Could not find StorageDriver with id {0}'.format(vdisk.storagedriver_id))
mds_service = MDSServiceController.get_preferred_mds(storagedriver.storagerouter, vdisk.vpool)
if mds_service is None:
raise RuntimeError('Could not find a MDS service')
logger.info('Clone snapshot {} of disk {} to location {}'.format(snapshotid, vdisk.name, location))
volume_id = vdisk.storagedriver_client.create_clone(
target_path=location,
metadata_backend_config=MDSMetaDataBackendConfig([MDSNodeConfig(address=str(mds_service.service.storagerouter.ip),
port=mds_service.service.ports[0])]),
parent_volume_id=str(vdisk.volume_id),
parent_snapshot_id=str(snapshotid),
node_id=str(vdisk.storagedriver_id)
)
except Exception as ex:
logger.error('Caught exception during clone, trying to delete the volume. {0}'.format(ex))
new_vdisk.delete()
VDiskController.delete_volume(location)
raise
new_vdisk.volume_id = volume_id
new_vdisk.save()
try:
MDSServiceController.ensure_safety(new_vdisk)
except Exception as ex:
logger.error('Caught exception during "ensure_safety" {0}'.format(ex))
return {'diskguid': new_vdisk.guid,
'name': new_vdisk.name,
'backingdevice': location}
示例6: sync_with_hypervisor
# 需要导入模块: from ovs.extensions.hypervisor.factory import Factory [as 别名]
# 或者: from ovs.extensions.hypervisor.factory.Factory import get [as 别名]
def sync_with_hypervisor(vmachineguid, storagedriver_id=None):
"""
Updates a given vmachine with data retreived from a given pmachine
"""
try:
vmachine = VMachine(vmachineguid)
if storagedriver_id is None and vmachine.hypervisor_id is not None and vmachine.pmachine is not None:
# Only the vmachine was received, so base the sync on hypervisorid and pmachine
hypervisor = Factory.get(vmachine.pmachine)
logger.info('Syncing vMachine (name {})'.format(vmachine.name))
vm_object = hypervisor.get_vm_agnostic_object(vmid=vmachine.hypervisor_id)
elif storagedriver_id is not None and vmachine.devicename is not None:
# Storage Driver id was given, using the devicename instead (to allow hypervisorid updates
# which can be caused by re-adding a vm to the inventory)
pmachine = PMachineList.get_by_storagedriver_id(storagedriver_id)
storagedriver = StorageDriverList.get_by_storagedriver_id(storagedriver_id)
hypervisor = Factory.get(pmachine)
if not hypervisor.file_exists(vmachine.vpool, hypervisor.clean_vmachine_filename(vmachine.devicename)):
return
vmachine.pmachine = pmachine
vmachine.save()
logger.info('Syncing vMachine (device {}, ip {}, mtpt {})'.format(vmachine.devicename,
storagedriver.storage_ip,
storagedriver.mountpoint))
vm_object = hypervisor.get_vm_object_by_devicename(devicename=vmachine.devicename,
ip=storagedriver.storage_ip,
mountpoint=storagedriver.mountpoint)
else:
message = 'Not enough information to sync vmachine'
logger.info('Error: {0}'.format(message))
raise RuntimeError(message)
except Exception as ex:
logger.info('Error while fetching vMachine info: {0}'.format(str(ex)))
raise
if vm_object is None:
message = 'Could not retreive hypervisor vmachine object'
logger.info('Error: {0}'.format(message))
raise RuntimeError(message)
else:
VMachineController.update_vmachine_config(vmachine, vm_object)
示例7: create_from_template
# 需要导入模块: from ovs.extensions.hypervisor.factory import Factory [as 别名]
# 或者: from ovs.extensions.hypervisor.factory.Factory import get [as 别名]
def create_from_template(diskguid, machinename, devicename, pmachineguid, machineguid=None, storagedriver_guid=None):
"""
Create a disk from a template
@param parentdiskguid: guid of the disk
@param location: location where virtual device should be created (eg: myVM)
@param devicename: device file name for the disk (eg: mydisk-flat.vmdk)
@param machineguid: guid of the machine to assign disk to
@return diskguid: guid of new disk
"""
pmachine = PMachine(pmachineguid)
hypervisor = Factory.get(pmachine)
disk_path = hypervisor.get_disk_path(machinename, devicename)
description = '{} {}'.format(machinename, devicename)
properties_to_clone = [
'description', 'size', 'type', 'retentionpolicyid',
'snapshotpolicyid', 'vmachine', 'vpool']
disk = VDisk(diskguid)
if disk.vmachine and not disk.vmachine.is_vtemplate:
# Disk might not be attached to a vmachine, but still be a template
raise RuntimeError('The given disk does not belong to a template')
if storagedriver_guid is not None:
storagedriver_id = StorageDriver(storagedriver_guid).storagedriver_id
else:
storagedriver_id = disk.storagedriver_id
new_disk = VDisk()
new_disk.copy(disk, include=properties_to_clone)
new_disk.vpool = disk.vpool
new_disk.devicename = hypervisor.clean_backing_disk_filename(disk_path)
new_disk.parent_vdisk = disk
new_disk.name = '{}-clone'.format(disk.name)
new_disk.description = description
new_disk.vmachine = VMachine(machineguid) if machineguid else disk.vmachine
new_disk.save()
logger.info('Create disk from template {} to new disk {} to location {}'.format(
disk.name, new_disk.name, disk_path
))
try:
volume_id = disk.storagedriver_client.create_clone_from_template(disk_path, str(disk.volume_id), node_id=str(storagedriver_id))
new_disk.volume_id = volume_id
new_disk.save()
except Exception as ex:
logger.error('Clone disk on volumedriver level failed with exception: {0}'.format(str(ex)))
new_disk.delete()
raise
return {'diskguid': new_disk.guid, 'name': new_disk.name,
'backingdevice': disk_path}
示例8: _hypervisor_status
# 需要导入模块: from ovs.extensions.hypervisor.factory import Factory [as 别名]
# 或者: from ovs.extensions.hypervisor.factory.Factory import get [as 别名]
def _hypervisor_status(self):
"""
Fetches the Status of the vMachine.
"""
if self.hypervisor_id is None or self.pmachine is None:
return 'UNKNOWN'
hv = hvFactory.get(self.pmachine)
try:
return hv.get_state(self.hypervisor_id)
except:
return 'UNKNOWN'
示例9: clone
# 需要导入模块: from ovs.extensions.hypervisor.factory import Factory [as 别名]
# 或者: from ovs.extensions.hypervisor.factory.Factory import get [as 别名]
def clone(machineguid, timestamp, name):
"""
Clone a vmachine using the disk snapshot based on a snapshot timestamp
@param machineguid: guid of the machine to clone
@param timestamp: timestamp of the disk snapshots to use for the clone
@param name: name for the new machine
"""
machine = VMachine(machineguid)
disks = {}
for snapshot in machine.snapshots:
if snapshot['timestamp'] == timestamp:
for diskguid, snapshotguid in snapshot['snapshots'].iteritems():
disks[diskguid] = snapshotguid
new_machine = VMachine()
new_machine.copy(machine)
new_machine.name = name
new_machine.pmachine = machine.pmachine
new_machine.save()
new_disk_guids = []
disks_by_order = sorted(machine.vdisks, key=lambda x: x.order)
for currentDisk in disks_by_order:
if machine.is_vtemplate and currentDisk.templatesnapshot:
snapshotid = currentDisk.templatesnapshot
else:
snapshotid = disks[currentDisk.guid]
prefix = '%s-clone' % currentDisk.name
result = VDiskController.clone(diskguid=currentDisk.guid,
snapshotid=snapshotid,
devicename=prefix,
pmachineguid=new_machine.pmachine_guid,
machinename=new_machine.name,
machineguid=new_machine.guid)
new_disk_guids.append(result['diskguid'])
hv = Factory.get(machine.pmachine)
try:
result = hv.clone_vm(machine.hypervisor_id, name, disks, None, True)
except:
VMachineController.delete(machineguid=new_machine.guid)
raise
new_machine.hypervisor_id = result
new_machine.save()
return new_machine.guid
示例10: sync_with_hypervisor
# 需要导入模块: from ovs.extensions.hypervisor.factory import Factory [as 别名]
# 或者: from ovs.extensions.hypervisor.factory.Factory import get [as 别名]
def sync_with_hypervisor(vpool_guid):
"""
Syncs all vMachines of a given vPool with the hypervisor
"""
vpool = VPool(vpool_guid)
for storagedriver in vpool.storagedrivers:
pmachine = storagedriver.storagerouter.pmachine
hypervisor = Factory.get(pmachine)
for vm_object in hypervisor.get_vms_by_nfs_mountinfo(storagedriver.storage_ip, storagedriver.mountpoint):
search_vpool = None if pmachine.hvtype == 'KVM' else vpool
vmachine = VMachineList.get_by_devicename_and_vpool(
devicename=vm_object['backing']['filename'],
vpool=search_vpool
)
VMachineController.update_vmachine_config(vmachine, vm_object, pmachine)
示例11: sync_with_hypervisor
# 需要导入模块: from ovs.extensions.hypervisor.factory import Factory [as 别名]
# 或者: from ovs.extensions.hypervisor.factory.Factory import get [as 别名]
def sync_with_hypervisor(vpool_guid):
"""
Syncs all vMachines of a given vPool with the hypervisor
:param vpool_guid: Guid of the vPool to synchronize
"""
vpool = VPool(vpool_guid)
if vpool.status != VPool.STATUSES.RUNNING:
raise ValueError('Synchronizing with hypervisor is only allowed if your vPool is in {0} status'.format(VPool.STATUSES.RUNNING))
for storagedriver in vpool.storagedrivers:
pmachine = storagedriver.storagerouter.pmachine
hypervisor = Factory.get(pmachine)
for vm_object in hypervisor.get_vms_by_nfs_mountinfo(storagedriver.storage_ip, storagedriver.mountpoint):
search_vpool = None if pmachine.hvtype == 'KVM' else vpool
vmachine = VMachineList.get_by_devicename_and_vpool(devicename=vm_object['backing']['filename'],
vpool=search_vpool)
VMachineController.update_vmachine_config(vmachine, vm_object, pmachine)
示例12: clone
# 需要导入模块: from ovs.extensions.hypervisor.factory import Factory [as 别名]
# 或者: from ovs.extensions.hypervisor.factory.Factory import get [as 别名]
def clone(diskguid, snapshotid, devicename, pmachineguid, machinename, machineguid=None):
"""
Clone a disk
"""
pmachine = PMachine(pmachineguid)
hypervisor = Factory.get(pmachine)
description = "{} {}".format(machinename, devicename)
properties_to_clone = ["description", "size", "type", "retentionpolicyguid", "snapshotpolicyguid", "autobackup"]
vdisk = VDisk(diskguid)
location = hypervisor.get_backing_disk_path(machinename, devicename)
new_vdisk = VDisk()
new_vdisk.copy(vdisk, include=properties_to_clone)
new_vdisk.parent_vdisk = vdisk
new_vdisk.name = "{0}-clone".format(vdisk.name)
new_vdisk.description = description
new_vdisk.devicename = hypervisor.clean_backing_disk_filename(location)
new_vdisk.parentsnapshot = snapshotid
new_vdisk.vmachine = VMachine(machineguid) if machineguid else vdisk.vmachine
new_vdisk.vpool = vdisk.vpool
new_vdisk.save()
storagedriver = StorageDriverList.get_by_storagedriver_id(vdisk.storagedriver_id)
if storagedriver is None:
raise RuntimeError("Could not find StorageDriver with id {0}".format(vdisk.storagedriver_id))
mds_service = MDSServiceController.get_preferred_mds(storagedriver.storagerouter, vdisk.vpool)
if mds_service is None:
raise RuntimeError("Could not find a MDS service")
logger.info("Clone snapshot {} of disk {} to location {}".format(snapshotid, vdisk.name, location))
volume_id = vdisk.storagedriver_client.create_clone(
target_path=location,
metadata_backend_config=MDSMetaDataBackendConfig(
[MDSNodeConfig(address=str(mds_service.service.storagerouter.ip), port=mds_service.service.ports[0])]
),
parent_volume_id=str(vdisk.volume_id),
parent_snapshot_id=str(snapshotid),
node_id=str(vdisk.storagedriver_id),
)
new_vdisk.volume_id = volume_id
new_vdisk.save()
MDSServiceController.ensure_safety(new_vdisk)
return {"diskguid": new_vdisk.guid, "name": new_vdisk.name, "backingdevice": location}
示例13: rename_from_voldrv
# 需要导入模块: from ovs.extensions.hypervisor.factory import Factory [as 别名]
# 或者: from ovs.extensions.hypervisor.factory.Factory import get [as 别名]
def rename_from_voldrv(volumename, volume_old_path, volume_new_path, storagedriver_id):
"""
Rename a disk
Triggered by volumedriver messages
@param volumename: volume id of the disk
@param volume_old_path: old path on hypervisor to the volume
@param volume_new_path: new path on hypervisor to the volume
"""
pmachine = PMachineList.get_by_storagedriver_id(storagedriver_id)
hypervisor = Factory.get(pmachine)
volume_old_path = hypervisor.clean_backing_disk_filename(volume_old_path)
volume_new_path = hypervisor.clean_backing_disk_filename(volume_new_path)
disk = VDiskList.get_vdisk_by_volume_id(volumename)
if disk:
logger.info("Move disk {} from {} to {}".format(disk.name, volume_old_path, volume_new_path))
disk.devicename = volume_new_path
disk.save()
示例14: sync_with_mgmtcenter
# 需要导入模块: from ovs.extensions.hypervisor.factory import Factory [as 别名]
# 或者: from ovs.extensions.hypervisor.factory.Factory import get [as 别名]
def sync_with_mgmtcenter(disk, pmachine, storagedriver):
"""
Update disk info using management center (if available)
If no management center, try with hypervisor
If no info retrieved, use devicename
@param disk: vDisk hybrid (vdisk to be updated)
@param pmachine: pmachine hybrid (pmachine running the storagedriver)
@param storagedriver: storagedriver hybrid (storagedriver serving the vdisk)
"""
disk_name = None
if pmachine.mgmtcenter is not None:
logger.debug('Sync vdisk {0} with management center {1} on storagedriver {2}'.format(disk.name, pmachine.mgmtcenter.name, storagedriver.name))
mgmt = Factory.get_mgmtcenter(mgmt_center = pmachine.mgmtcenter)
volumepath = disk.devicename
mountpoint = storagedriver.mountpoint
devicepath = '{0}/{1}'.format(mountpoint, volumepath)
try:
disk_mgmt_center_info = mgmt.get_vdisk_model_by_devicepath(devicepath)
if disk_mgmt_center_info is not None:
disk_name = disk_mgmt_center_info.get('name')
except Exception as ex:
logger.error('Failed to sync vdisk {0} with mgmt center {1}. {2}'.format(disk.name, pmachine.mgmtcenter.name, str(ex)))
if disk_name is None:
logger.info('Sync vdisk with hypervisor on {0}'.format(pmachine.name))
try:
hv = Factory.get(pmachine)
info = hv.get_vm_agnostic_object(disk.vmachine.hypervisor_id)
for disk in info['disks']:
if disk['filename'] == disk.devicename:
disk_name = disk['name']
break
except Exception as ex:
logger.error('Failed to get vdisk info from hypervisor. %s' % ex)
if disk_name is None:
logger.info('No info retrieved from hypervisor, using devicename')
disk_name = disk.devicename.split('/')[-1].split('.')[0]
if disk_name is not None:
disk.name = disk_name
disk.save()
示例15: rename_from_voldrv
# 需要导入模块: from ovs.extensions.hypervisor.factory import Factory [as 别名]
# 或者: from ovs.extensions.hypervisor.factory.Factory import get [as 别名]
def rename_from_voldrv(old_name, new_name, storagedriver_id):
"""
This machine will handle the rename of a vmx file
:param old_name: Old name of vmx
:param new_name: New name for the vmx
:param storagedriver_id: Storage Driver hosting the vmachine
"""
pmachine = PMachineList.get_by_storagedriver_id(storagedriver_id)
if pmachine.hvtype not in ['VMWARE', 'KVM']:
return
hypervisor = Factory.get(pmachine)
if pmachine.hvtype == 'VMWARE':
storagedriver = StorageDriverList.get_by_storagedriver_id(storagedriver_id)
vpool = storagedriver.vpool
else:
vpool = None
old_name = hypervisor.clean_vmachine_filename(old_name)
new_name = hypervisor.clean_vmachine_filename(new_name)
scenario = hypervisor.get_rename_scenario(old_name, new_name)
if scenario == 'RENAME':
# Most likely a change from path. Updating path
vm = VMachineList.get_by_devicename_and_vpool(old_name, vpool)
if vm is not None:
vm.devicename = new_name
vm.save()
elif scenario == 'UPDATE':
vm = VMachineList.get_by_devicename_and_vpool(new_name, vpool)
if vm is None:
# The vMachine doesn't seem to exist, so it's likely the create didn't came trough
# Let's create it anyway
VMachineController.update_from_voldrv(new_name, storagedriver_id=storagedriver_id)
vm = VMachineList.get_by_devicename_and_vpool(new_name, vpool)
if vm is None:
raise RuntimeError('Could not create vMachine on rename. Aborting.')
try:
VMachineController.sync_with_hypervisor(vm.guid, storagedriver_id=storagedriver_id)
vm.status = 'SYNC'
except:
vm.status = 'SYNC_NOK'
vm.save()