本文整理汇总了Python中ovs.lib.vdisk.VDiskController.clone方法的典型用法代码示例。如果您正苦于以下问题:Python VDiskController.clone方法的具体用法?Python VDiskController.clone怎么用?Python VDiskController.clone使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ovs.lib.vdisk.VDiskController
的用法示例。
在下文中一共展示了VDiskController.clone方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create_volume_from_snapshot
# 需要导入模块: from ovs.lib.vdisk import VDiskController [as 别名]
# 或者: from ovs.lib.vdisk.VDiskController import clone [as 别名]
def create_volume_from_snapshot(self, volume, snapshot):
"""Creates a volume from a snapshot.
Called on "cinder create --snapshot-id ..."
:param snapshot: snapshot reference (sqlalchemy Model)
:param volume: volume reference (sqlalchemy Model)
Volume here is just a ModelObject, it doesn't exist physically,
it will be created by OVS.
Diskguid to be passed to the clone method is the ovs diskguid of the
parent of the snapshot with snapshot.id
OVS: Clone from arbitrary volume,
requires volumedriver 3.6 release > 15.08.2014
"""
_debug_vol_info('CLONE_VOL', volume)
_debug_vol_info('CLONE_SNAP', snapshot)
mountpoint = self._get_hostname_mountpoint(str(volume.host))
ovs_snap_disk = self._find_ovs_model_disk_by_snapshot_id(snapshot.id)
devicename = volume.display_name
if not devicename:
devicename = volume.name
pmachineguid = self._find_ovs_model_pmachine_guid_by_hostname(
str(volume.host))
LOG.info('[CLONE FROM SNAP] %s %s %s %s'
% (ovs_snap_disk.guid, snapshot.id, devicename, pmachineguid))
try:
disk_meta = VDiskController.clone(diskguid = ovs_snap_disk.guid,
snapshotid = snapshot.id,
devicename = devicename,
pmachineguid = pmachineguid,
machinename = "",
machineguid=None)
volume['provider_location'] = '{}{}'.format(
mountpoint, disk_meta['backingdevice'])
LOG.debug('[CLONE FROM SNAP] Meta: %s' % str(disk_meta))
LOG.debug('[CLONE FROM SNAP] New volume %s'
% volume['provider_location'])
vdisk = VDisk(disk_meta['diskguid'])
vdisk.cinder_id = volume.id
vdisk.name = devicename
vdisk.save()
except Exception as ex:
LOG.error('CLONE FROM SNAP: Internal error %s ' % str(ex))
self.delete_volume(volume)
self.delete_snapshot(snapshot)
raise
return {'provider_location': volume['provider_location'],
'display_name': volume['display_name']}
示例2: clone
# 需要导入模块: from ovs.lib.vdisk import VDiskController [as 别名]
# 或者: from ovs.lib.vdisk.VDiskController import clone [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
示例3: create_cloned_volume
# 需要导入模块: from ovs.lib.vdisk import VDiskController [as 别名]
# 或者: from ovs.lib.vdisk.VDiskController import clone [as 别名]
def create_cloned_volume(self, volume, src_vref):
"""Create a cloned volume from another volume.
Called on "cinder create --source-volid ... "
:param volume: volume reference - target volume (sqlalchemy Model)
:param src_vref: volume reference - source volume (sqlalchemy Model)
OVS: Create clone from template if the source is a template
Create volume from snapshot if the source is a volume
- create snapshot of source volume if it doesn't have snapshots
"""
_debug_vol_info('CREATE_CLONED_VOL', volume)
_debug_vol_info('CREATE_CLONED_VOL Source', src_vref)
mountpoint = self._get_hostname_mountpoint(str(volume.host))
name = volume.display_name
if not name:
name = volume.name
volume.display_name = volume.name
pmachineguid = self._find_ovs_model_pmachine_guid_by_hostname(
str(volume.host))
#source
source_ovs_disk = self._find_ovs_model_disk_by_location(
str(src_vref.provider_location), src_vref.host)
if source_ovs_disk.info['object_type'] == 'TEMPLATE':
LOG.info('[CREATE_FROM_TEMPLATE] VDisk %s is a template'
% source_ovs_disk.devicename)
# cloning from a template
LOG.debug('[CREATE FROM TEMPLATE] ovs_disk %s '
% (source_ovs_disk.devicename))
disk_meta = VDiskController.create_from_template(
diskguid = source_ovs_disk.guid,
machinename = "",
devicename = str(name),
pmachineguid = pmachineguid,
machineguid = None,
storagedriver_guid = None)
volume['provider_location'] = '{}{}'.format(
mountpoint, disk_meta['backingdevice'])
LOG.debug('[CREATE FROM TEMPLATE] New volume %s'
% volume['provider_location'])
vdisk = VDisk(disk_meta['diskguid'])
vdisk.cinder_id = volume.id
vdisk.name = name
LOG.debug('[CREATE FROM TEMPLATE] Updating meta %s %s'
% (volume.id, name))
vdisk.save()
else:
LOG.info('[THIN CLONE] VDisk %s is not a template'
% source_ovs_disk.devicename)
# We do not support yet full volume clone
# - requires "emancipate" functionality
# So for now we'll take a snapshot
# (or the latest snapshot existing) and clone from that snapshot
available_snapshots = [snapshot for snapshot in source_ovs_disk.snapshots
if 'in_backend' not in snapshot or snapshot['in_backend'] is True]
if len(available_snapshots) == 0:
metadata = {'label': "Cinder clone snapshot {0}".format(name),
'is_consistent': False,
'timestamp': time.time(),
'machineguid': source_ovs_disk.vmachine_guid,
'is_automatic': False}
LOG.debug('CREATE_SNAP %s %s' % (name, str(metadata)))
snapshotid = VDiskController.create_snapshot(
diskguid = source_ovs_disk.guid,
metadata = metadata,
snapshotid = None)
LOG.debug('CREATE_SNAP OK')
OVSVolumeDriver._wait_for_snapshot(source_ovs_disk, snapshotid)
else:
snapshotid = available_snapshots[-1]['guid']
LOG.debug('[CREATE CLONE FROM SNAP] %s ' % snapshotid)
disk_meta = VDiskController.clone(diskguid = source_ovs_disk.guid,
snapshotid = snapshotid,
devicename = str(name),
pmachineguid = pmachineguid,
machinename = "",
machineguid=None)
volume['provider_location'] = '{}{}'.format(
mountpoint, disk_meta['backingdevice'])
LOG.debug('[CLONE FROM SNAP] Meta: %s' % str(disk_meta))
LOG.debug('[CLONE FROM SNAP] New volume %s'
% volume['provider_location'])
vdisk = VDisk(disk_meta['diskguid'])
vdisk.cinder_id = volume.id
vdisk.name = name
vdisk.save()
return {'provider_location': volume['provider_location'],
'display_name': volume['display_name']}
示例4: clone
# 需要导入模块: from ovs.lib.vdisk import VDiskController [as 别名]
# 或者: from ovs.lib.vdisk.VDiskController import clone [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)
timestamp = str(timestamp)
if timestamp not in (snap['timestamp'] for snap in machine.snapshots):
raise RuntimeError('Invalid timestamp provided, not a valid snapshot of this vmachine.')
vpool = None
storagerouter = None
if machine.pmachine is not None and machine.pmachine.hvtype == 'VMWARE':
for vdisk in machine.vdisks:
if vdisk.vpool is not None:
vpool = vdisk.vpool
break
for vdisk in machine.vdisks:
if vdisk.storagerouter_guid:
storagerouter = StorageRouter(vdisk.storagerouter_guid)
break
hv = Factory.get(machine.pmachine)
vm_path = hv.get_vmachine_path(name, storagerouter.machine_id if storagerouter is not None else '')
# mutex in sync_with_hypervisor uses "None" for KVM hvtype
mutex = volatile_mutex('{0}_{1}'.format(hv.clean_vmachine_filename(vm_path), vpool.guid if vpool is not None else 'none'))
disks = {}
for snapshot in machine.snapshots:
if snapshot['timestamp'] == timestamp:
for diskguid, snapshotguid in snapshot['snapshots'].iteritems():
disks[diskguid] = snapshotguid
try:
mutex.acquire(wait=120)
new_machine = VMachine()
new_machine.copy(machine)
new_machine.name = name
new_machine.devicename = hv.clean_vmachine_filename(vm_path)
new_machine.pmachine = machine.pmachine
new_machine.save()
finally:
mutex.release()
new_disk_guids = []
vm_disks = []
mountpoint = None
disks_by_order = sorted(machine.vdisks, key=lambda x: x.order)
try:
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'])
mountpoint = StorageDriverList.get_by_storagedriver_id(currentDisk.storagedriver_id).mountpoint
vm_disks.append(result)
except Exception as ex:
VMachineController._logger.error('Failed to clone disks. {0}'.format(ex))
VMachineController.delete(machineguid=new_machine.guid)
raise
try:
result = hv.clone_vm(machine.hypervisor_id, name, vm_disks, mountpoint)
except Exception as ex:
VMachineController._logger.error('Failed to clone vm. {0}'.format(ex))
VMachineController.delete(machineguid=new_machine.guid)
raise
try:
mutex.acquire(wait=120)
new_machine.hypervisor_id = result
new_machine.save()
finally:
mutex.release()
return new_machine.guid
示例5: test_clone
# 需要导入模块: from ovs.lib.vdisk import VDiskController [as 别名]
# 或者: from ovs.lib.vdisk.VDiskController import clone [as 别名]
def test_clone(self):
"""
Test the clone functionality
- Create a vDisk with name 'clone1'
- Clone the vDisk and make some assertions
- Attempt to clone again using same name and same devicename
- Attempt to clone on Storage Router which is not linked to the vPool on which the original vDisk is hosted
- Attempt to clone on Storage Driver without MDS service
- Attempt to clone from snapshot which is not yet completely synced to backend
- Attempt to delete the snapshot from which a clone was made
- Clone the vDisk on another Storage Router
- Clone another vDisk with name 'clone1' linked to another vPool
"""
structure = Helper.build_service_structure(
{'vpools': [1, 2],
'storagerouters': [1, 2, 3],
'storagedrivers': [(1, 1, 1), (2, 2, 1)], # (<id>, <vpool_id>, <storagerouter_id>)
'mds_services': [(1, 1), (2, 2)]} # (<id>, <storagedriver_id>)
)
vpools = structure['vpools']
mds_services = structure['mds_services']
service_type = structure['service_type']
storagedrivers = structure['storagedrivers']
storagerouters = structure['storagerouters']
self._roll_out_dtl_services(vpool=vpools[1], storagerouters=storagerouters)
self._roll_out_dtl_services(vpool=vpools[2], storagerouters=storagerouters)
# Basic clone scenario
vdisk1 = VDisk(VDiskController.create_new(volume_name='vdisk_1', volume_size=1024 ** 3, storagedriver_guid=storagedrivers[1].guid))
clone1_info = VDiskController.clone(vdisk_guid=vdisk1.guid,
name='clone1')
vdisks = VDiskList.get_vdisks()
self.assertTrue(expr=len(vdisks) == 2, msg='Expected to find 2 vDisks')
clones = VDiskList.get_by_parentsnapshot(vdisk1.snapshots[0]['guid'])
self.assertTrue(expr=len(clones) == 1, msg='Expected to find 1 vDisk with parent snapshot')
self.assertTrue(expr=len(vdisk1.child_vdisks) == 1, msg='Expected to find 1 child vDisk')
for expected_key in ['vdisk_guid', 'name', 'backingdevice']:
self.assertTrue(expr=expected_key in clone1_info, msg='Expected to find key "{0}" in clone_info'.format(expected_key))
self.assertTrue(expr=clones[0].guid == clone1_info['vdisk_guid'], msg='Guids do not match')
self.assertTrue(expr=clones[0].name == clone1_info['name'], msg='Names do not match')
self.assertTrue(expr=clones[0].devicename == clone1_info['backingdevice'], msg='Device names do not match')
# Attempt to clone again with same name
with self.assertRaises(RuntimeError):
VDiskController.clone(vdisk_guid=vdisk1.guid,
name='clone1')
vdisks = VDiskList.get_vdisks()
self.assertTrue(expr=len(vdisks) == 2, msg='Expected to find 2 vDisks after failed clone attempt 1')
# Attempt to clone again with a name which will have identical devicename
with self.assertRaises(RuntimeError):
VDiskController.clone(vdisk_guid=vdisk1.guid,
name='clone1%')
vdisks = VDiskList.get_vdisks()
self.assertTrue(expr=len(vdisks) == 2, msg='Expected to find 2 vDisks after failed clone attempt 2')
# Attempt to clone on Storage Router on which vPool is not extended
with self.assertRaises(RuntimeError):
VDiskController.clone(vdisk_guid=vdisk1.guid,
name='clone2',
storagerouter_guid=storagerouters[2].guid)
vdisks = VDiskList.get_vdisks()
self.assertTrue(expr=len(vdisks) == 2, msg='Expected to find 2 vDisks after failed clone attempt 3')
# Attempt to clone on non-existing Storage Driver
storagedrivers[1].storagedriver_id = 'non-existing'
storagedrivers[1].save()
with self.assertRaises(RuntimeError):
VDiskController.clone(vdisk_guid=vdisk1.guid,
name='clone2')
vdisks = VDiskList.get_vdisks()
self.assertTrue(expr=len(vdisks) == 2, msg='Expected to find 2 vDisks after failed clone attempt 4')
storagedrivers[1].storagedriver_id = '1'
storagedrivers[1].save()
# Attempt to clone on Storage Driver without MDS service
mds_services[1].service.storagerouter = storagerouters[3]
mds_services[1].service.save()
with self.assertRaises(RuntimeError):
VDiskController.clone(vdisk_guid=vdisk1.guid,
name='clone2')
vdisks = VDiskList.get_vdisks()
self.assertTrue(expr=len(vdisks) == 2, msg='Expected to find 2 vDisks after failed clone attempt 5')
mds_services[1].service.storagerouter = storagerouters[1]
mds_services[1].service.save()
# Attempt to clone by providing snapshot_id not synced to backend
self.assertTrue(expr=len(vdisk1.snapshots) == 1, msg='Expected to find only 1 snapshot before cloning')
metadata = {'label': 'label1',
'timestamp': int(time.time()),
'is_sticky': False,
'in_backend': False,
'is_automatic': True,
'is_consistent': True}
snapshot_id = VDiskController.create_snapshot(vdisk_guid=vdisk1.guid, metadata=metadata)
self.assertTrue(expr=len(vdisk1.snapshots) == 2, msg='Expected to find 2 snapshots')
with self.assertRaises(RuntimeError):
VDiskController.clone(vdisk_guid=vdisk1.guid,
#.........这里部分代码省略.........