Merge "Convert evacuation code to use objects"
This commit is contained in:
@@ -490,8 +490,7 @@ class ComputeManager(manager.SchedulerDependentManager):
|
||||
'trying to set it to ERROR'),
|
||||
instance_uuid=instance.uuid)
|
||||
|
||||
def _get_instances_on_driver(self, context, filters=None,
|
||||
columns_to_join=None):
|
||||
def _get_instances_on_driver(self, context, filters=None):
|
||||
"""Return a list of instance records for the instances found
|
||||
on the hypervisor which satisfy the specified filters. If filters=None
|
||||
return a list of instance records for all the instances found on the
|
||||
@@ -502,8 +501,8 @@ class ComputeManager(manager.SchedulerDependentManager):
|
||||
try:
|
||||
driver_uuids = self.driver.list_instance_uuids()
|
||||
filters['uuid'] = driver_uuids
|
||||
local_instances = self.conductor_api.instance_get_all_by_filters(
|
||||
context, filters, columns_to_join=columns_to_join)
|
||||
local_instances = instance_obj.InstanceList.get_by_filters(
|
||||
context, filters)
|
||||
return local_instances
|
||||
except NotImplementedError:
|
||||
pass
|
||||
@@ -511,9 +510,8 @@ class ComputeManager(manager.SchedulerDependentManager):
|
||||
# The driver doesn't support uuids listing, so we'll have
|
||||
# to brute force.
|
||||
driver_instances = self.driver.list_instances()
|
||||
instances = self.conductor_api.instance_get_all_by_filters(
|
||||
context, filters, columns_to_join=columns_to_join)
|
||||
name_map = dict((instance['name'], instance) for instance in instances)
|
||||
instances = instance_obj.InstanceList.get_by_filters(context, filters)
|
||||
name_map = dict((instance.name, instance) for instance in instances)
|
||||
local_instances = []
|
||||
for driver_instance in driver_instances:
|
||||
instance = name_map.get(driver_instance)
|
||||
@@ -534,12 +532,11 @@ class ComputeManager(manager.SchedulerDependentManager):
|
||||
filters = {'deleted': False}
|
||||
local_instances = self._get_instances_on_driver(context, filters)
|
||||
for instance in local_instances:
|
||||
instance_host = instance['host']
|
||||
if instance_host != our_host:
|
||||
if instance.host != our_host:
|
||||
LOG.info(_('Deleting instance as its host ('
|
||||
'%(instance_host)s) is not equal to our '
|
||||
'host (%(our_host)s).'),
|
||||
{'instance_host': instance_host,
|
||||
{'instance_host': instance.host,
|
||||
'our_host': our_host}, instance=instance)
|
||||
destroy_disks = False
|
||||
try:
|
||||
@@ -570,7 +567,7 @@ class ComputeManager(manager.SchedulerDependentManager):
|
||||
if data:
|
||||
shared_storage = (self.compute_rpcapi.
|
||||
check_instance_shared_storage(context,
|
||||
instance,
|
||||
obj_base.obj_to_primitive(instance),
|
||||
data))
|
||||
except NotImplementedError:
|
||||
LOG.warning(_('Hypervisor driver does not support '
|
||||
|
||||
@@ -5311,16 +5311,17 @@ class ComputeTestCase(BaseTestCase):
|
||||
# instances in central db
|
||||
instances = [
|
||||
# those are still related to this host
|
||||
jsonutils.to_primitive(self._create_fake_instance(
|
||||
{'host': self.compute.host})),
|
||||
jsonutils.to_primitive(self._create_fake_instance(
|
||||
{'host': self.compute.host})),
|
||||
jsonutils.to_primitive(self._create_fake_instance(
|
||||
{'host': self.compute.host}))
|
||||
]
|
||||
self._create_fake_instance_obj(
|
||||
{'host': self.compute.host}),
|
||||
self._create_fake_instance_obj(
|
||||
{'host': self.compute.host}),
|
||||
self._create_fake_instance_obj(
|
||||
{'host': self.compute.host})
|
||||
]
|
||||
|
||||
# those are already been evacuated to other host
|
||||
evacuated_instance = self._create_fake_instance({'host': 'otherhost'})
|
||||
evacuated_instance = self._create_fake_instance_obj(
|
||||
{'host': 'otherhost'})
|
||||
|
||||
instances.append(evacuated_instance)
|
||||
|
||||
@@ -5357,16 +5358,17 @@ class ComputeTestCase(BaseTestCase):
|
||||
# instances in central db
|
||||
instances = [
|
||||
# those are still related to this host
|
||||
jsonutils.to_primitive(self._create_fake_instance(
|
||||
{'host': self.compute.host})),
|
||||
jsonutils.to_primitive(self._create_fake_instance(
|
||||
{'host': self.compute.host})),
|
||||
jsonutils.to_primitive(self._create_fake_instance(
|
||||
{'host': self.compute.host}))
|
||||
self._create_fake_instance_obj(
|
||||
{'host': self.compute.host}),
|
||||
self._create_fake_instance(
|
||||
{'host': self.compute.host}),
|
||||
self._create_fake_instance(
|
||||
{'host': self.compute.host})
|
||||
]
|
||||
|
||||
# those are already been evacuated to other host
|
||||
evacuated_instance = self._create_fake_instance({'host': 'otherhost'})
|
||||
evacuated_instance = self._create_fake_instance_obj(
|
||||
{'host': 'otherhost'})
|
||||
|
||||
instances.append(evacuated_instance)
|
||||
|
||||
@@ -5394,7 +5396,7 @@ class ComputeTestCase(BaseTestCase):
|
||||
self.compute.driver.check_instance_shared_storage_local(fake_context,
|
||||
evacuated_instance).AndReturn({'filename': 'tmpfilename'})
|
||||
self.compute.compute_rpcapi.check_instance_shared_storage(fake_context,
|
||||
evacuated_instance,
|
||||
obj_base.obj_to_primitive(evacuated_instance),
|
||||
{'filename': 'tmpfilename'}).AndReturn(False)
|
||||
self.compute.driver.check_instance_shared_storage_cleanup(fake_context,
|
||||
{'filename': 'tmpfilename'})
|
||||
@@ -5412,16 +5414,17 @@ class ComputeTestCase(BaseTestCase):
|
||||
# instances in central db
|
||||
instances = [
|
||||
# those are still related to this host
|
||||
jsonutils.to_primitive(self._create_fake_instance(
|
||||
{'host': self.compute.host})),
|
||||
jsonutils.to_primitive(self._create_fake_instance(
|
||||
{'host': self.compute.host})),
|
||||
jsonutils.to_primitive(self._create_fake_instance(
|
||||
{'host': self.compute.host}))
|
||||
self._create_fake_instance_obj(
|
||||
{'host': self.compute.host}),
|
||||
self._create_fake_instance_obj(
|
||||
{'host': self.compute.host}),
|
||||
self._create_fake_instance_obj(
|
||||
{'host': self.compute.host})
|
||||
]
|
||||
|
||||
# those are already been evacuated to other host
|
||||
evacuated_instance = self._create_fake_instance({'host': 'otherhost'})
|
||||
evacuated_instance = self._create_fake_instance_obj(
|
||||
{'host': 'otherhost'})
|
||||
|
||||
instances.append(evacuated_instance)
|
||||
|
||||
|
||||
@@ -208,11 +208,8 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase):
|
||||
not_our_host = 'not-' + our_host
|
||||
fake_context = 'fake-context'
|
||||
|
||||
deleted_instance = {
|
||||
'name': 'fake-name',
|
||||
'host': not_our_host,
|
||||
'uuid': 'fake-uuid',
|
||||
}
|
||||
deleted_instance = instance_obj.Instance(host=not_our_host,
|
||||
uuid='fake-uuid')
|
||||
|
||||
self.mox.StubOutWithMock(self.compute.driver, 'init_host')
|
||||
self.mox.StubOutWithMock(self.compute.driver, 'destroy')
|
||||
@@ -336,28 +333,27 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase):
|
||||
|
||||
driver_instances = []
|
||||
for x in xrange(10):
|
||||
instance = dict(uuid=uuidutils.generate_uuid())
|
||||
driver_instances.append(instance)
|
||||
driver_instances.append(fake_instance.fake_db_instance())
|
||||
|
||||
self.mox.StubOutWithMock(self.compute.driver,
|
||||
'list_instance_uuids')
|
||||
self.mox.StubOutWithMock(self.compute.conductor_api,
|
||||
'instance_get_all_by_filters')
|
||||
self.mox.StubOutWithMock(db, 'instance_get_all_by_filters')
|
||||
|
||||
self.compute.driver.list_instance_uuids().AndReturn(
|
||||
[inst['uuid'] for inst in driver_instances])
|
||||
self.compute.conductor_api.instance_get_all_by_filters(
|
||||
db.instance_get_all_by_filters(
|
||||
fake_context,
|
||||
{'uuid': [inst['uuid'] for
|
||||
inst in driver_instances]},
|
||||
columns_to_join=[]).AndReturn(
|
||||
'created_at', 'desc', columns_to_join=None,
|
||||
limit=None, marker=None).AndReturn(
|
||||
driver_instances)
|
||||
|
||||
self.mox.ReplayAll()
|
||||
|
||||
result = self.compute._get_instances_on_driver(fake_context,
|
||||
columns_to_join=[])
|
||||
self.assertEqual(driver_instances, result)
|
||||
result = self.compute._get_instances_on_driver(fake_context)
|
||||
self.assertEqual([x['uuid'] for x in driver_instances],
|
||||
[x['uuid'] for x in result])
|
||||
|
||||
def test_get_instances_on_driver_fallback(self):
|
||||
# Test getting instances when driver doesn't support
|
||||
@@ -366,10 +362,13 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase):
|
||||
filters = {'host': self.compute.host}
|
||||
fake_context = context.get_admin_context()
|
||||
|
||||
self.flags(instance_name_template='inst-%i')
|
||||
|
||||
all_instances = []
|
||||
driver_instances = []
|
||||
for x in xrange(10):
|
||||
instance = dict(name=uuidutils.generate_uuid())
|
||||
instance = fake_instance.fake_db_instance(name='inst-%i' % x,
|
||||
id=x)
|
||||
if x % 2:
|
||||
driver_instances.append(instance)
|
||||
all_instances.append(instance)
|
||||
@@ -378,21 +377,22 @@ class ComputeManagerUnitTestCase(test.NoDBTestCase):
|
||||
'list_instance_uuids')
|
||||
self.mox.StubOutWithMock(self.compute.driver,
|
||||
'list_instances')
|
||||
self.mox.StubOutWithMock(self.compute.conductor_api,
|
||||
'instance_get_all_by_filters')
|
||||
self.mox.StubOutWithMock(db, 'instance_get_all_by_filters')
|
||||
|
||||
self.compute.driver.list_instance_uuids().AndRaise(
|
||||
NotImplementedError())
|
||||
self.compute.driver.list_instances().AndReturn(
|
||||
[inst['name'] for inst in driver_instances])
|
||||
self.compute.conductor_api.instance_get_all_by_filters(
|
||||
db.instance_get_all_by_filters(
|
||||
fake_context, filters,
|
||||
columns_to_join=None).AndReturn(all_instances)
|
||||
'created_at', 'desc', columns_to_join=None,
|
||||
limit=None, marker=None).AndReturn(all_instances)
|
||||
|
||||
self.mox.ReplayAll()
|
||||
|
||||
result = self.compute._get_instances_on_driver(fake_context, filters)
|
||||
self.assertEqual(driver_instances, result)
|
||||
self.assertEqual([x['uuid'] for x in driver_instances],
|
||||
[x['uuid'] for x in result])
|
||||
|
||||
def test_instance_usage_audit(self):
|
||||
instances = [{'uuid': 'foo'}]
|
||||
|
||||
Reference in New Issue
Block a user