<s> """<STR_LIT>""" <EOL> import datetime <EOL> from oslo_config import cfg <EOL> from oslo_context import context as common_context <EOL> from oslo_log import log <EOL> from oslo_utils import excutils <EOL> from oslo_utils import timeutils <EOL> import six <EOL> import nova . context <EOL> from nova import exception <EOL> from nova . i18n import _LE <EOL> from nova . image import glance <EOL> from nova import network <EOL> from nova . network import model as network_model <EOL> from nova import objects <EOL> from nova . objects import base as obj_base <EOL> from nova import rpc <EOL> from nova import utils <EOL> LOG = log . getLogger ( __name__ ) <EOL> notify_opts = [ <EOL> cfg . StrOpt ( '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> cfg . BoolOpt ( '<STR_LIT>' , default = False , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> cfg . StrOpt ( '<STR_LIT>' , <EOL> default = '<STR_LIT>' , <EOL> choices = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> help = '<STR_LIT>' ) , <EOL> cfg . StrOpt ( '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) , <EOL> ] <EOL> CONF = cfg . CONF <EOL> CONF . register_opts ( notify_opts ) <EOL> def notify_decorator ( name , fn ) : <EOL> """<STR_LIT>""" <EOL> def wrapped_func ( * args , ** kwarg ) : <EOL> body = { } <EOL> body [ '<STR_LIT:args>' ] = [ ] <EOL> body [ '<STR_LIT>' ] = { } <EOL> for arg in args : <EOL> body [ '<STR_LIT:args>' ] . append ( arg ) <EOL> for key in kwarg : <EOL> body [ '<STR_LIT>' ] [ key ] = kwarg [ key ] <EOL> ctxt = ( common_context . get_context_from_function_and_args ( <EOL> fn , args , kwarg ) or <EOL> common_context . get_current ( ) or <EOL> nova . context . RequestContext ( ) ) <EOL> notifier = rpc . get_notifier ( '<STR_LIT>' , <EOL> publisher_id = ( CONF . default_publisher_id <EOL> or CONF . host ) ) <EOL> method = getattr ( notifier , CONF . default_notification_level . lower ( ) , <EOL> '<STR_LIT:info>' ) <EOL> method ( ctxt , name , body ) <EOL> return fn ( * args , ** kwarg ) <EOL> return wrapped_func <EOL> def send_api_fault ( url , status , exception ) : <EOL> """<STR_LIT>""" <EOL> if not CONF . notify_api_faults : <EOL> return <EOL> payload = { '<STR_LIT:url>' : url , '<STR_LIT>' : six . text_type ( exception ) , <EOL> '<STR_LIT:status>' : status } <EOL> rpc . get_notifier ( '<STR_LIT>' ) . error ( common_context . get_current ( ) or <EOL> nova . context . get_admin_context ( ) , <EOL> '<STR_LIT>' , <EOL> payload ) <EOL> def send_update ( context , old_instance , new_instance , service = "<STR_LIT>" , <EOL> host = None ) : <EOL> """<STR_LIT>""" <EOL> if not CONF . notify_on_state_change : <EOL> return <EOL> update_with_state_change = False <EOL> old_vm_state = old_instance [ "<STR_LIT>" ] <EOL> new_vm_state = new_instance [ "<STR_LIT>" ] <EOL> old_task_state = old_instance [ "<STR_LIT>" ] <EOL> new_task_state = new_instance [ "<STR_LIT>" ] <EOL> if old_vm_state != new_vm_state : <EOL> update_with_state_change = True <EOL> elif ( CONF . notify_on_state_change . lower ( ) == "<STR_LIT>" and <EOL> old_task_state != new_task_state ) : <EOL> update_with_state_change = True <EOL> if update_with_state_change : <EOL> send_update_with_states ( context , new_instance , old_vm_state , <EOL> new_vm_state , old_task_state , new_task_state , service , host ) <EOL> else : <EOL> try : <EOL> old_display_name = None <EOL> if new_instance [ "<STR_LIT>" ] != old_instance [ "<STR_LIT>" ] : <EOL> old_display_name = old_instance [ "<STR_LIT>" ] <EOL> _send_instance_update_notification ( context , new_instance , <EOL> service = service , host = host , <EOL> old_display_name = old_display_name ) <EOL> except exception . InstanceNotFound : <EOL> LOG . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , instance = new_instance ) <EOL> except Exception : <EOL> LOG . exception ( _LE ( "<STR_LIT>" ) , <EOL> instance = new_instance ) <EOL> def send_update_with_states ( context , instance , old_vm_state , new_vm_state , <EOL> old_task_state , new_task_state , service = "<STR_LIT>" , host = None , <EOL> verify_states = False ) : <EOL> """<STR_LIT>""" <EOL> if not CONF . notify_on_state_change : <EOL> return <EOL> fire_update = True <EOL> if verify_states : <EOL> fire_update = False <EOL> if old_vm_state != new_vm_state : <EOL> fire_update = True <EOL> elif ( CONF . notify_on_state_change . lower ( ) == "<STR_LIT>" and <EOL> old_task_state != new_task_state ) : <EOL> fire_update = True <EOL> if fire_update : <EOL> try : <EOL> _send_instance_update_notification ( context , instance , <EOL> old_vm_state = old_vm_state , old_task_state = old_task_state , <EOL> new_vm_state = new_vm_state , new_task_state = new_task_state , <EOL> service = service , host = host ) <EOL> except exception . InstanceNotFound : <EOL> LOG . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , instance = instance ) <EOL> except Exception : <EOL> LOG . exception ( _LE ( "<STR_LIT>" ) , <EOL> instance = instance ) <EOL> def _compute_states_payload ( instance , old_vm_state = None , <EOL> old_task_state = None , new_vm_state = None , new_task_state = None ) : <EOL> if new_vm_state is None : <EOL> new_vm_state = instance [ "<STR_LIT>" ] <EOL> if new_task_state is None : <EOL> new_task_state = instance [ "<STR_LIT>" ] <EOL> if old_vm_state is None : <EOL> old_vm_state = instance [ "<STR_LIT>" ] <EOL> if old_task_state is None : <EOL> old_task_state = instance [ "<STR_LIT>" ] <EOL> states_payload = { <EOL> "<STR_LIT>" : old_vm_state , <EOL> "<STR_LIT:state>" : new_vm_state , <EOL> "<STR_LIT>" : old_task_state , <EOL> "<STR_LIT>" : new_task_state , <EOL> } <EOL> return states_payload <EOL> def _send_instance_update_notification ( context , instance , old_vm_state = None , <EOL> old_task_state = None , new_vm_state = None , new_task_state = None , <EOL> service = "<STR_LIT>" , host = None , old_display_name = None ) : <EOL> """<STR_LIT>""" <EOL> payload = info_from_instance ( context , instance , None , None ) <EOL> payload . update ( <EOL> _compute_states_payload ( <EOL> instance , old_vm_state , old_task_state , <EOL> new_vm_state , new_task_state ) ) <EOL> ( audit_start , audit_end ) = audit_period_bounds ( current_period = True ) <EOL> payload [ "<STR_LIT>" ] = audit_start <EOL> payload [ "<STR_LIT>" ] = audit_end <EOL> bw = bandwidth_usage ( instance , audit_start ) <EOL> payload [ "<STR_LIT>" ] = bw <EOL> if old_display_name : <EOL> payload [ "<STR_LIT>" ] = old_display_name <EOL> rpc . get_notifier ( service , host ) . info ( context , <EOL> '<STR_LIT>' , payload ) <EOL> def audit_period_bounds ( current_period = False ) : <EOL> """<STR_LIT>""" <EOL> begin , end = utils . last_completed_audit_period ( ) <EOL> if current_period : <EOL> audit_start = end <EOL> audit_end = timeutils . utcnow ( ) <EOL> else : <EOL> audit_start = begin <EOL> audit_end = end <EOL> return ( audit_start , audit_end ) <EOL> def bandwidth_usage ( instance_ref , audit_start , <EOL> ignore_missing_network_data = True ) : <EOL> """<STR_LIT>""" <EOL> admin_context = nova . context . get_admin_context ( read_deleted = '<STR_LIT:yes>' ) <EOL> def _get_nwinfo_old_skool ( ) : <EOL> """<STR_LIT>""" <EOL> if ( instance_ref . get ( '<STR_LIT>' ) and <EOL> instance_ref [ '<STR_LIT>' ] . get ( '<STR_LIT>' ) is not None ) : <EOL> cached_info = instance_ref [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> if isinstance ( cached_info , network_model . NetworkInfo ) : <EOL> return cached_info <EOL> return network_model . NetworkInfo . hydrate ( cached_info ) <EOL> try : <EOL> return network . API ( ) . get_instance_nw_info ( admin_context , <EOL> instance_ref ) <EOL> except Exception : <EOL> try : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> LOG . exception ( _LE ( '<STR_LIT>' ) , <EOL> instance = instance_ref ) <EOL> except Exception : <EOL> if ignore_missing_network_data : <EOL> return <EOL> raise <EOL> if isinstance ( instance_ref , obj_base . NovaObject ) : <EOL> nw_info = instance_ref . info_cache . network_info <EOL> if nw_info is None : <EOL> nw_info = network_model . NetworkInfo ( ) <EOL> else : <EOL> nw_info = _get_nwinfo_old_skool ( ) <EOL> macs = [ vif [ '<STR_LIT:address>' ] for vif in nw_info ] <EOL> uuids = [ instance_ref [ "<STR_LIT>" ] ] <EOL> bw_usages = objects . BandwidthUsageList . get_by_uuids ( admin_context , uuids , <EOL> audit_start ) <EOL> bw = { } <EOL> for b in bw_usages : <EOL> if b . mac in macs : <EOL> label = '<STR_LIT>' % b . mac <EOL> for vif in nw_info : <EOL> if vif [ '<STR_LIT:address>' ] == b . mac : <EOL> label = vif [ '<STR_LIT>' ] [ '<STR_LIT:label>' ] <EOL> break <EOL> bw [ label ] = dict ( bw_in = b . bw_in , bw_out = b . bw_out ) <EOL> return bw <EOL> def image_meta ( system_metadata ) : <EOL> """<STR_LIT>""" <EOL> image_meta = { } <EOL> for md_key , md_value in six . iteritems ( system_metadata ) : <EOL> if md_key . startswith ( '<STR_LIT>' ) : <EOL> image_meta [ md_key [ <NUM_LIT:6> : ] ] = md_value <EOL> return image_meta <EOL> def info_from_instance ( context , instance , network_info , <EOL> system_metadata , ** kw ) : <EOL> """<STR_LIT>""" <EOL> def null_safe_str ( s ) : <EOL> return str ( s ) if s else '<STR_LIT>' <EOL> def null_safe_int ( s ) : <EOL> return int ( s ) if s else '<STR_LIT>' <EOL> def null_safe_isotime ( s ) : <EOL> if isinstance ( s , datetime . datetime ) : <EOL> return utils . strtime ( s ) <EOL> else : <EOL> return str ( s ) if s else '<STR_LIT>' <EOL> image_ref_url = glance . generate_image_url ( instance . image_ref ) <EOL> instance_type = instance . get_flavor ( ) <EOL> instance_type_name = instance_type . get ( '<STR_LIT:name>' , '<STR_LIT>' ) <EOL> instance_flavorid = instance_type . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> instance_info = dict ( <EOL> tenant_id = instance . project_id , <EOL> user_id = instance . user_id , <EOL> instance_id = instance . uuid , <EOL> display_name = instance . display_name , <EOL> reservation_id = instance . reservation_id , <EOL> hostname = instance . hostname , <EOL> instance_type = instance_type_name , <EOL> instance_type_id = instance . instance_type_id , <EOL> instance_flavor_id = instance_flavorid , <EOL> architecture = instance . architecture , <EOL> memory_mb = instance . memory_mb , <EOL> disk_gb = instance . root_gb + instance . ephemeral_gb , <EOL> vcpus = instance . vcpus , <EOL> root_gb = instance . root_gb , <EOL> ephemeral_gb = instance . ephemeral_gb , <EOL> host = instance . host , <EOL> node = instance . node , <EOL> availability_zone = instance . availability_zone , <EOL> cell_name = null_safe_str ( instance . cell_name ) , <EOL> created_at = str ( instance . created_at ) , <EOL> terminated_at = null_safe_isotime ( instance . get ( '<STR_LIT>' , None ) ) , <EOL> deleted_at = null_safe_isotime ( instance . get ( '<STR_LIT>' , None ) ) , <EOL> launched_at = null_safe_isotime ( instance . get ( '<STR_LIT>' , None ) ) , <EOL> image_ref_url = image_ref_url , <EOL> os_type = instance . os_type , <EOL> kernel_id = instance . kernel_id , <EOL> ramdisk_id = instance . ramdisk_id , <EOL> state = instance . vm_state , <EOL> state_description = null_safe_str ( instance . task_state ) , <EOL> progress = null_safe_int ( instance . progress ) , <EOL> access_ip_v4 = instance . access_ip_v4 , <EOL> access_ip_v6 = instance . access_ip_v6 , <EOL> ) <EOL> if network_info is not None : <EOL> fixed_ips = [ ] <EOL> for vif in network_info : <EOL> for ip in vif . fixed_ips ( ) : <EOL> ip [ "<STR_LIT:label>" ] = vif [ "<STR_LIT>" ] [ "<STR_LIT:label>" ] <EOL> ip [ "<STR_LIT>" ] = vif [ "<STR_LIT:address>" ] <EOL> fixed_ips . append ( ip ) <EOL> instance_info [ '<STR_LIT>' ] = fixed_ips <EOL> image_meta_props = image_meta ( instance . system_metadata ) <EOL> instance_info [ "<STR_LIT>" ] = image_meta_props <EOL> instance_info [ '<STR_LIT>' ] = instance . metadata <EOL> instance_info . update ( kw ) <EOL> return instance_info </s>
