Skip to content

Commit

Permalink
Vmwareapi: Remove instance from logs
Browse files Browse the repository at this point in the history
The resource_id is now set automatically, so no need
to set it explicitly.

Change-Id: Ia2b9ed0167a6420b7441401c5f4614b2ce305962
  • Loading branch information
fwiesel committed Jan 25, 2022
1 parent 9670f44 commit bbb5870
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 225 deletions.
1 change: 1 addition & 0 deletions nova/tests/unit/compute/test_compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -7882,6 +7882,7 @@ def test_init_instance_for_partial_deletion(self):
admin_context = context.get_admin_context()
instance = objects.Instance(admin_context)
instance.id = 1
instance.uuid = uuids.fake
instance.vm_state = vm_states.DELETED
instance.deleted = False
instance.host = self.compute.host
Expand Down
27 changes: 23 additions & 4 deletions nova/tests/unit/compute/test_compute_mgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@
fake_host_list = [mock.sentinel.host1]


class _PropertiesChanged(object):
def __init__(self, obj, properties):
self._obj = obj
self._properties = properties

def __eq__(self, other):
if self._obj is not other:
return False
try:
for key, value in six.iteritems(self._properties):
if getattr(other, key) != value:
return False
return True
except KeyError:
return False


@ddt.ddt
class ComputeManagerUnitTestCase(test.NoDBTestCase):
def setUp(self):
Expand Down Expand Up @@ -1193,7 +1210,7 @@ def test_init_instance_failed_resume_sets_error(self, mock_set_inst,
power_state.SHUTDOWN)
mock_get_inst.return_value = 'fake-bdm'
mock_resume.side_effect = test.TestingException
self.compute._init_instance('fake-context', instance)
self.compute._init_instance(self.context, instance)
mock_get_power.assert_has_calls([mock.call(mock.ANY, instance),
mock.call(mock.ANY, instance)])
mock_plug.assert_called_once_with(instance, mock.ANY)
Expand Down Expand Up @@ -1271,7 +1288,7 @@ def test_init_instance_complete_partial_deletion_raises_exception(
with mock.patch.object(self.compute,
'_complete_partial_deletion') as mock_deletion:
mock_deletion.side_effect = test.TestingException()
self.compute._init_instance(self, instance)
self.compute._init_instance(self.context, instance)
msg = u'Failed to complete a deletion'
mock_log.exception.assert_called_once_with(msg, instance=instance)

Expand Down Expand Up @@ -2148,8 +2165,10 @@ def save(self):
pass

def _fake_get(ctx, filter, expected_attrs, use_slave):
changed_ctx = _PropertiesChanged(ctx, {'read_deleted': 'yes'},)

mock_get.assert_called_once_with(
{'read_deleted': 'yes'},
changed_ctx,
{'deleted': True, 'soft_deleted': False, 'host': 'fake-mini',
'cleaned': False},
expected_attrs=['system_metadata'],
Expand All @@ -2163,7 +2182,7 @@ def _fake_get(ctx, filter, expected_attrs, use_slave):
mock_get.side_effect = _fake_get
mock_delete.side_effect = [True, False]

self.compute._run_pending_deletes({})
self.compute._run_pending_deletes(self.context)

self.assertFalse(a.cleaned)
self.assertEqual('100', a.system_metadata['clean_attempts'])
Expand Down
36 changes: 15 additions & 21 deletions nova/virt/vmwareapi/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ def get_console_output(self, context, instance):
path = os.path.join(CONF.vmware.serial_log_dir, fname)
if not os.path.exists(path):
LOG.warning('The console log is missing. Check your VSPC '
'configuration', instance=instance)
'configuration')
return b""
read_log_data, remaining = nova.privsep.path.last_bytes(
path, MAX_CONSOLE_BYTES)
Expand Down Expand Up @@ -526,8 +526,7 @@ def detach_volume(self, context, connection_info, instance, mountpoint,
"""Detach volume storage to VM instance."""
if not self._vmops.is_instance_in_resource_pool(instance):
LOG.debug("Not detaching %s, vm is in different cluster",
connection_info["volume_id"],
instance=instance)
connection_info["volume_id"])
return True
# NOTE(claudiub): if context parameter is to be used in the future,
# the _detach_instance_volumes method will have to be updated as well.
Expand Down Expand Up @@ -570,15 +569,13 @@ def _detach_instance_volumes(self, instance, block_device_info):
disk.get('device_name'))
except exception.DiskNotFound:
LOG.warning('The volume %s does not exist!',
disk.get('device_name'),
instance=instance)
disk.get('device_name'))
except Exception as e:
with excutils.save_and_reraise_exception():
LOG.error("Failed to detach %(device_name)s. "
"Exception: %(exc)s",
{'device_name': disk.get('device_name'),
'exc': e},
instance=instance)
'exc': e})

def destroy(self, context, instance, network_info, block_device_info=None,
destroy_disks=True):
Expand All @@ -602,8 +599,7 @@ def destroy(self, context, instance, network_info, block_device_info=None,
except (vexc.ManagedObjectNotFoundException,
exception.InstanceNotFound):
LOG.warning('Instance does not exists. Proceeding to '
'delete instance properties on datastore',
instance=instance)
'delete instance properties on datastore')
self._vmops.destroy(context, instance, destroy_disks)

def pause(self, instance):
Expand Down Expand Up @@ -849,7 +845,7 @@ def _pre_live_migration(self, context, instance, block_device_info,

if hasattr(result, 'drsFault'):
LOG.error("Placement Error: %s", vim_util.serialize_object(
result.drsFault), instance=instance)
result.drsFault))

if (not hasattr(result, 'recommendations') or
not result.recommendations):
Expand Down Expand Up @@ -904,12 +900,12 @@ def live_migration(self, context, instance, dest,
"""Live migration of an instance to another host."""
if not migrate_data:
LOG.error("live_migration() called without migration_data"
" - cannot continue operations", instance=instance)
" - cannot continue operations")
recover_method(context, instance, dest, migrate_data)
raise ValueError("Missing migrate_data")

if migrate_data.instance_already_migrated:
LOG.info("Recovering migration", instance=instance)
LOG.info("Recovering migration")
post_method(context, instance, dest, block_migration, migrate_data)
return

Expand All @@ -931,16 +927,15 @@ def live_migration(self, context, instance, dest,
required_volume_attributes)
self._set_vif_infos(migrate_data, dest_session)
self._vmops.live_migration(instance, migrate_data, volumes)
LOG.info("Migration operation completed", instance=instance)
LOG.info("Migration operation completed")
post_method(context, instance, dest, block_migration, migrate_data)
except Exception:
LOG.exception("Failed due to an exception", instance=instance)
LOG.exception("Failed due to an exception")
with excutils.save_and_reraise_exception():
# We are still in the task-state migrating, so cannot
# recover the DRS settings. We rely on the sync to do that
LOG.debug("Calling live migration recover_method "
"for instance: %s", instance["name"],
instance=instance)
"for instance: %s", instance["name"])
recover_method(context, instance, dest, migrate_data)

def _get_volume_mappings(self, context, instance):
Expand All @@ -965,7 +960,7 @@ def _get_volume_mappings(self, context, instance):
message = ("Could not parse connection_info for volume {}."
" Reason: {}"
).format(bdm.volume_id, e)
LOG.warning(message, instance=instance)
LOG.warning(message)

# Normalize the datastore reference
# As it depends on the caller, if actually need the
Expand Down Expand Up @@ -1017,7 +1012,7 @@ def rollback_live_migration_at_destination(self, context, instance,
migrate_data=None):
"""Clean up destination node after a failed live migration."""
LOG.info("rollback_live_migration_at_destination %s",
block_device_info, instance=instance)
block_device_info)
if not migrate_data.is_same_vcenter:
self._volumeops.delete_shadow_vms(block_device_info, instance)

Expand All @@ -1027,8 +1022,7 @@ def _error_out_instance_on_exception(self, instance, message):
yield
except Exception as error:
LOG.exception("Failed to %s, setting to ERROR state",
message,
instance=instance, error=error)
message, error=error)
instance.vm_state = vm_states.ERROR
instance.save()

Expand Down Expand Up @@ -1065,7 +1059,7 @@ def post_live_migration_at_destination(self, context, instance,
with self._error_out_instance_on_exception(instance,
"fixup shadow vms"):
volumes = self._get_volume_mappings(context, instance)
LOG.debug("Fixing shadow vms %s", volumes, instance=instance)
LOG.debug("Fixing shadow vms %s", volumes)
self._volumeops.fixup_shadow_vms(instance, volumes)

def ensure_filtering_rules_for_instance(self, instance, network_info):
Expand Down
27 changes: 10 additions & 17 deletions nova/virt/vmwareapi/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,7 @@ def image_transfer(read_handle, write_handle):


def upload_iso_to_datastore(iso_path, instance, **kwargs):
LOG.debug("Uploading iso %s to datastore", iso_path,
instance=instance)
LOG.debug("Uploading iso %s to datastore", iso_path)
with open(iso_path, 'r') as iso_file:
write_file_handle = rw_handles.FileWriteHandle(
kwargs.get("host"),
Expand All @@ -248,8 +247,7 @@ def upload_iso_to_datastore(iso_path, instance, **kwargs):
data = iso_file.read(block_size)
write_file_handle.close()

LOG.debug("Uploaded iso %s to datastore", iso_path,
instance=instance)
LOG.debug("Uploaded iso %s to datastore", iso_path)


def fetch_image(context, instance, host, port, dc_name, ds_name, file_path,
Expand All @@ -259,8 +257,7 @@ def fetch_image(context, instance, host, port, dc_name, ds_name, file_path,
LOG.debug("Downloading image file data %(image_ref)s to the "
"data store %(data_store_name)s",
{'image_ref': image_ref,
'data_store_name': ds_name},
instance=instance)
'data_store_name': ds_name})

metadata = IMAGE_API.get(context, image_ref)
file_size = int(metadata['size'])
Expand All @@ -274,8 +271,7 @@ def fetch_image(context, instance, host, port, dc_name, ds_name, file_path,
"%(data_store_name)s",
{'image_ref': image_ref,
'upload_name': 'n/a' if file_path is None else file_path,
'data_store_name': 'n/a' if ds_name is None else ds_name},
instance=instance)
'data_store_name': 'n/a' if ds_name is None else ds_name})


def _build_shadow_vm_config_spec(session, name, size_kb, disk_type, ds_name):
Expand Down Expand Up @@ -350,8 +346,7 @@ def fetch_image_stream_optimized(context, instance, session, vm_name,
image_ref = image_id if image_id else instance.image_ref
LOG.debug("Downloading image file data %(image_ref)s to the ESX "
"as VM named '%(vm_name)s'",
{'image_ref': image_ref, 'vm_name': vm_name},
instance=instance)
{'image_ref': image_ref, 'vm_name': vm_name})

metadata = IMAGE_API.get(context, image_ref)
file_size = int(metadata['size'])
Expand All @@ -367,7 +362,7 @@ def fetch_image_stream_optimized(context, instance, session, vm_name,
file_size)

LOG.info("Downloaded image file data %(image_ref)s",
{'image_ref': instance.image_ref}, instance=instance)
{'image_ref': instance.image_ref})
vmdk = vm_util.get_vmdk_info(session, imported_vm_ref, vm_name)
vm_util.mark_vm_as_template(session, instance, imported_vm_ref)
return vmdk.capacity_in_bytes, vmdk.path
Expand Down Expand Up @@ -487,8 +482,7 @@ def fetch_image_ova(context, instance, session, vm_name, ds_name,
image_ref = instance.image_ref
LOG.debug("Downloading OVA image file %(image_ref)s to the ESX "
"as VM named '%(vm_name)s'",
{'image_ref': image_ref, 'vm_name': vm_name},
instance=instance)
{'image_ref': image_ref, 'vm_name': vm_name})

metadata = IMAGE_API.get(context, image_ref)
file_size = int(metadata['size'])
Expand All @@ -515,7 +509,7 @@ def fetch_image_ova(context, instance, session, vm_name, ds_name,
file_size)

LOG.info("Downloaded OVA image file %(image_ref)s",
{'image_ref': instance.image_ref}, instance=instance)
{'image_ref': instance.image_ref})
vmdk = vm_util.get_vmdk_info(session,
imported_vm_ref,
vm_name)
Expand All @@ -529,7 +523,7 @@ def fetch_image_ova(context, instance, session, vm_name, ds_name,
def upload_image_stream_optimized(context, image_id, instance, session,
vm, vmdk_size):
"""Upload the snapshotted vm disk file to Glance image server."""
LOG.debug("Uploading image %s", image_id, instance=instance)
LOG.debug("Uploading image %s", image_id)
metadata = IMAGE_API.get(context, image_id)

read_handle = rw_handles.VmdkReadHandle(session,
Expand Down Expand Up @@ -560,5 +554,4 @@ def upload_image_stream_optimized(context, image_id, instance, session,
updater.stop()
read_handle.close()

LOG.debug("Uploaded image %s to the Glance image server", image_id,
instance=instance)
LOG.debug("Uploaded image %s to the Glance image server", image_id)
30 changes: 15 additions & 15 deletions nova/virt/vmwareapi/vm_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -1822,7 +1822,7 @@ def get_vmdk_adapter_type(adapter_type):

def create_vm(session, instance, vm_folder, config_spec, res_pool_ref):
"""Create VM on ESX host."""
LOG.debug("Creating VM on the ESX host", instance=instance)
LOG.debug("Creating VM on the ESX host")
vm_create_task = session._call_method(
session.vim,
"CreateVM_Task", vm_folder,
Expand All @@ -1844,19 +1844,19 @@ def create_vm(session, instance, vm_folder, config_spec, res_pool_ref):
'\'%(ostype)s\'. An invalid os type may be '
'one cause of this instance creation failure',
{'ostype': config_spec.guestId})
LOG.debug("Created VM on the ESX host", instance=instance)
LOG.debug("Created VM on the ESX host")
return task_info.result


@vm_ref_cache_heal_from_instance
def _destroy_vm(session, instance, vm_ref=None):
if not vm_ref:
vm_ref = get_vm_ref(session, instance)
LOG.debug("Destroying the VM", instance=instance)
LOG.debug("Destroying the VM")
destroy_task = session._call_method(session.vim, "Destroy_Task",
vm_ref)
session._wait_for_task(destroy_task)
LOG.info("Destroyed the VM", instance=instance)
LOG.info("Destroyed the VM")


def destroy_vm(session, instance, vm_ref=None):
Expand All @@ -1865,25 +1865,25 @@ def destroy_vm(session, instance, vm_ref=None):
return _destroy_vm(session, instance, vm_ref=vm_ref)
except vexc.VimFaultException as e:
with excutils.save_and_reraise_exception() as ctx:
LOG.exception(_('Destroy VM failed'), instance=instance)
LOG.exception(_('Destroy VM failed'))
# we need the `InvalidArgument` fault to bubble out of this
# function so it can be acted upon on higher levels
if 'InvalidArgument' not in e.fault_list:
ctx.reraise = False
except Exception:
LOG.exception(_('Destroy VM failed'), instance=instance)
LOG.exception(_('Destroy VM failed'))


def mark_vm_as_template(session, instance, vm_ref=None):
"""Mark a VM instance as template. Assumes VM is powered off."""
try:
if not vm_ref:
vm_ref = get_vm_ref(session, instance)
LOG.debug("Marking the VM as template", instance=instance)
LOG.debug("Marking the VM as template")
session._call_method(session.vim, "MarkAsTemplate", vm_ref)
LOG.info("Marked the VM as template", instance=instance)
LOG.info("Marked the VM as template")
except Exception:
LOG.exception(_('Mark VM as template failed'), instance=instance)
LOG.exception(_('Mark VM as template failed'))


def create_virtual_disk(session, dc_ref, adapter_type, disk_type,
Expand Down Expand Up @@ -1962,15 +1962,15 @@ def power_on_instance(session, instance, vm_ref=None):
if vm_ref is None:
vm_ref = get_vm_ref(session, instance)

LOG.debug("Powering on the VM", instance=instance)
LOG.debug("Powering on the VM")
try:
poweron_task = session._call_method(
session.vim,
"PowerOnVM_Task", vm_ref)
session._wait_for_task(poweron_task)
LOG.debug("Powered on the VM", instance=instance)
LOG.debug("Powered on the VM")
except vexc.InvalidPowerStateException:
LOG.debug("VM already powered on", instance=instance)
LOG.debug("VM already powered on")


def _get_vm_port_indices(session, vm_ref):
Expand Down Expand Up @@ -2023,14 +2023,14 @@ def power_off_instance(session, instance, vm_ref=None):
if vm_ref is None:
vm_ref = get_vm_ref(session, instance)

LOG.debug("Powering off the VM", instance=instance)
LOG.debug("Powering off the VM")
try:
poweroff_task = session._call_method(session.vim,
"PowerOffVM_Task", vm_ref)
session._wait_for_task(poweroff_task)
LOG.debug("Powered off the VM", instance=instance)
LOG.debug("Powered off the VM")
except vexc.InvalidPowerStateException:
LOG.debug("VM already powered off", instance=instance)
LOG.debug("VM already powered off")


def find_rescue_device(hardware_devices, instance):
Expand Down
Loading

0 comments on commit bbb5870

Please sign in to comment.