def notify_about_instance_usage(notifier, context, instance, event_suffix, network_info=None, system_metadata=None, extra_usage_info=None): """ Send a notification about an instance. :param notifier: a messaging.Notifier :param event_suffix: Event type like "delete.start" or "exists" :param network_info: Networking information, if provided. :param system_metadata: system_metadata DB entries for the instance, if provided. :param extra_usage_info: Dictionary containing extra values to add or override in the notification. """ if not extra_usage_info: extra_usage_info = {} usage_info = notifications.info_from_instance(context, instance, network_info, system_metadata, **extra_usage_info) if event_suffix.endswith("error"): method = notifier.error else: method = notifier.info method(context, 'compute.instance.%s' % event_suffix, usage_info)
def notify_about_instance_usage(notifier, context, instance, event_suffix, network_info=None, extra_usage_info=None, fault=None): """Send an unversioned legacy notification about an instance. All new notifications should use notify_about_instance_action which sends a versioned notification. :param notifier: a messaging.Notifier :param event_suffix: Event type like "delete.start" or "exists" :param network_info: Networking information, if provided. :param extra_usage_info: Dictionary containing extra values to add or override in the notification. """ if not extra_usage_info: extra_usage_info = {} usage_info = notifications.info_from_instance(context, instance, network_info, **extra_usage_info) if fault: # NOTE(johngarbutt) mirrors the format in wrap_exception fault_payload = exception_to_dict(fault) LOG.debug(fault_payload["message"], instance=instance) usage_info.update(fault_payload) if event_suffix.endswith("error"): method = notifier.error else: method = notifier.info method(context, 'compute.instance.%s' % event_suffix, usage_info)
def notify_about_instance_usage(context, instance, event_suffix, network_info=None, system_metadata=None, extra_usage_info=None, host=None): """ Send a notification about an instance. :param event_suffix: Event type like "delete.start" or "exists" :param network_info: Networking information, if provided. :param system_metadata: system_metadata DB entries for the instance, if provided. :param extra_usage_info: Dictionary containing extra values to add or override in the notification. :param host: Compute host for the instance, if specified. Default is CONF.host """ if not host: host = CONF.host if not extra_usage_info: extra_usage_info = {} usage_info = notifications.info_from_instance(context, instance, network_info, system_metadata, **extra_usage_info) notifier_api.notify(context, 'compute.%s' % host, 'compute.instance.%s' % event_suffix, notifier_api.INFO, usage_info)
def notify(context, message): if message["event_type"] != "compute.instance.delete.start": LOG.debug("ignoring %s", message["event_type"]) return LOG.info("processing %s", message["event_type"]) gatherer = initialize_gatherer() instance_id = message["payload"]["instance_id"] LOG.debug("polling final stats for %r", instance_id) # Ask for the instance details instance_ref = instance_info_source.instance_get_by_uuid(context, instance_id) # Get the default notification payload payload = notifications.info_from_instance(context, instance_ref, None, None) # Extend the payload with samples from our plugins. We only need # to send some of the data from the counter objects, since a lot # of the fields are the same. instance = Instance(instance_ref) counters = gatherer(instance) payload["samples"] = [{"name": c.name, "type": c.type, "unit": c.unit, "volume": c.volume} for c in counters] publisher_id = notifier_api.publisher_id("compute", None) # We could simply modify the incoming message payload, but we # can't be sure that this notifier will be called before the RPC # notifier. Modifying the content may also break the message # signature. So, we start a new message publishing. We will be # called again recursively as a result, but we ignore the event we # generate so it doesn't matter. notifier_api.notify(context, publisher_id, "compute.instance.delete.samples", notifier_api.INFO, payload)
def notify_about_instance_usage(notifier, context, instance, event_suffix, network_info=None, system_metadata=None, extra_usage_info=None, fault=None): """Send a notification about an instance. :param notifier: a messaging.Notifier :param event_suffix: Event type like "delete.start" or "exists" :param network_info: Networking information, if provided. :param system_metadata: system_metadata DB entries for the instance, if provided. :param extra_usage_info: Dictionary containing extra values to add or override in the notification. """ if not extra_usage_info: extra_usage_info = {} usage_info = notifications.info_from_instance(context, instance, network_info, system_metadata, **extra_usage_info) if fault: # NOTE(johngarbutt) mirrors the format in wrap_exception fault_payload = exception_to_dict(fault) LOG.debug(fault_payload["message"], instance=instance) usage_info.update(fault_payload) if event_suffix.endswith("error"): method = notifier.error else: method = notifier.info method(context, 'compute.instance.{0!s}'.format(event_suffix), usage_info)
def notify_about_instance_usage(notifier, context, instance, event_suffix, network_info=None, system_metadata=None, extra_usage_info=None, fault=None): """Send an unversioned legacy notification about an instance. All new notifications should use notify_about_instance_action which sends a versioned notification. :param notifier: a messaging.Notifier :param event_suffix: Event type like "delete.start" or "exists" :param network_info: Networking information, if provided. :param system_metadata: system_metadata DB entries for the instance, if provided. :param extra_usage_info: Dictionary containing extra values to add or override in the notification. """ if not extra_usage_info: extra_usage_info = {} usage_info = notifications.info_from_instance(context, instance, network_info, system_metadata, **extra_usage_info) if fault: # NOTE(johngarbutt) mirrors the format in wrap_exception fault_payload = exception_to_dict(fault) LOG.debug(fault_payload["message"], instance=instance) usage_info.update(fault_payload) if event_suffix.endswith("error"): method = notifier.error else: method = notifier.info # WRS: send server group notifications try: objects.InstanceGroup.get_by_instance_uuid(context, instance.uuid) cgcs_messaging.send_server_grp_notification( context, 'compute.instance.%s' % event_suffix, usage_info, instance.uuid) except exception.InstanceGroupNotFound: pass method(context, 'compute.instance.%s' % event_suffix, usage_info)
def _notify(self, context, instance_ref, operation, network_info=None): try: usage_info = notifications.info_from_instance(context, instance_ref, network_info=network_info, system_metadata=None) notifier.notify(context, 'gridcentric.%s' % self.host, 'gridcentric.instance.%s' % operation, notifier.INFO, usage_info) except: # (amscanne): We do not put the instance into an error state during a notify exception. # It doesn't seem reasonable to do this, as the instance may still be up and running, # using resources, etc. and the ACTIVE state more accurately reflects this than # the ERROR state. So if there are real systems scanning instances in addition to # using notification events, they will eventually pick up the instance and correct # for their missing notification. _log_error("notify %s" % operation)
def _notify(self, context, instance_ref, operation, network_info=None): try: usage_info = notifications.info_from_instance( context, instance_ref, network_info=network_info, system_metadata=None) notifier.notify(context, 'gridcentric.%s' % self.host, 'gridcentric.instance.%s' % operation, notifier.INFO, usage_info) except: # (amscanne): We do not put the instance into an error state during a notify exception. # It doesn't seem reasonable to do this, as the instance may still be up and running, # using resources, etc. and the ACTIVE state more accurately reflects this than # the ERROR state. So if there are real systems scanning instances in addition to # using notification events, they will eventually pick up the instance and correct # for their missing notification. _log_error("notify %s" % operation)
def _notify_instance_update_spec(context, instance, updates): """Internal Helper Method to send notifications if allocations changed""" updates = updates.copy() # Parse out the Old and New Meta-Data and PowerSpecs to merge them together new_pwrspecs = updates.pop('power_specs', {}) new_metadata = updates.pop('system_metadata', {}) old_metadata = instance.get('system_metadata', {}).copy() old_pwrspecs = instance.get('power_specs', {}).copy() # Merge the Old and New MetaData and PowerSpecs together for the Event old_pwrspecs.update(new_pwrspecs) old_metadata.update(new_metadata) # Use the Utility to Build the base Instance, and add MetaData/PowerSpecs info = notifyutil.info_from_instance(context, instance, None, None) info.update(dict(system_metadata=old_metadata, power_specs=old_pwrspecs)) info.update(updates) # Now we can send the Notification to the Scheduler that the Specs changed notifier = rpc.get_notifier(service='compute', host=instance['host']) notifier.info(context, 'compute.instance.update.spec', info)
def notify(context, message): if message['event_type'] != 'compute.instance.delete.start': LOG.debug(_('ignoring %s'), message['event_type']) return LOG.info(_('processing %s'), message['event_type']) gatherer = initialize_gatherer() instance_id = message['payload']['instance_id'] LOG.debug(_('polling final stats for %r'), instance_id) # Ask for the instance details instance_ref = conductor_api.instance_get_by_uuid( context, instance_id, ) # Get the default notification payload payload = notifications.info_from_instance( context, instance_ref, None, None) # Extend the payload with samples from our plugins. We only need # to send some of the data from the sample objects, since a lot # of the fields are the same. instance = Instance(context, instance_ref) samples = gatherer(instance) payload['samples'] = [{'name': s.name, 'type': s.type, 'unit': s.unit, 'volume': s.volume} for s in samples] publisher_id = notifier_api.publisher_id('compute', None) # We could simply modify the incoming message payload, but we # can't be sure that this notifier will be called before the RPC # notifier. Modifying the content may also break the message # signature. So, we start a new message publishing. We will be # called again recursively as a result, but we ignore the event we # generate so it doesn't matter. notifier_api.notify(context, publisher_id, 'compute.instance.delete.samples', notifier_api.INFO, payload)
def test_payload_has_progress(self): self.instance.progress = 50 info = notifications.info_from_instance(self.context, self.instance, self.net_info, None) self.assertIn("progress", info) self.assertEqual(50, info["progress"])
def test_payload_has_progress_empty(self): info = notifications.info_from_instance(self.context, self.instance, self.net_info, None) self.assertIn("progress", info) self.assertIsNone(self.instance.progress) self.assertEqual("", info["progress"])
def test_payload_has_cell_name(self): self.instance.cell_name = "cell1" info = notifications.info_from_instance(self.context, self.instance, self.net_info, None) self.assertIn("cell_name", info) self.assertEqual("cell1", info["cell_name"])
def test_payload_has_vif_mac_address(self): info = notifications.info_from_instance(self.context, self.instance, self.net_info, None) self.assertIn("fixed_ips", info) self.assertEqual(self.net_info[0]['address'], info["fixed_ips"][0]["vif_mac"])
def test_payload_has_fixed_ip_labels(self): info = notifications.info_from_instance(self.context, self.instance, self.net_info, None) self.assertIn("fixed_ips", info) self.assertEqual(info["fixed_ips"][0]["label"], "test1")
def test_payload_has_cell_name_empty(self): info = notifications.info_from_instance(self.context, self.instance, self.net_info, None) self.assertIn("cell_name", info) self.assertIsNone(self.instance['cell_name']) self.assertEqual("", info["cell_name"])