Merge "xenapi: Remove dead code, moves, tests"
This commit is contained in:
commit
d6b41ffd41
@ -20,9 +20,47 @@ from nova.virt.xenapi import volumeops
|
||||
|
||||
|
||||
class VolumeAttachTestCase(test.TestCase):
|
||||
def test_detach_volume_call(self):
|
||||
ops = volumeops.VolumeOps('session')
|
||||
self.mox.StubOutWithMock(volumeops.vm_utils, 'vm_ref_or_raise')
|
||||
self.mox.StubOutWithMock(volumeops.vm_utils, 'find_vbd_by_number')
|
||||
self.mox.StubOutWithMock(volumeops.vm_utils, '_is_vm_shutdown')
|
||||
self.mox.StubOutWithMock(volumeops.vm_utils, 'unplug_vbd')
|
||||
self.mox.StubOutWithMock(volumeops.vm_utils, 'destroy_vbd')
|
||||
self.mox.StubOutWithMock(volumeops.volume_utils, 'get_device_number')
|
||||
self.mox.StubOutWithMock(volumeops.volume_utils, 'find_sr_from_vbd')
|
||||
self.mox.StubOutWithMock(volumeops.volume_utils, 'purge_sr')
|
||||
|
||||
volumeops.vm_utils.vm_ref_or_raise('session', 'instance_1').AndReturn(
|
||||
'vmref')
|
||||
|
||||
volumeops.volume_utils.get_device_number('mountpoint').AndReturn(
|
||||
'devnumber')
|
||||
|
||||
volumeops.vm_utils.find_vbd_by_number(
|
||||
'session', 'vmref', 'devnumber').AndReturn('vbdref')
|
||||
|
||||
volumeops.vm_utils._is_vm_shutdown('session', 'vmref').AndReturn(
|
||||
False)
|
||||
|
||||
volumeops.vm_utils.unplug_vbd('session', 'vbdref')
|
||||
|
||||
volumeops.vm_utils.destroy_vbd('session', 'vbdref')
|
||||
|
||||
volumeops.volume_utils.find_sr_from_vbd(
|
||||
'session', 'vbdref').AndReturn('srref')
|
||||
|
||||
volumeops.volume_utils.purge_sr('session', 'srref')
|
||||
|
||||
self.mox.ReplayAll()
|
||||
|
||||
ops.detach_volume(
|
||||
dict(driver_volume_type='iscsi', data='conn_data'),
|
||||
'instance_1', 'mountpoint')
|
||||
|
||||
def test_attach_volume_call(self):
|
||||
ops = volumeops.VolumeOps('session')
|
||||
self.mox.StubOutWithMock(ops, 'connect_volume')
|
||||
self.mox.StubOutWithMock(ops, '_connect_volume')
|
||||
self.mox.StubOutWithMock(volumeops.vm_utils, 'vm_ref_or_raise')
|
||||
self.mox.StubOutWithMock(volumeops.volume_utils, 'get_device_number')
|
||||
|
||||
@ -32,7 +70,7 @@ class VolumeAttachTestCase(test.TestCase):
|
||||
volumeops.volume_utils.get_device_number('mountpoint').AndReturn(
|
||||
'devnumber')
|
||||
|
||||
ops.connect_volume(
|
||||
ops._connect_volume(
|
||||
'conn_data', 'devnumber', 'instance_1', 'vmref', hotplug=True)
|
||||
|
||||
self.mox.ReplayAll()
|
||||
@ -42,7 +80,7 @@ class VolumeAttachTestCase(test.TestCase):
|
||||
|
||||
def test_attach_volume_no_hotplug(self):
|
||||
ops = volumeops.VolumeOps('session')
|
||||
self.mox.StubOutWithMock(ops, 'connect_volume')
|
||||
self.mox.StubOutWithMock(ops, '_connect_volume')
|
||||
self.mox.StubOutWithMock(volumeops.vm_utils, 'vm_ref_or_raise')
|
||||
self.mox.StubOutWithMock(volumeops.volume_utils, 'get_device_number')
|
||||
|
||||
@ -52,7 +90,7 @@ class VolumeAttachTestCase(test.TestCase):
|
||||
volumeops.volume_utils.get_device_number('mountpoint').AndReturn(
|
||||
'devnumber')
|
||||
|
||||
ops.connect_volume(
|
||||
ops._connect_volume(
|
||||
'conn_data', 'devnumber', 'instance_1', 'vmref', hotplug=False)
|
||||
|
||||
self.mox.ReplayAll()
|
||||
@ -85,7 +123,8 @@ class VolumeAttachTestCase(test.TestCase):
|
||||
self.stubs.Set(ops._session, 'call_xenapi', fake_call_xenapi)
|
||||
|
||||
self.mox.StubOutWithMock(volumeops.volume_utils, 'parse_sr_info')
|
||||
self.mox.StubOutWithMock(ops, 'introduce_sr')
|
||||
self.mox.StubOutWithMock(
|
||||
volumeops.volume_utils, 'introduce_sr_unless_present')
|
||||
self.mox.StubOutWithMock(volumeops.volume_utils, 'introduce_vdi')
|
||||
self.mox.StubOutWithMock(volumeops.vm_utils, 'create_vbd')
|
||||
|
||||
@ -93,7 +132,8 @@ class VolumeAttachTestCase(test.TestCase):
|
||||
connection_data, sr_label).AndReturn(
|
||||
tuple([sr_uuid, sr_label, sr_params]))
|
||||
|
||||
ops.introduce_sr(sr_uuid, sr_label, sr_params).AndReturn(sr_ref)
|
||||
volumeops.volume_utils.introduce_sr_unless_present(
|
||||
session, sr_uuid, sr_label, sr_params).AndReturn(sr_ref)
|
||||
|
||||
volumeops.volume_utils.introduce_vdi(
|
||||
session, sr_ref, vdi_uuid, None).AndReturn(vdi_ref)
|
||||
@ -104,7 +144,7 @@ class VolumeAttachTestCase(test.TestCase):
|
||||
|
||||
self.mox.ReplayAll()
|
||||
|
||||
ops.connect_volume(connection_data, dev_number, instance_name,
|
||||
ops._connect_volume(connection_data, dev_number, instance_name,
|
||||
vm_ref, hotplug=False)
|
||||
|
||||
self.assertEquals(False, called['xenapi'])
|
||||
|
@ -22,6 +22,7 @@ and storage repositories
|
||||
import re
|
||||
import string
|
||||
|
||||
from nova import exception
|
||||
from nova.openstack.common import cfg
|
||||
from nova.openstack.common import log as logging
|
||||
|
||||
@ -381,3 +382,28 @@ def _get_target_port(iscsi_string):
|
||||
return iscsi_string[iscsi_string.find(':') + 1:]
|
||||
elif iscsi_string is None or CONF.target_port:
|
||||
return CONF.target_port
|
||||
|
||||
|
||||
def introduce_sr_unless_present(session, sr_uuid, label, params):
|
||||
LOG.debug(_("Introducing SR %s") % label)
|
||||
sr_ref = find_sr_by_uuid(session, sr_uuid)
|
||||
if sr_ref:
|
||||
LOG.debug(_('SR found in xapi database. No need to introduce'))
|
||||
return sr_ref
|
||||
sr_ref = introduce_sr(session, sr_uuid, label, params)
|
||||
|
||||
if sr_ref is None:
|
||||
raise exception.NovaException(_('Could not introduce SR'))
|
||||
return sr_ref
|
||||
|
||||
|
||||
def forget_sr_if_present(session, sr_uuid):
|
||||
sr_ref = find_sr_by_uuid(session, sr_uuid)
|
||||
if sr_ref is None:
|
||||
LOG.debug(_('SR %s not found in the xapi database') % sr_uuid)
|
||||
return
|
||||
try:
|
||||
forget_sr(session, sr_uuid)
|
||||
except StorageError, exc:
|
||||
LOG.exception(exc)
|
||||
raise exception.NovaException(_('Could not forget SR'))
|
||||
|
@ -35,76 +35,6 @@ class VolumeOps(object):
|
||||
def __init__(self, session):
|
||||
self._session = session
|
||||
|
||||
def create_volume_for_sm(self, volume, sr_uuid):
|
||||
LOG.debug("Creating volume for Storage Manager")
|
||||
|
||||
sm_vol_rec = {}
|
||||
try:
|
||||
sr_ref = self._session.call_xenapi("SR.get_by_uuid", sr_uuid)
|
||||
except self._session.XenAPI.Failure, exc:
|
||||
LOG.exception(exc)
|
||||
raise volume_utils.StorageError(_('Unable to get SR using uuid'))
|
||||
#Create VDI
|
||||
label = 'vol-' + volume['id']
|
||||
desc = 'xensm volume for ' + volume['id']
|
||||
# size presented to xenapi is in bytes, while euca api is in GB
|
||||
vdi_size = volume['size'] * 1024 * 1024 * 1024
|
||||
vdi_ref = vm_utils.create_vdi(self._session, sr_ref,
|
||||
None, label, desc,
|
||||
vdi_size, False)
|
||||
vdi_rec = self._session.call_xenapi("VDI.get_record", vdi_ref)
|
||||
sm_vol_rec['vdi_uuid'] = vdi_rec['uuid']
|
||||
return sm_vol_rec
|
||||
|
||||
def delete_volume_for_sm(self, vdi_uuid):
|
||||
vdi_ref = self._session.call_xenapi("VDI.get_by_uuid", vdi_uuid)
|
||||
if vdi_ref is None:
|
||||
raise exception.NovaException(_('Could not find VDI ref'))
|
||||
|
||||
vm_utils.destroy_vdi(self._session, vdi_ref)
|
||||
|
||||
def create_sr(self, label, params):
|
||||
LOG.debug(_("Creating SR %s") % label)
|
||||
sr_ref = volume_utils.create_sr(self._session, label, params)
|
||||
if sr_ref is None:
|
||||
raise exception.NovaException(_('Could not create SR'))
|
||||
sr_rec = self._session.call_xenapi("SR.get_record", sr_ref)
|
||||
if sr_rec is None:
|
||||
raise exception.NovaException(_('Could not retrieve SR record'))
|
||||
return sr_rec['uuid']
|
||||
|
||||
# Checks if sr has already been introduced to this host
|
||||
def introduce_sr(self, sr_uuid, label, params):
|
||||
LOG.debug(_("Introducing SR %s") % label)
|
||||
sr_ref = volume_utils.find_sr_by_uuid(self._session, sr_uuid)
|
||||
if sr_ref:
|
||||
LOG.debug(_('SR found in xapi database. No need to introduce'))
|
||||
return sr_ref
|
||||
sr_ref = volume_utils.introduce_sr(self._session, sr_uuid, label,
|
||||
params)
|
||||
if sr_ref is None:
|
||||
raise exception.NovaException(_('Could not introduce SR'))
|
||||
return sr_ref
|
||||
|
||||
def is_sr_on_host(self, sr_uuid):
|
||||
LOG.debug(_('Checking for SR %s') % sr_uuid)
|
||||
sr_ref = volume_utils.find_sr_by_uuid(self._session, sr_uuid)
|
||||
if sr_ref:
|
||||
return True
|
||||
return False
|
||||
|
||||
# Checks if sr has been introduced
|
||||
def forget_sr(self, sr_uuid):
|
||||
sr_ref = volume_utils.find_sr_by_uuid(self._session, sr_uuid)
|
||||
if sr_ref is None:
|
||||
LOG.INFO(_('SR %s not found in the xapi database') % sr_uuid)
|
||||
return
|
||||
try:
|
||||
volume_utils.forget_sr(self._session, sr_uuid)
|
||||
except volume_utils.StorageError, exc:
|
||||
LOG.exception(exc)
|
||||
raise exception.NovaException(_('Could not forget SR'))
|
||||
|
||||
def attach_volume(self, connection_info, instance_name, mountpoint,
|
||||
hotplug=True):
|
||||
"""Attach volume storage to VM instance."""
|
||||
@ -122,13 +52,13 @@ class VolumeOps(object):
|
||||
connection_data = connection_info['data']
|
||||
dev_number = volume_utils.get_device_number(mountpoint)
|
||||
|
||||
self.connect_volume(connection_data, dev_number, instance_name,
|
||||
self._connect_volume(connection_data, dev_number, instance_name,
|
||||
vm_ref, hotplug=hotplug)
|
||||
|
||||
LOG.info(_('Mountpoint %(mountpoint)s attached to'
|
||||
' instance %(instance_name)s') % locals())
|
||||
|
||||
def connect_volume(self, connection_data, dev_number, instance_name,
|
||||
def _connect_volume(self, connection_data, dev_number, instance_name,
|
||||
vm_ref, hotplug=True):
|
||||
|
||||
description = 'Disk-for:%s' % instance_name
|
||||
@ -137,7 +67,8 @@ class VolumeOps(object):
|
||||
|
||||
# Introduce SR
|
||||
try:
|
||||
sr_ref = self.introduce_sr(uuid, label, sr_params)
|
||||
sr_ref = volume_utils.introduce_sr_unless_present(
|
||||
self._session, uuid, label, sr_params)
|
||||
LOG.debug(_('Introduced %(label)s as %(sr_ref)s.') % locals())
|
||||
except self._session.XenAPI.Failure, exc:
|
||||
LOG.exception(exc)
|
||||
@ -159,7 +90,7 @@ class VolumeOps(object):
|
||||
vdi_uuid, target_lun)
|
||||
except volume_utils.StorageError, exc:
|
||||
LOG.exception(exc)
|
||||
self.forget_sr(uuid)
|
||||
volume_utils.forget_sr_if_present(self._session, uuid)
|
||||
raise Exception(_('Unable to create VDI on SR %(sr_ref)s for'
|
||||
' instance %(instance_name)s') % locals())
|
||||
|
||||
@ -169,7 +100,7 @@ class VolumeOps(object):
|
||||
osvol=True)
|
||||
except self._session.XenAPI.Failure, exc:
|
||||
LOG.exception(exc)
|
||||
self.forget_sr(uuid)
|
||||
volume_utils.forget_sr_if_present(self._session, uuid)
|
||||
raise Exception(_('Unable to use SR %(sr_ref)s for'
|
||||
' instance %(instance_name)s') % locals())
|
||||
|
||||
@ -178,7 +109,7 @@ class VolumeOps(object):
|
||||
self._session.call_xenapi("VBD.plug", vbd_ref)
|
||||
except self._session.XenAPI.Failure, exc:
|
||||
LOG.exception(exc)
|
||||
self.forget_sr(uuid)
|
||||
volume_utils.forget_sr_if_present(self._session, uuid)
|
||||
raise Exception(_('Unable to attach volume to instance %s')
|
||||
% instance_name)
|
||||
|
||||
@ -190,7 +121,7 @@ class VolumeOps(object):
|
||||
# Detach VBD from VM
|
||||
LOG.debug(_("Detach_volume: %(instance_name)s, %(mountpoint)s")
|
||||
% locals())
|
||||
device_number = volume_utils.mountpoint_to_number(mountpoint)
|
||||
device_number = volume_utils.get_device_number(mountpoint)
|
||||
try:
|
||||
vbd_ref = vm_utils.find_vbd_by_number(self._session, vm_ref,
|
||||
device_number)
|
||||
@ -199,9 +130,7 @@ class VolumeOps(object):
|
||||
raise Exception(_('Unable to locate volume %s') % mountpoint)
|
||||
|
||||
try:
|
||||
vm_rec = self._session.call_xenapi("VM.get_record", vm_ref)
|
||||
sr_ref = volume_utils.find_sr_from_vbd(self._session, vbd_ref)
|
||||
if vm_rec['power_state'] != 'Halted':
|
||||
if not vm_utils._is_vm_shutdown(self._session, vm_ref):
|
||||
vm_utils.unplug_vbd(self._session, vbd_ref)
|
||||
except volume_utils.StorageError, exc:
|
||||
LOG.exception(exc)
|
||||
@ -214,6 +143,7 @@ class VolumeOps(object):
|
||||
|
||||
# Forget SR only if no other volumes on this host are using it
|
||||
try:
|
||||
sr_ref = volume_utils.find_sr_from_vbd(self._session, vbd_ref)
|
||||
volume_utils.purge_sr(self._session, sr_ref)
|
||||
except volume_utils.StorageError, exc:
|
||||
LOG.exception(exc)
|
||||
|
Loading…
x
Reference in New Issue
Block a user