<s> """<STR_LIT>""" <EOL> import itertools <EOL> import string <EOL> import traceback <EOL> import netifaces <EOL> from oslo_config import cfg <EOL> from oslo_log import log <EOL> import six <EOL> from nova import block_device <EOL> from nova . compute import power_state <EOL> from nova . compute import task_states <EOL> from nova import exception <EOL> from nova . i18n import _LW <EOL> from nova . network import model as network_model <EOL> from nova import notifications <EOL> from nova import objects <EOL> from nova import rpc <EOL> from nova import utils <EOL> from nova . virt import driver <EOL> CONF = cfg . CONF <EOL> CONF . import_opt ( '<STR_LIT:host>' , '<STR_LIT>' ) <EOL> LOG = log . getLogger ( __name__ ) <EOL> def exception_to_dict ( fault , message = None ) : <EOL> """<STR_LIT>""" <EOL> code = <NUM_LIT> <EOL> if hasattr ( fault , "<STR_LIT>" ) : <EOL> code = fault . kwargs . get ( '<STR_LIT:code>' , <NUM_LIT> ) <EOL> try : <EOL> if not message : <EOL> message = fault . format_message ( ) <EOL> except Exception : <EOL> try : <EOL> message = six . text_type ( fault ) <EOL> except Exception : <EOL> message = None <EOL> if not message : <EOL> message = fault . __class__ . __name__ <EOL> u_message = utils . safe_truncate ( message , <NUM_LIT:255> ) <EOL> fault_dict = dict ( exception = fault ) <EOL> fault_dict [ "<STR_LIT:message>" ] = u_message <EOL> fault_dict [ "<STR_LIT:code>" ] = code <EOL> return fault_dict <EOL> def _get_fault_details ( exc_info , error_code ) : <EOL> details = '<STR_LIT>' <EOL> if exc_info and error_code == <NUM_LIT> : <EOL> tb = exc_info [ <NUM_LIT:2> ] <EOL> if tb : <EOL> details = '<STR_LIT>' . join ( traceback . format_tb ( tb ) ) <EOL> return six . text_type ( details ) <EOL> def add_instance_fault_from_exc ( context , instance , fault , exc_info = None , <EOL> fault_message = None ) : <EOL> """<STR_LIT>""" <EOL> fault_obj = objects . InstanceFault ( context = context ) <EOL> fault_obj . host = CONF . host <EOL> fault_obj . instance_uuid = instance . uuid <EOL> fault_obj . update ( exception_to_dict ( fault , message = fault_message ) ) <EOL> code = fault_obj . code <EOL> fault_obj . details = _get_fault_details ( exc_info , code ) <EOL> fault_obj . create ( ) <EOL> def get_device_name_for_instance ( instance , bdms , device ) : <EOL> """<STR_LIT>""" <EOL> mappings = block_device . instance_block_mapping ( instance , bdms ) <EOL> return get_next_device_name ( instance , mappings . values ( ) , <EOL> mappings [ '<STR_LIT:root>' ] , device ) <EOL> def default_device_names_for_instance ( instance , root_device_name , <EOL> * block_device_lists ) : <EOL> """<STR_LIT>""" <EOL> dev_list = [ bdm . device_name <EOL> for bdm in itertools . chain ( * block_device_lists ) <EOL> if bdm . device_name ] <EOL> if root_device_name not in dev_list : <EOL> dev_list . append ( root_device_name ) <EOL> for bdm in itertools . chain ( * block_device_lists ) : <EOL> dev = bdm . device_name <EOL> if not dev : <EOL> dev = get_next_device_name ( instance , dev_list , <EOL> root_device_name ) <EOL> bdm . device_name = dev <EOL> bdm . save ( ) <EOL> dev_list . append ( dev ) <EOL> def get_next_device_name ( instance , device_name_list , <EOL> root_device_name = None , device = None ) : <EOL> """<STR_LIT>""" <EOL> req_prefix = None <EOL> req_letter = None <EOL> if device : <EOL> try : <EOL> req_prefix , req_letter = block_device . match_device ( device ) <EOL> except ( TypeError , AttributeError , ValueError ) : <EOL> raise exception . InvalidDevicePath ( path = device ) <EOL> if not root_device_name : <EOL> root_device_name = block_device . DEFAULT_ROOT_DEV_NAME <EOL> try : <EOL> prefix = block_device . match_device ( <EOL> block_device . prepend_dev ( root_device_name ) ) [ <NUM_LIT:0> ] <EOL> except ( TypeError , AttributeError , ValueError ) : <EOL> raise exception . InvalidDevicePath ( path = root_device_name ) <EOL> if driver . is_xenapi ( ) : <EOL> prefix = '<STR_LIT>' <EOL> if req_prefix != prefix : <EOL> LOG . debug ( "<STR_LIT>" , <EOL> { '<STR_LIT>' : prefix , '<STR_LIT>' : req_prefix } ) <EOL> used_letters = set ( ) <EOL> for device_path in device_name_list : <EOL> letter = block_device . get_device_letter ( device_path ) <EOL> used_letters . add ( letter ) <EOL> if driver . is_xenapi ( ) : <EOL> flavor = instance . get_flavor ( ) <EOL> if flavor . ephemeral_gb : <EOL> used_letters . add ( '<STR_LIT:b>' ) <EOL> if flavor . swap : <EOL> used_letters . add ( '<STR_LIT:c>' ) <EOL> if not req_letter : <EOL> req_letter = _get_unused_letter ( used_letters ) <EOL> if req_letter in used_letters : <EOL> raise exception . DevicePathInUse ( path = device ) <EOL> return prefix + req_letter <EOL> def _get_unused_letter ( used_letters ) : <EOL> doubles = [ first + second for second in string . ascii_lowercase <EOL> for first in string . ascii_lowercase ] <EOL> all_letters = set ( list ( string . ascii_lowercase ) + doubles ) <EOL> letters = list ( all_letters - used_letters ) <EOL> letters . sort ( key = lambda x : x . rjust ( <NUM_LIT:2> , '<STR_LIT>' ) ) <EOL> return letters [ <NUM_LIT:0> ] <EOL> def get_value_from_system_metadata ( instance , key , type , default ) : <EOL> """<STR_LIT>""" <EOL> value = instance . system_metadata . get ( key , default ) <EOL> try : <EOL> return type ( value ) <EOL> except ValueError : <EOL> LOG . warning ( _LW ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT:value>' : value , '<STR_LIT:key>' : key , '<STR_LIT:type>' : type , <EOL> '<STR_LIT:default>' : default } , instance = instance ) <EOL> return default <EOL> def notify_usage_exists ( notifier , context , instance_ref , current_period = False , <EOL> ignore_missing_network_data = True , <EOL> system_metadata = None , extra_usage_info = None ) : <EOL> """<STR_LIT>""" <EOL> audit_start , audit_end = notifications . audit_period_bounds ( current_period ) <EOL> bw = notifications . bandwidth_usage ( instance_ref , audit_start , <EOL> ignore_missing_network_data ) <EOL> if system_metadata is None : <EOL> system_metadata = utils . instance_sys_meta ( instance_ref ) <EOL> image_meta = notifications . image_meta ( system_metadata ) <EOL> extra_info = dict ( audit_period_beginning = str ( audit_start ) , <EOL> audit_period_ending = str ( audit_end ) , <EOL> bandwidth = bw , image_meta = image_meta ) <EOL> if extra_usage_info : <EOL> extra_info . update ( extra_usage_info ) <EOL> notify_about_instance_usage ( notifier , context , instance_ref , '<STR_LIT>' , <EOL> system_metadata = system_metadata , extra_usage_info = extra_info ) <EOL> def notify_about_instance_usage ( notifier , context , instance , event_suffix , <EOL> network_info = None , system_metadata = None , <EOL> extra_usage_info = None , fault = None ) : <EOL> """<STR_LIT>""" <EOL> if not extra_usage_info : <EOL> extra_usage_info = { } <EOL> usage_info = notifications . info_from_instance ( context , instance , <EOL> network_info , system_metadata , ** extra_usage_info ) <EOL> if fault : <EOL> fault_payload = exception_to_dict ( fault ) <EOL> LOG . debug ( fault_payload [ "<STR_LIT:message>" ] , instance = instance ) <EOL> usage_info . update ( fault_payload ) <EOL> if event_suffix . endswith ( "<STR_LIT:error>" ) : <EOL> method = notifier . error <EOL> else : <EOL> method = notifier . info <EOL> method ( context , '<STR_LIT>' % event_suffix , usage_info ) <EOL> def notify_about_server_group_update ( context , event_suffix , sg_payload ) : <EOL> """<STR_LIT>""" <EOL> notifier = rpc . get_notifier ( service = '<STR_LIT>' ) <EOL> notifier . info ( context , '<STR_LIT>' % event_suffix , sg_payload ) <EOL> def notify_about_aggregate_update ( context , event_suffix , aggregate_payload ) : <EOL> """<STR_LIT>""" <EOL> aggregate_identifier = aggregate_payload . get ( '<STR_LIT>' , None ) <EOL> if not aggregate_identifier : <EOL> aggregate_identifier = aggregate_payload . get ( '<STR_LIT:name>' , None ) <EOL> if not aggregate_identifier : <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return <EOL> notifier = rpc . get_notifier ( service = '<STR_LIT>' , <EOL> host = aggregate_identifier ) <EOL> notifier . info ( context , '<STR_LIT>' % event_suffix , aggregate_payload ) <EOL> def notify_about_host_update ( context , event_suffix , host_payload ) : <EOL> """<STR_LIT>""" <EOL> host_identifier = host_payload . get ( '<STR_LIT>' ) <EOL> if not host_identifier : <EOL> LOG . warning ( _LW ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , event_suffix ) <EOL> return <EOL> notifier = rpc . get_notifier ( service = '<STR_LIT>' , host = host_identifier ) <EOL> notifier . info ( context , '<STR_LIT>' % event_suffix , host_payload ) <EOL> def get_nw_info_for_instance ( instance ) : <EOL> if instance . info_cache is None : <EOL> return network_model . NetworkInfo . hydrate ( [ ] ) <EOL> return instance . info_cache . network_info <EOL> def refresh_info_cache_for_instance ( context , instance ) : <EOL> """<STR_LIT>""" <EOL> if instance . info_cache is not None : <EOL> instance . info_cache . refresh ( ) <EOL> def usage_volume_info ( vol_usage ) : <EOL> def null_safe_str ( s ) : <EOL> return str ( s ) if s else '<STR_LIT>' <EOL> tot_refreshed = vol_usage . tot_last_refreshed <EOL> curr_refreshed = vol_usage . curr_last_refreshed <EOL> if tot_refreshed and curr_refreshed : <EOL> last_refreshed_time = max ( tot_refreshed , curr_refreshed ) <EOL> elif tot_refreshed : <EOL> last_refreshed_time = tot_refreshed <EOL> else : <EOL> last_refreshed_time = curr_refreshed <EOL> usage_info = dict ( <EOL> volume_id = vol_usage . volume_id , <EOL> tenant_id = vol_usage . project_id , <EOL> user_id = vol_usage . user_id , <EOL> availability_zone = vol_usage . availability_zone , <EOL> instance_id = vol_usage . instance_uuid , <EOL> last_refreshed = null_safe_str ( last_refreshed_time ) , <EOL> reads = vol_usage . tot_reads + vol_usage . curr_reads , <EOL> read_bytes = vol_usage . tot_read_bytes + <EOL> vol_usage . curr_read_bytes , <EOL> writes = vol_usage . tot_writes + vol_usage . curr_writes , <EOL> write_bytes = vol_usage . tot_write_bytes + <EOL> vol_usage . curr_write_bytes ) <EOL> return usage_info <EOL> def get_reboot_type ( task_state , current_power_state ) : <EOL> """<STR_LIT>""" <EOL> if current_power_state != power_state . RUNNING : <EOL> return '<STR_LIT>' <EOL> soft_types = [ task_states . REBOOT_STARTED , task_states . REBOOT_PENDING , <EOL> task_states . REBOOTING ] <EOL> reboot_type = '<STR_LIT>' if task_state in soft_types else '<STR_LIT>' <EOL> return reboot_type <EOL> def get_machine_ips ( ) : <EOL> """<STR_LIT>""" <EOL> addresses = [ ] <EOL> for interface in netifaces . interfaces ( ) : <EOL> try : <EOL> iface_data = netifaces . ifaddresses ( interface ) <EOL> for family in iface_data : <EOL> if family not in ( netifaces . AF_INET , netifaces . AF_INET6 ) : <EOL> continue <EOL> for address in iface_data [ family ] : <EOL> addr = address [ '<STR_LIT>' ] <EOL> if family == netifaces . AF_INET6 : <EOL> addr = addr . split ( '<STR_LIT:%>' ) [ <NUM_LIT:0> ] <EOL> addresses . append ( addr ) <EOL> except ValueError : <EOL> pass <EOL> return addresses <EOL> def resize_quota_delta ( context , new_flavor , old_flavor , sense , compare ) : <EOL> """<STR_LIT>""" <EOL> def _quota_delta ( resource ) : <EOL> return sense * ( new_flavor [ resource ] - old_flavor [ resource ] ) <EOL> deltas = { } <EOL> if compare * _quota_delta ( '<STR_LIT>' ) > <NUM_LIT:0> : <EOL> deltas [ '<STR_LIT>' ] = _quota_delta ( '<STR_LIT>' ) <EOL> if compare * _quota_delta ( '<STR_LIT>' ) > <NUM_LIT:0> : <EOL> deltas [ '<STR_LIT>' ] = _quota_delta ( '<STR_LIT>' ) <EOL> return deltas <EOL> def upsize_quota_delta ( context , new_flavor , old_flavor ) : <EOL> """<STR_LIT>""" <EOL> return resize_quota_delta ( context , new_flavor , old_flavor , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def reverse_upsize_quota_delta ( context , migration_ref ) : <EOL> """<STR_LIT>""" <EOL> old_flavor = objects . Flavor . get_by_id ( <EOL> context , migration_ref [ '<STR_LIT>' ] ) <EOL> new_flavor = objects . Flavor . get_by_id ( <EOL> context , migration_ref [ '<STR_LIT>' ] ) <EOL> return resize_quota_delta ( context , new_flavor , old_flavor , - <NUM_LIT:1> , - <NUM_LIT:1> ) <EOL> def downsize_quota_delta ( context , instance ) : <EOL> """<STR_LIT>""" <EOL> old_flavor = instance . get_flavor ( '<STR_LIT>' ) <EOL> new_flavor = instance . get_flavor ( '<STR_LIT>' ) <EOL> return resize_quota_delta ( context , new_flavor , old_flavor , <NUM_LIT:1> , - <NUM_LIT:1> ) <EOL> def reserve_quota_delta ( context , deltas , instance ) : <EOL> """<STR_LIT>""" <EOL> quotas = objects . Quotas ( context = context ) <EOL> if deltas : <EOL> project_id , user_id = objects . quotas . ids_from_instance ( context , <EOL> instance ) <EOL> quotas . reserve ( project_id = project_id , user_id = user_id , <EOL> ** deltas ) <EOL> return quotas <EOL> def remove_shelved_keys_from_system_metadata ( instance ) : <EOL> for key in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if key in instance . system_metadata : <EOL> del ( instance . system_metadata [ key ] ) <EOL> class EventReporter ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , context , event_name , * instance_uuids ) : <EOL> self . context = context <EOL> self . event_name = event_name <EOL> self . instance_uuids = instance_uuids <EOL> def __enter__ ( self ) : <EOL> for uuid in self . instance_uuids : <EOL> objects . InstanceActionEvent . event_start ( <EOL> self . context , uuid , self . event_name , want_result = False ) <EOL> return self <EOL> def __exit__ ( self , exc_type , exc_val , exc_tb ) : <EOL> for uuid in self . instance_uuids : <EOL> objects . InstanceActionEvent . event_finish_with_failure ( <EOL> self . context , uuid , self . event_name , exc_val = exc_val , <EOL> exc_tb = exc_tb , want_result = False ) <EOL> return False <EOL> class UnlimitedSemaphore ( object ) : <EOL> def __enter__ ( self ) : <EOL> pass <EOL> def __exit__ ( self ) : <EOL> pass <EOL> @ property <EOL> def balance ( self ) : <EOL> return <NUM_LIT:0> </s>
