<s> """<STR_LIT>""" <EOL> import base64 <EOL> import contextlib <EOL> import functools <EOL> import inspect <EOL> import socket <EOL> import sys <EOL> import time <EOL> import traceback <EOL> import uuid <EOL> from cinderclient import exceptions as cinder_exception <EOL> import eventlet . event <EOL> from eventlet import greenthread <EOL> import eventlet . semaphore <EOL> import eventlet . timeout <EOL> from keystoneauth1 import exceptions as keystone_exception <EOL> from oslo_config import cfg <EOL> from oslo_log import log as logging <EOL> import oslo_messaging as messaging <EOL> from oslo_serialization import jsonutils <EOL> from oslo_service import loopingcall <EOL> from oslo_service import periodic_task <EOL> from oslo_utils import excutils <EOL> from oslo_utils import strutils <EOL> from oslo_utils import timeutils <EOL> import six <EOL> from six . moves import range <EOL> from nova import block_device <EOL> from nova . cells import rpcapi as cells_rpcapi <EOL> from nova . cloudpipe import pipelib <EOL> from nova import compute <EOL> from nova . compute import build_results <EOL> from nova . compute import claims <EOL> from nova . compute import power_state <EOL> from nova . compute import resource_tracker <EOL> from nova . compute import rpcapi as compute_rpcapi <EOL> from nova . compute import task_states <EOL> from nova . compute import utils as compute_utils <EOL> from nova . compute import vm_states <EOL> from nova import conductor <EOL> import nova . conf <EOL> from nova import consoleauth <EOL> import nova . context <EOL> from nova import exception <EOL> from nova import hooks <EOL> from nova . i18n import _ <EOL> from nova . i18n import _LE <EOL> from nova . i18n import _LI <EOL> from nova . i18n import _LW <EOL> from nova import image <EOL> from nova . image import glance <EOL> from nova import manager <EOL> from nova import network <EOL> from nova . network import base_api as base_net_api <EOL> from nova . network import model as network_model <EOL> from nova . network . security_group import openstack_driver <EOL> from nova import objects <EOL> from nova . objects import base as obj_base <EOL> from nova . objects import instance as obj_instance <EOL> from nova . objects import migrate_data as migrate_data_obj <EOL> from nova import paths <EOL> from nova import rpc <EOL> from nova import safe_utils <EOL> from nova . scheduler import client as scheduler_client <EOL> from nova import utils <EOL> from nova . virt import block_device as driver_block_device <EOL> from nova . virt import configdrive <EOL> from nova . virt import driver <EOL> from nova . virt import event as virtevent <EOL> from nova . virt import storage_users <EOL> from nova . virt import virtapi <EOL> from nova import volume <EOL> from nova . volume import encryptors <EOL> compute_opts = [ <EOL> cfg . StrOpt ( '<STR_LIT>' , <EOL> default = socket . gethostname ( ) , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> cfg . StrOpt ( '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) , <EOL> cfg . BoolOpt ( '<STR_LIT>' , <EOL> default = False , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> cfg . StrOpt ( '<STR_LIT>' , <EOL> default = paths . state_path_def ( '<STR_LIT>' ) , <EOL> help = '<STR_LIT>' ) , <EOL> cfg . BoolOpt ( '<STR_LIT>' , <EOL> default = False , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> cfg . IntOpt ( '<STR_LIT>' , <EOL> default = <NUM_LIT:30> , <EOL> help = "<STR_LIT>" ) , <EOL> cfg . BoolOpt ( '<STR_LIT>' , <EOL> default = False , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> cfg . IntOpt ( '<STR_LIT>' , <EOL> default = <NUM_LIT:0> , <EOL> help = "<STR_LIT>" ) , <EOL> cfg . IntOpt ( '<STR_LIT>' , <EOL> default = <NUM_LIT:10> , <EOL> help = '<STR_LIT>' ) , <EOL> cfg . IntOpt ( '<STR_LIT>' , <EOL> default = <NUM_LIT:1> , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> cfg . IntOpt ( '<STR_LIT>' , <EOL> default = <NUM_LIT> , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> ] <EOL> interval_opts = [ <EOL> cfg . IntOpt ( '<STR_LIT>' , <EOL> default = <NUM_LIT> , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> cfg . IntOpt ( '<STR_LIT>' , <EOL> default = <NUM_LIT> , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> cfg . IntOpt ( "<STR_LIT>" , <EOL> default = <NUM_LIT> , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> cfg . IntOpt ( '<STR_LIT>' , <EOL> min = <NUM_LIT:0> , <EOL> default = <NUM_LIT:0> , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> cfg . IntOpt ( '<STR_LIT>' , <EOL> default = <NUM_LIT:0> , <EOL> help = '<STR_LIT>' ) , <EOL> cfg . IntOpt ( '<STR_LIT>' , <EOL> default = <NUM_LIT> , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> cfg . IntOpt ( '<STR_LIT>' , <EOL> default = <NUM_LIT:0> , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> cfg . IntOpt ( '<STR_LIT>' , <EOL> default = <NUM_LIT> , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> cfg . IntOpt ( '<STR_LIT>' , <EOL> default = <NUM_LIT:3> , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> cfg . IntOpt ( '<STR_LIT>' , <EOL> default = <NUM_LIT> , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> cfg . IntOpt ( '<STR_LIT>' , <EOL> default = <NUM_LIT:0> , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> ] <EOL> timeout_opts = [ <EOL> cfg . IntOpt ( "<STR_LIT>" , <EOL> default = <NUM_LIT:0> , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> cfg . IntOpt ( "<STR_LIT>" , <EOL> default = <NUM_LIT:0> , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> cfg . IntOpt ( "<STR_LIT>" , <EOL> default = <NUM_LIT:0> , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> cfg . IntOpt ( "<STR_LIT>" , <EOL> default = <NUM_LIT:0> , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> cfg . IntOpt ( "<STR_LIT>" , <EOL> default = <NUM_LIT> , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> ] <EOL> running_deleted_opts = [ <EOL> cfg . StrOpt ( "<STR_LIT>" , <EOL> default = "<STR_LIT>" , <EOL> choices = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> cfg . IntOpt ( "<STR_LIT>" , <EOL> default = <NUM_LIT> , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> cfg . IntOpt ( "<STR_LIT>" , <EOL> default = <NUM_LIT:0> , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> ] <EOL> instance_cleaning_opts = [ <EOL> cfg . IntOpt ( '<STR_LIT>' , <EOL> default = <NUM_LIT:5> , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> ] <EOL> CONF = nova . conf . CONF <EOL> CONF . register_opts ( compute_opts ) <EOL> CONF . register_opts ( interval_opts ) <EOL> CONF . register_opts ( timeout_opts ) <EOL> CONF . register_opts ( running_deleted_opts ) <EOL> CONF . register_opts ( instance_cleaning_opts ) <EOL> CONF . import_opt ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> CONF . import_opt ( '<STR_LIT:host>' , '<STR_LIT>' ) <EOL> CONF . import_opt ( '<STR_LIT>' , '<STR_LIT>' , group = '<STR_LIT>' ) <EOL> CONF . import_opt ( '<STR_LIT>' , '<STR_LIT>' , group = '<STR_LIT>' ) <EOL> CONF . import_opt ( '<STR_LIT>' , '<STR_LIT>' , group = '<STR_LIT>' ) <EOL> CONF . import_opt ( '<STR_LIT>' , '<STR_LIT>' , group = '<STR_LIT>' ) <EOL> LOG = logging . getLogger ( __name__ ) <EOL> get_notifier = functools . partial ( rpc . get_notifier , service = '<STR_LIT>' ) <EOL> wrap_exception = functools . partial ( exception . wrap_exception , <EOL> get_notifier = get_notifier ) <EOL> @ utils . expects_func_args ( '<STR_LIT>' ) <EOL> def errors_out_migration ( function ) : <EOL> """<STR_LIT>""" <EOL> @ functools . wraps ( function ) <EOL> def decorated_function ( self , context , * args , ** kwargs ) : <EOL> try : <EOL> return function ( self , context , * args , ** kwargs ) <EOL> except Exception as ex : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> wrapped_func = safe_utils . get_wrapped_function ( function ) <EOL> keyed_args = inspect . getcallargs ( wrapped_func , self , context , <EOL> * args , ** kwargs ) <EOL> migration = keyed_args [ '<STR_LIT>' ] <EOL> if not isinstance ( ex , exception . InstanceNotFound ) : <EOL> status = migration . status <EOL> if status not in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> return <EOL> migration . status = '<STR_LIT:error>' <EOL> try : <EOL> with migration . obj_as_admin ( ) : <EOL> migration . save ( ) <EOL> except Exception : <EOL> LOG . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> migration . instance_uuid , exc_info = True ) <EOL> return decorated_function <EOL> @ utils . expects_func_args ( '<STR_LIT>' ) <EOL> def reverts_task_state ( function ) : <EOL> """<STR_LIT>""" <EOL> @ functools . wraps ( function ) <EOL> def decorated_function ( self , context , * args , ** kwargs ) : <EOL> try : <EOL> return function ( self , context , * args , ** kwargs ) <EOL> except exception . UnexpectedTaskStateError as e : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> LOG . info ( _LI ( "<STR_LIT>" ) , <EOL> e . format_message ( ) ) <EOL> except Exception : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> wrapped_func = safe_utils . get_wrapped_function ( function ) <EOL> keyed_args = inspect . getcallargs ( wrapped_func , self , context , <EOL> * args , ** kwargs ) <EOL> instance = keyed_args [ '<STR_LIT>' ] <EOL> original_task_state = instance . task_state <EOL> try : <EOL> self . _instance_update ( context , instance , task_state = None ) <EOL> LOG . info ( _LI ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , original_task_state , <EOL> instance = instance ) <EOL> except exception . InstanceNotFound : <EOL> pass <EOL> except Exception as e : <EOL> msg = _LW ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> LOG . warning ( msg , e , instance = instance ) <EOL> return decorated_function <EOL> @ utils . expects_func_args ( '<STR_LIT>' ) <EOL> def wrap_instance_fault ( function ) : <EOL> """<STR_LIT>""" <EOL> @ functools . wraps ( function ) <EOL> def decorated_function ( self , context , * args , ** kwargs ) : <EOL> try : <EOL> return function ( self , context , * args , ** kwargs ) <EOL> except exception . InstanceNotFound : <EOL> raise <EOL> except Exception as e : <EOL> kwargs . update ( dict ( zip ( function . __code__ . co_varnames [ <NUM_LIT:2> : ] , args ) ) ) <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> compute_utils . add_instance_fault_from_exc ( context , <EOL> kwargs [ '<STR_LIT>' ] , e , sys . exc_info ( ) ) <EOL> return decorated_function <EOL> @ utils . expects_func_args ( '<STR_LIT>' ) <EOL> def wrap_instance_event ( function ) : <EOL> """<STR_LIT>""" <EOL> @ functools . wraps ( function ) <EOL> def decorated_function ( self , context , * args , ** kwargs ) : <EOL> wrapped_func = safe_utils . get_wrapped_function ( function ) <EOL> keyed_args = inspect . getcallargs ( wrapped_func , self , context , * args , <EOL> ** kwargs ) <EOL> instance_uuid = keyed_args [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> event_name = '<STR_LIT>' . format ( function . __name__ ) <EOL> with compute_utils . EventReporter ( context , event_name , instance_uuid ) : <EOL> return function ( self , context , * args , ** kwargs ) <EOL> return decorated_function <EOL> @ utils . expects_func_args ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def delete_image_on_error ( function ) : <EOL> """<STR_LIT>""" <EOL> @ functools . wraps ( function ) <EOL> def decorated_function ( self , context , image_id , instance , <EOL> * args , ** kwargs ) : <EOL> try : <EOL> return function ( self , context , image_id , instance , <EOL> * args , ** kwargs ) <EOL> except Exception : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> LOG . debug ( "<STR_LIT>" , image_id , <EOL> exc_info = True , instance = instance ) <EOL> try : <EOL> self . image_api . delete ( context , image_id ) <EOL> except Exception : <EOL> LOG . exception ( _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , image_id , <EOL> instance = instance ) <EOL> return decorated_function <EOL> def object_compat ( function ) : <EOL> """<STR_LIT>""" <EOL> @ functools . wraps ( function ) <EOL> def decorated_function ( self , context , * args , ** kwargs ) : <EOL> def _load_instance ( instance_or_dict ) : <EOL> if isinstance ( instance_or_dict , dict ) : <EOL> metas = [ meta for meta in ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if meta in instance_or_dict ] <EOL> instance = objects . Instance . _from_db_object ( <EOL> context , objects . Instance ( ) , instance_or_dict , <EOL> expected_attrs = metas ) <EOL> instance . _context = context <EOL> return instance <EOL> return instance_or_dict <EOL> try : <EOL> kwargs [ '<STR_LIT>' ] = _load_instance ( kwargs [ '<STR_LIT>' ] ) <EOL> except KeyError : <EOL> args = ( _load_instance ( args [ <NUM_LIT:0> ] ) , ) + args [ <NUM_LIT:1> : ] <EOL> migration = kwargs . get ( '<STR_LIT>' ) <EOL> if isinstance ( migration , dict ) : <EOL> migration = objects . Migration . _from_db_object ( <EOL> context . elevated ( ) , objects . Migration ( ) , <EOL> migration ) <EOL> kwargs [ '<STR_LIT>' ] = migration <EOL> return function ( self , context , * args , ** kwargs ) <EOL> return decorated_function <EOL> class InstanceEvents ( object ) : <EOL> def __init__ ( self ) : <EOL> self . _events = { } <EOL> @ staticmethod <EOL> def _lock_name ( instance ) : <EOL> return '<STR_LIT>' % ( instance . uuid , '<STR_LIT>' ) <EOL> def prepare_for_instance_event ( self , instance , event_name ) : <EOL> """<STR_LIT>""" <EOL> if self . _events is None : <EOL> raise exception . NovaException ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> @ utils . synchronized ( self . _lock_name ( instance ) ) <EOL> def _create_or_get_event ( ) : <EOL> instance_events = self . _events . setdefault ( instance . uuid , { } ) <EOL> return instance_events . setdefault ( event_name , <EOL> eventlet . event . Event ( ) ) <EOL> LOG . debug ( '<STR_LIT>' , <EOL> { '<STR_LIT>' : event_name } , instance = instance ) <EOL> return _create_or_get_event ( ) <EOL> def pop_instance_event ( self , instance , event ) : <EOL> """<STR_LIT>""" <EOL> no_events_sentinel = object ( ) <EOL> no_matching_event_sentinel = object ( ) <EOL> @ utils . synchronized ( self . _lock_name ( instance ) ) <EOL> def _pop_event ( ) : <EOL> if not self . _events : <EOL> LOG . debug ( '<STR_LIT>' , <EOL> instance = instance ) <EOL> return no_events_sentinel <EOL> events = self . _events . get ( instance . uuid ) <EOL> if not events : <EOL> return no_events_sentinel <EOL> _event = events . pop ( event . key , None ) <EOL> if not events : <EOL> del self . _events [ instance . uuid ] <EOL> if _event is None : <EOL> return no_matching_event_sentinel <EOL> return _event <EOL> result = _pop_event ( ) <EOL> if result is no_events_sentinel : <EOL> LOG . debug ( '<STR_LIT>' , <EOL> { '<STR_LIT>' : event . key } , <EOL> instance = instance ) <EOL> return None <EOL> elif result is no_matching_event_sentinel : <EOL> LOG . debug ( '<STR_LIT>' , <EOL> { '<STR_LIT>' : event . key , <EOL> '<STR_LIT>' : self . _events . get ( instance . uuid , { } ) . keys ( ) } , <EOL> instance = instance ) <EOL> return None <EOL> else : <EOL> return result <EOL> def clear_events_for_instance ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> @ utils . synchronized ( self . _lock_name ( instance ) ) <EOL> def _clear_events ( ) : <EOL> if self . _events is None : <EOL> LOG . debug ( '<STR_LIT>' , <EOL> instance = instance ) <EOL> return dict ( ) <EOL> return self . _events . pop ( instance . uuid , { } ) <EOL> return _clear_events ( ) <EOL> def cancel_all_events ( self ) : <EOL> if self . _events is None : <EOL> LOG . debug ( '<STR_LIT>' ) <EOL> return <EOL> our_events = self . _events <EOL> self . _events = None <EOL> for instance_uuid , events in our_events . items ( ) : <EOL> for event_name , eventlet_event in events . items ( ) : <EOL> LOG . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> { '<STR_LIT>' : event_name , <EOL> '<STR_LIT>' : instance_uuid } ) <EOL> name , tag = event_name . rsplit ( '<STR_LIT:->' , <NUM_LIT:1> ) <EOL> event = objects . InstanceExternalEvent ( <EOL> instance_uuid = instance_uuid , <EOL> name = name , status = '<STR_LIT>' , <EOL> tag = tag , data = { } ) <EOL> eventlet_event . send ( event ) <EOL> class ComputeVirtAPI ( virtapi . VirtAPI ) : <EOL> def __init__ ( self , compute ) : <EOL> super ( ComputeVirtAPI , self ) . __init__ ( ) <EOL> self . _compute = compute <EOL> def _default_error_callback ( self , event_name , instance ) : <EOL> raise exception . NovaException ( _ ( '<STR_LIT>' ) ) <EOL> @ contextlib . contextmanager <EOL> def wait_for_instance_event ( self , instance , event_names , deadline = <NUM_LIT> , <EOL> error_callback = None ) : <EOL> """<STR_LIT>""" <EOL> if error_callback is None : <EOL> error_callback = self . _default_error_callback <EOL> events = { } <EOL> for event_name in event_names : <EOL> if isinstance ( event_name , tuple ) : <EOL> name , tag = event_name <EOL> event_name = objects . InstanceExternalEvent . make_key ( <EOL> name , tag ) <EOL> try : <EOL> events [ event_name ] = ( <EOL> self . _compute . instance_events . prepare_for_instance_event ( <EOL> instance , event_name ) ) <EOL> except exception . NovaException : <EOL> error_callback ( event_name , instance ) <EOL> deadline = <NUM_LIT:0> <EOL> yield <EOL> with eventlet . timeout . Timeout ( deadline ) : <EOL> for event_name , event in events . items ( ) : <EOL> actual_event = event . wait ( ) <EOL> if actual_event . status == '<STR_LIT>' : <EOL> continue <EOL> decision = error_callback ( event_name , instance ) <EOL> if decision is False : <EOL> break <EOL> class ComputeManager ( manager . Manager ) : <EOL> """<STR_LIT>""" <EOL> target = messaging . Target ( version = '<STR_LIT>' ) <EOL> SHUTDOWN_RETRY_INTERVAL = <NUM_LIT:10> <EOL> def __init__ ( self , compute_driver = None , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . virtapi = ComputeVirtAPI ( self ) <EOL> self . network_api = network . API ( ) <EOL> self . volume_api = volume . API ( ) <EOL> self . image_api = image . API ( ) <EOL> self . _last_host_check = <NUM_LIT:0> <EOL> self . _last_bw_usage_poll = <NUM_LIT:0> <EOL> self . _bw_usage_supported = True <EOL> self . _last_bw_usage_cell_update = <NUM_LIT:0> <EOL> self . compute_api = compute . API ( ) <EOL> self . compute_rpcapi = compute_rpcapi . ComputeAPI ( ) <EOL> self . conductor_api = conductor . API ( ) <EOL> self . compute_task_api = conductor . ComputeTaskAPI ( ) <EOL> self . is_neutron_security_groups = ( <EOL> openstack_driver . is_neutron_security_groups ( ) ) <EOL> self . consoleauth_rpcapi = consoleauth . rpcapi . ConsoleAuthAPI ( ) <EOL> self . cells_rpcapi = cells_rpcapi . CellsAPI ( ) <EOL> self . scheduler_client = scheduler_client . SchedulerClient ( ) <EOL> self . _resource_tracker_dict = { } <EOL> self . instance_events = InstanceEvents ( ) <EOL> self . _sync_power_pool = eventlet . GreenPool ( ) <EOL> self . _syncs_in_progress = { } <EOL> self . send_instance_updates = CONF . scheduler_tracks_instance_changes <EOL> if CONF . max_concurrent_builds != <NUM_LIT:0> : <EOL> self . _build_semaphore = eventlet . semaphore . Semaphore ( <EOL> CONF . max_concurrent_builds ) <EOL> else : <EOL> self . _build_semaphore = compute_utils . UnlimitedSemaphore ( ) <EOL> if max ( CONF . max_concurrent_live_migrations , <NUM_LIT:0> ) != <NUM_LIT:0> : <EOL> self . _live_migration_semaphore = eventlet . semaphore . Semaphore ( <EOL> CONF . max_concurrent_live_migrations ) <EOL> else : <EOL> self . _live_migration_semaphore = compute_utils . UnlimitedSemaphore ( ) <EOL> super ( ComputeManager , self ) . __init__ ( service_name = "<STR_LIT>" , <EOL> * args , ** kwargs ) <EOL> self . driver = driver . load_compute_driver ( self . virtapi , compute_driver ) <EOL> self . use_legacy_block_device_info = self . driver . need_legacy_block_device_info <EOL> def reset ( self ) : <EOL> LOG . info ( _LI ( '<STR_LIT>' ) ) <EOL> compute_rpcapi . LAST_VERSION = None <EOL> self . compute_rpcapi = compute_rpcapi . ComputeAPI ( ) <EOL> def _get_resource_tracker ( self , nodename ) : <EOL> rt = self . _resource_tracker_dict . get ( nodename ) <EOL> if not rt : <EOL> if not self . driver . node_is_available ( nodename ) : <EOL> raise exception . NovaException ( <EOL> _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % nodename ) <EOL> rt = resource_tracker . ResourceTracker ( self . host , <EOL> self . driver , <EOL> nodename ) <EOL> self . _resource_tracker_dict [ nodename ] = rt <EOL> return rt <EOL> def _update_resource_tracker ( self , context , instance ) : <EOL> """<STR_LIT>""" <EOL> if ( instance . host == self . host and <EOL> self . driver . node_is_available ( instance . node ) ) : <EOL> rt = self . _get_resource_tracker ( instance . node ) <EOL> rt . update_usage ( context , instance ) <EOL> def _instance_update ( self , context , instance , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> for k , v in kwargs . items ( ) : <EOL> setattr ( instance , k , v ) <EOL> instance . save ( ) <EOL> self . _update_resource_tracker ( context , instance ) <EOL> def _nil_out_instance_obj_host_and_node ( self , instance ) : <EOL> instance . host = None <EOL> instance . node = None <EOL> def _set_instance_obj_error_state ( self , context , instance , <EOL> clean_task_state = False ) : <EOL> try : <EOL> instance . vm_state = vm_states . ERROR <EOL> if clean_task_state : <EOL> instance . task_state = None <EOL> instance . save ( ) <EOL> except exception . InstanceNotFound : <EOL> LOG . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' , instance = instance ) <EOL> def _get_instances_on_driver ( self , context , filters = None ) : <EOL> """<STR_LIT>""" <EOL> if not filters : <EOL> filters = { } <EOL> try : <EOL> driver_uuids = self . driver . list_instance_uuids ( ) <EOL> if len ( driver_uuids ) == <NUM_LIT:0> : <EOL> return objects . InstanceList ( ) <EOL> filters [ '<STR_LIT>' ] = driver_uuids <EOL> local_instances = objects . InstanceList . get_by_filters ( <EOL> context , filters , use_slave = True ) <EOL> return local_instances <EOL> except NotImplementedError : <EOL> pass <EOL> driver_instances = self . driver . list_instances ( ) <EOL> instances = objects . InstanceList . get_by_filters ( context , filters , <EOL> use_slave = True ) <EOL> name_map = { instance . name : instance for instance in instances } <EOL> local_instances = [ ] <EOL> for driver_instance in driver_instances : <EOL> instance = name_map . get ( driver_instance ) <EOL> if not instance : <EOL> continue <EOL> local_instances . append ( instance ) <EOL> return local_instances <EOL> def _destroy_evacuated_instances ( self , context ) : <EOL> """<STR_LIT>""" <EOL> filters = { <EOL> '<STR_LIT>' : self . host , <EOL> '<STR_LIT:status>' : [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> evacuations = objects . MigrationList . get_by_filters ( context , filters ) <EOL> if not evacuations : <EOL> return <EOL> evacuations = { mig . instance_uuid : mig for mig in evacuations } <EOL> filters = { '<STR_LIT>' : False } <EOL> local_instances = self . _get_instances_on_driver ( context , filters ) <EOL> evacuated = [ inst for inst in local_instances <EOL> if inst . uuid in evacuations ] <EOL> for instance in evacuated : <EOL> migration = evacuations [ instance . uuid ] <EOL> LOG . info ( _LI ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , instance = instance ) <EOL> try : <EOL> network_info = self . network_api . get_instance_nw_info ( <EOL> context , instance ) <EOL> bdi = self . _get_instance_block_device_info ( context , <EOL> instance ) <EOL> destroy_disks = not ( self . _is_instance_storage_shared ( <EOL> context , instance ) ) <EOL> except exception . InstanceNotFound : <EOL> network_info = network_model . NetworkInfo ( ) <EOL> bdi = { } <EOL> LOG . info ( _LI ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> instance = instance ) <EOL> destroy_disks = True <EOL> self . driver . destroy ( context , instance , <EOL> network_info , <EOL> bdi , destroy_disks ) <EOL> migration . status = '<STR_LIT>' <EOL> migration . save ( ) <EOL> def _is_instance_storage_shared ( self , context , instance , host = None ) : <EOL> shared_storage = True <EOL> data = None <EOL> try : <EOL> data = self . driver . check_instance_shared_storage_local ( context , <EOL> instance ) <EOL> if data : <EOL> shared_storage = ( self . compute_rpcapi . <EOL> check_instance_shared_storage ( context , <EOL> instance , data , host = host ) ) <EOL> except NotImplementedError : <EOL> LOG . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> instance = instance ) <EOL> shared_storage = False <EOL> except Exception : <EOL> LOG . exception ( _LE ( '<STR_LIT>' ) , <EOL> instance = instance ) <EOL> finally : <EOL> if data : <EOL> self . driver . check_instance_shared_storage_cleanup ( context , <EOL> data ) <EOL> return shared_storage <EOL> def _complete_partial_deletion ( self , context , instance ) : <EOL> """<STR_LIT>""" <EOL> system_meta = instance . system_metadata <EOL> instance . destroy ( ) <EOL> bdms = objects . BlockDeviceMappingList . get_by_instance_uuid ( <EOL> context , instance . uuid ) <EOL> quotas = objects . Quotas ( context = context ) <EOL> project_id , user_id = objects . quotas . ids_from_instance ( context , <EOL> instance ) <EOL> quotas . reserve ( project_id = project_id , user_id = user_id , instances = - <NUM_LIT:1> , <EOL> cores = - instance . vcpus , ram = - instance . memory_mb ) <EOL> self . _complete_deletion ( context , <EOL> instance , <EOL> bdms , <EOL> quotas , <EOL> system_meta ) <EOL> def _complete_deletion ( self , context , instance , bdms , <EOL> quotas , system_meta ) : <EOL> if quotas : <EOL> quotas . commit ( ) <EOL> for bdm in bdms : <EOL> bdm . destroy ( ) <EOL> self . _update_resource_tracker ( context , instance ) <EOL> self . _notify_about_instance_usage ( context , instance , "<STR_LIT>" , <EOL> system_metadata = system_meta ) <EOL> self . _clean_instance_console_tokens ( context , instance ) <EOL> self . _delete_scheduler_instance_info ( context , instance . uuid ) <EOL> def _create_reservations ( self , context , instance , project_id , user_id ) : <EOL> vcpus = instance . vcpus <EOL> mem_mb = instance . memory_mb <EOL> quotas = objects . Quotas ( context = context ) <EOL> quotas . reserve ( project_id = project_id , <EOL> user_id = user_id , <EOL> instances = - <NUM_LIT:1> , <EOL> cores = - vcpus , <EOL> ram = - mem_mb ) <EOL> return quotas <EOL> def _init_instance ( self , context , instance ) : <EOL> '''<STR_LIT>''' <EOL> if instance . host != self . host : <EOL> LOG . warning ( _LW ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> { '<STR_LIT>' : instance . uuid , <EOL> '<STR_LIT:host>' : instance . host } ) <EOL> return <EOL> if ( instance . vm_state == vm_states . SOFT_DELETED or <EOL> ( instance . vm_state == vm_states . ERROR and <EOL> instance . task_state not in <EOL> ( task_states . RESIZE_MIGRATING , task_states . DELETING ) ) ) : <EOL> LOG . debug ( "<STR_LIT>" , <EOL> instance . vm_state , instance = instance ) <EOL> return <EOL> if instance . vm_state == vm_states . DELETED : <EOL> try : <EOL> self . _complete_partial_deletion ( context , instance ) <EOL> except Exception : <EOL> msg = _LE ( '<STR_LIT>' ) <EOL> LOG . exception ( msg , instance = instance ) <EOL> return <EOL> if ( instance . vm_state == vm_states . BUILDING or <EOL> instance . task_state in [ task_states . SCHEDULING , <EOL> task_states . BLOCK_DEVICE_MAPPING , <EOL> task_states . NETWORKING , <EOL> task_states . SPAWNING ] ) : <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , instance = instance ) <EOL> instance . task_state = None <EOL> instance . vm_state = vm_states . ERROR <EOL> instance . save ( ) <EOL> return <EOL> if ( instance . vm_state in [ vm_states . ACTIVE , vm_states . STOPPED ] and <EOL> instance . task_state in [ task_states . REBUILDING , <EOL> task_states . REBUILD_BLOCK_DEVICE_MAPPING , <EOL> task_states . REBUILD_SPAWNING ] ) : <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , instance = instance ) <EOL> instance . task_state = None <EOL> instance . vm_state = vm_states . ERROR <EOL> instance . save ( ) <EOL> return <EOL> if ( instance . vm_state != vm_states . ERROR and <EOL> instance . task_state in [ task_states . IMAGE_SNAPSHOT_PENDING , <EOL> task_states . IMAGE_PENDING_UPLOAD , <EOL> task_states . IMAGE_UPLOADING , <EOL> task_states . IMAGE_SNAPSHOT ] ) : <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> instance . task_state , instance = instance ) <EOL> try : <EOL> self . _post_interrupted_snapshot_cleanup ( context , instance ) <EOL> except Exception : <EOL> msg = _LE ( '<STR_LIT>' ) <EOL> LOG . exception ( msg , instance = instance ) <EOL> instance . task_state = None <EOL> instance . save ( ) <EOL> if ( instance . vm_state != vm_states . ERROR and <EOL> instance . task_state in [ task_states . RESIZE_PREP ] ) : <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> instance [ '<STR_LIT>' ] , instance = instance ) <EOL> instance . task_state = None <EOL> instance . save ( ) <EOL> if instance . task_state == task_states . DELETING : <EOL> try : <EOL> LOG . info ( _LI ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , instance = instance ) <EOL> instance . obj_load_attr ( '<STR_LIT>' ) <EOL> instance . obj_load_attr ( '<STR_LIT>' ) <EOL> bdms = objects . BlockDeviceMappingList . get_by_instance_uuid ( <EOL> context , instance . uuid ) <EOL> project_id , user_id = objects . quotas . ids_from_instance ( <EOL> context , instance ) <EOL> quotas = self . _create_reservations ( context , instance , <EOL> project_id , user_id ) <EOL> self . _delete_instance ( context , instance , bdms , quotas ) <EOL> except Exception : <EOL> msg = _LE ( '<STR_LIT>' ) <EOL> LOG . exception ( msg , instance = instance ) <EOL> self . _set_instance_obj_error_state ( context , instance ) <EOL> return <EOL> try_reboot , reboot_type = self . _retry_reboot ( context , instance ) <EOL> current_power_state = self . _get_power_state ( context , instance ) <EOL> if try_reboot : <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> { '<STR_LIT>' : instance . task_state , <EOL> '<STR_LIT>' : current_power_state } , <EOL> instance = instance ) <EOL> soft_types = [ task_states . REBOOT_STARTED , <EOL> task_states . REBOOT_PENDING , <EOL> task_states . REBOOTING ] <EOL> if instance . task_state in soft_types and reboot_type == '<STR_LIT>' : <EOL> instance . task_state = task_states . REBOOT_PENDING_HARD <EOL> instance . save ( ) <EOL> self . reboot_instance ( context , instance , block_device_info = None , <EOL> reboot_type = reboot_type ) <EOL> return <EOL> elif ( current_power_state == power_state . RUNNING and <EOL> instance . task_state in [ task_states . REBOOT_STARTED , <EOL> task_states . REBOOT_STARTED_HARD , <EOL> task_states . PAUSING , <EOL> task_states . UNPAUSING ] ) : <EOL> LOG . warning ( _LW ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : instance . task_state , <EOL> '<STR_LIT>' : current_power_state } , <EOL> instance = instance ) <EOL> instance . task_state = None <EOL> instance . vm_state = vm_states . ACTIVE <EOL> instance . save ( ) <EOL> elif ( current_power_state == power_state . PAUSED and <EOL> instance . task_state == task_states . UNPAUSING ) : <EOL> LOG . warning ( _LW ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : instance . task_state , <EOL> '<STR_LIT>' : current_power_state } , <EOL> instance = instance ) <EOL> try : <EOL> self . unpause_instance ( context , instance ) <EOL> except NotImplementedError : <EOL> pass <EOL> except Exception : <EOL> LOG . exception ( _LE ( '<STR_LIT>' ) , <EOL> instance = instance ) <EOL> return <EOL> if instance . task_state == task_states . POWERING_OFF : <EOL> try : <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> instance . task_state , instance = instance ) <EOL> self . stop_instance ( context , instance , True ) <EOL> except Exception : <EOL> msg = _LE ( '<STR_LIT>' ) <EOL> LOG . exception ( msg , instance = instance ) <EOL> return <EOL> if instance . task_state == task_states . POWERING_ON : <EOL> try : <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> instance . task_state , instance = instance ) <EOL> self . start_instance ( context , instance ) <EOL> except Exception : <EOL> msg = _LE ( '<STR_LIT>' ) <EOL> LOG . exception ( msg , instance = instance ) <EOL> return <EOL> net_info = compute_utils . get_nw_info_for_instance ( instance ) <EOL> try : <EOL> self . driver . plug_vifs ( instance , net_info ) <EOL> except NotImplementedError as e : <EOL> LOG . debug ( e , instance = instance ) <EOL> except exception . VirtualInterfacePlugException : <EOL> LOG . exception ( _LE ( "<STR_LIT>" ) , instance = instance ) <EOL> self . _set_instance_obj_error_state ( context , instance ) <EOL> return <EOL> if instance . task_state == task_states . RESIZE_MIGRATING : <EOL> try : <EOL> power_on = ( instance . system_metadata . get ( '<STR_LIT>' ) != <EOL> vm_states . STOPPED ) <EOL> block_dev_info = self . _get_instance_block_device_info ( context , <EOL> instance ) <EOL> self . driver . finish_revert_migration ( context , <EOL> instance , net_info , block_dev_info , power_on ) <EOL> except Exception : <EOL> LOG . exception ( _LE ( '<STR_LIT>' ) , <EOL> instance = instance ) <EOL> finally : <EOL> LOG . info ( _LI ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> instance = instance ) <EOL> instance . task_state = None <EOL> instance . save ( ) <EOL> if instance . task_state == task_states . MIGRATING : <EOL> instance . task_state = None <EOL> instance . save ( expected_task_state = [ task_states . MIGRATING ] ) <EOL> db_state = instance . power_state <EOL> drv_state = self . _get_power_state ( context , instance ) <EOL> expect_running = ( db_state == power_state . RUNNING and <EOL> drv_state != db_state ) <EOL> LOG . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> { '<STR_LIT>' : drv_state , '<STR_LIT>' : db_state } , <EOL> instance = instance ) <EOL> if expect_running and CONF . resume_guests_state_on_host_boot : <EOL> LOG . info ( _LI ( '<STR_LIT>' ) , <EOL> instance = instance ) <EOL> block_device_info = self . _get_instance_block_device_info ( context , instance ) <EOL> try : <EOL> self . driver . resume_state_on_host_boot ( <EOL> context , instance , net_info , block_device_info ) <EOL> except NotImplementedError : <EOL> LOG . warning ( _LW ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , instance = instance ) <EOL> except Exception : <EOL> LOG . warning ( _LW ( '<STR_LIT>' ) , <EOL> instance = instance ) <EOL> self . _set_instance_obj_error_state ( context , instance ) <EOL> elif drv_state == power_state . RUNNING : <EOL> try : <EOL> self . driver . ensure_filtering_rules_for_instance ( <EOL> instance , net_info ) <EOL> except NotImplementedError : <EOL> LOG . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' , instance = instance ) <EOL> def _retry_reboot ( self , context , instance ) : <EOL> current_power_state = self . _get_power_state ( context , instance ) <EOL> current_task_state = instance . task_state <EOL> retry_reboot = False <EOL> reboot_type = compute_utils . get_reboot_type ( current_task_state , <EOL> current_power_state ) <EOL> pending_soft = ( current_task_state == task_states . REBOOT_PENDING and <EOL> instance . vm_state in vm_states . ALLOW_SOFT_REBOOT ) <EOL> pending_hard = ( current_task_state == task_states . REBOOT_PENDING_HARD <EOL> and instance . vm_state in vm_states . ALLOW_HARD_REBOOT ) <EOL> started_not_running = ( current_task_state in <EOL> [ task_states . REBOOT_STARTED , <EOL> task_states . REBOOT_STARTED_HARD ] and <EOL> current_power_state != power_state . RUNNING ) <EOL> if pending_soft or pending_hard or started_not_running : <EOL> retry_reboot = True <EOL> return retry_reboot , reboot_type <EOL> def handle_lifecycle_event ( self , event ) : <EOL> LOG . info ( _LI ( "<STR_LIT>" ) , <EOL> { '<STR_LIT:state>' : event . get_name ( ) } , <EOL> instance_uuid = event . get_instance_uuid ( ) ) <EOL> context = nova . context . get_admin_context ( read_deleted = '<STR_LIT:yes>' ) <EOL> instance = objects . Instance . get_by_uuid ( context , <EOL> event . get_instance_uuid ( ) , <EOL> expected_attrs = [ ] ) <EOL> vm_power_state = None <EOL> if event . get_transition ( ) == virtevent . EVENT_LIFECYCLE_STOPPED : <EOL> vm_power_state = power_state . SHUTDOWN <EOL> elif event . get_transition ( ) == virtevent . EVENT_LIFECYCLE_STARTED : <EOL> vm_power_state = power_state . RUNNING <EOL> elif event . get_transition ( ) == virtevent . EVENT_LIFECYCLE_PAUSED : <EOL> vm_power_state = power_state . PAUSED <EOL> elif event . get_transition ( ) == virtevent . EVENT_LIFECYCLE_RESUMED : <EOL> vm_power_state = power_state . RUNNING <EOL> elif event . get_transition ( ) == virtevent . EVENT_LIFECYCLE_SUSPENDED : <EOL> vm_power_state = power_state . SUSPENDED <EOL> else : <EOL> LOG . warning ( _LW ( "<STR_LIT>" ) , <EOL> event . get_transition ( ) ) <EOL> current_power_state = self . _get_power_state ( context , instance ) <EOL> if current_power_state == vm_power_state : <EOL> LOG . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> { '<STR_LIT>' : event . get_name ( ) , <EOL> '<STR_LIT>' : instance . vm_state , <EOL> '<STR_LIT>' : instance . task_state , <EOL> '<STR_LIT>' : instance . power_state , <EOL> '<STR_LIT>' : vm_power_state } , <EOL> instance_uuid = instance . uuid ) <EOL> self . _sync_instance_power_state ( context , <EOL> instance , <EOL> vm_power_state ) <EOL> def handle_events ( self , event ) : <EOL> if isinstance ( event , virtevent . LifecycleEvent ) : <EOL> try : <EOL> self . handle_lifecycle_event ( event ) <EOL> except exception . InstanceNotFound : <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , event ) <EOL> else : <EOL> LOG . debug ( "<STR_LIT>" , event ) <EOL> def init_virt_events ( self ) : <EOL> if CONF . workarounds . handle_virt_lifecycle_events : <EOL> self . driver . register_event_listener ( self . handle_events ) <EOL> else : <EOL> if CONF . sync_power_state_interval < <NUM_LIT:0> : <EOL> LOG . warning ( _LW ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> else : <EOL> LOG . info ( _LI ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> def init_host ( self ) : <EOL> """<STR_LIT>""" <EOL> self . driver . init_host ( host = self . host ) <EOL> context = nova . context . get_admin_context ( ) <EOL> instances = objects . InstanceList . get_by_host ( <EOL> context , self . host , expected_attrs = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> if CONF . defer_iptables_apply : <EOL> self . driver . filter_defer_apply_on ( ) <EOL> self . init_virt_events ( ) <EOL> try : <EOL> self . _destroy_evacuated_instances ( context ) <EOL> for instance in instances : <EOL> self . _init_instance ( context , instance ) <EOL> finally : <EOL> if CONF . defer_iptables_apply : <EOL> self . driver . filter_defer_apply_off ( ) <EOL> self . _update_scheduler_instance_info ( context , instances ) <EOL> def cleanup_host ( self ) : <EOL> self . driver . register_event_listener ( None ) <EOL> self . instance_events . cancel_all_events ( ) <EOL> self . driver . cleanup_host ( host = self . host ) <EOL> def pre_start_hook ( self ) : <EOL> """<STR_LIT>""" <EOL> self . update_available_resource ( nova . context . get_admin_context ( ) ) <EOL> def _get_power_state ( self , context , instance ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( '<STR_LIT>' , instance = instance ) <EOL> try : <EOL> return self . driver . get_info ( instance ) . state <EOL> except exception . InstanceNotFound : <EOL> return power_state . NOSTATE <EOL> def get_console_topic ( self , context ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' % ( CONF . console_topic , CONF . console_host ) <EOL> @ wrap_exception ( ) <EOL> def get_console_pool_info ( self , context , console_type ) : <EOL> return self . driver . get_console_pool_info ( console_type ) <EOL> @ wrap_exception ( ) <EOL> def refresh_security_group_rules ( self , context , security_group_id ) : <EOL> """<STR_LIT>""" <EOL> return self . driver . refresh_security_group_rules ( security_group_id ) <EOL> @ object_compat <EOL> @ wrap_exception ( ) <EOL> def refresh_instance_security_rules ( self , context , instance ) : <EOL> """<STR_LIT>""" <EOL> @ utils . synchronized ( instance . uuid ) <EOL> def _sync_refresh ( ) : <EOL> try : <EOL> return self . driver . refresh_instance_security_rules ( instance ) <EOL> except NotImplementedError : <EOL> LOG . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' , instance = instance ) <EOL> return _sync_refresh ( ) <EOL> def _await_block_device_map_created ( self , context , vol_id ) : <EOL> start = time . time ( ) <EOL> retries = CONF . block_device_allocate_retries <EOL> if retries < <NUM_LIT:0> : <EOL> LOG . warning ( _LW ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : retries } ) <EOL> attempts = <NUM_LIT:1> <EOL> if retries >= <NUM_LIT:1> : <EOL> attempts = retries + <NUM_LIT:1> <EOL> for attempt in range ( <NUM_LIT:1> , attempts + <NUM_LIT:1> ) : <EOL> volume = self . volume_api . get ( context , vol_id ) <EOL> volume_status = volume [ '<STR_LIT:status>' ] <EOL> if volume_status not in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if volume_status == '<STR_LIT>' : <EOL> return attempt <EOL> LOG . warning ( _LW ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : vol_id , <EOL> '<STR_LIT>' : volume_status } ) <EOL> break <EOL> greenthread . sleep ( CONF . block_device_allocate_retries_interval ) <EOL> raise exception . VolumeNotCreated ( volume_id = vol_id , <EOL> seconds = int ( time . time ( ) - start ) , <EOL> attempts = attempt , <EOL> volume_status = volume_status ) <EOL> def _decode_files ( self , injected_files ) : <EOL> """<STR_LIT>""" <EOL> if not injected_files : <EOL> return [ ] <EOL> def _decode ( f ) : <EOL> path , contents = f <EOL> try : <EOL> decoded = base64 . b64decode ( contents ) <EOL> return path , decoded <EOL> except TypeError : <EOL> raise exception . Base64Exception ( path = path ) <EOL> return [ _decode ( f ) for f in injected_files ] <EOL> def _validate_instance_group_policy ( self , context , instance , <EOL> filter_properties ) : <EOL> scheduler_hints = filter_properties . get ( '<STR_LIT>' ) or { } <EOL> group_hint = scheduler_hints . get ( '<STR_LIT>' ) <EOL> if not group_hint : <EOL> return <EOL> @ utils . synchronized ( group_hint ) <EOL> def _do_validation ( context , instance , group_hint ) : <EOL> group = objects . InstanceGroup . get_by_hint ( context , group_hint ) <EOL> if '<STR_LIT>' in group . policies : <EOL> group_hosts = group . get_hosts ( exclude = [ instance . uuid ] ) <EOL> if self . host in group_hosts : <EOL> msg = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> raise exception . RescheduledException ( <EOL> instance_uuid = instance . uuid , <EOL> reason = msg ) <EOL> elif '<STR_LIT>' in group . policies : <EOL> group_hosts = group . get_hosts ( exclude = [ instance . uuid ] ) <EOL> if group_hosts and self . host not in group_hosts : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise exception . RescheduledException ( <EOL> instance_uuid = instance . uuid , <EOL> reason = msg ) <EOL> _do_validation ( context , instance , group_hint ) <EOL> def _log_original_error ( self , exc_info , instance_uuid ) : <EOL> LOG . error ( _LE ( '<STR_LIT>' ) , exc_info [ <NUM_LIT:1> ] , instance_uuid = instance_uuid , <EOL> exc_info = exc_info ) <EOL> def _reschedule ( self , context , request_spec , filter_properties , <EOL> instance , reschedule_method , method_args , task_state , <EOL> exc_info = None ) : <EOL> """<STR_LIT>""" <EOL> instance_uuid = instance . uuid <EOL> retry = filter_properties . get ( '<STR_LIT>' ) <EOL> if not retry : <EOL> LOG . debug ( "<STR_LIT>" , <EOL> instance_uuid = instance_uuid ) <EOL> return <EOL> if not request_spec : <EOL> LOG . debug ( "<STR_LIT>" , <EOL> instance_uuid = instance_uuid ) <EOL> return <EOL> LOG . debug ( "<STR_LIT>" , <EOL> { '<STR_LIT>' : reschedule_method . __name__ , <EOL> '<STR_LIT>' : retry [ '<STR_LIT>' ] } , instance_uuid = instance_uuid ) <EOL> self . _instance_update ( context , instance , task_state = task_state ) <EOL> if exc_info : <EOL> retry [ '<STR_LIT>' ] = traceback . format_exception_only ( exc_info [ <NUM_LIT:0> ] , <EOL> exc_info [ <NUM_LIT:1> ] ) <EOL> reschedule_method ( context , * method_args ) <EOL> return True <EOL> @ periodic_task . periodic_task <EOL> def _check_instance_build_time ( self , context ) : <EOL> """<STR_LIT>""" <EOL> timeout = CONF . instance_build_timeout <EOL> if timeout == <NUM_LIT:0> : <EOL> return <EOL> filters = { '<STR_LIT>' : vm_states . BUILDING , <EOL> '<STR_LIT:host>' : self . host } <EOL> building_insts = objects . InstanceList . get_by_filters ( context , <EOL> filters , expected_attrs = [ ] , use_slave = True ) <EOL> for instance in building_insts : <EOL> if timeutils . is_older_than ( instance . created_at , timeout ) : <EOL> self . _set_instance_obj_error_state ( context , instance ) <EOL> LOG . warning ( _LW ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , instance = instance ) <EOL> def _check_instance_exists ( self , context , instance ) : <EOL> """<STR_LIT>""" <EOL> if self . driver . instance_exists ( instance ) : <EOL> raise exception . InstanceExists ( name = instance . name ) <EOL> def _allocate_network_async ( self , context , instance , requested_networks , <EOL> macs , security_groups , is_vpn , dhcp_options ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( "<STR_LIT>" , <EOL> instance = instance ) <EOL> retries = CONF . network_allocate_retries <EOL> if retries < <NUM_LIT:0> : <EOL> LOG . warning ( _LW ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : retries } ) <EOL> retries = <NUM_LIT:0> <EOL> attempts = retries + <NUM_LIT:1> <EOL> retry_time = <NUM_LIT:1> <EOL> bind_host_id = self . driver . network_binding_host_id ( context , instance ) <EOL> for attempt in range ( <NUM_LIT:1> , attempts + <NUM_LIT:1> ) : <EOL> try : <EOL> nwinfo = self . network_api . allocate_for_instance ( <EOL> context , instance , vpn = is_vpn , <EOL> requested_networks = requested_networks , <EOL> macs = macs , <EOL> security_groups = security_groups , <EOL> dhcp_options = dhcp_options , <EOL> bind_host_id = bind_host_id ) <EOL> LOG . debug ( '<STR_LIT>' , nwinfo , <EOL> instance = instance ) <EOL> instance . system_metadata [ '<STR_LIT>' ] = '<STR_LIT:True>' <EOL> return nwinfo <EOL> except Exception : <EOL> exc_info = sys . exc_info ( ) <EOL> log_info = { '<STR_LIT>' : attempt , <EOL> '<STR_LIT>' : attempts } <EOL> if attempt == attempts : <EOL> LOG . exception ( _LE ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> log_info ) <EOL> six . reraise ( * exc_info ) <EOL> LOG . warning ( _LW ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> log_info , instance = instance ) <EOL> time . sleep ( retry_time ) <EOL> retry_time *= <NUM_LIT:2> <EOL> if retry_time > <NUM_LIT:30> : <EOL> retry_time = <NUM_LIT:30> <EOL> def _build_networks_for_instance ( self , context , instance , <EOL> requested_networks , security_groups ) : <EOL> if strutils . bool_from_string ( <EOL> instance . system_metadata . get ( '<STR_LIT>' , '<STR_LIT:False>' ) ) : <EOL> self . network_api . setup_instance_network_on_host ( <EOL> context , instance , instance . host ) <EOL> return self . network_api . get_instance_nw_info ( context , instance ) <EOL> if not self . is_neutron_security_groups : <EOL> security_groups = [ ] <EOL> macs = self . driver . macs_for_instance ( instance ) <EOL> dhcp_options = self . driver . dhcp_options_for_instance ( instance ) <EOL> network_info = self . _allocate_network ( context , instance , <EOL> requested_networks , macs , security_groups , dhcp_options ) <EOL> return network_info <EOL> def _allocate_network ( self , context , instance , requested_networks , macs , <EOL> security_groups , dhcp_options ) : <EOL> """<STR_LIT>""" <EOL> instance . vm_state = vm_states . BUILDING <EOL> instance . task_state = task_states . NETWORKING <EOL> instance . save ( expected_task_state = [ None ] ) <EOL> self . _update_resource_tracker ( context , instance ) <EOL> is_vpn = pipelib . is_vpn_image ( instance . image_ref ) <EOL> return network_model . NetworkInfoAsyncWrapper ( <EOL> self . _allocate_network_async , context , instance , <EOL> requested_networks , macs , security_groups , is_vpn , <EOL> dhcp_options ) <EOL> def _default_root_device_name ( self , instance , image_meta , root_bdm ) : <EOL> try : <EOL> return self . driver . default_root_device_name ( instance , <EOL> image_meta , <EOL> root_bdm ) <EOL> except NotImplementedError : <EOL> return compute_utils . get_next_device_name ( instance , [ ] ) <EOL> def _default_device_names_for_instance ( self , instance , <EOL> root_device_name , <EOL> * block_device_lists ) : <EOL> try : <EOL> self . driver . default_device_names_for_instance ( instance , <EOL> root_device_name , <EOL> * block_device_lists ) <EOL> except NotImplementedError : <EOL> compute_utils . default_device_names_for_instance ( <EOL> instance , root_device_name , * block_device_lists ) <EOL> def _get_device_name_for_instance ( self , instance , bdms , block_device_obj ) : <EOL> block_device_obj = block_device_obj . obj_clone ( ) <EOL> try : <EOL> return self . driver . get_device_name_for_instance ( <EOL> instance , bdms , block_device_obj ) <EOL> except NotImplementedError : <EOL> return compute_utils . get_device_name_for_instance ( <EOL> instance , bdms , block_device_obj . get ( "<STR_LIT>" ) ) <EOL> def _default_block_device_names ( self , context , instance , <EOL> image_meta , block_devices ) : <EOL> """<STR_LIT>""" <EOL> root_bdm = block_device . get_root_bdm ( block_devices ) <EOL> if not root_bdm : <EOL> return <EOL> root_device_name = None <EOL> update_root_bdm = False <EOL> if root_bdm . device_name : <EOL> root_device_name = root_bdm . device_name <EOL> instance . root_device_name = root_device_name <EOL> elif instance . root_device_name : <EOL> root_device_name = instance . root_device_name <EOL> root_bdm . device_name = root_device_name <EOL> update_root_bdm = True <EOL> else : <EOL> root_device_name = self . _default_root_device_name ( instance , <EOL> image_meta , <EOL> root_bdm ) <EOL> instance . root_device_name = root_device_name <EOL> root_bdm . device_name = root_device_name <EOL> update_root_bdm = True <EOL> if update_root_bdm : <EOL> root_bdm . save ( ) <EOL> ephemerals = list ( filter ( block_device . new_format_is_ephemeral , <EOL> block_devices ) ) <EOL> swap = list ( filter ( block_device . new_format_is_swap , <EOL> block_devices ) ) <EOL> block_device_mapping = list ( filter ( <EOL> driver_block_device . is_block_device_mapping , block_devices ) ) <EOL> self . _default_device_names_for_instance ( instance , <EOL> root_device_name , <EOL> ephemerals , <EOL> swap , <EOL> block_device_mapping ) <EOL> def _block_device_info_to_legacy ( self , block_device_info ) : <EOL> """<STR_LIT>""" <EOL> if self . use_legacy_block_device_info : <EOL> ephemerals = driver_block_device . legacy_block_devices ( <EOL> driver . block_device_info_get_ephemerals ( block_device_info ) ) <EOL> mapping = driver_block_device . legacy_block_devices ( <EOL> driver . block_device_info_get_mapping ( block_device_info ) ) <EOL> swap = block_device_info [ '<STR_LIT>' ] <EOL> if swap : <EOL> swap = swap . legacy ( ) <EOL> block_device_info . update ( { <EOL> '<STR_LIT>' : ephemerals , <EOL> '<STR_LIT>' : swap , <EOL> '<STR_LIT>' : mapping } ) <EOL> def _check_dev_name ( self , bdms , instance ) : <EOL> bdms_no_device_name = [ x for x in bdms if x . device_name is None ] <EOL> for bdm in bdms_no_device_name : <EOL> device_name = self . _get_device_name_for_instance ( instance , <EOL> bdms , <EOL> bdm ) <EOL> values = { '<STR_LIT>' : device_name } <EOL> bdm . update ( values ) <EOL> def _prep_block_device ( self , context , instance , bdms , <EOL> do_check_attach = True ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _check_dev_name ( bdms , instance ) <EOL> block_device_info = driver . get_block_device_info ( instance , bdms ) <EOL> mapping = driver . block_device_info_get_mapping ( block_device_info ) <EOL> driver_block_device . attach_block_devices ( <EOL> mapping , context , instance , self . volume_api , self . driver , <EOL> do_check_attach = do_check_attach , <EOL> wait_func = self . _await_block_device_map_created ) <EOL> self . _block_device_info_to_legacy ( block_device_info ) <EOL> return block_device_info <EOL> except exception . OverQuota : <EOL> msg = _LW ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> LOG . warning ( msg , instance = instance ) <EOL> raise exception . VolumeLimitExceeded ( ) <EOL> except Exception : <EOL> LOG . exception ( _LE ( '<STR_LIT>' ) , <EOL> instance = instance ) <EOL> raise exception . InvalidBDM ( ) <EOL> def _update_instance_after_spawn ( self , context , instance ) : <EOL> instance . power_state = self . _get_power_state ( context , instance ) <EOL> instance . vm_state = vm_states . ACTIVE <EOL> instance . task_state = None <EOL> instance . launched_at = timeutils . utcnow ( ) <EOL> configdrive . update_instance ( instance ) <EOL> def _update_scheduler_instance_info ( self , context , instance ) : <EOL> """<STR_LIT>""" <EOL> if not self . send_instance_updates : <EOL> return <EOL> if isinstance ( instance , obj_instance . Instance ) : <EOL> instance = objects . InstanceList ( objects = [ instance ] ) <EOL> context = context . elevated ( ) <EOL> self . scheduler_client . update_instance_info ( context , self . host , <EOL> instance ) <EOL> def _delete_scheduler_instance_info ( self , context , instance_uuid ) : <EOL> """<STR_LIT>""" <EOL> if not self . send_instance_updates : <EOL> return <EOL> context = context . elevated ( ) <EOL> self . scheduler_client . delete_instance_info ( context , self . host , <EOL> instance_uuid ) <EOL> @ periodic_task . periodic_task ( spacing = CONF . scheduler_instance_sync_interval ) <EOL> def _sync_scheduler_instance_info ( self , context ) : <EOL> if not self . send_instance_updates : <EOL> return <EOL> context = context . elevated ( ) <EOL> instances = objects . InstanceList . get_by_host ( context , self . host , <EOL> expected_attrs = [ ] , <EOL> use_slave = True ) <EOL> uuids = [ instance . uuid for instance in instances ] <EOL> self . scheduler_client . sync_instance_info ( context , self . host , uuids ) <EOL> def _notify_about_instance_usage ( self , context , instance , event_suffix , <EOL> network_info = None , system_metadata = None , <EOL> extra_usage_info = None , fault = None ) : <EOL> compute_utils . notify_about_instance_usage ( <EOL> self . notifier , context , instance , event_suffix , <EOL> network_info = network_info , <EOL> system_metadata = system_metadata , <EOL> extra_usage_info = extra_usage_info , fault = fault ) <EOL> def _deallocate_network ( self , context , instance , <EOL> requested_networks = None ) : <EOL> LOG . debug ( '<STR_LIT>' , instance = instance ) <EOL> with timeutils . StopWatch ( ) as timer : <EOL> self . network_api . deallocate_for_instance ( <EOL> context , instance , requested_networks = requested_networks ) <EOL> LOG . info ( _LI ( '<STR_LIT>' ) , <EOL> timer . elapsed ( ) , instance = instance ) <EOL> def _get_instance_block_device_info ( self , context , instance , <EOL> refresh_conn_info = False , <EOL> bdms = None ) : <EOL> """<STR_LIT>""" <EOL> if not bdms : <EOL> bdms = objects . BlockDeviceMappingList . get_by_instance_uuid ( <EOL> context , instance . uuid ) <EOL> block_device_info = driver . get_block_device_info ( instance , bdms ) <EOL> if not refresh_conn_info : <EOL> block_device_info [ '<STR_LIT>' ] = [ <EOL> bdm for bdm in driver . block_device_info_get_mapping ( <EOL> block_device_info ) <EOL> if bdm . get ( '<STR_LIT>' ) ] <EOL> else : <EOL> driver_block_device . refresh_conn_infos ( <EOL> driver . block_device_info_get_mapping ( block_device_info ) , <EOL> context , instance , self . volume_api , self . driver ) <EOL> self . _block_device_info_to_legacy ( block_device_info ) <EOL> return block_device_info <EOL> @ wrap_exception ( ) <EOL> @ reverts_task_state <EOL> @ wrap_instance_fault <EOL> def build_and_run_instance ( self , context , instance , image , request_spec , <EOL> filter_properties , admin_password = None , <EOL> injected_files = None , requested_networks = None , <EOL> security_groups = None , block_device_mapping = None , <EOL> node = None , limits = None ) : <EOL> @ utils . synchronized ( instance . uuid ) <EOL> def _locked_do_build_and_run_instance ( * args , ** kwargs ) : <EOL> with self . _build_semaphore : <EOL> self . _do_build_and_run_instance ( * args , ** kwargs ) <EOL> utils . spawn_n ( _locked_do_build_and_run_instance , <EOL> context , instance , image , request_spec , <EOL> filter_properties , admin_password , injected_files , <EOL> requested_networks , security_groups , <EOL> block_device_mapping , node , limits ) <EOL> @ hooks . add_hook ( '<STR_LIT>' ) <EOL> @ wrap_exception ( ) <EOL> @ reverts_task_state <EOL> @ wrap_instance_event <EOL> @ wrap_instance_fault <EOL> def _do_build_and_run_instance ( self , context , instance , image , <EOL> request_spec , filter_properties , admin_password , injected_files , <EOL> requested_networks , security_groups , block_device_mapping , <EOL> node = None , limits = None ) : <EOL> try : <EOL> LOG . debug ( '<STR_LIT>' , context = context , <EOL> instance = instance ) <EOL> instance . vm_state = vm_states . BUILDING <EOL> instance . task_state = None <EOL> instance . save ( expected_task_state = <EOL> ( task_states . SCHEDULING , None ) ) <EOL> except exception . InstanceNotFound : <EOL> msg = '<STR_LIT>' <EOL> LOG . debug ( msg , instance = instance ) <EOL> return build_results . FAILED <EOL> except exception . UnexpectedTaskStateError as e : <EOL> LOG . debug ( e . format_message ( ) , instance = instance ) <EOL> return build_results . FAILED <EOL> decoded_files = self . _decode_files ( injected_files ) <EOL> if limits is None : <EOL> limits = { } <EOL> if node is None : <EOL> node = self . driver . get_available_nodes ( refresh = True ) [ <NUM_LIT:0> ] <EOL> LOG . debug ( '<STR_LIT>' , node , <EOL> instance = instance ) <EOL> try : <EOL> with timeutils . StopWatch ( ) as timer : <EOL> self . _build_and_run_instance ( context , instance , image , <EOL> decoded_files , admin_password , requested_networks , <EOL> security_groups , block_device_mapping , node , limits , <EOL> filter_properties ) <EOL> LOG . info ( _LI ( '<STR_LIT>' ) , <EOL> timer . elapsed ( ) , instance = instance ) <EOL> return build_results . ACTIVE <EOL> except exception . RescheduledException as e : <EOL> retry = filter_properties . get ( '<STR_LIT>' ) <EOL> if not retry : <EOL> LOG . debug ( "<STR_LIT>" , <EOL> instance = instance ) <EOL> self . _cleanup_allocated_networks ( context , instance , <EOL> requested_networks ) <EOL> compute_utils . add_instance_fault_from_exc ( context , <EOL> instance , e , sys . exc_info ( ) , <EOL> fault_message = e . kwargs [ '<STR_LIT>' ] ) <EOL> self . _nil_out_instance_obj_host_and_node ( instance ) <EOL> self . _set_instance_obj_error_state ( context , instance , <EOL> clean_task_state = True ) <EOL> return build_results . FAILED <EOL> LOG . debug ( e . format_message ( ) , instance = instance ) <EOL> retry [ '<STR_LIT>' ] = traceback . format_exception ( * sys . exc_info ( ) ) <EOL> retry [ '<STR_LIT>' ] = e . kwargs [ '<STR_LIT>' ] <EOL> if ( self . driver . deallocate_networks_on_reschedule ( instance ) or <EOL> self . deallocate_sriov_ports_on_reschedule ( instance ) ) : <EOL> self . _cleanup_allocated_networks ( context , instance , <EOL> requested_networks ) <EOL> else : <EOL> self . network_api . cleanup_instance_network_on_host ( <EOL> context , instance , self . host ) <EOL> self . _nil_out_instance_obj_host_and_node ( instance ) <EOL> instance . task_state = task_states . SCHEDULING <EOL> instance . save ( ) <EOL> self . compute_task_api . build_instances ( context , [ instance ] , <EOL> image , filter_properties , admin_password , <EOL> injected_files , requested_networks , security_groups , <EOL> block_device_mapping ) <EOL> return build_results . RESCHEDULED <EOL> except ( exception . InstanceNotFound , <EOL> exception . UnexpectedDeletingTaskStateError ) : <EOL> msg = '<STR_LIT>' <EOL> LOG . debug ( msg , instance = instance ) <EOL> self . _cleanup_allocated_networks ( context , instance , <EOL> requested_networks ) <EOL> return build_results . FAILED <EOL> except exception . BuildAbortException as e : <EOL> LOG . exception ( e . format_message ( ) , instance = instance ) <EOL> self . _cleanup_allocated_networks ( context , instance , <EOL> requested_networks ) <EOL> self . _cleanup_volumes ( context , instance . uuid , <EOL> block_device_mapping , raise_exc = False ) <EOL> compute_utils . add_instance_fault_from_exc ( context , instance , <EOL> e , sys . exc_info ( ) ) <EOL> self . _nil_out_instance_obj_host_and_node ( instance ) <EOL> self . _set_instance_obj_error_state ( context , instance , <EOL> clean_task_state = True ) <EOL> return build_results . FAILED <EOL> except Exception as e : <EOL> msg = _LE ( '<STR_LIT>' ) <EOL> LOG . exception ( msg , instance = instance ) <EOL> self . _cleanup_allocated_networks ( context , instance , <EOL> requested_networks ) <EOL> self . _cleanup_volumes ( context , instance . uuid , <EOL> block_device_mapping , raise_exc = False ) <EOL> compute_utils . add_instance_fault_from_exc ( context , instance , <EOL> e , sys . exc_info ( ) ) <EOL> self . _nil_out_instance_obj_host_and_node ( instance ) <EOL> self . _set_instance_obj_error_state ( context , instance , <EOL> clean_task_state = True ) <EOL> return build_results . FAILED <EOL> def deallocate_sriov_ports_on_reschedule ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> info_cache = instance . info_cache <EOL> def _has_sriov_port ( vif ) : <EOL> return vif [ '<STR_LIT>' ] in network_model . VNIC_TYPES_SRIOV <EOL> if ( info_cache and info_cache . network_info ) : <EOL> for vif in info_cache . network_info : <EOL> if _has_sriov_port ( vif ) : <EOL> return True <EOL> return False <EOL> def _build_and_run_instance ( self , context , instance , image , injected_files , <EOL> admin_password , requested_networks , security_groups , <EOL> block_device_mapping , node , limits , filter_properties ) : <EOL> image_name = image . get ( '<STR_LIT:name>' ) <EOL> self . _notify_about_instance_usage ( context , instance , '<STR_LIT>' , <EOL> extra_usage_info = { '<STR_LIT>' : image_name } ) <EOL> try : <EOL> rt = self . _get_resource_tracker ( node ) <EOL> with rt . instance_claim ( context , instance , limits ) : <EOL> self . _validate_instance_group_policy ( context , instance , <EOL> filter_properties ) <EOL> image_meta = objects . ImageMeta . from_dict ( image ) <EOL> with self . _build_resources ( context , instance , <EOL> requested_networks , security_groups , image_meta , <EOL> block_device_mapping ) as resources : <EOL> instance . vm_state = vm_states . BUILDING <EOL> instance . task_state = task_states . SPAWNING <EOL> instance . save ( expected_task_state = <EOL> task_states . BLOCK_DEVICE_MAPPING ) <EOL> block_device_info = resources [ '<STR_LIT>' ] <EOL> network_info = resources [ '<STR_LIT>' ] <EOL> LOG . debug ( '<STR_LIT>' , <EOL> instance = instance ) <EOL> with timeutils . StopWatch ( ) as timer : <EOL> self . driver . spawn ( context , instance , image_meta , <EOL> injected_files , admin_password , <EOL> network_info = network_info , <EOL> block_device_info = block_device_info ) <EOL> LOG . info ( _LI ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , timer . elapsed ( ) , <EOL> instance = instance ) <EOL> except ( exception . InstanceNotFound , <EOL> exception . UnexpectedDeletingTaskStateError ) as e : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> self . _notify_about_instance_usage ( context , instance , <EOL> '<STR_LIT>' , fault = e ) <EOL> except exception . ComputeResourcesUnavailable as e : <EOL> LOG . debug ( e . format_message ( ) , instance = instance ) <EOL> self . _notify_about_instance_usage ( context , instance , <EOL> '<STR_LIT>' , fault = e ) <EOL> raise exception . RescheduledException ( <EOL> instance_uuid = instance . uuid , reason = e . format_message ( ) ) <EOL> except exception . BuildAbortException as e : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> LOG . debug ( e . format_message ( ) , instance = instance ) <EOL> self . _notify_about_instance_usage ( context , instance , <EOL> '<STR_LIT>' , fault = e ) <EOL> except ( exception . FixedIpLimitExceeded , <EOL> exception . NoMoreNetworks , exception . NoMoreFixedIps ) as e : <EOL> LOG . warning ( _LW ( '<STR_LIT>' ) , <EOL> instance = instance ) <EOL> self . _notify_about_instance_usage ( context , instance , <EOL> '<STR_LIT>' , fault = e ) <EOL> msg = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % e . format_message ( ) <EOL> raise exception . BuildAbortException ( instance_uuid = instance . uuid , <EOL> reason = msg ) <EOL> except ( exception . VirtualInterfaceCreateException , <EOL> exception . VirtualInterfaceMacAddressException ) as e : <EOL> LOG . exception ( _LE ( '<STR_LIT>' ) , <EOL> instance = instance ) <EOL> self . _notify_about_instance_usage ( context , instance , <EOL> '<STR_LIT>' , fault = e ) <EOL> msg = _ ( '<STR_LIT>' ) <EOL> raise exception . BuildAbortException ( instance_uuid = instance . uuid , <EOL> reason = msg ) <EOL> except ( exception . FlavorDiskTooSmall , <EOL> exception . FlavorMemoryTooSmall , <EOL> exception . ImageNotActive , <EOL> exception . ImageUnacceptable , <EOL> exception . InvalidDiskInfo ) as e : <EOL> self . _notify_about_instance_usage ( context , instance , <EOL> '<STR_LIT>' , fault = e ) <EOL> raise exception . BuildAbortException ( instance_uuid = instance . uuid , <EOL> reason = e . format_message ( ) ) <EOL> except Exception as e : <EOL> self . _notify_about_instance_usage ( context , instance , <EOL> '<STR_LIT>' , fault = e ) <EOL> raise exception . RescheduledException ( <EOL> instance_uuid = instance . uuid , reason = six . text_type ( e ) ) <EOL> instance . system_metadata . pop ( '<STR_LIT>' , None ) <EOL> network_name = CONF . default_access_ip_network_name <EOL> if ( network_name and not instance . access_ip_v4 and <EOL> not instance . access_ip_v6 ) : <EOL> for vif in network_info : <EOL> if vif [ '<STR_LIT>' ] [ '<STR_LIT:label>' ] == network_name : <EOL> for ip in vif . fixed_ips ( ) : <EOL> if not instance . access_ip_v4 and ip [ '<STR_LIT:version>' ] == <NUM_LIT:4> : <EOL> instance . access_ip_v4 = ip [ '<STR_LIT:address>' ] <EOL> if not instance . access_ip_v6 and ip [ '<STR_LIT:version>' ] == <NUM_LIT:6> : <EOL> instance . access_ip_v6 = ip [ '<STR_LIT:address>' ] <EOL> break <EOL> self . _update_instance_after_spawn ( context , instance ) <EOL> try : <EOL> instance . save ( expected_task_state = task_states . SPAWNING ) <EOL> except ( exception . InstanceNotFound , <EOL> exception . UnexpectedDeletingTaskStateError ) as e : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> self . _notify_about_instance_usage ( context , instance , <EOL> '<STR_LIT>' , fault = e ) <EOL> self . _update_scheduler_instance_info ( context , instance ) <EOL> self . _notify_about_instance_usage ( context , instance , '<STR_LIT>' , <EOL> extra_usage_info = { '<STR_LIT:message>' : _ ( '<STR_LIT>' ) } , <EOL> network_info = network_info ) <EOL> @ contextlib . contextmanager <EOL> def _build_resources ( self , context , instance , requested_networks , <EOL> security_groups , image_meta , block_device_mapping ) : <EOL> resources = { } <EOL> network_info = None <EOL> try : <EOL> LOG . debug ( '<STR_LIT>' , <EOL> instance = instance ) <EOL> network_info = self . _build_networks_for_instance ( context , instance , <EOL> requested_networks , security_groups ) <EOL> resources [ '<STR_LIT>' ] = network_info <EOL> except ( exception . InstanceNotFound , <EOL> exception . UnexpectedDeletingTaskStateError ) : <EOL> raise <EOL> except exception . UnexpectedTaskStateError as e : <EOL> raise exception . BuildAbortException ( instance_uuid = instance . uuid , <EOL> reason = e . format_message ( ) ) <EOL> except Exception : <EOL> LOG . exception ( _LE ( '<STR_LIT>' ) , <EOL> instance = instance ) <EOL> msg = _ ( '<STR_LIT>' ) <EOL> raise exception . BuildAbortException ( instance_uuid = instance . uuid , <EOL> reason = msg ) <EOL> try : <EOL> self . _default_block_device_names ( context , instance , image_meta , <EOL> block_device_mapping ) <EOL> LOG . debug ( '<STR_LIT>' , <EOL> instance = instance ) <EOL> instance . vm_state = vm_states . BUILDING <EOL> instance . task_state = task_states . BLOCK_DEVICE_MAPPING <EOL> instance . save ( ) <EOL> block_device_info = self . _prep_block_device ( context , instance , <EOL> block_device_mapping ) <EOL> resources [ '<STR_LIT>' ] = block_device_info <EOL> except ( exception . InstanceNotFound , <EOL> exception . UnexpectedDeletingTaskStateError ) : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> if network_info is not None : <EOL> network_info . wait ( do_raise = False ) <EOL> except ( exception . UnexpectedTaskStateError , <EOL> exception . VolumeLimitExceeded , <EOL> exception . InvalidBDM ) as e : <EOL> if network_info is not None : <EOL> network_info . wait ( do_raise = False ) <EOL> raise exception . BuildAbortException ( instance_uuid = instance . uuid , <EOL> reason = e . format_message ( ) ) <EOL> except Exception : <EOL> LOG . exception ( _LE ( '<STR_LIT>' ) , <EOL> instance = instance ) <EOL> if network_info is not None : <EOL> network_info . wait ( do_raise = False ) <EOL> msg = _ ( '<STR_LIT>' ) <EOL> raise exception . BuildAbortException ( instance_uuid = instance . uuid , <EOL> reason = msg ) <EOL> try : <EOL> yield resources <EOL> except Exception as exc : <EOL> with excutils . save_and_reraise_exception ( ) as ctxt : <EOL> if not isinstance ( exc , ( exception . InstanceNotFound , <EOL> exception . UnexpectedDeletingTaskStateError ) ) : <EOL> LOG . exception ( _LE ( '<STR_LIT>' ) , <EOL> instance = instance ) <EOL> if network_info is not None : <EOL> network_info . wait ( do_raise = False ) <EOL> deallocate_networks = False if network_info else True <EOL> try : <EOL> self . _shutdown_instance ( context , instance , <EOL> block_device_mapping , requested_networks , <EOL> try_deallocate_networks = deallocate_networks ) <EOL> except Exception as exc2 : <EOL> ctxt . reraise = False <EOL> LOG . warning ( _LW ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> six . text_type ( exc2 ) ) <EOL> raise exception . BuildAbortException ( <EOL> instance_uuid = instance . uuid , <EOL> reason = six . text_type ( exc ) ) <EOL> def _cleanup_allocated_networks ( self , context , instance , <EOL> requested_networks ) : <EOL> try : <EOL> self . _deallocate_network ( context , instance , requested_networks ) <EOL> except Exception : <EOL> msg = _LE ( '<STR_LIT>' ) <EOL> LOG . exception ( msg , instance = instance ) <EOL> return <EOL> instance . system_metadata [ '<STR_LIT>' ] = '<STR_LIT:False>' <EOL> try : <EOL> instance . save ( ) <EOL> except exception . InstanceNotFound : <EOL> pass <EOL> def _try_deallocate_network ( self , context , instance , <EOL> requested_networks = None ) : <EOL> try : <EOL> self . _deallocate_network ( context , instance , requested_networks ) <EOL> except Exception : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> LOG . error ( _LE ( '<STR_LIT>' ) , <EOL> instance = instance ) <EOL> self . _set_instance_obj_error_state ( context , instance ) <EOL> def _get_power_off_values ( self , context , instance , clean_shutdown ) : <EOL> """<STR_LIT>""" <EOL> if clean_shutdown : <EOL> timeout = compute_utils . get_value_from_system_metadata ( instance , <EOL> key = '<STR_LIT>' , type = int , <EOL> default = CONF . shutdown_timeout ) <EOL> retry_interval = self . SHUTDOWN_RETRY_INTERVAL <EOL> else : <EOL> timeout = <NUM_LIT:0> <EOL> retry_interval = <NUM_LIT:0> <EOL> return timeout , retry_interval <EOL> def _power_off_instance ( self , context , instance , clean_shutdown = True ) : <EOL> """<STR_LIT>""" <EOL> timeout , retry_interval = self . _get_power_off_values ( context , <EOL> instance , clean_shutdown ) <EOL> self . driver . power_off ( instance , timeout , retry_interval ) <EOL> def _shutdown_instance ( self , context , instance , <EOL> bdms , requested_networks = None , notify = True , <EOL> try_deallocate_networks = True ) : <EOL> """<STR_LIT>""" <EOL> context = context . elevated ( ) <EOL> LOG . info ( _LI ( '<STR_LIT>' ) , <EOL> context = context , instance = instance ) <EOL> if notify : <EOL> self . _notify_about_instance_usage ( context , instance , <EOL> "<STR_LIT>" ) <EOL> network_info = compute_utils . get_nw_info_for_instance ( instance ) <EOL> vol_bdms = [ bdm for bdm in bdms if bdm . is_volume ] <EOL> block_device_info = self . _get_instance_block_device_info ( <EOL> context , instance , bdms = bdms ) <EOL> timer = timeutils . StopWatch ( ) <EOL> try : <EOL> LOG . debug ( '<STR_LIT>' , <EOL> instance = instance ) <EOL> timer . start ( ) <EOL> self . driver . destroy ( context , instance , network_info , <EOL> block_device_info ) <EOL> LOG . info ( _LI ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , timer . elapsed ( ) , instance = instance ) <EOL> except exception . InstancePowerOffFailure : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> pass <EOL> except Exception : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> if try_deallocate_networks : <EOL> self . _try_deallocate_network ( context , instance , <EOL> requested_networks ) <EOL> if try_deallocate_networks : <EOL> self . _try_deallocate_network ( context , instance , requested_networks ) <EOL> timer . restart ( ) <EOL> for bdm in vol_bdms : <EOL> try : <EOL> connector = self . driver . get_volume_connector ( instance ) <EOL> self . volume_api . terminate_connection ( context , <EOL> bdm . volume_id , <EOL> connector ) <EOL> self . volume_api . detach ( context , bdm . volume_id , instance . uuid ) <EOL> except exception . DiskNotFound as exc : <EOL> LOG . debug ( '<STR_LIT>' , exc , <EOL> instance = instance ) <EOL> except exception . VolumeNotFound as exc : <EOL> LOG . debug ( '<STR_LIT>' , exc , <EOL> instance = instance ) <EOL> except ( cinder_exception . EndpointNotFound , <EOL> keystone_exception . EndpointNotFound ) as exc : <EOL> LOG . warning ( _LW ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> { '<STR_LIT>' : exc , '<STR_LIT>' : bdm . volume_id } , <EOL> instance = instance ) <EOL> except cinder_exception . ClientException as exc : <EOL> LOG . warning ( _LW ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> { '<STR_LIT>' : exc , '<STR_LIT>' : bdm . volume_id } , <EOL> instance = instance ) <EOL> except Exception as exc : <EOL> LOG . warning ( _LW ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> { '<STR_LIT>' : exc , '<STR_LIT>' : bdm . volume_id } , <EOL> instance = instance ) <EOL> if vol_bdms : <EOL> LOG . info ( _LI ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> { '<STR_LIT:time>' : timer . elapsed ( ) , '<STR_LIT>' : len ( vol_bdms ) } , <EOL> instance = instance ) <EOL> if notify : <EOL> self . _notify_about_instance_usage ( context , instance , <EOL> "<STR_LIT>" ) <EOL> def _cleanup_volumes ( self , context , instance_uuid , bdms , raise_exc = True ) : <EOL> exc_info = None <EOL> for bdm in bdms : <EOL> LOG . debug ( "<STR_LIT>" , bdm , <EOL> instance_uuid = instance_uuid ) <EOL> if bdm . volume_id and bdm . delete_on_termination : <EOL> try : <EOL> self . volume_api . delete ( context , bdm . volume_id ) <EOL> except Exception as exc : <EOL> exc_info = sys . exc_info ( ) <EOL> LOG . warning ( _LW ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> { '<STR_LIT>' : bdm . volume_id , '<STR_LIT>' : exc } ) <EOL> if exc_info is not None and raise_exc : <EOL> six . reraise ( exc_info [ <NUM_LIT:0> ] , exc_info [ <NUM_LIT:1> ] , exc_info [ <NUM_LIT:2> ] ) <EOL> @ hooks . add_hook ( "<STR_LIT>" ) <EOL> def _delete_instance ( self , context , instance , bdms , quotas ) : <EOL> """<STR_LIT>""" <EOL> was_soft_deleted = instance . vm_state == vm_states . SOFT_DELETED <EOL> if was_soft_deleted : <EOL> try : <EOL> quotas . rollback ( ) <EOL> except Exception : <EOL> pass <EOL> try : <EOL> events = self . instance_events . clear_events_for_instance ( instance ) <EOL> if events : <EOL> LOG . debug ( '<STR_LIT>' , <EOL> { '<STR_LIT>' : '<STR_LIT:U+002C>' . join ( events . keys ( ) ) } , <EOL> instance = instance ) <EOL> self . _notify_about_instance_usage ( context , instance , <EOL> "<STR_LIT>" ) <EOL> self . _shutdown_instance ( context , instance , bdms ) <EOL> if instance . info_cache is not None : <EOL> instance . info_cache . delete ( ) <EOL> else : <EOL> LOG . warning ( _LW ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , instance = instance ) <EOL> self . _cleanup_volumes ( context , instance . uuid , bdms , <EOL> raise_exc = False ) <EOL> instance . vm_state = vm_states . DELETED <EOL> instance . task_state = None <EOL> instance . power_state = power_state . NOSTATE <EOL> instance . terminated_at = timeutils . utcnow ( ) <EOL> instance . save ( ) <EOL> system_meta = instance . system_metadata <EOL> instance . destroy ( ) <EOL> except Exception : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> quotas . rollback ( ) <EOL> self . _complete_deletion ( context , <EOL> instance , <EOL> bdms , <EOL> quotas , <EOL> system_meta ) <EOL> @ wrap_exception ( ) <EOL> @ reverts_task_state <EOL> @ wrap_instance_event <EOL> @ wrap_instance_fault <EOL> def terminate_instance ( self , context , instance , bdms , reservations ) : <EOL> """<STR_LIT>""" <EOL> quotas = objects . Quotas . from_reservations ( context , <EOL> reservations , <EOL> instance = instance ) <EOL> @ utils . synchronized ( instance . uuid ) <EOL> def do_terminate_instance ( instance , bdms ) : <EOL> for bdm in list ( bdms ) : <EOL> if bdm . is_volume and not bdm . volume_id : <EOL> LOG . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> instance = instance ) <EOL> bdms = objects . BlockDeviceMappingList . get_by_instance_uuid ( <EOL> context , instance . uuid ) <EOL> break <EOL> try : <EOL> self . _delete_instance ( context , instance , bdms , quotas ) <EOL> except exception . InstanceNotFound : <EOL> LOG . info ( _LI ( "<STR_LIT>" ) , <EOL> instance = instance ) <EOL> except Exception : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> LOG . exception ( _LE ( '<STR_LIT>' ) , <EOL> instance = instance ) <EOL> self . _set_instance_obj_error_state ( context , instance ) <EOL> do_terminate_instance ( instance , bdms ) <EOL> @ wrap_exception ( ) <EOL> @ reverts_task_state <EOL> @ wrap_instance_event <EOL> @ wrap_instance_fault <EOL> def stop_instance ( self , context , instance , clean_shutdown ) : <EOL> """<STR_LIT>""" <EOL> @ utils . synchronized ( instance . uuid ) <EOL> def do_stop_instance ( ) : <EOL> current_power_state = self . _get_power_state ( context , instance ) <EOL> LOG . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> { '<STR_LIT>' : instance . vm_state , <EOL> '<STR_LIT>' : instance . task_state , <EOL> '<STR_LIT>' : instance . power_state , <EOL> '<STR_LIT>' : current_power_state } , <EOL> instance_uuid = instance . uuid ) <EOL> expected_task_state = [ task_states . POWERING_OFF ] <EOL> if current_power_state in ( power_state . NOSTATE , <EOL> power_state . SHUTDOWN , <EOL> power_state . CRASHED ) : <EOL> LOG . info ( _LI ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> instance = instance ) <EOL> expected_task_state . append ( None ) <EOL> self . _notify_about_instance_usage ( context , instance , <EOL> "<STR_LIT>" ) <EOL> self . _power_off_instance ( context , instance , clean_shutdown ) <EOL> instance . power_state = self . _get_power_state ( context , instance ) <EOL> instance . vm_state = vm_states . STOPPED <EOL> instance . task_state = None <EOL> instance . save ( expected_task_state = expected_task_state ) <EOL> self . _notify_about_instance_usage ( context , instance , <EOL> "<STR_LIT>" ) <EOL> do_stop_instance ( ) <EOL> def _power_on ( self , context , instance ) : <EOL> network_info = self . network_api . get_instance_nw_info ( context , instance ) <EOL> block_device_info = self . _get_instance_block_device_info ( context , <EOL> instance ) <EOL> self . driver . power_on ( context , instance , <EOL> network_info , <EOL> block_device_info ) <EOL> def _delete_snapshot_of_shelved_instance ( self , context , instance , <EOL> snapshot_id ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . image_api . delete ( context , snapshot_id ) <EOL> except ( exception . ImageNotFound , <EOL> exception . ImageNotAuthorized ) as exc : <EOL> LOG . warning ( _LW ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> exc . format_message ( ) , instance = instance ) <EOL> except Exception : <EOL> LOG . exception ( _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> instance = instance ) <EOL> @ wrap_exception ( ) <EOL> @ reverts_task_state <EOL> @ wrap_instance_event <EOL> @ wrap_instance_fault <EOL> def start_instance ( self , context , instance ) : <EOL> """<STR_LIT>""" <EOL> self . _notify_about_instance_usage ( context , instance , "<STR_LIT>" ) <EOL> self . _power_on ( context , instance ) <EOL> instance . power_state = self . _get_power_state ( context , instance ) <EOL> instance . vm_state = vm_states . ACTIVE <EOL> instance . task_state = None <EOL> snapshot_id = instance . system_metadata . get ( '<STR_LIT>' ) <EOL> if snapshot_id : <EOL> self . _delete_snapshot_of_shelved_instance ( context , instance , <EOL> snapshot_id ) <EOL> compute_utils . remove_shelved_keys_from_system_metadata ( instance ) <EOL> instance . save ( expected_task_state = task_states . POWERING_ON ) <EOL> self . _notify_about_instance_usage ( context , instance , "<STR_LIT>" ) <EOL> @ messaging . expected_exceptions ( NotImplementedError , <EOL> exception . TriggerCrashDumpNotSupported , <EOL> exception . InstanceNotRunning ) <EOL> @ wrap_exception ( ) <EOL> @ wrap_instance_event <EOL> @ wrap_instance_fault <EOL> def trigger_crash_dump ( self , context , instance ) : <EOL> """<STR_LIT>""" <EOL> self . _notify_about_instance_usage ( context , instance , <EOL> "<STR_LIT>" ) <EOL> self . driver . trigger_crash_dump ( instance ) <EOL> self . _notify_about_instance_usage ( context , instance , <EOL> "<STR_LIT>" ) <EOL> @ wrap_exception ( ) <EOL> @ reverts_task_state <EOL> @ wrap_instance_event <EOL> @ wrap_instance_fault <EOL> def soft_delete_instance ( self , context , instance , reservations ) : <EOL> """<STR_LIT>""" <EOL> quotas = objects . Quotas . from_reservations ( context , <EOL> reservations , <EOL> instance = instance ) <EOL> try : <EOL> self . _notify_about_instance_usage ( context , instance , <EOL> "<STR_LIT>" ) <EOL> try : <EOL> self . driver . soft_delete ( instance ) <EOL> except NotImplementedError : <EOL> self . driver . power_off ( instance ) <EOL> instance . power_state = self . _get_power_state ( context , instance ) <EOL> instance . vm_state = vm_states . SOFT_DELETED <EOL> instance . task_state = None <EOL> instance . save ( expected_task_state = [ task_states . SOFT_DELETING ] ) <EOL> except Exception : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> quotas . rollback ( ) <EOL> quotas . commit ( ) <EOL> self . _notify_about_instance_usage ( context , instance , "<STR_LIT>" ) <EOL> @ wrap_exception ( ) <EOL> @ reverts_task_state <EOL> @ wrap_instance_event <EOL> @ wrap_instance_fault <EOL> def restore_instance ( self , context , instance ) : <EOL> """<STR_LIT>""" <EOL> self . _notify_about_instance_usage ( context , instance , "<STR_LIT>" ) <EOL> try : <EOL> self . driver . restore ( instance ) <EOL> except NotImplementedError : <EOL> self . _power_on ( context , instance ) <EOL> instance . power_state = self . _get_power_state ( context , instance ) <EOL> instance . vm_state = vm_states . ACTIVE <EOL> instance . task_state = None <EOL> instance . save ( expected_task_state = task_states . RESTORING ) <EOL> self . _notify_about_instance_usage ( context , instance , "<STR_LIT>" ) <EOL> @ staticmethod <EOL> def _set_migration_status ( migration , status ) : <EOL> """<STR_LIT>""" <EOL> if migration : <EOL> migration . status = status <EOL> migration . save ( ) <EOL> def _rebuild_default_impl ( self , context , instance , image_meta , <EOL> injected_files , admin_password , bdms , <EOL> detach_block_devices , attach_block_devices , <EOL> network_info = None , <EOL> recreate = False , block_device_info = None , <EOL> preserve_ephemeral = False ) : <EOL> if preserve_ephemeral : <EOL> raise exception . PreserveEphemeralNotSupported ( ) <EOL> if recreate : <EOL> detach_block_devices ( context , bdms ) <EOL> else : <EOL> self . _power_off_instance ( context , instance , clean_shutdown = True ) <EOL> detach_block_devices ( context , bdms ) <EOL> self . driver . destroy ( context , instance , <EOL> network_info = network_info , <EOL> block_device_info = block_device_info ) <EOL> instance . task_state = task_states . REBUILD_BLOCK_DEVICE_MAPPING <EOL> instance . save ( expected_task_state = [ task_states . REBUILDING ] ) <EOL> new_block_device_info = attach_block_devices ( context , instance , bdms ) <EOL> instance . task_state = task_states . REBUILD_SPAWNING <EOL> instance . save ( <EOL> expected_task_state = [ task_states . REBUILD_BLOCK_DEVICE_MAPPING ] ) <EOL> with instance . mutated_migration_context ( ) : <EOL> self . driver . spawn ( context , instance , image_meta , injected_files , <EOL> admin_password , network_info = network_info , <EOL> block_device_info = new_block_device_info ) <EOL> @ messaging . expected_exceptions ( exception . PreserveEphemeralNotSupported ) <EOL> @ wrap_exception ( ) <EOL> @ reverts_task_state <EOL> @ wrap_instance_event <EOL> @ wrap_instance_fault <EOL> def rebuild_instance ( self , context , instance , orig_image_ref , image_ref , <EOL> injected_files , new_pass , orig_sys_metadata , <EOL> bdms , recreate , on_shared_storage = None , <EOL> preserve_ephemeral = False , migration = None , <EOL> scheduled_node = None , limits = None ) : <EOL> """<STR_LIT>""" <EOL> context = context . elevated ( ) <EOL> LOG . info ( _LI ( "<STR_LIT>" ) , context = context , <EOL> instance = instance ) <EOL> if scheduled_node is not None : <EOL> rt = self . _get_resource_tracker ( scheduled_node ) <EOL> rebuild_claim = rt . rebuild_claim <EOL> else : <EOL> rebuild_claim = claims . NopClaim <EOL> image_meta = { } <EOL> if image_ref : <EOL> image_meta = self . image_api . get ( context , image_ref ) <EOL> if not scheduled_node : <EOL> try : <EOL> compute_node = self . _get_compute_info ( context , self . host ) <EOL> scheduled_node = compute_node . hypervisor_hostname <EOL> except exception . ComputeHostNotFound : <EOL> LOG . exception ( _LE ( '<STR_LIT>' ) , <EOL> self . host ) <EOL> with self . _error_out_instance_on_exception ( context , instance ) : <EOL> try : <EOL> claim_ctxt = rebuild_claim ( <EOL> context , instance , limits = limits , image_meta = image_meta , <EOL> migration = migration ) <EOL> self . _do_rebuild_instance_with_claim ( <EOL> claim_ctxt , context , instance , orig_image_ref , <EOL> image_ref , injected_files , new_pass , orig_sys_metadata , <EOL> bdms , recreate , on_shared_storage , preserve_ephemeral ) <EOL> except exception . ComputeResourcesUnavailable as e : <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , instance = instance ) <EOL> self . _set_migration_status ( migration , '<STR_LIT>' ) <EOL> self . _notify_about_instance_usage ( context , instance , <EOL> '<STR_LIT>' , fault = e ) <EOL> raise exception . BuildAbortException ( <EOL> instance_uuid = instance . uuid , reason = e . format_message ( ) ) <EOL> except ( exception . InstanceNotFound , <EOL> exception . UnexpectedDeletingTaskStateError ) as e : <EOL> LOG . debug ( '<STR_LIT>' , <EOL> instance = instance ) <EOL> self . _set_migration_status ( migration , '<STR_LIT>' ) <EOL> self . _notify_about_instance_usage ( context , instance , <EOL> '<STR_LIT>' , fault = e ) <EOL> except Exception as e : <EOL> self . _set_migration_status ( migration , '<STR_LIT>' ) <EOL> self . _notify_about_instance_usage ( context , instance , <EOL> '<STR_LIT>' , fault = e ) <EOL> raise <EOL> else : <EOL> instance . apply_migration_context ( ) <EOL> instance . host = self . host <EOL> instance . node = scheduled_node <EOL> instance . save ( ) <EOL> instance . drop_migration_context ( ) <EOL> self . _set_migration_status ( migration , '<STR_LIT>' ) <EOL> def _do_rebuild_instance_with_claim ( self , claim_context , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> with claim_context : <EOL> self . _do_rebuild_instance ( * args , ** kwargs ) <EOL> @ staticmethod <EOL> def _get_image_name ( image_meta ) : <EOL> if image_meta . obj_attr_is_set ( "<STR_LIT:name>" ) : <EOL> return image_meta . name <EOL> else : <EOL> return '<STR_LIT>' <EOL> def _do_rebuild_instance ( self , context , instance , orig_image_ref , <EOL> image_ref , injected_files , new_pass , <EOL> orig_sys_metadata , bdms , recreate , <EOL> on_shared_storage , preserve_ephemeral ) : <EOL> orig_vm_state = instance . vm_state <EOL> if recreate : <EOL> if not self . driver . capabilities [ "<STR_LIT>" ] : <EOL> raise exception . InstanceRecreateNotSupported <EOL> self . _check_instance_exists ( context , instance ) <EOL> if on_shared_storage is None : <EOL> LOG . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> on_shared_storage = self . driver . instance_on_disk ( instance ) <EOL> elif ( on_shared_storage != <EOL> self . driver . instance_on_disk ( instance ) ) : <EOL> raise exception . InvalidSharedStorage ( <EOL> _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> if on_shared_storage : <EOL> LOG . info ( _LI ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> else : <EOL> image_ref = orig_image_ref = instance . image_ref <EOL> LOG . info ( _LI ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , str ( image_ref ) ) <EOL> if image_ref : <EOL> image_meta = objects . ImageMeta . from_image_ref ( <EOL> context , self . image_api , image_ref ) <EOL> else : <EOL> image_meta = objects . ImageMeta . from_dict ( { } ) <EOL> orig_image_ref_url = glance . generate_image_url ( orig_image_ref ) <EOL> extra_usage_info = { '<STR_LIT>' : orig_image_ref_url } <EOL> compute_utils . notify_usage_exists ( <EOL> self . notifier , context , instance , <EOL> current_period = True , system_metadata = orig_sys_metadata , <EOL> extra_usage_info = extra_usage_info ) <EOL> extra_usage_info = { '<STR_LIT>' : self . _get_image_name ( image_meta ) } <EOL> self . _notify_about_instance_usage ( context , instance , <EOL> "<STR_LIT>" , extra_usage_info = extra_usage_info ) <EOL> instance . power_state = self . _get_power_state ( context , instance ) <EOL> instance . task_state = task_states . REBUILDING <EOL> instance . save ( expected_task_state = [ task_states . REBUILDING ] ) <EOL> if recreate : <EOL> self . network_api . setup_networks_on_host ( <EOL> context , instance , self . host ) <EOL> self . network_api . setup_instance_network_on_host ( <EOL> context , instance , self . host ) <EOL> network_info = compute_utils . get_nw_info_for_instance ( instance ) <EOL> if bdms is None : <EOL> bdms = objects . BlockDeviceMappingList . get_by_instance_uuid ( <EOL> context , instance . uuid ) <EOL> block_device_info = self . _get_instance_block_device_info ( <EOL> context , instance , bdms = bdms ) <EOL> def detach_block_devices ( context , bdms ) : <EOL> for bdm in bdms : <EOL> if bdm . is_volume : <EOL> self . _detach_volume ( context , bdm . volume_id , instance , <EOL> destroy_bdm = False ) <EOL> files = self . _decode_files ( injected_files ) <EOL> kwargs = dict ( <EOL> context = context , <EOL> instance = instance , <EOL> image_meta = image_meta , <EOL> injected_files = files , <EOL> admin_password = new_pass , <EOL> bdms = bdms , <EOL> detach_block_devices = detach_block_devices , <EOL> attach_block_devices = self . _prep_block_device , <EOL> block_device_info = block_device_info , <EOL> network_info = network_info , <EOL> preserve_ephemeral = preserve_ephemeral , <EOL> recreate = recreate ) <EOL> try : <EOL> with instance . mutated_migration_context ( ) : <EOL> self . driver . rebuild ( ** kwargs ) <EOL> except NotImplementedError : <EOL> self . _rebuild_default_impl ( ** kwargs ) <EOL> self . _update_instance_after_spawn ( context , instance ) <EOL> instance . save ( expected_task_state = [ task_states . REBUILD_SPAWNING ] ) <EOL> if orig_vm_state == vm_states . STOPPED : <EOL> LOG . info ( _LI ( "<STR_LIT>" ) , <EOL> orig_vm_state , instance = instance ) <EOL> instance . vm_state = vm_states . ACTIVE <EOL> instance . task_state = task_states . POWERING_OFF <EOL> instance . progress = <NUM_LIT:0> <EOL> instance . save ( ) <EOL> self . stop_instance ( context , instance , False ) <EOL> self . _update_scheduler_instance_info ( context , instance ) <EOL> self . _notify_about_instance_usage ( <EOL> context , instance , "<STR_LIT>" , <EOL> network_info = network_info , <EOL> extra_usage_info = extra_usage_info ) <EOL> def _handle_bad_volumes_detached ( self , context , instance , bad_devices , <EOL> block_device_info ) : <EOL> """<STR_LIT>""" <EOL> for bdm in block_device_info [ '<STR_LIT>' ] : <EOL> if bdm . get ( '<STR_LIT>' ) in bad_devices : <EOL> try : <EOL> volume_id = bdm [ '<STR_LIT>' ] [ '<STR_LIT:data>' ] [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> continue <EOL> LOG . info ( _LI ( "<STR_LIT>" ) , volume_id ) <EOL> volume = self . volume_api . get ( context , volume_id ) <EOL> self . volume_api . check_detach ( context , volume ) <EOL> self . volume_api . begin_detaching ( context , volume_id ) <EOL> self . detach_volume ( context , volume_id , instance ) <EOL> @ wrap_exception ( ) <EOL> @ reverts_task_state <EOL> @ wrap_instance_event <EOL> @ wrap_instance_fault <EOL> def reboot_instance ( self , context , instance , block_device_info , <EOL> reboot_type ) : <EOL> """<STR_LIT>""" <EOL> if reboot_type == "<STR_LIT>" : <EOL> instance . task_state = task_states . REBOOT_PENDING <EOL> expected_states = ( task_states . REBOOTING , <EOL> task_states . REBOOT_PENDING , <EOL> task_states . REBOOT_STARTED ) <EOL> else : <EOL> instance . task_state = task_states . REBOOT_PENDING_HARD <EOL> expected_states = ( task_states . REBOOTING_HARD , <EOL> task_states . REBOOT_PENDING_HARD , <EOL> task_states . REBOOT_STARTED_HARD ) <EOL> context = context . elevated ( ) <EOL> LOG . info ( _LI ( "<STR_LIT>" ) , context = context , instance = instance ) <EOL> block_device_info = self . _get_instance_block_device_info ( context , <EOL> instance ) <EOL> network_info = self . network_api . get_instance_nw_info ( context , instance ) <EOL> self . _notify_about_instance_usage ( context , instance , "<STR_LIT>" ) <EOL> instance . power_state = self . _get_power_state ( context , instance ) <EOL> instance . save ( expected_task_state = expected_states ) <EOL> if instance . power_state != power_state . RUNNING : <EOL> state = instance . power_state <EOL> running = power_state . RUNNING <EOL> LOG . warning ( _LW ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> { '<STR_LIT:state>' : state , '<STR_LIT>' : running } , <EOL> context = context , instance = instance ) <EOL> def bad_volumes_callback ( bad_devices ) : <EOL> self . _handle_bad_volumes_detached ( <EOL> context , instance , bad_devices , block_device_info ) <EOL> try : <EOL> if instance . vm_state == vm_states . RESCUED : <EOL> new_vm_state = vm_states . RESCUED <EOL> else : <EOL> new_vm_state = vm_states . ACTIVE <EOL> new_power_state = None <EOL> if reboot_type == "<STR_LIT>" : <EOL> instance . task_state = task_states . REBOOT_STARTED <EOL> expected_state = task_states . REBOOT_PENDING <EOL> else : <EOL> instance . task_state = task_states . REBOOT_STARTED_HARD <EOL> expected_state = task_states . REBOOT_PENDING_HARD <EOL> instance . save ( expected_task_state = expected_state ) <EOL> self . driver . reboot ( context , instance , <EOL> network_info , <EOL> reboot_type , <EOL> block_device_info = block_device_info , <EOL> bad_volumes_callback = bad_volumes_callback ) <EOL> except Exception as error : <EOL> with excutils . save_and_reraise_exception ( ) as ctxt : <EOL> exc_info = sys . exc_info ( ) <EOL> new_power_state = self . _get_power_state ( context , instance ) <EOL> if new_power_state == power_state . RUNNING : <EOL> LOG . warning ( _LW ( '<STR_LIT>' ) , <EOL> context = context , instance = instance ) <EOL> compute_utils . add_instance_fault_from_exc ( context , <EOL> instance , error , exc_info ) <EOL> self . _notify_about_instance_usage ( context , instance , <EOL> '<STR_LIT>' , fault = error ) <EOL> ctxt . reraise = False <EOL> else : <EOL> LOG . error ( _LE ( '<STR_LIT>' ) , error , <EOL> context = context , instance = instance ) <EOL> self . _set_instance_obj_error_state ( context , instance ) <EOL> if not new_power_state : <EOL> new_power_state = self . _get_power_state ( context , instance ) <EOL> try : <EOL> instance . power_state = new_power_state <EOL> instance . vm_state = new_vm_state <EOL> instance . task_state = None <EOL> instance . save ( ) <EOL> except exception . InstanceNotFound : <EOL> LOG . warning ( _LW ( "<STR_LIT>" ) , <EOL> context = context , instance = instance ) <EOL> self . _notify_about_instance_usage ( context , instance , "<STR_LIT>" ) <EOL> @ delete_image_on_error <EOL> def _do_snapshot_instance ( self , context , image_id , instance , rotation ) : <EOL> self . _snapshot_instance ( context , image_id , instance , <EOL> task_states . IMAGE_BACKUP ) <EOL> @ wrap_exception ( ) <EOL> @ reverts_task_state <EOL> @ wrap_instance_fault <EOL> def backup_instance ( self , context , image_id , instance , backup_type , <EOL> rotation ) : <EOL> """<STR_LIT>""" <EOL> self . _do_snapshot_instance ( context , image_id , instance , rotation ) <EOL> self . _rotate_backups ( context , instance , backup_type , rotation ) <EOL> @ wrap_exception ( ) <EOL> @ reverts_task_state <EOL> @ wrap_instance_fault <EOL> @ delete_image_on_error <EOL> def snapshot_instance ( self , context , image_id , instance ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> instance . task_state = task_states . IMAGE_SNAPSHOT <EOL> instance . save ( <EOL> expected_task_state = task_states . IMAGE_SNAPSHOT_PENDING ) <EOL> except exception . InstanceNotFound : <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> task_states . IMAGE_SNAPSHOT , instance = instance ) <EOL> return <EOL> except exception . UnexpectedDeletingTaskStateError : <EOL> LOG . debug ( "<STR_LIT>" , <EOL> instance = instance ) <EOL> return <EOL> self . _snapshot_instance ( context , image_id , instance , <EOL> task_states . IMAGE_SNAPSHOT ) <EOL> def _snapshot_instance ( self , context , image_id , instance , <EOL> expected_task_state ) : <EOL> context = context . elevated ( ) <EOL> instance . power_state = self . _get_power_state ( context , instance ) <EOL> try : <EOL> instance . save ( ) <EOL> LOG . info ( _LI ( '<STR_LIT>' ) , context = context , <EOL> instance = instance ) <EOL> if instance . power_state != power_state . RUNNING : <EOL> state = instance . power_state <EOL> running = power_state . RUNNING <EOL> LOG . warning ( _LW ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> { '<STR_LIT:state>' : state , '<STR_LIT>' : running } , <EOL> instance = instance ) <EOL> self . _notify_about_instance_usage ( <EOL> context , instance , "<STR_LIT>" ) <EOL> def update_task_state ( task_state , <EOL> expected_state = expected_task_state ) : <EOL> instance . task_state = task_state <EOL> instance . save ( expected_task_state = expected_state ) <EOL> self . driver . snapshot ( context , instance , image_id , <EOL> update_task_state ) <EOL> instance . task_state = None <EOL> instance . save ( expected_task_state = task_states . IMAGE_UPLOADING ) <EOL> self . _notify_about_instance_usage ( context , instance , <EOL> "<STR_LIT>" ) <EOL> except ( exception . InstanceNotFound , <EOL> exception . UnexpectedDeletingTaskStateError ) : <EOL> msg = '<STR_LIT>' <EOL> LOG . debug ( msg , instance = instance ) <EOL> try : <EOL> image_service = glance . get_default_image_service ( ) <EOL> image = image_service . show ( context , image_id ) <EOL> if image [ '<STR_LIT:status>' ] != '<STR_LIT>' : <EOL> image_service . delete ( context , image_id ) <EOL> except Exception : <EOL> LOG . warning ( _LW ( "<STR_LIT>" ) , <EOL> image_id , instance = instance ) <EOL> except exception . ImageNotFound : <EOL> instance . task_state = None <EOL> instance . save ( ) <EOL> msg = _LW ( "<STR_LIT>" ) <EOL> LOG . warning ( msg , instance = instance ) <EOL> def _post_interrupted_snapshot_cleanup ( self , context , instance ) : <EOL> self . driver . post_interrupted_snapshot_cleanup ( context , instance ) <EOL> @ messaging . expected_exceptions ( NotImplementedError ) <EOL> @ wrap_exception ( ) <EOL> def volume_snapshot_create ( self , context , instance , volume_id , <EOL> create_info ) : <EOL> self . driver . volume_snapshot_create ( context , instance , volume_id , <EOL> create_info ) <EOL> @ messaging . expected_exceptions ( NotImplementedError ) <EOL> @ wrap_exception ( ) <EOL> def volume_snapshot_delete ( self , context , instance , volume_id , <EOL> snapshot_id , delete_info ) : <EOL> self . driver . volume_snapshot_delete ( context , instance , volume_id , <EOL> snapshot_id , delete_info ) <EOL> @ wrap_instance_fault <EOL> def _rotate_backups ( self , context , instance , backup_type , rotation ) : <EOL> """<STR_LIT>""" <EOL> filters = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : backup_type , <EOL> '<STR_LIT>' : instance . uuid } <EOL> images = self . image_api . get_all ( context , filters = filters , <EOL> sort_key = '<STR_LIT>' , sort_dir = '<STR_LIT>' ) <EOL> num_images = len ( images ) <EOL> LOG . debug ( "<STR_LIT>" , <EOL> { '<STR_LIT>' : num_images , '<STR_LIT>' : rotation } , <EOL> instance = instance ) <EOL> if num_images > rotation : <EOL> excess = len ( images ) - rotation <EOL> LOG . debug ( "<STR_LIT>" , excess , <EOL> instance = instance ) <EOL> for i in range ( excess ) : <EOL> image = images . pop ( ) <EOL> image_id = image [ '<STR_LIT:id>' ] <EOL> LOG . debug ( "<STR_LIT>" , image_id , <EOL> instance = instance ) <EOL> self . image_api . delete ( context , image_id ) <EOL> @ wrap_exception ( ) <EOL> @ reverts_task_state <EOL> @ wrap_instance_event <EOL> @ wrap_instance_fault <EOL> def set_admin_password ( self , context , instance , new_pass ) : <EOL> """<STR_LIT>""" <EOL> context = context . elevated ( ) <EOL> if new_pass is None : <EOL> new_pass = utils . generate_password ( ) <EOL> current_power_state = self . _get_power_state ( context , instance ) <EOL> expected_state = power_state . RUNNING <EOL> if current_power_state != expected_state : <EOL> instance . task_state = None <EOL> instance . save ( expected_task_state = task_states . UPDATING_PASSWORD ) <EOL> _msg = _ ( '<STR_LIT>' ) % instance . uuid <EOL> raise exception . InstancePasswordSetFailed ( <EOL> instance = instance . uuid , reason = _msg ) <EOL> try : <EOL> self . driver . set_admin_password ( instance , new_pass ) <EOL> LOG . info ( _LI ( "<STR_LIT>" ) , instance = instance ) <EOL> instance . task_state = None <EOL> instance . save ( <EOL> expected_task_state = task_states . UPDATING_PASSWORD ) <EOL> except NotImplementedError : <EOL> LOG . warning ( _LW ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> instance = instance ) <EOL> instance . task_state = None <EOL> instance . save ( <EOL> expected_task_state = task_states . UPDATING_PASSWORD ) <EOL> raise NotImplementedError ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> except exception . UnexpectedTaskStateError : <EOL> raise <EOL> except Exception : <EOL> LOG . exception ( _LE ( '<STR_LIT>' ) , <EOL> instance = instance ) <EOL> self . _set_instance_obj_error_state ( context , instance ) <EOL> _msg = _ ( '<STR_LIT>' ) <EOL> raise exception . InstancePasswordSetFailed ( <EOL> instance = instance . uuid , reason = _msg ) <EOL> @ wrap_exception ( ) <EOL> @ reverts_task_state <EOL> @ wrap_instance_fault <EOL> def inject_file ( self , context , path , file_contents , instance ) : <EOL> """<STR_LIT>""" <EOL> context = context . elevated ( ) <EOL> current_power_state = self . _get_power_state ( context , instance ) <EOL> expected_state = power_state . RUNNING <EOL> if current_power_state != expected_state : <EOL> LOG . warning ( _LW ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> { '<STR_LIT>' : current_power_state , <EOL> '<STR_LIT>' : expected_state } , <EOL> instance = instance ) <EOL> LOG . info ( _LI ( '<STR_LIT>' ) , path , <EOL> instance = instance ) <EOL> self . driver . inject_file ( instance , path , file_contents ) <EOL> def _get_rescue_image ( self , context , instance , rescue_image_ref = None ) : <EOL> """<STR_LIT>""" <EOL> if not rescue_image_ref : <EOL> system_meta = utils . instance_sys_meta ( instance ) <EOL> rescue_image_ref = system_meta . get ( '<STR_LIT>' ) <EOL> if not rescue_image_ref : <EOL> LOG . warning ( _LW ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> instance = instance ) <EOL> rescue_image_ref = instance . image_ref <EOL> return objects . ImageMeta . from_image_ref ( <EOL> context , self . image_api , rescue_image_ref ) <EOL> @ wrap_exception ( ) <EOL> @ reverts_task_state <EOL> @ wrap_instance_event <EOL> @ wrap_instance_fault <EOL> def rescue_instance ( self , context , instance , rescue_password , <EOL> rescue_image_ref , clean_shutdown ) : <EOL> context = context . elevated ( ) <EOL> LOG . info ( _LI ( '<STR_LIT>' ) , context = context , instance = instance ) <EOL> admin_password = ( rescue_password if rescue_password else <EOL> utils . generate_password ( ) ) <EOL> network_info = self . network_api . get_instance_nw_info ( context , instance ) <EOL> rescue_image_meta = self . _get_rescue_image ( context , instance , <EOL> rescue_image_ref ) <EOL> extra_usage_info = { '<STR_LIT>' : <EOL> self . _get_image_name ( rescue_image_meta ) } <EOL> self . _notify_about_instance_usage ( context , instance , <EOL> "<STR_LIT>" , extra_usage_info = extra_usage_info , <EOL> network_info = network_info ) <EOL> try : <EOL> self . _power_off_instance ( context , instance , clean_shutdown ) <EOL> self . driver . rescue ( context , instance , <EOL> network_info , <EOL> rescue_image_meta , admin_password ) <EOL> except Exception as e : <EOL> LOG . exception ( _LE ( "<STR_LIT>" ) , <EOL> instance = instance ) <EOL> self . _set_instance_obj_error_state ( context , instance ) <EOL> raise exception . InstanceNotRescuable ( <EOL> instance_id = instance . uuid , <EOL> reason = _ ( "<STR_LIT>" ) % e ) <EOL> compute_utils . notify_usage_exists ( self . notifier , context , instance , <EOL> current_period = True ) <EOL> instance . vm_state = vm_states . RESCUED <EOL> instance . task_state = None <EOL> instance . power_state = self . _get_power_state ( context , instance ) <EOL> instance . launched_at = timeutils . utcnow ( ) <EOL> instance . save ( expected_task_state = task_states . RESCUING ) <EOL> self . _notify_about_instance_usage ( context , instance , <EOL> "<STR_LIT>" , extra_usage_info = extra_usage_info , <EOL> network_info = network_info ) <EOL> @ wrap_exception ( ) <EOL> @ reverts_task_state <EOL> @ wrap_instance_event <EOL> @ wrap_instance_fault <EOL> def unrescue_instance ( self , context , instance ) : <EOL> context = context . elevated ( ) <EOL> LOG . info ( _LI ( '<STR_LIT>' ) , context = context , instance = instance ) <EOL> network_info = self . network_api . get_instance_nw_info ( context , instance ) <EOL> self . _notify_about_instance_usage ( context , instance , <EOL> "<STR_LIT>" , network_info = network_info ) <EOL> with self . _error_out_instance_on_exception ( context , instance ) : <EOL> self . driver . unrescue ( instance , <EOL> network_info ) <EOL> instance . vm_state = vm_states . ACTIVE <EOL> instance . task_state = None <EOL> instance . power_state = self . _get_power_state ( context , instance ) <EOL> instance . save ( expected_task_state = task_states . UNRESCUING ) <EOL> self . _notify_about_instance_usage ( context , <EOL> instance , <EOL> "<STR_LIT>" , <EOL> network_info = network_info ) <EOL> @ wrap_exception ( ) <EOL> @ wrap_instance_fault <EOL> def change_instance_metadata ( self , context , diff , instance ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( "<STR_LIT>" , <EOL> diff , instance = instance ) <EOL> self . driver . change_instance_metadata ( context , instance , diff ) <EOL> @ wrap_exception ( ) <EOL> @ wrap_instance_event <EOL> @ wrap_instance_fault <EOL> def confirm_resize ( self , context , instance , reservations , migration ) : <EOL> quotas = objects . Quotas . from_reservations ( context , <EOL> reservations , <EOL> instance = instance ) <EOL> @ utils . synchronized ( instance . uuid ) <EOL> def do_confirm_resize ( context , instance , migration_id ) : <EOL> LOG . debug ( "<STR_LIT>" , migration_id , <EOL> context = context , instance = instance ) <EOL> try : <EOL> migration = objects . Migration . get_by_id ( <EOL> context . elevated ( ) , migration_id ) <EOL> except exception . MigrationNotFound : <EOL> LOG . error ( _LE ( "<STR_LIT>" ) , <EOL> migration_id , context = context , instance = instance ) <EOL> quotas . rollback ( ) <EOL> return <EOL> if migration . status == '<STR_LIT>' : <EOL> LOG . info ( _LI ( "<STR_LIT>" ) , <EOL> migration_id , context = context , instance = instance ) <EOL> quotas . rollback ( ) <EOL> return <EOL> elif migration . status not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> LOG . warning ( _LW ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { "<STR_LIT:status>" : migration . status , "<STR_LIT:id>" : migration_id } , <EOL> context = context , instance = instance ) <EOL> quotas . rollback ( ) <EOL> return <EOL> expected_attrs = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> try : <EOL> instance = objects . Instance . get_by_uuid ( <EOL> context , instance . uuid , <EOL> expected_attrs = expected_attrs ) <EOL> except exception . InstanceNotFound : <EOL> LOG . info ( _LI ( "<STR_LIT>" ) , <EOL> context = context , instance = instance ) <EOL> quotas . rollback ( ) <EOL> return <EOL> self . _confirm_resize ( context , instance , quotas , <EOL> migration = migration ) <EOL> do_confirm_resize ( context , instance , migration . id ) <EOL> def _confirm_resize ( self , context , instance , quotas , <EOL> migration = None ) : <EOL> """<STR_LIT>""" <EOL> self . _notify_about_instance_usage ( context , instance , <EOL> "<STR_LIT>" ) <EOL> with self . _error_out_instance_on_exception ( context , instance , <EOL> quotas = quotas ) : <EOL> old_instance_type = instance . old_flavor <EOL> instance . old_flavor = None <EOL> instance . new_flavor = None <EOL> instance . system_metadata . pop ( '<STR_LIT>' , None ) <EOL> instance . save ( ) <EOL> self . network_api . setup_networks_on_host ( context , instance , <EOL> migration . source_compute , teardown = True ) <EOL> network_info = self . network_api . get_instance_nw_info ( context , <EOL> instance ) <EOL> self . driver . confirm_migration ( migration , instance , <EOL> network_info ) <EOL> migration . status = '<STR_LIT>' <EOL> with migration . obj_as_admin ( ) : <EOL> migration . save ( ) <EOL> rt = self . _get_resource_tracker ( migration . source_node ) <EOL> rt . drop_move_claim ( context , instance , old_instance_type ) <EOL> p_state = instance . power_state <EOL> vm_state = None <EOL> if p_state == power_state . SHUTDOWN : <EOL> vm_state = vm_states . STOPPED <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , vm_state , <EOL> instance = instance ) <EOL> else : <EOL> vm_state = vm_states . ACTIVE <EOL> instance . vm_state = vm_state <EOL> instance . task_state = None <EOL> instance . save ( expected_task_state = [ None , task_states . DELETING ] ) <EOL> self . _notify_about_instance_usage ( <EOL> context , instance , "<STR_LIT>" , <EOL> network_info = network_info ) <EOL> quotas . commit ( ) <EOL> @ wrap_exception ( ) <EOL> @ reverts_task_state <EOL> @ wrap_instance_event <EOL> @ errors_out_migration <EOL> @ wrap_instance_fault <EOL> def revert_resize ( self , context , instance , migration , reservations ) : <EOL> """<STR_LIT>""" <EOL> quotas = objects . Quotas . from_reservations ( context , <EOL> reservations , <EOL> instance = instance ) <EOL> compute_utils . notify_usage_exists ( self . notifier , context , instance , <EOL> current_period = True ) <EOL> with self . _error_out_instance_on_exception ( context , instance , <EOL> quotas = quotas ) : <EOL> self . network_api . setup_networks_on_host ( context , instance , <EOL> teardown = True ) <EOL> migration_p = obj_base . obj_to_primitive ( migration ) <EOL> self . network_api . migrate_instance_start ( context , <EOL> instance , <EOL> migration_p ) <EOL> network_info = self . network_api . get_instance_nw_info ( context , <EOL> instance ) <EOL> bdms = objects . BlockDeviceMappingList . get_by_instance_uuid ( <EOL> context , instance . uuid ) <EOL> block_device_info = self . _get_instance_block_device_info ( <EOL> context , instance , bdms = bdms ) <EOL> destroy_disks = not self . _is_instance_storage_shared ( <EOL> context , instance , host = migration . source_compute ) <EOL> self . driver . destroy ( context , instance , network_info , <EOL> block_device_info , destroy_disks ) <EOL> self . _terminate_volume_connections ( context , instance , bdms ) <EOL> migration . status = '<STR_LIT>' <EOL> with migration . obj_as_admin ( ) : <EOL> migration . save ( ) <EOL> instance . revert_migration_context ( ) <EOL> instance . save ( ) <EOL> rt = self . _get_resource_tracker ( instance . node ) <EOL> rt . drop_move_claim ( context , instance ) <EOL> self . compute_rpcapi . finish_revert_resize ( context , instance , <EOL> migration , migration . source_compute , <EOL> quotas . reservations ) <EOL> @ wrap_exception ( ) <EOL> @ reverts_task_state <EOL> @ wrap_instance_event <EOL> @ errors_out_migration <EOL> @ wrap_instance_fault <EOL> def finish_revert_resize ( self , context , instance , reservations , migration ) : <EOL> """<STR_LIT>""" <EOL> quotas = objects . Quotas . from_reservations ( context , <EOL> reservations , <EOL> instance = instance ) <EOL> with self . _error_out_instance_on_exception ( context , instance , <EOL> quotas = quotas ) : <EOL> network_info = self . network_api . get_instance_nw_info ( context , <EOL> instance ) <EOL> self . _notify_about_instance_usage ( <EOL> context , instance , "<STR_LIT>" ) <EOL> old_vm_state = instance . system_metadata . pop ( '<STR_LIT>' , <EOL> vm_states . ACTIVE ) <EOL> self . _set_instance_info ( instance , instance . old_flavor ) <EOL> instance . old_flavor = None <EOL> instance . new_flavor = None <EOL> instance . host = migration . source_compute <EOL> instance . node = migration . source_node <EOL> instance . save ( ) <EOL> migration . dest_compute = migration . source_compute <EOL> with migration . obj_as_admin ( ) : <EOL> migration . save ( ) <EOL> self . network_api . setup_networks_on_host ( context , instance , <EOL> migration . source_compute ) <EOL> block_device_info = self . _get_instance_block_device_info ( <EOL> context , instance , refresh_conn_info = True ) <EOL> power_on = old_vm_state != vm_states . STOPPED <EOL> self . driver . finish_revert_migration ( context , instance , <EOL> network_info , <EOL> block_device_info , power_on ) <EOL> instance . launched_at = timeutils . utcnow ( ) <EOL> instance . save ( expected_task_state = task_states . RESIZE_REVERTING ) <EOL> migration_p = obj_base . obj_to_primitive ( migration ) <EOL> self . network_api . migrate_instance_finish ( context , <EOL> instance , <EOL> migration_p ) <EOL> LOG . info ( _LI ( "<STR_LIT>" ) , <EOL> old_vm_state , instance = instance ) <EOL> if power_on : <EOL> instance . vm_state = vm_states . ACTIVE <EOL> instance . task_state = None <EOL> instance . save ( ) <EOL> else : <EOL> instance . task_state = task_states . POWERING_OFF <EOL> instance . save ( ) <EOL> self . stop_instance ( context , instance = instance , <EOL> clean_shutdown = True ) <EOL> self . _notify_about_instance_usage ( <EOL> context , instance , "<STR_LIT>" ) <EOL> quotas . commit ( ) <EOL> def _prep_resize ( self , context , image , instance , instance_type , <EOL> quotas , request_spec , filter_properties , node , <EOL> clean_shutdown = True ) : <EOL> if not filter_properties : <EOL> filter_properties = { } <EOL> if not instance . host : <EOL> self . _set_instance_obj_error_state ( context , instance ) <EOL> msg = _ ( '<STR_LIT>' ) <EOL> raise exception . MigrationError ( reason = msg ) <EOL> same_host = instance . host == self . host <EOL> if same_host and instance_type . id == instance [ '<STR_LIT>' ] : <EOL> if not self . driver . capabilities [ '<STR_LIT>' ] : <EOL> raise exception . UnableToMigrateToSelf ( <EOL> instance_id = instance . uuid , host = self . host ) <EOL> instance . new_flavor = instance_type <EOL> vm_state = instance . vm_state <EOL> LOG . debug ( '<STR_LIT>' , vm_state , instance = instance ) <EOL> instance . system_metadata [ '<STR_LIT>' ] = vm_state <EOL> instance . save ( ) <EOL> limits = filter_properties . get ( '<STR_LIT>' , { } ) <EOL> rt = self . _get_resource_tracker ( node ) <EOL> with rt . resize_claim ( context , instance , instance_type , <EOL> image_meta = image , limits = limits ) as claim : <EOL> LOG . info ( _LI ( '<STR_LIT>' ) , context = context , instance = instance ) <EOL> self . compute_rpcapi . resize_instance ( <EOL> context , instance , claim . migration , image , <EOL> instance_type , quotas . reservations , <EOL> clean_shutdown ) <EOL> @ wrap_exception ( ) <EOL> @ reverts_task_state <EOL> @ wrap_instance_event <EOL> @ wrap_instance_fault <EOL> def prep_resize ( self , context , image , instance , instance_type , <EOL> reservations , request_spec , filter_properties , node , <EOL> clean_shutdown ) : <EOL> """<STR_LIT>""" <EOL> if node is None : <EOL> node = self . driver . get_available_nodes ( refresh = True ) [ <NUM_LIT:0> ] <EOL> LOG . debug ( "<STR_LIT>" , node , <EOL> instance = instance ) <EOL> if not isinstance ( instance_type , objects . Flavor ) : <EOL> instance_type = objects . Flavor . get_by_id ( context , <EOL> instance_type [ '<STR_LIT:id>' ] ) <EOL> quotas = objects . Quotas . from_reservations ( context , <EOL> reservations , <EOL> instance = instance ) <EOL> with self . _error_out_instance_on_exception ( context , instance , <EOL> quotas = quotas ) : <EOL> compute_utils . notify_usage_exists ( self . notifier , context , instance , <EOL> current_period = True ) <EOL> self . _notify_about_instance_usage ( <EOL> context , instance , "<STR_LIT>" ) <EOL> try : <EOL> self . _prep_resize ( context , image , instance , <EOL> instance_type , quotas , <EOL> request_spec , filter_properties , <EOL> node , clean_shutdown ) <EOL> except exception . MigrationPreCheckError : <EOL> raise <EOL> except Exception : <EOL> exc_info = sys . exc_info ( ) <EOL> self . _reschedule_resize_or_reraise ( context , image , instance , <EOL> exc_info , instance_type , quotas , request_spec , <EOL> filter_properties ) <EOL> finally : <EOL> extra_usage_info = dict ( <EOL> new_instance_type = instance_type . name , <EOL> new_instance_type_id = instance_type . id ) <EOL> self . _notify_about_instance_usage ( <EOL> context , instance , "<STR_LIT>" , <EOL> extra_usage_info = extra_usage_info ) <EOL> def _reschedule_resize_or_reraise ( self , context , image , instance , exc_info , <EOL> instance_type , quotas , request_spec , filter_properties ) : <EOL> """<STR_LIT>""" <EOL> if not request_spec : <EOL> request_spec = { } <EOL> if not filter_properties : <EOL> filter_properties = { } <EOL> rescheduled = False <EOL> instance_uuid = instance . uuid <EOL> try : <EOL> reschedule_method = self . compute_task_api . resize_instance <EOL> scheduler_hint = dict ( filter_properties = filter_properties ) <EOL> method_args = ( instance , None , scheduler_hint , instance_type , <EOL> quotas . reservations ) <EOL> task_state = task_states . RESIZE_PREP <EOL> rescheduled = self . _reschedule ( context , request_spec , <EOL> filter_properties , instance , reschedule_method , <EOL> method_args , task_state , exc_info ) <EOL> except Exception as error : <EOL> rescheduled = False <EOL> LOG . exception ( _LE ( "<STR_LIT>" ) , <EOL> instance_uuid = instance_uuid ) <EOL> compute_utils . add_instance_fault_from_exc ( context , <EOL> instance , error , <EOL> exc_info = sys . exc_info ( ) ) <EOL> self . _notify_about_instance_usage ( context , instance , <EOL> '<STR_LIT>' , fault = error ) <EOL> if rescheduled : <EOL> self . _log_original_error ( exc_info , instance_uuid ) <EOL> compute_utils . add_instance_fault_from_exc ( context , <EOL> instance , exc_info [ <NUM_LIT:1> ] , exc_info = exc_info ) <EOL> self . _notify_about_instance_usage ( context , instance , <EOL> '<STR_LIT>' , fault = exc_info [ <NUM_LIT:1> ] ) <EOL> else : <EOL> six . reraise ( * exc_info ) <EOL> @ wrap_exception ( ) <EOL> @ reverts_task_state <EOL> @ wrap_instance_event <EOL> @ errors_out_migration <EOL> @ wrap_instance_fault <EOL> def resize_instance ( self , context , instance , image , <EOL> reservations , migration , instance_type , <EOL> clean_shutdown ) : <EOL> """<STR_LIT>""" <EOL> quotas = objects . Quotas . from_reservations ( context , <EOL> reservations , <EOL> instance = instance ) <EOL> with self . _error_out_instance_on_exception ( context , instance , <EOL> quotas = quotas ) : <EOL> if ( not instance_type or <EOL> not isinstance ( instance_type , objects . Flavor ) ) : <EOL> instance_type = objects . Flavor . get_by_id ( <EOL> context , migration [ '<STR_LIT>' ] ) <EOL> network_info = self . network_api . get_instance_nw_info ( context , <EOL> instance ) <EOL> migration . status = '<STR_LIT>' <EOL> with migration . obj_as_admin ( ) : <EOL> migration . save ( ) <EOL> instance . task_state = task_states . RESIZE_MIGRATING <EOL> instance . save ( expected_task_state = task_states . RESIZE_PREP ) <EOL> self . _notify_about_instance_usage ( <EOL> context , instance , "<STR_LIT>" , network_info = network_info ) <EOL> bdms = objects . BlockDeviceMappingList . get_by_instance_uuid ( <EOL> context , instance . uuid ) <EOL> block_device_info = self . _get_instance_block_device_info ( <EOL> context , instance , bdms = bdms ) <EOL> timeout , retry_interval = self . _get_power_off_values ( context , <EOL> instance , clean_shutdown ) <EOL> disk_info = self . driver . migrate_disk_and_power_off ( <EOL> context , instance , migration . dest_host , <EOL> instance_type , network_info , <EOL> block_device_info , <EOL> timeout , retry_interval ) <EOL> self . _terminate_volume_connections ( context , instance , bdms ) <EOL> migration_p = obj_base . obj_to_primitive ( migration ) <EOL> self . network_api . migrate_instance_start ( context , <EOL> instance , <EOL> migration_p ) <EOL> migration . status = '<STR_LIT>' <EOL> with migration . obj_as_admin ( ) : <EOL> migration . save ( ) <EOL> instance . host = migration . dest_compute <EOL> instance . node = migration . dest_node <EOL> instance . task_state = task_states . RESIZE_MIGRATED <EOL> instance . save ( expected_task_state = task_states . RESIZE_MIGRATING ) <EOL> self . compute_rpcapi . finish_resize ( context , instance , <EOL> migration , image , disk_info , <EOL> migration . dest_compute , reservations = quotas . reservations ) <EOL> self . _notify_about_instance_usage ( context , instance , "<STR_LIT>" , <EOL> network_info = network_info ) <EOL> self . instance_events . clear_events_for_instance ( instance ) <EOL> def _terminate_volume_connections ( self , context , instance , bdms ) : <EOL> connector = self . driver . get_volume_connector ( instance ) <EOL> for bdm in bdms : <EOL> if bdm . is_volume : <EOL> self . volume_api . terminate_connection ( context , bdm . volume_id , <EOL> connector ) <EOL> @ staticmethod <EOL> def _set_instance_info ( instance , instance_type ) : <EOL> instance . instance_type_id = instance_type . id <EOL> instance . memory_mb = instance_type . memory_mb <EOL> instance . vcpus = instance_type . vcpus <EOL> instance . root_gb = instance_type . root_gb <EOL> instance . ephemeral_gb = instance_type . ephemeral_gb <EOL> instance . flavor = instance_type <EOL> def _finish_resize ( self , context , instance , migration , disk_info , <EOL> image_meta ) : <EOL> resize_instance = False <EOL> old_instance_type_id = migration [ '<STR_LIT>' ] <EOL> new_instance_type_id = migration [ '<STR_LIT>' ] <EOL> old_instance_type = instance . get_flavor ( ) <EOL> old_vm_state = instance . system_metadata . get ( '<STR_LIT>' , <EOL> vm_states . ACTIVE ) <EOL> instance . old_flavor = old_instance_type <EOL> if old_instance_type_id != new_instance_type_id : <EOL> instance_type = instance . get_flavor ( '<STR_LIT>' ) <EOL> self . _set_instance_info ( instance , instance_type ) <EOL> for key in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if old_instance_type [ key ] != instance_type [ key ] : <EOL> resize_instance = True <EOL> break <EOL> instance . apply_migration_context ( ) <EOL> self . network_api . setup_networks_on_host ( context , instance , <EOL> migration [ '<STR_LIT>' ] ) <EOL> migration_p = obj_base . obj_to_primitive ( migration ) <EOL> self . network_api . migrate_instance_finish ( context , <EOL> instance , <EOL> migration_p ) <EOL> network_info = self . network_api . get_instance_nw_info ( context , instance ) <EOL> instance . task_state = task_states . RESIZE_FINISH <EOL> instance . save ( expected_task_state = task_states . RESIZE_MIGRATED ) <EOL> self . _notify_about_instance_usage ( <EOL> context , instance , "<STR_LIT>" , <EOL> network_info = network_info ) <EOL> block_device_info = self . _get_instance_block_device_info ( <EOL> context , instance , refresh_conn_info = True ) <EOL> power_on = old_vm_state != vm_states . STOPPED <EOL> try : <EOL> self . driver . finish_migration ( context , migration , instance , <EOL> disk_info , <EOL> network_info , <EOL> image_meta , resize_instance , <EOL> block_device_info , power_on ) <EOL> except Exception : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> if old_instance_type_id != new_instance_type_id : <EOL> self . _set_instance_info ( instance , <EOL> old_instance_type ) <EOL> migration . status = '<STR_LIT>' <EOL> with migration . obj_as_admin ( ) : <EOL> migration . save ( ) <EOL> instance . vm_state = vm_states . RESIZED <EOL> instance . task_state = None <EOL> instance . launched_at = timeutils . utcnow ( ) <EOL> instance . save ( expected_task_state = task_states . RESIZE_FINISH ) <EOL> self . _update_scheduler_instance_info ( context , instance ) <EOL> self . _notify_about_instance_usage ( <EOL> context , instance , "<STR_LIT>" , <EOL> network_info = network_info ) <EOL> @ wrap_exception ( ) <EOL> @ reverts_task_state <EOL> @ wrap_instance_event <EOL> @ errors_out_migration <EOL> @ wrap_instance_fault <EOL> def finish_resize ( self , context , disk_info , image , instance , <EOL> reservations , migration ) : <EOL> """<STR_LIT>""" <EOL> quotas = objects . Quotas . from_reservations ( context , <EOL> reservations , <EOL> instance = instance ) <EOL> try : <EOL> image_meta = objects . ImageMeta . from_dict ( image ) <EOL> self . _finish_resize ( context , instance , migration , <EOL> disk_info , image_meta ) <EOL> quotas . commit ( ) <EOL> except Exception : <EOL> LOG . exception ( _LE ( '<STR_LIT>' ) , <EOL> instance = instance ) <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> try : <EOL> quotas . rollback ( ) <EOL> except Exception : <EOL> LOG . exception ( _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> instance = instance ) <EOL> self . _set_instance_obj_error_state ( context , instance ) <EOL> @ wrap_exception ( ) <EOL> @ wrap_instance_fault <EOL> def add_fixed_ip_to_instance ( self , context , network_id , instance ) : <EOL> """<STR_LIT>""" <EOL> self . _notify_about_instance_usage ( <EOL> context , instance , "<STR_LIT>" ) <EOL> network_info = self . network_api . add_fixed_ip_to_instance ( context , <EOL> instance , <EOL> network_id ) <EOL> self . _inject_network_info ( context , instance , network_info ) <EOL> self . reset_network ( context , instance ) <EOL> instance . updated_at = timeutils . utcnow ( ) <EOL> instance . save ( ) <EOL> self . _notify_about_instance_usage ( <EOL> context , instance , "<STR_LIT>" , network_info = network_info ) <EOL> @ wrap_exception ( ) <EOL> @ wrap_instance_fault <EOL> def remove_fixed_ip_from_instance ( self , context , address , instance ) : <EOL> """<STR_LIT>""" <EOL> self . _notify_about_instance_usage ( <EOL> context , instance , "<STR_LIT>" ) <EOL> network_info = self . network_api . remove_fixed_ip_from_instance ( context , <EOL> instance , <EOL> address ) <EOL> self . _inject_network_info ( context , instance , network_info ) <EOL> self . reset_network ( context , instance ) <EOL> instance . updated_at = timeutils . utcnow ( ) <EOL> instance . save ( ) <EOL> self . _notify_about_instance_usage ( <EOL> context , instance , "<STR_LIT>" , network_info = network_info ) <EOL> @ wrap_exception ( ) <EOL> @ reverts_task_state <EOL> @ wrap_instance_event <EOL> @ wrap_instance_fault <EOL> def pause_instance ( self , context , instance ) : <EOL> """<STR_LIT>""" <EOL> context = context . elevated ( ) <EOL> LOG . info ( _LI ( '<STR_LIT>' ) , context = context , instance = instance ) <EOL> self . _notify_about_instance_usage ( context , instance , '<STR_LIT>' ) <EOL> self . driver . pause ( instance ) <EOL> instance . power_state = self . _get_power_state ( context , instance ) <EOL> instance . vm_state = vm_states . PAUSED <EOL> instance . task_state = None <EOL> instance . save ( expected_task_state = task_states . PAUSING ) <EOL> self . _notify_about_instance_usage ( context , instance , '<STR_LIT>' ) <EOL> @ wrap_exception ( ) <EOL> @ reverts_task_state <EOL> @ wrap_instance_event <EOL> @ wrap_instance_fault <EOL> def unpause_instance ( self , context , instance ) : <EOL> """<STR_LIT>""" <EOL> context = context . elevated ( ) <EOL> LOG . info ( _LI ( '<STR_LIT>' ) , context = context , instance = instance ) <EOL> self . _notify_about_instance_usage ( context , instance , '<STR_LIT>' ) <EOL> self . driver . unpause ( instance ) <EOL> instance . power_state = self . _get_power_state ( context , instance ) <EOL> instance . vm_state = vm_states . ACTIVE <EOL> instance . task_state = None <EOL> instance . save ( expected_task_state = task_states . UNPAUSING ) <EOL> self . _notify_about_instance_usage ( context , instance , '<STR_LIT>' ) <EOL> @ wrap_exception ( ) <EOL> def host_power_action ( self , context , action ) : <EOL> """<STR_LIT>""" <EOL> return self . driver . host_power_action ( action ) <EOL> @ wrap_exception ( ) <EOL> def host_maintenance_mode ( self , context , host , mode ) : <EOL> """<STR_LIT>""" <EOL> return self . driver . host_maintenance_mode ( host , mode ) <EOL> @ wrap_exception ( ) <EOL> def set_host_enabled ( self , context , enabled ) : <EOL> """<STR_LIT>""" <EOL> return self . driver . set_host_enabled ( enabled ) <EOL> @ wrap_exception ( ) <EOL> def get_host_uptime ( self , context ) : <EOL> """<STR_LIT>""" <EOL> return self . driver . get_host_uptime ( ) <EOL> @ wrap_exception ( ) <EOL> @ wrap_instance_fault <EOL> def get_diagnostics ( self , context , instance ) : <EOL> """<STR_LIT>""" <EOL> current_power_state = self . _get_power_state ( context , instance ) <EOL> if current_power_state == power_state . RUNNING : <EOL> LOG . info ( _LI ( "<STR_LIT>" ) , context = context , <EOL> instance = instance ) <EOL> return self . driver . get_diagnostics ( instance ) <EOL> else : <EOL> raise exception . InstanceInvalidState ( <EOL> attr = '<STR_LIT>' , <EOL> instance_uuid = instance . uuid , <EOL> state = instance . power_state , <EOL> method = '<STR_LIT>' ) <EOL> @ object_compat <EOL> @ wrap_exception ( ) <EOL> @ wrap_instance_fault <EOL> def get_instance_diagnostics ( self , context , instance ) : <EOL> """<STR_LIT>""" <EOL> current_power_state = self . _get_power_state ( context , instance ) <EOL> if current_power_state == power_state . RUNNING : <EOL> LOG . info ( _LI ( "<STR_LIT>" ) , context = context , <EOL> instance = instance ) <EOL> diags = self . driver . get_instance_diagnostics ( instance ) <EOL> return diags . serialize ( ) <EOL> else : <EOL> raise exception . InstanceInvalidState ( <EOL> attr = '<STR_LIT>' , <EOL> instance_uuid = instance . uuid , <EOL> state = instance . power_state , <EOL> method = '<STR_LIT>' ) <EOL> @ wrap_exception ( ) <EOL> @ reverts_task_state <EOL> @ wrap_instance_event <EOL> @ wrap_instance_fault <EOL> def suspend_instance ( self , context , instance ) : <EOL> """<STR_LIT>""" <EOL> context = context . elevated ( ) <EOL> instance . system_metadata [ '<STR_LIT>' ] = instance . vm_state <EOL> self . _notify_about_instance_usage ( context , instance , '<STR_LIT>' ) <EOL> with self . _error_out_instance_on_exception ( context , instance , <EOL> instance_state = instance . vm_state ) : <EOL> self . driver . suspend ( context , instance ) <EOL> instance . power_state = self . _get_power_state ( context , instance ) <EOL> instance . vm_state = vm_states . SUSPENDED <EOL> instance . task_state = None <EOL> instance . save ( expected_task_state = task_states . SUSPENDING ) <EOL> self . _notify_about_instance_usage ( context , instance , '<STR_LIT>' ) <EOL> @ wrap_exception ( ) <EOL> @ reverts_task_state <EOL> @ wrap_instance_event <EOL> @ wrap_instance_fault <EOL> def resume_instance ( self , context , instance ) : <EOL> """<STR_LIT>""" <EOL> context = context . elevated ( ) <EOL> LOG . info ( _LI ( '<STR_LIT>' ) , context = context , instance = instance ) <EOL> self . _notify_about_instance_usage ( context , instance , '<STR_LIT>' ) <EOL> network_info = self . network_api . get_instance_nw_info ( context , instance ) <EOL> block_device_info = self . _get_instance_block_device_info ( <EOL> context , instance ) <EOL> with self . _error_out_instance_on_exception ( context , instance , <EOL> instance_state = instance . vm_state ) : <EOL> self . driver . resume ( context , instance , network_info , <EOL> block_device_info ) <EOL> instance . power_state = self . _get_power_state ( context , instance ) <EOL> instance . vm_state = instance . system_metadata . pop ( '<STR_LIT>' , <EOL> vm_states . ACTIVE ) <EOL> instance . task_state = None <EOL> instance . save ( expected_task_state = task_states . RESUMING ) <EOL> self . _notify_about_instance_usage ( context , instance , '<STR_LIT>' ) <EOL> @ wrap_exception ( ) <EOL> @ reverts_task_state <EOL> @ wrap_instance_event <EOL> @ wrap_instance_fault <EOL> def shelve_instance ( self , context , instance , image_id , <EOL> clean_shutdown ) : <EOL> """<STR_LIT>""" <EOL> compute_utils . notify_usage_exists ( self . notifier , context , instance , <EOL> current_period = True ) <EOL> self . _notify_about_instance_usage ( context , instance , '<STR_LIT>' ) <EOL> def update_task_state ( task_state , expected_state = task_states . SHELVING ) : <EOL> shelving_state_map = { <EOL> task_states . IMAGE_PENDING_UPLOAD : <EOL> task_states . SHELVING_IMAGE_PENDING_UPLOAD , <EOL> task_states . IMAGE_UPLOADING : <EOL> task_states . SHELVING_IMAGE_UPLOADING , <EOL> task_states . SHELVING : task_states . SHELVING } <EOL> task_state = shelving_state_map [ task_state ] <EOL> expected_state = shelving_state_map [ expected_state ] <EOL> instance . task_state = task_state <EOL> instance . save ( expected_task_state = expected_state ) <EOL> self . _power_off_instance ( context , instance , clean_shutdown ) <EOL> self . driver . snapshot ( context , instance , image_id , update_task_state ) <EOL> instance . system_metadata [ '<STR_LIT>' ] = timeutils . utcnow ( ) . isoformat ( ) <EOL> instance . system_metadata [ '<STR_LIT>' ] = image_id <EOL> instance . system_metadata [ '<STR_LIT>' ] = self . host <EOL> instance . vm_state = vm_states . SHELVED <EOL> instance . task_state = None <EOL> if CONF . shelved_offload_time == <NUM_LIT:0> : <EOL> instance . task_state = task_states . SHELVING_OFFLOADING <EOL> instance . power_state = self . _get_power_state ( context , instance ) <EOL> instance . save ( expected_task_state = [ <EOL> task_states . SHELVING , <EOL> task_states . SHELVING_IMAGE_UPLOADING ] ) <EOL> self . _notify_about_instance_usage ( context , instance , '<STR_LIT>' ) <EOL> if CONF . shelved_offload_time == <NUM_LIT:0> : <EOL> self . shelve_offload_instance ( context , instance , <EOL> clean_shutdown = False ) <EOL> @ wrap_exception ( ) <EOL> @ reverts_task_state <EOL> @ wrap_instance_fault <EOL> def shelve_offload_instance ( self , context , instance , clean_shutdown ) : <EOL> """<STR_LIT>""" <EOL> self . _notify_about_instance_usage ( context , instance , <EOL> '<STR_LIT>' ) <EOL> self . _power_off_instance ( context , instance , clean_shutdown ) <EOL> current_power_state = self . _get_power_state ( context , instance ) <EOL> self . network_api . cleanup_instance_network_on_host ( context , instance , <EOL> instance . host ) <EOL> network_info = self . network_api . get_instance_nw_info ( context , instance ) <EOL> block_device_info = self . _get_instance_block_device_info ( context , <EOL> instance ) <EOL> self . driver . destroy ( context , instance , network_info , <EOL> block_device_info ) <EOL> instance . power_state = current_power_state <EOL> instance . host = None <EOL> instance . node = None <EOL> instance . vm_state = vm_states . SHELVED_OFFLOADED <EOL> instance . task_state = None <EOL> instance . save ( expected_task_state = [ task_states . SHELVING , <EOL> task_states . SHELVING_OFFLOADING ] ) <EOL> self . _update_resource_tracker ( context , instance ) <EOL> self . _delete_scheduler_instance_info ( context , instance . uuid ) <EOL> self . _notify_about_instance_usage ( context , instance , <EOL> '<STR_LIT>' ) <EOL> @ wrap_exception ( ) <EOL> @ reverts_task_state <EOL> @ wrap_instance_event <EOL> @ wrap_instance_fault <EOL> def unshelve_instance ( self , context , instance , image , <EOL> filter_properties , node ) : <EOL> """<STR_LIT>""" <EOL> if filter_properties is None : <EOL> filter_properties = { } <EOL> @ utils . synchronized ( instance . uuid ) <EOL> def do_unshelve_instance ( ) : <EOL> self . _unshelve_instance ( context , instance , image , <EOL> filter_properties , node ) <EOL> do_unshelve_instance ( ) <EOL> def _unshelve_instance_key_scrub ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> cleaned_keys = dict ( <EOL> key_data = instance . key_data , <EOL> auto_disk_config = instance . auto_disk_config ) <EOL> instance . key_data = None <EOL> instance . auto_disk_config = False <EOL> return cleaned_keys <EOL> def _unshelve_instance_key_restore ( self , instance , keys ) : <EOL> """<STR_LIT>""" <EOL> instance . update ( keys ) <EOL> def _unshelve_instance ( self , context , instance , image , filter_properties , <EOL> node ) : <EOL> self . _notify_about_instance_usage ( context , instance , '<STR_LIT>' ) <EOL> instance . task_state = task_states . SPAWNING <EOL> instance . save ( ) <EOL> bdms = objects . BlockDeviceMappingList . get_by_instance_uuid ( <EOL> context , instance . uuid ) <EOL> block_device_info = self . _prep_block_device ( context , instance , bdms , <EOL> do_check_attach = False ) <EOL> scrubbed_keys = self . _unshelve_instance_key_scrub ( instance ) <EOL> if node is None : <EOL> node = self . driver . get_available_nodes ( ) [ <NUM_LIT:0> ] <EOL> LOG . debug ( '<STR_LIT>' , node , <EOL> instance = instance ) <EOL> rt = self . _get_resource_tracker ( node ) <EOL> limits = filter_properties . get ( '<STR_LIT>' , { } ) <EOL> shelved_image_ref = instance . image_ref <EOL> if image : <EOL> instance . image_ref = image [ '<STR_LIT:id>' ] <EOL> image_meta = objects . ImageMeta . from_dict ( image ) <EOL> else : <EOL> image_meta = objects . ImageMeta . from_dict ( <EOL> utils . get_image_from_system_metadata ( <EOL> instance . system_metadata ) ) <EOL> self . network_api . setup_instance_network_on_host ( context , instance , <EOL> self . host ) <EOL> network_info = self . network_api . get_instance_nw_info ( context , instance ) <EOL> try : <EOL> with rt . instance_claim ( context , instance , limits ) : <EOL> self . driver . spawn ( context , instance , image_meta , <EOL> injected_files = [ ] , <EOL> admin_password = None , <EOL> network_info = network_info , <EOL> block_device_info = block_device_info ) <EOL> except Exception : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> LOG . exception ( _LE ( '<STR_LIT>' ) , <EOL> instance = instance ) <EOL> if image : <EOL> instance . image_ref = shelved_image_ref <EOL> self . _delete_snapshot_of_shelved_instance ( context , instance , <EOL> image [ '<STR_LIT:id>' ] ) <EOL> self . _unshelve_instance_key_restore ( instance , scrubbed_keys ) <EOL> self . _update_instance_after_spawn ( context , instance ) <EOL> compute_utils . remove_shelved_keys_from_system_metadata ( instance ) <EOL> instance . save ( expected_task_state = task_states . SPAWNING ) <EOL> self . _update_scheduler_instance_info ( context , instance ) <EOL> self . _notify_about_instance_usage ( context , instance , '<STR_LIT>' ) <EOL> @ messaging . expected_exceptions ( NotImplementedError ) <EOL> @ wrap_instance_fault <EOL> def reset_network ( self , context , instance ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( '<STR_LIT>' , context = context , instance = instance ) <EOL> self . driver . reset_network ( instance ) <EOL> def _inject_network_info ( self , context , instance , network_info ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( '<STR_LIT>' , context = context , instance = instance ) <EOL> LOG . debug ( '<STR_LIT>' , network_info , <EOL> instance = instance ) <EOL> self . driver . inject_network_info ( instance , <EOL> network_info ) <EOL> @ wrap_instance_fault <EOL> def inject_network_info ( self , context , instance ) : <EOL> """<STR_LIT>""" <EOL> network_info = self . network_api . get_instance_nw_info ( context , instance ) <EOL> self . _inject_network_info ( context , instance , network_info ) <EOL> @ messaging . expected_exceptions ( NotImplementedError , <EOL> exception . ConsoleNotAvailable , <EOL> exception . InstanceNotFound ) <EOL> @ wrap_exception ( ) <EOL> @ wrap_instance_fault <EOL> def get_console_output ( self , context , instance , tail_length ) : <EOL> """<STR_LIT>""" <EOL> context = context . elevated ( ) <EOL> LOG . info ( _LI ( "<STR_LIT>" ) , context = context , <EOL> instance = instance ) <EOL> output = self . driver . get_console_output ( context , instance ) <EOL> if type ( output ) is six . text_type : <EOL> output = six . b ( output ) <EOL> if tail_length is not None : <EOL> output = self . _tail_log ( output , tail_length ) <EOL> return output . decode ( '<STR_LIT:utf-8>' , '<STR_LIT:replace>' ) . encode ( '<STR_LIT:ascii>' , '<STR_LIT:replace>' ) <EOL> def _tail_log ( self , log , length ) : <EOL> try : <EOL> length = int ( length ) <EOL> except ValueError : <EOL> length = <NUM_LIT:0> <EOL> if length == <NUM_LIT:0> : <EOL> return b'<STR_LIT>' <EOL> else : <EOL> return b'<STR_LIT:\n>' . join ( log . split ( b'<STR_LIT:\n>' ) [ - int ( length ) : ] ) <EOL> @ messaging . expected_exceptions ( exception . ConsoleTypeInvalid , <EOL> exception . InstanceNotReady , <EOL> exception . InstanceNotFound , <EOL> exception . ConsoleTypeUnavailable , <EOL> NotImplementedError ) <EOL> @ wrap_exception ( ) <EOL> @ wrap_instance_fault <EOL> def get_vnc_console ( self , context , console_type , instance ) : <EOL> """<STR_LIT>""" <EOL> context = context . elevated ( ) <EOL> LOG . debug ( "<STR_LIT>" , instance = instance ) <EOL> token = str ( uuid . uuid4 ( ) ) <EOL> if not CONF . vnc . enabled : <EOL> raise exception . ConsoleTypeUnavailable ( console_type = console_type ) <EOL> if console_type == '<STR_LIT>' : <EOL> access_url = '<STR_LIT>' % ( CONF . vnc . novncproxy_base_url , token ) <EOL> elif console_type == '<STR_LIT>' : <EOL> access_url = '<STR_LIT>' % ( CONF . vnc . xvpvncproxy_base_url , token ) <EOL> else : <EOL> raise exception . ConsoleTypeInvalid ( console_type = console_type ) <EOL> try : <EOL> console = self . driver . get_vnc_console ( context , instance ) <EOL> connect_info = console . get_connection_info ( token , access_url ) <EOL> except exception . InstanceNotFound : <EOL> if instance . vm_state != vm_states . BUILDING : <EOL> raise <EOL> raise exception . InstanceNotReady ( instance_id = instance . uuid ) <EOL> return connect_info <EOL> @ messaging . expected_exceptions ( exception . ConsoleTypeInvalid , <EOL> exception . InstanceNotReady , <EOL> exception . InstanceNotFound , <EOL> exception . ConsoleTypeUnavailable , <EOL> NotImplementedError ) <EOL> @ wrap_exception ( ) <EOL> @ wrap_instance_fault <EOL> def get_spice_console ( self , context , console_type , instance ) : <EOL> """<STR_LIT>""" <EOL> context = context . elevated ( ) <EOL> LOG . debug ( "<STR_LIT>" , instance = instance ) <EOL> token = str ( uuid . uuid4 ( ) ) <EOL> if not CONF . spice . enabled : <EOL> raise exception . ConsoleTypeUnavailable ( console_type = console_type ) <EOL> if console_type == '<STR_LIT>' : <EOL> access_url = '<STR_LIT>' % ( CONF . spice . html5proxy_base_url , <EOL> token ) <EOL> else : <EOL> raise exception . ConsoleTypeInvalid ( console_type = console_type ) <EOL> try : <EOL> console = self . driver . get_spice_console ( context , instance ) <EOL> connect_info = console . get_connection_info ( token , access_url ) <EOL> except exception . InstanceNotFound : <EOL> if instance . vm_state != vm_states . BUILDING : <EOL> raise <EOL> raise exception . InstanceNotReady ( instance_id = instance . uuid ) <EOL> return connect_info <EOL> @ messaging . expected_exceptions ( exception . ConsoleTypeInvalid , <EOL> exception . InstanceNotReady , <EOL> exception . InstanceNotFound , <EOL> exception . ConsoleTypeUnavailable , <EOL> NotImplementedError ) <EOL> @ wrap_exception ( ) <EOL> @ wrap_instance_fault <EOL> def get_rdp_console ( self , context , console_type , instance ) : <EOL> """<STR_LIT>""" <EOL> context = context . elevated ( ) <EOL> LOG . debug ( "<STR_LIT>" , instance = instance ) <EOL> token = str ( uuid . uuid4 ( ) ) <EOL> if not CONF . rdp . enabled : <EOL> raise exception . ConsoleTypeUnavailable ( console_type = console_type ) <EOL> if console_type == '<STR_LIT>' : <EOL> access_url = '<STR_LIT>' % ( CONF . rdp . html5_proxy_base_url , <EOL> token ) <EOL> else : <EOL> raise exception . ConsoleTypeInvalid ( console_type = console_type ) <EOL> try : <EOL> console = self . driver . get_rdp_console ( context , instance ) <EOL> connect_info = console . get_connection_info ( token , access_url ) <EOL> except exception . InstanceNotFound : <EOL> if instance . vm_state != vm_states . BUILDING : <EOL> raise <EOL> raise exception . InstanceNotReady ( instance_id = instance . uuid ) <EOL> return connect_info <EOL> @ messaging . expected_exceptions ( exception . ConsoleTypeInvalid , <EOL> exception . InstanceNotReady , <EOL> exception . InstanceNotFound , <EOL> exception . ConsoleTypeUnavailable , <EOL> NotImplementedError ) <EOL> @ wrap_exception ( ) <EOL> @ wrap_instance_fault <EOL> def get_mks_console ( self , context , console_type , instance ) : <EOL> """<STR_LIT>""" <EOL> context = context . elevated ( ) <EOL> LOG . debug ( "<STR_LIT>" , instance = instance ) <EOL> token = str ( uuid . uuid4 ( ) ) <EOL> if not CONF . mks . enabled : <EOL> raise exception . ConsoleTypeUnavailable ( console_type = console_type ) <EOL> if console_type == '<STR_LIT>' : <EOL> access_url = '<STR_LIT>' % ( CONF . mks . mksproxy_base_url , <EOL> token ) <EOL> else : <EOL> raise exception . ConsoleTypeInvalid ( console_type = console_type ) <EOL> try : <EOL> console = self . driver . get_mks_console ( context , instance ) <EOL> connect_info = console . get_connection_info ( token , access_url ) <EOL> except exception . InstanceNotFound : <EOL> if instance . vm_state != vm_states . BUILDING : <EOL> raise <EOL> raise exception . InstanceNotReady ( instance_id = instance . uuid ) <EOL> return connect_info <EOL> @ messaging . expected_exceptions ( <EOL> exception . ConsoleTypeInvalid , <EOL> exception . InstanceNotReady , <EOL> exception . InstanceNotFound , <EOL> exception . ConsoleTypeUnavailable , <EOL> exception . SocketPortRangeExhaustedException , <EOL> exception . ImageSerialPortNumberInvalid , <EOL> exception . ImageSerialPortNumberExceedFlavorValue , <EOL> NotImplementedError ) <EOL> @ wrap_exception ( ) <EOL> @ wrap_instance_fault <EOL> def get_serial_console ( self , context , console_type , instance ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( "<STR_LIT>" , instance = instance ) <EOL> if not CONF . serial_console . enabled : <EOL> raise exception . ConsoleTypeUnavailable ( console_type = console_type ) <EOL> context = context . elevated ( ) <EOL> token = str ( uuid . uuid4 ( ) ) <EOL> access_url = '<STR_LIT>' % ( CONF . serial_console . base_url , token ) <EOL> try : <EOL> console = self . driver . get_serial_console ( context , instance ) <EOL> connect_info = console . get_connection_info ( token , access_url ) <EOL> except exception . InstanceNotFound : <EOL> if instance . vm_state != vm_states . BUILDING : <EOL> raise <EOL> raise exception . InstanceNotReady ( instance_id = instance . uuid ) <EOL> return connect_info <EOL> @ messaging . expected_exceptions ( exception . ConsoleTypeInvalid , <EOL> exception . InstanceNotReady , <EOL> exception . InstanceNotFound ) <EOL> @ wrap_exception ( ) <EOL> @ wrap_instance_fault <EOL> def validate_console_port ( self , ctxt , instance , port , console_type ) : <EOL> if console_type == "<STR_LIT>" : <EOL> console_info = self . driver . get_spice_console ( ctxt , instance ) <EOL> elif console_type == "<STR_LIT>" : <EOL> console_info = self . driver . get_rdp_console ( ctxt , instance ) <EOL> elif console_type == "<STR_LIT>" : <EOL> console_info = self . driver . get_serial_console ( ctxt , instance ) <EOL> elif console_type == "<STR_LIT>" : <EOL> console_info = self . driver . get_mks_console ( ctxt , instance ) <EOL> else : <EOL> console_info = self . driver . get_vnc_console ( ctxt , instance ) <EOL> return console_info . port == port <EOL> @ wrap_exception ( ) <EOL> @ reverts_task_state <EOL> @ wrap_instance_fault <EOL> def reserve_block_device_name ( self , context , instance , device , <EOL> volume_id , disk_bus , device_type ) : <EOL> @ utils . synchronized ( instance . uuid ) <EOL> def do_reserve ( ) : <EOL> bdms = ( <EOL> objects . BlockDeviceMappingList . get_by_instance_uuid ( <EOL> context , instance . uuid ) ) <EOL> new_bdm = objects . BlockDeviceMapping ( <EOL> context = context , <EOL> source_type = '<STR_LIT>' , destination_type = '<STR_LIT>' , <EOL> instance_uuid = instance . uuid , boot_index = None , <EOL> volume_id = volume_id , <EOL> device_name = device , guest_format = None , <EOL> disk_bus = disk_bus , device_type = device_type ) <EOL> new_bdm . device_name = self . _get_device_name_for_instance ( <EOL> instance , bdms , new_bdm ) <EOL> new_bdm . create ( ) <EOL> return new_bdm <EOL> return do_reserve ( ) <EOL> @ wrap_exception ( ) <EOL> @ wrap_instance_fault <EOL> def attach_volume ( self , context , instance , bdm ) : <EOL> """<STR_LIT>""" <EOL> driver_bdm = driver_block_device . convert_volume ( bdm ) <EOL> @ utils . synchronized ( instance . uuid ) <EOL> def do_attach_volume ( context , instance , driver_bdm ) : <EOL> try : <EOL> return self . _attach_volume ( context , instance , driver_bdm ) <EOL> except Exception : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> bdm . destroy ( ) <EOL> do_attach_volume ( context , instance , driver_bdm ) <EOL> def _attach_volume ( self , context , instance , bdm ) : <EOL> context = context . elevated ( ) <EOL> LOG . info ( _LI ( '<STR_LIT>' ) , <EOL> { '<STR_LIT>' : bdm . volume_id , <EOL> '<STR_LIT>' : bdm [ '<STR_LIT>' ] } , <EOL> context = context , instance = instance ) <EOL> try : <EOL> bdm . attach ( context , instance , self . volume_api , self . driver , <EOL> do_check_attach = False , do_driver_attach = True ) <EOL> except Exception : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> LOG . exception ( _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : bdm . volume_id , <EOL> '<STR_LIT>' : bdm [ '<STR_LIT>' ] } , <EOL> context = context , instance = instance ) <EOL> self . volume_api . unreserve_volume ( context , bdm . volume_id ) <EOL> info = { '<STR_LIT>' : bdm . volume_id } <EOL> self . _notify_about_instance_usage ( <EOL> context , instance , "<STR_LIT>" , extra_usage_info = info ) <EOL> def _driver_detach_volume ( self , context , instance , bdm ) : <EOL> """<STR_LIT>""" <EOL> mp = bdm . device_name <EOL> volume_id = bdm . volume_id <EOL> LOG . info ( _LI ( '<STR_LIT>' ) , <EOL> { '<STR_LIT>' : volume_id , '<STR_LIT>' : mp } , <EOL> context = context , instance = instance ) <EOL> connection_info = jsonutils . loads ( bdm . connection_info ) <EOL> if connection_info and '<STR_LIT>' not in connection_info : <EOL> connection_info [ '<STR_LIT>' ] = volume_id <EOL> try : <EOL> if not self . driver . instance_exists ( instance ) : <EOL> LOG . warning ( _LW ( '<STR_LIT>' ) , <EOL> context = context , instance = instance ) <EOL> encryption = encryptors . get_encryption_metadata ( <EOL> context , self . volume_api , volume_id , connection_info ) <EOL> self . driver . detach_volume ( connection_info , <EOL> instance , <EOL> mp , <EOL> encryption = encryption ) <EOL> except exception . DiskNotFound as err : <EOL> LOG . warning ( _LW ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> { '<STR_LIT>' : volume_id , '<STR_LIT>' : mp , '<STR_LIT>' : err } , <EOL> instance = instance ) <EOL> except Exception : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> LOG . exception ( _LE ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> { '<STR_LIT>' : volume_id , '<STR_LIT>' : mp } , <EOL> context = context , instance = instance ) <EOL> self . volume_api . roll_detaching ( context , volume_id ) <EOL> return connection_info <EOL> def _detach_volume ( self , context , volume_id , instance , destroy_bdm = True , <EOL> attachment_id = None ) : <EOL> """<STR_LIT>""" <EOL> bdm = objects . BlockDeviceMapping . get_by_volume_and_instance ( <EOL> context , volume_id , instance . uuid ) <EOL> if CONF . volume_usage_poll_interval > <NUM_LIT:0> : <EOL> vol_stats = [ ] <EOL> mp = bdm . device_name <EOL> if '<STR_LIT>' in mp : <EOL> mp = mp [ <NUM_LIT:5> : ] <EOL> try : <EOL> vol_stats = self . driver . block_stats ( instance , mp ) <EOL> except NotImplementedError : <EOL> pass <EOL> if vol_stats : <EOL> LOG . debug ( "<STR_LIT>" , <EOL> instance = instance ) <EOL> rd_req , rd_bytes , wr_req , wr_bytes , flush_ops = vol_stats <EOL> vol_usage = objects . VolumeUsage ( context ) <EOL> vol_usage . volume_id = volume_id <EOL> vol_usage . instance_uuid = instance . uuid <EOL> vol_usage . project_id = instance . project_id <EOL> vol_usage . user_id = instance . user_id <EOL> vol_usage . availability_zone = instance . availability_zone <EOL> vol_usage . curr_reads = rd_req <EOL> vol_usage . curr_read_bytes = rd_bytes <EOL> vol_usage . curr_writes = wr_req <EOL> vol_usage . curr_write_bytes = wr_bytes <EOL> vol_usage . save ( update_totals = True ) <EOL> self . notifier . info ( context , '<STR_LIT>' , <EOL> compute_utils . usage_volume_info ( vol_usage ) ) <EOL> connection_info = self . _driver_detach_volume ( context , instance , bdm ) <EOL> connector = self . driver . get_volume_connector ( instance ) <EOL> if connection_info and not destroy_bdm and ( <EOL> connector . get ( '<STR_LIT:host>' ) != instance . host ) : <EOL> stashed_connector = connection_info . get ( '<STR_LIT>' ) <EOL> if not stashed_connector : <EOL> LOG . warning ( _LW ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : instance . host , <EOL> '<STR_LIT>' : connector . get ( '<STR_LIT:host>' ) } ) <EOL> elif stashed_connector . get ( '<STR_LIT:host>' ) != instance . host : <EOL> LOG . error ( _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : instance . host , <EOL> '<STR_LIT>' : connector . get ( '<STR_LIT:host>' ) , <EOL> '<STR_LIT>' : stashed_connector . get ( '<STR_LIT:host>' ) } ) <EOL> else : <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> { '<STR_LIT>' : instance . host , <EOL> '<STR_LIT>' : connector . get ( '<STR_LIT:host>' ) , <EOL> '<STR_LIT>' : stashed_connector . get ( '<STR_LIT:host>' ) } ) <EOL> connector = stashed_connector <EOL> self . volume_api . terminate_connection ( context , volume_id , connector ) <EOL> if destroy_bdm : <EOL> bdm . destroy ( ) <EOL> info = dict ( volume_id = volume_id ) <EOL> self . _notify_about_instance_usage ( <EOL> context , instance , "<STR_LIT>" , extra_usage_info = info ) <EOL> self . volume_api . detach ( context . elevated ( ) , volume_id , instance . uuid , <EOL> attachment_id ) <EOL> @ wrap_exception ( ) <EOL> @ wrap_instance_fault <EOL> def detach_volume ( self , context , volume_id , instance , attachment_id = None ) : <EOL> """<STR_LIT>""" <EOL> self . _detach_volume ( context , volume_id , instance , <EOL> attachment_id = attachment_id ) <EOL> def _init_volume_connection ( self , context , new_volume_id , <EOL> old_volume_id , connector , instance , bdm ) : <EOL> new_cinfo = self . volume_api . initialize_connection ( context , <EOL> new_volume_id , <EOL> connector ) <EOL> old_cinfo = jsonutils . loads ( bdm [ '<STR_LIT>' ] ) <EOL> if old_cinfo and '<STR_LIT>' not in old_cinfo : <EOL> old_cinfo [ '<STR_LIT>' ] = old_volume_id <EOL> new_cinfo [ '<STR_LIT>' ] = old_cinfo [ '<STR_LIT>' ] <EOL> return ( old_cinfo , new_cinfo ) <EOL> def _swap_volume ( self , context , instance , bdm , connector , <EOL> old_volume_id , new_volume_id , resize_to ) : <EOL> mountpoint = bdm [ '<STR_LIT>' ] <EOL> failed = False <EOL> new_cinfo = None <EOL> try : <EOL> old_cinfo , new_cinfo = self . _init_volume_connection ( context , <EOL> new_volume_id , <EOL> old_volume_id , <EOL> connector , <EOL> instance , <EOL> bdm ) <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> { '<STR_LIT>' : new_cinfo , '<STR_LIT>' : old_cinfo } , <EOL> contex = context , instance = instance ) <EOL> self . driver . swap_volume ( old_cinfo , new_cinfo , instance , mountpoint , <EOL> resize_to ) <EOL> except Exception : <EOL> failed = True <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> if new_cinfo : <EOL> msg = _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> LOG . exception ( msg , { '<STR_LIT>' : old_volume_id , <EOL> '<STR_LIT>' : new_volume_id } , <EOL> context = context , <EOL> instance = instance ) <EOL> else : <EOL> msg = _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> LOG . exception ( msg , { '<STR_LIT>' : new_volume_id , <EOL> '<STR_LIT>' : bdm [ '<STR_LIT>' ] } , <EOL> context = context , <EOL> instance = instance ) <EOL> self . volume_api . roll_detaching ( context , old_volume_id ) <EOL> self . volume_api . unreserve_volume ( context , new_volume_id ) <EOL> finally : <EOL> conn_volume = new_volume_id if failed else old_volume_id <EOL> if new_cinfo : <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , { '<STR_LIT>' : conn_volume } , <EOL> context = context , instance = instance ) <EOL> self . volume_api . terminate_connection ( context , <EOL> conn_volume , <EOL> connector ) <EOL> comp_ret = self . volume_api . migrate_volume_completion ( <EOL> context , <EOL> old_volume_id , <EOL> new_volume_id , <EOL> error = failed ) <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , { '<STR_LIT>' : comp_ret } , <EOL> context = context , instance = instance ) <EOL> return ( comp_ret , new_cinfo ) <EOL> @ wrap_exception ( ) <EOL> @ reverts_task_state <EOL> @ wrap_instance_fault <EOL> def swap_volume ( self , context , old_volume_id , new_volume_id , instance ) : <EOL> """<STR_LIT>""" <EOL> context = context . elevated ( ) <EOL> bdm = objects . BlockDeviceMapping . get_by_volume_and_instance ( <EOL> context , old_volume_id , instance . uuid ) <EOL> connector = self . driver . get_volume_connector ( instance ) <EOL> resize_to = <NUM_LIT:0> <EOL> old_vol_size = self . volume_api . get ( context , old_volume_id ) [ '<STR_LIT:size>' ] <EOL> new_vol_size = self . volume_api . get ( context , new_volume_id ) [ '<STR_LIT:size>' ] <EOL> if new_vol_size > old_vol_size : <EOL> resize_to = new_vol_size <EOL> LOG . info ( _LI ( '<STR_LIT>' ) , <EOL> { '<STR_LIT>' : old_volume_id , '<STR_LIT>' : new_volume_id } , <EOL> context = context , instance = instance ) <EOL> comp_ret , new_cinfo = self . _swap_volume ( context , instance , <EOL> bdm , <EOL> connector , <EOL> old_volume_id , <EOL> new_volume_id , <EOL> resize_to ) <EOL> save_volume_id = comp_ret [ '<STR_LIT>' ] <EOL> values = { <EOL> '<STR_LIT>' : jsonutils . dumps ( new_cinfo ) , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : save_volume_id , <EOL> '<STR_LIT>' : None } <EOL> if resize_to : <EOL> values [ '<STR_LIT>' ] = resize_to <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , { '<STR_LIT>' : bdm . volume_id , <EOL> '<STR_LIT>' : values } , <EOL> context = context , instance = instance ) <EOL> bdm . update ( values ) <EOL> bdm . save ( ) <EOL> @ wrap_exception ( ) <EOL> def remove_volume_connection ( self , context , volume_id , instance ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> bdm = objects . BlockDeviceMapping . get_by_volume_and_instance ( <EOL> context , volume_id , instance . uuid ) <EOL> self . _driver_detach_volume ( context , instance , bdm ) <EOL> connector = self . driver . get_volume_connector ( instance ) <EOL> self . volume_api . terminate_connection ( context , volume_id , connector ) <EOL> except exception . NotFound : <EOL> pass <EOL> @ wrap_exception ( ) <EOL> @ wrap_instance_fault <EOL> def attach_interface ( self , context , instance , network_id , port_id , <EOL> requested_ip ) : <EOL> """<STR_LIT>""" <EOL> bind_host_id = self . driver . network_binding_host_id ( context , instance ) <EOL> network_info = self . network_api . allocate_port_for_instance ( <EOL> context , instance , port_id , network_id , requested_ip , <EOL> bind_host_id = bind_host_id ) <EOL> if len ( network_info ) != <NUM_LIT:1> : <EOL> LOG . error ( _LE ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , { '<STR_LIT>' : len ( network_info ) } ) <EOL> raise exception . InterfaceAttachFailed ( <EOL> instance_uuid = instance . uuid ) <EOL> image_meta = objects . ImageMeta . from_instance ( instance ) <EOL> try : <EOL> self . driver . attach_interface ( instance , image_meta , network_info [ <NUM_LIT:0> ] ) <EOL> except exception . NovaException as ex : <EOL> port_id = network_info [ <NUM_LIT:0> ] . get ( '<STR_LIT:id>' ) <EOL> LOG . warning ( _LW ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : port_id , '<STR_LIT>' : ex } , <EOL> instance = instance ) <EOL> try : <EOL> self . network_api . deallocate_port_for_instance ( <EOL> context , instance , port_id ) <EOL> except Exception : <EOL> LOG . warning ( _LW ( "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : port_id } , instance = instance ) <EOL> raise exception . InterfaceAttachFailed ( <EOL> instance_uuid = instance . uuid ) <EOL> return network_info [ <NUM_LIT:0> ] <EOL> @ wrap_exception ( ) <EOL> @ wrap_instance_fault <EOL> def detach_interface ( self , context , instance , port_id ) : <EOL> """<STR_LIT>""" <EOL> network_info = instance . info_cache . network_info <EOL> condemned = None <EOL> for vif in network_info : <EOL> if vif [ '<STR_LIT:id>' ] == port_id : <EOL> condemned = vif <EOL> break <EOL> if condemned is None : <EOL> raise exception . PortNotFound ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % port_id ) <EOL> try : <EOL> self . driver . detach_interface ( instance , condemned ) <EOL> except exception . NovaException as ex : <EOL> LOG . warning ( _LW ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : port_id , '<STR_LIT>' : ex } , instance = instance ) <EOL> raise exception . InterfaceDetachFailed ( instance_uuid = instance . uuid ) <EOL> else : <EOL> try : <EOL> self . network_api . deallocate_port_for_instance ( <EOL> context , instance , port_id ) <EOL> except Exception as ex : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> LOG . warning ( _LW ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> { '<STR_LIT>' : port_id , '<STR_LIT:error>' : ex } , <EOL> instance = instance ) <EOL> def _get_compute_info ( self , context , host ) : <EOL> return objects . ComputeNode . get_first_node_by_host_for_old_compat ( <EOL> context , host ) <EOL> @ wrap_exception ( ) <EOL> def check_instance_shared_storage ( self , ctxt , instance , data ) : <EOL> """<STR_LIT>""" <EOL> return self . driver . check_instance_shared_storage_remote ( ctxt , data ) <EOL> @ wrap_exception ( ) <EOL> @ wrap_instance_event <EOL> @ wrap_instance_fault <EOL> def check_can_live_migrate_destination ( self , ctxt , instance , <EOL> block_migration , disk_over_commit ) : <EOL> """<STR_LIT>""" <EOL> return self . _do_check_can_live_migrate_destination ( ctxt , instance , <EOL> block_migration , <EOL> disk_over_commit ) <EOL> def _do_check_can_live_migrate_destination ( self , ctxt , instance , <EOL> block_migration , <EOL> disk_over_commit ) : <EOL> src_compute_info = obj_base . obj_to_primitive ( <EOL> self . _get_compute_info ( ctxt , instance . host ) ) <EOL> dst_compute_info = obj_base . obj_to_primitive ( <EOL> self . _get_compute_info ( ctxt , CONF . host ) ) <EOL> dest_check_data = self . driver . check_can_live_migrate_destination ( ctxt , <EOL> instance , src_compute_info , dst_compute_info , <EOL> block_migration , disk_over_commit ) <EOL> LOG . debug ( '<STR_LIT>' , dest_check_data ) <EOL> try : <EOL> migrate_data = self . compute_rpcapi . check_can_live_migrate_source ( ctxt , instance , <EOL> dest_check_data ) <EOL> finally : <EOL> self . driver . check_can_live_migrate_destination_cleanup ( ctxt , <EOL> dest_check_data ) <EOL> return migrate_data <EOL> @ wrap_exception ( ) <EOL> @ wrap_instance_event <EOL> @ wrap_instance_fault <EOL> def check_can_live_migrate_source ( self , ctxt , instance , dest_check_data ) : <EOL> """<STR_LIT>""" <EOL> is_volume_backed = self . compute_api . is_volume_backed_instance ( ctxt , <EOL> instance ) <EOL> got_migrate_data_object = isinstance ( dest_check_data , <EOL> migrate_data_obj . LiveMigrateData ) <EOL> if not got_migrate_data_object : <EOL> dest_check_data = migrate_data_obj . LiveMigrateData . detect_implementation ( <EOL> dest_check_data ) <EOL> dest_check_data . is_volume_backed = is_volume_backed <EOL> block_device_info = self . _get_instance_block_device_info ( <EOL> ctxt , instance , refresh_conn_info = True ) <EOL> result = self . driver . check_can_live_migrate_source ( ctxt , instance , <EOL> dest_check_data , <EOL> block_device_info ) <EOL> if not got_migrate_data_object : <EOL> result = result . to_legacy_dict ( ) <EOL> LOG . debug ( '<STR_LIT>' , result ) <EOL> return result <EOL> @ wrap_exception ( ) <EOL> @ wrap_instance_event <EOL> @ wrap_instance_fault <EOL> def pre_live_migration ( self , context , instance , block_migration , disk , <EOL> migrate_data ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( '<STR_LIT>' , migrate_data ) <EOL> got_migrate_data_object = isinstance ( migrate_data , <EOL> migrate_data_obj . LiveMigrateData ) <EOL> if not got_migrate_data_object : <EOL> migrate_data = migrate_data_obj . LiveMigrateData . detect_implementation ( <EOL> migrate_data ) <EOL> block_device_info = self . _get_instance_block_device_info ( <EOL> context , instance , refresh_conn_info = True ) <EOL> network_info = self . network_api . get_instance_nw_info ( context , instance ) <EOL> self . _notify_about_instance_usage ( <EOL> context , instance , "<STR_LIT>" , <EOL> network_info = network_info ) <EOL> migrate_data = self . driver . pre_live_migration ( context , <EOL> instance , <EOL> block_device_info , <EOL> network_info , <EOL> disk , <EOL> migrate_data ) <EOL> LOG . debug ( '<STR_LIT>' % migrate_data ) <EOL> self . network_api . setup_networks_on_host ( context , instance , <EOL> self . host ) <EOL> self . driver . ensure_filtering_rules_for_instance ( instance , <EOL> network_info ) <EOL> self . _notify_about_instance_usage ( <EOL> context , instance , "<STR_LIT>" , <EOL> network_info = network_info ) <EOL> if not got_migrate_data_object and migrate_data : <EOL> migrate_data = migrate_data . to_legacy_dict ( <EOL> pre_migration_result = True ) <EOL> migrate_data = migrate_data [ '<STR_LIT>' ] <EOL> LOG . debug ( '<STR_LIT>' , migrate_data ) <EOL> return migrate_data <EOL> def _do_live_migration ( self , context , dest , instance , block_migration , <EOL> migration , migrate_data ) : <EOL> self . _set_migration_status ( migration , '<STR_LIT>' ) <EOL> got_migrate_data_object = isinstance ( migrate_data , <EOL> migrate_data_obj . LiveMigrateData ) <EOL> if not got_migrate_data_object : <EOL> migrate_data = migrate_data_obj . LiveMigrateData . detect_implementation ( <EOL> migrate_data ) <EOL> try : <EOL> if ( '<STR_LIT>' in migrate_data and <EOL> migrate_data . block_migration ) : <EOL> block_device_info = self . _get_instance_block_device_info ( <EOL> context , instance ) <EOL> disk = self . driver . get_instance_disk_info ( <EOL> instance , block_device_info = block_device_info ) <EOL> else : <EOL> disk = None <EOL> migrate_data = self . compute_rpcapi . pre_live_migration ( <EOL> context , instance , <EOL> block_migration , disk , dest , migrate_data ) <EOL> except Exception : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> LOG . exception ( _LE ( '<STR_LIT>' ) , <EOL> dest , instance = instance ) <EOL> self . _set_migration_status ( migration , '<STR_LIT>' ) <EOL> self . _rollback_live_migration ( context , instance , dest , <EOL> block_migration , migrate_data ) <EOL> self . _set_migration_status ( migration , '<STR_LIT>' ) <EOL> if migrate_data : <EOL> migrate_data . migration = migration <EOL> LOG . debug ( '<STR_LIT>' , migrate_data ) <EOL> try : <EOL> self . driver . live_migration ( context , instance , dest , <EOL> self . _post_live_migration , <EOL> self . _rollback_live_migration , <EOL> block_migration , migrate_data ) <EOL> except Exception : <EOL> LOG . exception ( _LE ( '<STR_LIT>' ) , instance = instance ) <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> self . _set_migration_status ( migration , '<STR_LIT>' ) <EOL> @ wrap_exception ( ) <EOL> @ wrap_instance_event <EOL> @ wrap_instance_fault <EOL> def live_migration ( self , context , dest , instance , block_migration , <EOL> migration , migrate_data ) : <EOL> """<STR_LIT>""" <EOL> self . _set_migration_status ( migration , '<STR_LIT>' ) <EOL> def dispatch_live_migration ( * args , ** kwargs ) : <EOL> with self . _live_migration_semaphore : <EOL> self . _do_live_migration ( * args , ** kwargs ) <EOL> utils . spawn_n ( dispatch_live_migration , <EOL> context , dest , instance , <EOL> block_migration , migration , <EOL> migrate_data ) <EOL> @ wrap_exception ( ) <EOL> @ wrap_instance_event <EOL> @ wrap_instance_fault <EOL> def live_migration_force_complete ( self , context , instance , migration_id ) : <EOL> """<STR_LIT>""" <EOL> migration = objects . Migration . get_by_id ( context , migration_id ) <EOL> if migration . status != '<STR_LIT>' : <EOL> raise exception . InvalidMigrationState ( migration_id = migration_id , <EOL> instance_uuid = instance . uuid , <EOL> state = migration . status , <EOL> method = '<STR_LIT>' ) <EOL> self . _notify_about_instance_usage ( <EOL> context , instance , '<STR_LIT>' ) <EOL> self . driver . live_migration_force_complete ( instance ) <EOL> self . _notify_about_instance_usage ( <EOL> context , instance , '<STR_LIT>' ) <EOL> @ wrap_exception ( ) <EOL> @ wrap_instance_event <EOL> @ wrap_instance_fault <EOL> def live_migration_abort ( self , context , instance , migration_id ) : <EOL> """<STR_LIT>""" <EOL> migration = objects . Migration . get_by_id ( context , migration_id ) <EOL> if migration . status != '<STR_LIT>' : <EOL> raise exception . InvalidMigrationState ( migration_id = migration_id , <EOL> instance_uuid = instance . uuid , <EOL> state = migration . status , <EOL> method = '<STR_LIT>' ) <EOL> self . _notify_about_instance_usage ( <EOL> context , instance , '<STR_LIT>' ) <EOL> self . driver . live_migration_abort ( instance ) <EOL> self . _notify_about_instance_usage ( <EOL> context , instance , '<STR_LIT>' ) <EOL> def _live_migration_cleanup_flags ( self , migrate_data ) : <EOL> """<STR_LIT>""" <EOL> is_shared_block_storage = True <EOL> is_shared_instance_path = True <EOL> if isinstance ( migrate_data , migrate_data_obj . LibvirtLiveMigrateData ) : <EOL> is_shared_block_storage = migrate_data . is_shared_block_storage <EOL> is_shared_instance_path = migrate_data . is_shared_instance_path <EOL> elif isinstance ( migrate_data , migrate_data_obj . XenapiLiveMigrateData ) : <EOL> is_shared_block_storage = not migrate_data . block_migration <EOL> is_shared_instance_path = not migrate_data . block_migration <EOL> do_cleanup = not is_shared_instance_path <EOL> destroy_disks = not is_shared_block_storage <EOL> return ( do_cleanup , destroy_disks ) <EOL> @ wrap_exception ( ) <EOL> @ wrap_instance_fault <EOL> def _post_live_migration ( self , ctxt , instance , <EOL> dest , block_migration = False , migrate_data = None ) : <EOL> """<STR_LIT>""" <EOL> LOG . info ( _LI ( '<STR_LIT>' ) , <EOL> instance = instance ) <EOL> bdms = objects . BlockDeviceMappingList . get_by_instance_uuid ( <EOL> ctxt , instance . uuid ) <EOL> block_device_info = self . _get_instance_block_device_info ( <EOL> ctxt , instance , bdms = bdms ) <EOL> self . driver . post_live_migration ( ctxt , instance , block_device_info , <EOL> migrate_data ) <EOL> connector = self . driver . get_volume_connector ( instance ) <EOL> for bdm in bdms : <EOL> if bdm . is_volume : <EOL> self . volume_api . terminate_connection ( ctxt , bdm . volume_id , <EOL> connector ) <EOL> network_info = self . network_api . get_instance_nw_info ( ctxt , instance ) <EOL> self . _notify_about_instance_usage ( ctxt , instance , <EOL> "<STR_LIT>" , <EOL> network_info = network_info ) <EOL> LOG . debug ( '<STR_LIT>' , <EOL> instance = instance ) <EOL> self . driver . unfilter_instance ( instance , <EOL> network_info ) <EOL> migration = { '<STR_LIT>' : self . host , <EOL> '<STR_LIT>' : dest , } <EOL> self . network_api . migrate_instance_start ( ctxt , <EOL> instance , <EOL> migration ) <EOL> destroy_vifs = False <EOL> try : <EOL> self . driver . post_live_migration_at_source ( ctxt , instance , <EOL> network_info ) <EOL> except NotImplementedError as ex : <EOL> LOG . debug ( ex , instance = instance ) <EOL> destroy_vifs = True <EOL> self . compute_rpcapi . post_live_migration_at_destination ( ctxt , <EOL> instance , block_migration , dest ) <EOL> do_cleanup , destroy_disks = self . _live_migration_cleanup_flags ( <EOL> migrate_data ) <EOL> if do_cleanup : <EOL> LOG . debug ( '<STR_LIT>' , <EOL> instance = instance ) <EOL> self . driver . cleanup ( ctxt , instance , network_info , <EOL> destroy_disks = destroy_disks , <EOL> migrate_data = migrate_data , <EOL> destroy_vifs = destroy_vifs ) <EOL> self . instance_events . clear_events_for_instance ( instance ) <EOL> self . update_available_resource ( ctxt ) <EOL> self . _update_scheduler_instance_info ( ctxt , instance ) <EOL> self . _notify_about_instance_usage ( ctxt , instance , <EOL> "<STR_LIT>" , <EOL> network_info = network_info ) <EOL> LOG . info ( _LI ( '<STR_LIT>' ) , <EOL> dest , instance = instance ) <EOL> LOG . info ( _LI ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> instance = instance ) <EOL> self . _clean_instance_console_tokens ( ctxt , instance ) <EOL> if migrate_data and migrate_data . obj_attr_is_set ( '<STR_LIT>' ) : <EOL> migrate_data . migration . status = '<STR_LIT>' <EOL> migrate_data . migration . save ( ) <EOL> def _consoles_enabled ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( CONF . vnc . enabled or CONF . spice . enabled or <EOL> CONF . rdp . enabled or CONF . serial_console . enabled or <EOL> CONF . mks . enabled ) <EOL> def _clean_instance_console_tokens ( self , ctxt , instance ) : <EOL> """<STR_LIT>""" <EOL> if self . _consoles_enabled ( ) : <EOL> if CONF . cells . enable : <EOL> self . cells_rpcapi . consoleauth_delete_tokens ( <EOL> ctxt , instance . uuid ) <EOL> else : <EOL> self . consoleauth_rpcapi . delete_tokens_for_instance ( <EOL> ctxt , instance . uuid ) <EOL> @ wrap_exception ( ) <EOL> @ wrap_instance_event <EOL> @ wrap_instance_fault <EOL> def post_live_migration_at_destination ( self , context , instance , <EOL> block_migration ) : <EOL> """<STR_LIT>""" <EOL> LOG . info ( _LI ( '<STR_LIT>' ) , <EOL> instance = instance ) <EOL> self . network_api . setup_networks_on_host ( context , instance , <EOL> self . host ) <EOL> migration = { '<STR_LIT>' : instance . host , <EOL> '<STR_LIT>' : self . host , } <EOL> self . network_api . migrate_instance_finish ( context , <EOL> instance , <EOL> migration ) <EOL> network_info = self . network_api . get_instance_nw_info ( context , instance ) <EOL> self . _notify_about_instance_usage ( <EOL> context , instance , "<STR_LIT>" , <EOL> network_info = network_info ) <EOL> block_device_info = self . _get_instance_block_device_info ( context , <EOL> instance ) <EOL> try : <EOL> self . driver . post_live_migration_at_destination ( <EOL> context , instance , network_info , block_migration , <EOL> block_device_info ) <EOL> except Exception : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> instance . vm_state = vm_states . ERROR <EOL> LOG . error ( _LE ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , instance = instance ) <EOL> finally : <EOL> current_power_state = self . _get_power_state ( context , instance ) <EOL> node_name = None <EOL> prev_host = instance . host <EOL> try : <EOL> compute_node = self . _get_compute_info ( context , self . host ) <EOL> node_name = compute_node . hypervisor_hostname <EOL> except exception . ComputeHostNotFound : <EOL> LOG . exception ( _LE ( '<STR_LIT>' ) , <EOL> self . host ) <EOL> finally : <EOL> instance . host = self . host <EOL> instance . power_state = current_power_state <EOL> instance . task_state = None <EOL> instance . node = node_name <EOL> instance . progress = <NUM_LIT:0> <EOL> instance . save ( expected_task_state = task_states . MIGRATING ) <EOL> self . network_api . setup_networks_on_host ( context , instance , <EOL> prev_host , teardown = True ) <EOL> self . network_api . setup_networks_on_host ( context , instance , self . host ) <EOL> self . _notify_about_instance_usage ( <EOL> context , instance , "<STR_LIT>" , <EOL> network_info = network_info ) <EOL> @ wrap_exception ( ) <EOL> @ wrap_instance_fault <EOL> def _rollback_live_migration ( self , context , instance , <EOL> dest , block_migration , migrate_data = None , <EOL> migration_status = '<STR_LIT:error>' ) : <EOL> """<STR_LIT>""" <EOL> instance . task_state = None <EOL> instance . progress = <NUM_LIT:0> <EOL> instance . save ( expected_task_state = [ task_states . MIGRATING ] ) <EOL> if isinstance ( migrate_data , dict ) : <EOL> migration = migrate_data . pop ( '<STR_LIT>' , None ) <EOL> migrate_data = migrate_data_obj . LiveMigrateData . detect_implementation ( <EOL> migrate_data ) <EOL> elif ( isinstance ( migrate_data , migrate_data_obj . LiveMigrateData ) and <EOL> migrate_data . obj_attr_is_set ( '<STR_LIT>' ) ) : <EOL> migration = migrate_data . migration <EOL> else : <EOL> migration = None <EOL> self . network_api . setup_networks_on_host ( context , instance , self . host ) <EOL> bdms = objects . BlockDeviceMappingList . get_by_instance_uuid ( <EOL> context , instance . uuid ) <EOL> for bdm in bdms : <EOL> if bdm . is_volume : <EOL> self . compute_rpcapi . remove_volume_connection ( <EOL> context , bdm . volume_id , instance , dest ) <EOL> self . _notify_about_instance_usage ( context , instance , <EOL> "<STR_LIT>" ) <EOL> do_cleanup , destroy_disks = self . _live_migration_cleanup_flags ( <EOL> migrate_data ) <EOL> if do_cleanup : <EOL> self . compute_rpcapi . rollback_live_migration_at_destination ( <EOL> context , instance , dest , destroy_disks = destroy_disks , <EOL> migrate_data = migrate_data ) <EOL> self . _notify_about_instance_usage ( context , instance , <EOL> "<STR_LIT>" ) <EOL> self . _set_migration_status ( migration , migration_status ) <EOL> @ wrap_exception ( ) <EOL> @ wrap_instance_event <EOL> @ wrap_instance_fault <EOL> def rollback_live_migration_at_destination ( self , context , instance , <EOL> destroy_disks , <EOL> migrate_data ) : <EOL> """<STR_LIT>""" <EOL> network_info = self . network_api . get_instance_nw_info ( context , instance ) <EOL> self . _notify_about_instance_usage ( <EOL> context , instance , "<STR_LIT>" , <EOL> network_info = network_info ) <EOL> try : <EOL> self . network_api . setup_networks_on_host ( context , instance , <EOL> self . host , teardown = True ) <EOL> except Exception : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> LOG . exception ( <EOL> _LE ( '<STR_LIT>' ) , <EOL> instance = instance ) <EOL> finally : <EOL> block_device_info = self . _get_instance_block_device_info ( context , <EOL> instance ) <EOL> if isinstance ( migrate_data , dict ) : <EOL> migrate_data = migrate_data_obj . LiveMigrateData . detect_implementation ( <EOL> migrate_data ) <EOL> self . driver . rollback_live_migration_at_destination ( <EOL> context , instance , network_info , block_device_info , <EOL> destroy_disks = destroy_disks , migrate_data = migrate_data ) <EOL> self . _notify_about_instance_usage ( <EOL> context , instance , "<STR_LIT>" , <EOL> network_info = network_info ) <EOL> @ periodic_task . periodic_task ( <EOL> spacing = CONF . heal_instance_info_cache_interval ) <EOL> def _heal_instance_info_cache ( self , context ) : <EOL> """<STR_LIT>""" <EOL> heal_interval = CONF . heal_instance_info_cache_interval <EOL> if not heal_interval : <EOL> return <EOL> instance_uuids = getattr ( self , '<STR_LIT>' , [ ] ) <EOL> instance = None <EOL> LOG . debug ( '<STR_LIT>' ) <EOL> if not instance_uuids : <EOL> LOG . debug ( '<STR_LIT>' ) <EOL> db_instances = objects . InstanceList . get_by_host ( <EOL> context , self . host , expected_attrs = [ ] , use_slave = True ) <EOL> for inst in db_instances : <EOL> if ( inst . vm_state == vm_states . BUILDING ) : <EOL> LOG . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' , instance = inst ) <EOL> continue <EOL> if ( inst . task_state == task_states . DELETING ) : <EOL> LOG . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' , instance = inst ) <EOL> continue <EOL> if not instance : <EOL> instance = inst <EOL> else : <EOL> instance_uuids . append ( inst [ '<STR_LIT>' ] ) <EOL> self . _instance_uuids_to_heal = instance_uuids <EOL> else : <EOL> while instance_uuids : <EOL> try : <EOL> inst = objects . Instance . get_by_uuid ( <EOL> context , instance_uuids . pop ( <NUM_LIT:0> ) , <EOL> expected_attrs = [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] , <EOL> use_slave = True ) <EOL> except exception . InstanceNotFound : <EOL> continue <EOL> if inst . host != self . host : <EOL> LOG . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , instance = inst ) <EOL> elif inst . task_state == task_states . DELETING : <EOL> LOG . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' , instance = inst ) <EOL> else : <EOL> instance = inst <EOL> break <EOL> if instance : <EOL> try : <EOL> self . network_api . get_instance_nw_info ( context , instance ) <EOL> LOG . debug ( '<STR_LIT>' , <EOL> instance = instance ) <EOL> except exception . InstanceNotFound : <EOL> LOG . debug ( '<STR_LIT>' , <EOL> instance = instance ) <EOL> return <EOL> except exception . InstanceInfoCacheNotFound : <EOL> LOG . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' , instance = instance ) <EOL> except Exception : <EOL> LOG . error ( _LE ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , instance = instance , exc_info = True ) <EOL> else : <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> @ periodic_task . periodic_task <EOL> def _poll_rebooting_instances ( self , context ) : <EOL> if CONF . reboot_timeout > <NUM_LIT:0> : <EOL> filters = { '<STR_LIT>' : <EOL> [ task_states . REBOOTING , <EOL> task_states . REBOOT_STARTED , <EOL> task_states . REBOOT_PENDING ] , <EOL> '<STR_LIT:host>' : self . host } <EOL> rebooting = objects . InstanceList . get_by_filters ( <EOL> context , filters , expected_attrs = [ ] , use_slave = True ) <EOL> to_poll = [ ] <EOL> for instance in rebooting : <EOL> if timeutils . is_older_than ( instance . updated_at , <EOL> CONF . reboot_timeout ) : <EOL> to_poll . append ( instance ) <EOL> self . driver . poll_rebooting_instances ( CONF . reboot_timeout , to_poll ) <EOL> @ periodic_task . periodic_task <EOL> def _poll_rescued_instances ( self , context ) : <EOL> if CONF . rescue_timeout > <NUM_LIT:0> : <EOL> filters = { '<STR_LIT>' : vm_states . RESCUED , <EOL> '<STR_LIT:host>' : self . host } <EOL> rescued_instances = objects . InstanceList . get_by_filters ( <EOL> context , filters , expected_attrs = [ "<STR_LIT>" ] , <EOL> use_slave = True ) <EOL> to_unrescue = [ ] <EOL> for instance in rescued_instances : <EOL> if timeutils . is_older_than ( instance . launched_at , <EOL> CONF . rescue_timeout ) : <EOL> to_unrescue . append ( instance ) <EOL> for instance in to_unrescue : <EOL> self . compute_api . unrescue ( context , instance ) <EOL> @ periodic_task . periodic_task <EOL> def _poll_unconfirmed_resizes ( self , context ) : <EOL> if CONF . resize_confirm_window == <NUM_LIT:0> : <EOL> return <EOL> migrations = objects . MigrationList . get_unconfirmed_by_dest_compute ( <EOL> context , CONF . resize_confirm_window , self . host , <EOL> use_slave = True ) <EOL> migrations_info = dict ( migration_count = len ( migrations ) , <EOL> confirm_window = CONF . resize_confirm_window ) <EOL> if migrations_info [ "<STR_LIT>" ] > <NUM_LIT:0> : <EOL> LOG . info ( _LI ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> migrations_info ) <EOL> def _set_migration_to_error ( migration , reason , ** kwargs ) : <EOL> LOG . warning ( _LW ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : migration [ '<STR_LIT:id>' ] , '<STR_LIT>' : reason } , <EOL> ** kwargs ) <EOL> migration . status = '<STR_LIT:error>' <EOL> with migration . obj_as_admin ( ) : <EOL> migration . save ( ) <EOL> for migration in migrations : <EOL> instance_uuid = migration . instance_uuid <EOL> LOG . info ( _LI ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : migration . id , <EOL> '<STR_LIT>' : instance_uuid } ) <EOL> expected_attrs = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> try : <EOL> instance = objects . Instance . get_by_uuid ( context , <EOL> instance_uuid , expected_attrs = expected_attrs , <EOL> use_slave = True ) <EOL> except exception . InstanceNotFound : <EOL> reason = ( _ ( "<STR_LIT>" ) % <EOL> instance_uuid ) <EOL> _set_migration_to_error ( migration , reason ) <EOL> continue <EOL> if instance . vm_state == vm_states . ERROR : <EOL> reason = _ ( "<STR_LIT>" ) <EOL> _set_migration_to_error ( migration , reason , <EOL> instance = instance ) <EOL> continue <EOL> if instance . task_state in [ task_states . DELETING , <EOL> task_states . SOFT_DELETING ] : <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> LOG . debug ( msg , instance = instance ) <EOL> continue <EOL> if instance . task_state == task_states . RESIZE_FINISH : <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> LOG . debug ( msg , instance = instance ) <EOL> continue <EOL> vm_state = instance . vm_state <EOL> task_state = instance . task_state <EOL> if vm_state != vm_states . RESIZED or task_state is not None : <EOL> reason = ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % <EOL> { '<STR_LIT>' : vm_state , <EOL> '<STR_LIT>' : task_state } ) <EOL> _set_migration_to_error ( migration , reason , <EOL> instance = instance ) <EOL> continue <EOL> try : <EOL> self . compute_api . confirm_resize ( context , instance , <EOL> migration = migration ) <EOL> except Exception as e : <EOL> LOG . info ( _LI ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> e , instance = instance ) <EOL> @ periodic_task . periodic_task ( spacing = CONF . shelved_poll_interval ) <EOL> def _poll_shelved_instances ( self , context ) : <EOL> if CONF . shelved_offload_time <= <NUM_LIT:0> : <EOL> return <EOL> filters = { '<STR_LIT>' : vm_states . SHELVED , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT:host>' : self . host } <EOL> shelved_instances = objects . InstanceList . get_by_filters ( <EOL> context , filters = filters , expected_attrs = [ '<STR_LIT>' ] , <EOL> use_slave = True ) <EOL> to_gc = [ ] <EOL> for instance in shelved_instances : <EOL> sys_meta = instance . system_metadata <EOL> shelved_at = timeutils . parse_strtime ( sys_meta [ '<STR_LIT>' ] ) <EOL> if timeutils . is_older_than ( shelved_at , CONF . shelved_offload_time ) : <EOL> to_gc . append ( instance ) <EOL> for instance in to_gc : <EOL> try : <EOL> instance . task_state = task_states . SHELVING_OFFLOADING <EOL> instance . save ( expected_task_state = ( None , ) ) <EOL> self . shelve_offload_instance ( context , instance , <EOL> clean_shutdown = False ) <EOL> except Exception : <EOL> LOG . exception ( _LE ( '<STR_LIT>' ) , <EOL> instance = instance ) <EOL> @ periodic_task . periodic_task <EOL> def _instance_usage_audit ( self , context ) : <EOL> if not CONF . instance_usage_audit : <EOL> return <EOL> begin , end = utils . last_completed_audit_period ( ) <EOL> if objects . TaskLog . get ( context , '<STR_LIT>' , begin , end , <EOL> self . host ) : <EOL> return <EOL> instances = objects . InstanceList . get_active_by_window_joined ( <EOL> context , begin , end , host = self . host , <EOL> expected_attrs = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] , <EOL> use_slave = True ) <EOL> num_instances = len ( instances ) <EOL> errors = <NUM_LIT:0> <EOL> successes = <NUM_LIT:0> <EOL> LOG . info ( _LI ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT:host>' : self . host , <EOL> '<STR_LIT>' : begin , <EOL> '<STR_LIT>' : end , <EOL> '<STR_LIT>' : num_instances } ) <EOL> start_time = time . time ( ) <EOL> task_log = objects . TaskLog ( context ) <EOL> task_log . task_name = '<STR_LIT>' <EOL> task_log . period_beginning = begin <EOL> task_log . period_ending = end <EOL> task_log . host = self . host <EOL> task_log . task_items = num_instances <EOL> task_log . message = '<STR_LIT>' <EOL> task_log . begin_task ( ) <EOL> for instance in instances : <EOL> try : <EOL> compute_utils . notify_usage_exists ( <EOL> self . notifier , context , instance , <EOL> ignore_missing_network_data = False ) <EOL> successes += <NUM_LIT:1> <EOL> except Exception : <EOL> LOG . exception ( _LE ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , self . host , <EOL> instance = instance ) <EOL> errors += <NUM_LIT:1> <EOL> task_log . errors = errors <EOL> task_log . message = ( <EOL> '<STR_LIT>' <EOL> % ( self . host , num_instances , time . time ( ) - start_time ) ) <EOL> task_log . end_task ( ) <EOL> @ periodic_task . periodic_task ( spacing = CONF . bandwidth_poll_interval ) <EOL> def _poll_bandwidth_usage ( self , context ) : <EOL> if not self . _bw_usage_supported : <EOL> return <EOL> prev_time , start_time = utils . last_completed_audit_period ( ) <EOL> curr_time = time . time ( ) <EOL> if ( curr_time - self . _last_bw_usage_poll > <EOL> CONF . bandwidth_poll_interval ) : <EOL> self . _last_bw_usage_poll = curr_time <EOL> LOG . info ( _LI ( "<STR_LIT>" ) ) <EOL> cells_update_interval = CONF . cells . bandwidth_update_interval <EOL> if ( cells_update_interval > <NUM_LIT:0> and <EOL> curr_time - self . _last_bw_usage_cell_update > <EOL> cells_update_interval ) : <EOL> self . _last_bw_usage_cell_update = curr_time <EOL> update_cells = True <EOL> else : <EOL> update_cells = False <EOL> instances = objects . InstanceList . get_by_host ( context , <EOL> self . host , <EOL> use_slave = True ) <EOL> try : <EOL> bw_counters = self . driver . get_all_bw_counters ( instances ) <EOL> except NotImplementedError : <EOL> LOG . info ( _LI ( "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> self . _bw_usage_supported = False <EOL> return <EOL> refreshed = timeutils . utcnow ( ) <EOL> for bw_ctr in bw_counters : <EOL> greenthread . sleep ( <NUM_LIT:0> ) <EOL> bw_in = <NUM_LIT:0> <EOL> bw_out = <NUM_LIT:0> <EOL> last_ctr_in = None <EOL> last_ctr_out = None <EOL> usage = objects . BandwidthUsage . get_by_instance_uuid_and_mac ( <EOL> context , bw_ctr [ '<STR_LIT>' ] , bw_ctr [ '<STR_LIT>' ] , <EOL> start_period = start_time , use_slave = True ) <EOL> if usage : <EOL> bw_in = usage . bw_in <EOL> bw_out = usage . bw_out <EOL> last_ctr_in = usage . last_ctr_in <EOL> last_ctr_out = usage . last_ctr_out <EOL> else : <EOL> usage = ( objects . BandwidthUsage . <EOL> get_by_instance_uuid_and_mac ( <EOL> context , bw_ctr [ '<STR_LIT>' ] , bw_ctr [ '<STR_LIT>' ] , <EOL> start_period = prev_time , use_slave = True ) ) <EOL> if usage : <EOL> last_ctr_in = usage . last_ctr_in <EOL> last_ctr_out = usage . last_ctr_out <EOL> if last_ctr_in is not None : <EOL> if bw_ctr [ '<STR_LIT>' ] < last_ctr_in : <EOL> bw_in += bw_ctr [ '<STR_LIT>' ] <EOL> else : <EOL> bw_in += ( bw_ctr [ '<STR_LIT>' ] - last_ctr_in ) <EOL> if last_ctr_out is not None : <EOL> if bw_ctr [ '<STR_LIT>' ] < last_ctr_out : <EOL> bw_out += bw_ctr [ '<STR_LIT>' ] <EOL> else : <EOL> bw_out += ( bw_ctr [ '<STR_LIT>' ] - last_ctr_out ) <EOL> objects . BandwidthUsage ( context = context ) . create ( <EOL> bw_ctr [ '<STR_LIT>' ] , <EOL> bw_ctr [ '<STR_LIT>' ] , <EOL> bw_in , <EOL> bw_out , <EOL> bw_ctr [ '<STR_LIT>' ] , <EOL> bw_ctr [ '<STR_LIT>' ] , <EOL> start_period = start_time , <EOL> last_refreshed = refreshed , <EOL> update_cells = update_cells ) <EOL> def _get_host_volume_bdms ( self , context , use_slave = False ) : <EOL> """<STR_LIT>""" <EOL> compute_host_bdms = [ ] <EOL> instances = objects . InstanceList . get_by_host ( context , self . host , <EOL> use_slave = use_slave ) <EOL> for instance in instances : <EOL> bdms = objects . BlockDeviceMappingList . get_by_instance_uuid ( <EOL> context , instance . uuid , use_slave = use_slave ) <EOL> instance_bdms = [ bdm for bdm in bdms if bdm . is_volume ] <EOL> compute_host_bdms . append ( dict ( instance = instance , <EOL> instance_bdms = instance_bdms ) ) <EOL> return compute_host_bdms <EOL> def _update_volume_usage_cache ( self , context , vol_usages ) : <EOL> """<STR_LIT>""" <EOL> for usage in vol_usages : <EOL> greenthread . sleep ( <NUM_LIT:0> ) <EOL> vol_usage = objects . VolumeUsage ( context ) <EOL> vol_usage . volume_id = usage [ '<STR_LIT>' ] <EOL> vol_usage . instance_uuid = usage [ '<STR_LIT>' ] . uuid <EOL> vol_usage . project_id = usage [ '<STR_LIT>' ] . project_id <EOL> vol_usage . user_id = usage [ '<STR_LIT>' ] . user_id <EOL> vol_usage . availability_zone = usage [ '<STR_LIT>' ] . availability_zone <EOL> vol_usage . curr_reads = usage [ '<STR_LIT>' ] <EOL> vol_usage . curr_read_bytes = usage [ '<STR_LIT>' ] <EOL> vol_usage . curr_writes = usage [ '<STR_LIT>' ] <EOL> vol_usage . curr_write_bytes = usage [ '<STR_LIT>' ] <EOL> vol_usage . save ( ) <EOL> self . notifier . info ( context , '<STR_LIT>' , <EOL> compute_utils . usage_volume_info ( vol_usage ) ) <EOL> @ periodic_task . periodic_task ( spacing = CONF . volume_usage_poll_interval ) <EOL> def _poll_volume_usage ( self , context ) : <EOL> if CONF . volume_usage_poll_interval == <NUM_LIT:0> : <EOL> return <EOL> compute_host_bdms = self . _get_host_volume_bdms ( context , <EOL> use_slave = True ) <EOL> if not compute_host_bdms : <EOL> return <EOL> LOG . debug ( "<STR_LIT>" ) <EOL> try : <EOL> vol_usages = self . driver . get_all_volume_usage ( context , <EOL> compute_host_bdms ) <EOL> except NotImplementedError : <EOL> return <EOL> self . _update_volume_usage_cache ( context , vol_usages ) <EOL> @ periodic_task . periodic_task ( spacing = CONF . sync_power_state_interval , <EOL> run_immediately = True ) <EOL> def _sync_power_states ( self , context ) : <EOL> """<STR_LIT>""" <EOL> db_instances = objects . InstanceList . get_by_host ( context , self . host , <EOL> expected_attrs = [ ] , <EOL> use_slave = True ) <EOL> num_vm_instances = self . driver . get_num_instances ( ) <EOL> num_db_instances = len ( db_instances ) <EOL> if num_vm_instances != num_db_instances : <EOL> LOG . warning ( _LW ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : num_db_instances , <EOL> '<STR_LIT>' : num_vm_instances } ) <EOL> def _sync ( db_instance ) : <EOL> @ utils . synchronized ( db_instance . uuid ) <EOL> def query_driver_power_state_and_sync ( ) : <EOL> self . _query_driver_power_state_and_sync ( context , db_instance ) <EOL> try : <EOL> query_driver_power_state_and_sync ( ) <EOL> except Exception : <EOL> LOG . exception ( _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> instance = db_instance ) <EOL> self . _syncs_in_progress . pop ( db_instance . uuid ) <EOL> for db_instance in db_instances : <EOL> uuid = db_instance . uuid <EOL> if uuid in self . _syncs_in_progress : <EOL> LOG . debug ( '<STR_LIT>' % uuid ) <EOL> else : <EOL> LOG . debug ( '<STR_LIT>' % uuid ) <EOL> self . _syncs_in_progress [ uuid ] = True <EOL> self . _sync_power_pool . spawn_n ( _sync , db_instance ) <EOL> def _query_driver_power_state_and_sync ( self , context , db_instance ) : <EOL> if db_instance . task_state is not None : <EOL> LOG . info ( _LI ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : db_instance . task_state } , instance = db_instance ) <EOL> return <EOL> try : <EOL> vm_instance = self . driver . get_info ( db_instance ) <EOL> vm_power_state = vm_instance . state <EOL> except exception . InstanceNotFound : <EOL> vm_power_state = power_state . NOSTATE <EOL> try : <EOL> self . _sync_instance_power_state ( context , <EOL> db_instance , <EOL> vm_power_state , <EOL> use_slave = True ) <EOL> except exception . InstanceNotFound : <EOL> pass <EOL> def _sync_instance_power_state ( self , context , db_instance , vm_power_state , <EOL> use_slave = False ) : <EOL> """<STR_LIT>""" <EOL> db_instance . refresh ( use_slave = use_slave ) <EOL> db_power_state = db_instance . power_state <EOL> vm_state = db_instance . vm_state <EOL> if self . host != db_instance . host : <EOL> LOG . info ( _LI ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT:src>' : db_instance . host , <EOL> '<STR_LIT>' : self . host } , <EOL> instance = db_instance ) <EOL> return <EOL> elif db_instance . task_state is not None : <EOL> LOG . info ( _LI ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : db_instance . task_state } , <EOL> instance = db_instance ) <EOL> return <EOL> orig_db_power_state = db_power_state <EOL> if vm_power_state != db_power_state : <EOL> LOG . info ( _LI ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> { '<STR_LIT>' : db_power_state , <EOL> '<STR_LIT>' : vm_power_state } , <EOL> instance = db_instance ) <EOL> db_instance . power_state = vm_power_state <EOL> db_instance . save ( ) <EOL> db_power_state = vm_power_state <EOL> if vm_state in ( vm_states . BUILDING , <EOL> vm_states . RESCUED , <EOL> vm_states . RESIZED , <EOL> vm_states . SUSPENDED , <EOL> vm_states . ERROR ) : <EOL> pass <EOL> elif vm_state == vm_states . ACTIVE : <EOL> if vm_power_state in ( power_state . SHUTDOWN , <EOL> power_state . CRASHED ) : <EOL> LOG . warning ( _LW ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : vm_state , <EOL> '<STR_LIT>' : db_instance . task_state , <EOL> '<STR_LIT>' : orig_db_power_state , <EOL> '<STR_LIT>' : vm_power_state } , <EOL> instance = db_instance ) <EOL> try : <EOL> if db_instance . shutdown_terminate : <EOL> self . compute_api . delete ( context , db_instance ) <EOL> else : <EOL> self . compute_api . stop ( context , db_instance ) <EOL> except Exception : <EOL> LOG . exception ( _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> instance = db_instance ) <EOL> elif vm_power_state == power_state . SUSPENDED : <EOL> LOG . warning ( _LW ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , instance = db_instance ) <EOL> try : <EOL> self . compute_api . stop ( context , db_instance ) <EOL> except Exception : <EOL> LOG . exception ( _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> instance = db_instance ) <EOL> elif vm_power_state == power_state . PAUSED : <EOL> LOG . warning ( _LW ( "<STR_LIT>" ) , <EOL> instance = db_instance ) <EOL> elif vm_power_state == power_state . NOSTATE : <EOL> LOG . warning ( _LW ( "<STR_LIT>" ) , <EOL> instance = db_instance ) <EOL> elif vm_state == vm_states . STOPPED : <EOL> if vm_power_state not in ( power_state . NOSTATE , <EOL> power_state . SHUTDOWN , <EOL> power_state . CRASHED ) : <EOL> LOG . warning ( _LW ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : vm_state , <EOL> '<STR_LIT>' : db_instance . task_state , <EOL> '<STR_LIT>' : orig_db_power_state , <EOL> '<STR_LIT>' : vm_power_state } , <EOL> instance = db_instance ) <EOL> try : <EOL> self . compute_api . force_stop ( context , db_instance ) <EOL> except Exception : <EOL> LOG . exception ( _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> instance = db_instance ) <EOL> elif vm_state == vm_states . PAUSED : <EOL> if vm_power_state in ( power_state . SHUTDOWN , <EOL> power_state . CRASHED ) : <EOL> LOG . warning ( _LW ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , instance = db_instance ) <EOL> try : <EOL> self . compute_api . force_stop ( context , db_instance ) <EOL> except Exception : <EOL> LOG . exception ( _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> instance = db_instance ) <EOL> elif vm_state in ( vm_states . SOFT_DELETED , <EOL> vm_states . DELETED ) : <EOL> if vm_power_state not in ( power_state . NOSTATE , <EOL> power_state . SHUTDOWN ) : <EOL> LOG . warning ( _LW ( "<STR_LIT>" ) , <EOL> instance = db_instance ) <EOL> @ periodic_task . periodic_task <EOL> def _reclaim_queued_deletes ( self , context ) : <EOL> """<STR_LIT>""" <EOL> interval = CONF . reclaim_instance_interval <EOL> if interval <= <NUM_LIT:0> : <EOL> LOG . debug ( "<STR_LIT>" ) <EOL> return <EOL> quotas = objects . Quotas . from_reservations ( context , None ) <EOL> filters = { '<STR_LIT>' : vm_states . SOFT_DELETED , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT:host>' : self . host } <EOL> instances = objects . InstanceList . get_by_filters ( <EOL> context , filters , <EOL> expected_attrs = objects . instance . INSTANCE_DEFAULT_FIELDS , <EOL> use_slave = True ) <EOL> for instance in instances : <EOL> if self . _deleted_old_enough ( instance , interval ) : <EOL> bdms = objects . BlockDeviceMappingList . get_by_instance_uuid ( <EOL> context , instance . uuid ) <EOL> LOG . info ( _LI ( '<STR_LIT>' ) , instance = instance ) <EOL> try : <EOL> self . _delete_instance ( context , instance , bdms , quotas ) <EOL> except Exception as e : <EOL> LOG . warning ( _LW ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> e , instance = instance ) <EOL> def update_available_resource_for_node ( self , context , nodename ) : <EOL> rt = self . _get_resource_tracker ( nodename ) <EOL> try : <EOL> rt . update_available_resource ( context ) <EOL> except exception . ComputeHostNotFound : <EOL> LOG . info ( _LI ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , nodename ) <EOL> self . _resource_tracker_dict . pop ( nodename , None ) <EOL> return <EOL> except Exception : <EOL> LOG . exception ( _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , { '<STR_LIT>' : nodename } ) <EOL> self . _resource_tracker_dict [ nodename ] = rt <EOL> @ periodic_task . periodic_task ( spacing = CONF . update_resources_interval ) <EOL> def update_available_resource ( self , context ) : <EOL> """<STR_LIT>""" <EOL> compute_nodes_in_db = self . _get_compute_nodes_in_db ( context , <EOL> use_slave = True ) <EOL> nodenames = set ( self . driver . get_available_nodes ( ) ) <EOL> for nodename in nodenames : <EOL> self . update_available_resource_for_node ( context , nodename ) <EOL> self . _resource_tracker_dict = { <EOL> k : v for k , v in self . _resource_tracker_dict . items ( ) <EOL> if k in nodenames } <EOL> for cn in compute_nodes_in_db : <EOL> if cn . hypervisor_hostname not in nodenames : <EOL> LOG . info ( _LI ( "<STR_LIT>" ) , cn . id ) <EOL> cn . destroy ( ) <EOL> def _get_compute_nodes_in_db ( self , context , use_slave = False ) : <EOL> try : <EOL> return objects . ComputeNodeList . get_all_by_host ( context , self . host , <EOL> use_slave = use_slave ) <EOL> except exception . NotFound : <EOL> LOG . error ( _LE ( "<STR_LIT>" ) , self . host ) <EOL> return [ ] <EOL> @ periodic_task . periodic_task ( <EOL> spacing = CONF . running_deleted_instance_poll_interval ) <EOL> def _cleanup_running_deleted_instances ( self , context ) : <EOL> """<STR_LIT>""" <EOL> action = CONF . running_deleted_instance_action <EOL> if action == "<STR_LIT>" : <EOL> return <EOL> with utils . temporary_mutation ( context , read_deleted = "<STR_LIT:yes>" ) : <EOL> for instance in self . _running_deleted_instances ( context ) : <EOL> if action == "<STR_LIT>" : <EOL> LOG . warning ( _LW ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> instance . name , instance = instance ) <EOL> elif action == '<STR_LIT>' : <EOL> LOG . info ( _LI ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> instance . name , instance = instance ) <EOL> try : <EOL> try : <EOL> self . driver . set_bootable ( instance , False ) <EOL> except NotImplementedError : <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . driver . power_off ( instance ) <EOL> except Exception : <EOL> msg = _LW ( "<STR_LIT>" ) <EOL> LOG . warning ( msg , instance = instance , exc_info = True ) <EOL> elif action == '<STR_LIT>' : <EOL> LOG . info ( _LI ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> instance . name , instance = instance ) <EOL> bdms = objects . BlockDeviceMappingList . get_by_instance_uuid ( <EOL> context , instance . uuid , use_slave = True ) <EOL> self . instance_events . clear_events_for_instance ( instance ) <EOL> try : <EOL> self . _shutdown_instance ( context , instance , bdms , <EOL> notify = False ) <EOL> self . _cleanup_volumes ( context , instance . uuid , bdms ) <EOL> except Exception as e : <EOL> LOG . warning ( _LW ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> e , instance = instance ) <EOL> else : <EOL> raise Exception ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) % action ) <EOL> def _running_deleted_instances ( self , context ) : <EOL> """<STR_LIT>""" <EOL> timeout = CONF . running_deleted_instance_timeout <EOL> filters = { '<STR_LIT>' : True , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT:host>' : self . host } <EOL> instances = self . _get_instances_on_driver ( context , filters ) <EOL> return [ i for i in instances if self . _deleted_old_enough ( i , timeout ) ] <EOL> def _deleted_old_enough ( self , instance , timeout ) : <EOL> deleted_at = instance . deleted_at <EOL> if deleted_at : <EOL> deleted_at = deleted_at . replace ( tzinfo = None ) <EOL> return ( not deleted_at or timeutils . is_older_than ( deleted_at , timeout ) ) <EOL> @ contextlib . contextmanager <EOL> def _error_out_instance_on_exception ( self , context , instance , <EOL> quotas = None , <EOL> instance_state = vm_states . ACTIVE ) : <EOL> instance_uuid = instance . uuid <EOL> try : <EOL> yield <EOL> except NotImplementedError as error : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> if quotas : <EOL> quotas . rollback ( ) <EOL> LOG . info ( _LI ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT:state>' : instance_state , '<STR_LIT:error>' : error } , <EOL> instance_uuid = instance_uuid ) <EOL> self . _instance_update ( context , instance , <EOL> vm_state = instance_state , <EOL> task_state = None ) <EOL> except exception . InstanceFaultRollback as error : <EOL> if quotas : <EOL> quotas . rollback ( ) <EOL> LOG . info ( _LI ( "<STR_LIT>" ) , <EOL> error , instance_uuid = instance_uuid ) <EOL> self . _instance_update ( context , instance , <EOL> vm_state = vm_states . ACTIVE , <EOL> task_state = None ) <EOL> raise error . inner_exception <EOL> except Exception : <EOL> LOG . exception ( _LE ( '<STR_LIT>' ) , <EOL> instance_uuid = instance_uuid ) <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> if quotas : <EOL> quotas . rollback ( ) <EOL> self . _set_instance_obj_error_state ( context , instance ) <EOL> @ wrap_exception ( ) <EOL> def add_aggregate_host ( self , context , aggregate , host , slave_info ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . driver . add_to_aggregate ( context , aggregate , host , <EOL> slave_info = slave_info ) <EOL> except NotImplementedError : <EOL> LOG . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> except exception . AggregateError : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> self . driver . undo_aggregate_operation ( <EOL> context , <EOL> aggregate . delete_host , <EOL> aggregate , host ) <EOL> @ wrap_exception ( ) <EOL> def remove_aggregate_host ( self , context , host , slave_info , aggregate ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . driver . remove_from_aggregate ( context , aggregate , host , <EOL> slave_info = slave_info ) <EOL> except NotImplementedError : <EOL> LOG . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> except ( exception . AggregateError , <EOL> exception . InvalidAggregateAction ) as e : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> self . driver . undo_aggregate_operation ( <EOL> context , <EOL> aggregate . add_host , <EOL> aggregate , host , <EOL> isinstance ( e , exception . AggregateError ) ) <EOL> def _process_instance_event ( self , instance , event ) : <EOL> _event = self . instance_events . pop_instance_event ( instance , event ) <EOL> if _event : <EOL> LOG . debug ( '<STR_LIT>' , <EOL> { '<STR_LIT>' : event . key } , instance = instance ) <EOL> _event . send ( event ) <EOL> def _process_instance_vif_deleted_event ( self , context , instance , <EOL> deleted_vif_id ) : <EOL> network_info = instance . info_cache . network_info <EOL> for index , vif in enumerate ( network_info ) : <EOL> if vif [ '<STR_LIT:id>' ] == deleted_vif_id : <EOL> LOG . info ( _LI ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> { '<STR_LIT>' : vif [ '<STR_LIT:id>' ] } , <EOL> instance = instance ) <EOL> del network_info [ index ] <EOL> base_net_api . update_instance_cache_with_nw_info ( <EOL> self . network_api , context , <EOL> instance , <EOL> nw_info = network_info ) <EOL> try : <EOL> self . driver . detach_interface ( instance , vif ) <EOL> except exception . NovaException as ex : <EOL> LOG . warning ( _LW ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> { '<STR_LIT>' : deleted_vif_id , '<STR_LIT>' : ex } , <EOL> instance = instance ) <EOL> break <EOL> @ wrap_exception ( ) <EOL> def external_instance_event ( self , context , instances , events ) : <EOL> for event in events : <EOL> instance = [ inst for inst in instances <EOL> if inst . uuid == event . instance_uuid ] [ <NUM_LIT:0> ] <EOL> LOG . debug ( '<STR_LIT>' , <EOL> { '<STR_LIT>' : event . key } , <EOL> instance = instance ) <EOL> if event . name == '<STR_LIT>' : <EOL> try : <EOL> self . network_api . get_instance_nw_info ( context , instance ) <EOL> except exception . NotFound as e : <EOL> LOG . info ( _LI ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> { '<STR_LIT>' : event . key , '<STR_LIT:error>' : six . text_type ( e ) } , <EOL> instance = instance ) <EOL> elif event . name == '<STR_LIT>' : <EOL> self . _process_instance_vif_deleted_event ( context , <EOL> instance , <EOL> event . tag ) <EOL> else : <EOL> self . _process_instance_event ( instance , event ) <EOL> @ periodic_task . periodic_task ( spacing = CONF . image_cache_manager_interval , <EOL> external_process_ok = True ) <EOL> def _run_image_cache_manager_pass ( self , context ) : <EOL> """<STR_LIT>""" <EOL> if not self . driver . capabilities [ "<STR_LIT>" ] : <EOL> return <EOL> storage_users . register_storage_use ( CONF . instances_path , CONF . host ) <EOL> nodes = storage_users . get_storage_users ( CONF . instances_path ) <EOL> filters = { '<STR_LIT>' : False , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT:host>' : nodes } <EOL> filtered_instances = objects . InstanceList . get_by_filters ( context , <EOL> filters , expected_attrs = [ ] , use_slave = True ) <EOL> self . driver . manage_image_cache ( context , filtered_instances ) <EOL> @ periodic_task . periodic_task ( spacing = CONF . instance_delete_interval ) <EOL> def _run_pending_deletes ( self , context ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( '<STR_LIT>' ) <EOL> filters = { '<STR_LIT>' : True , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT:host>' : CONF . host , <EOL> '<STR_LIT>' : False } <EOL> attrs = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> with utils . temporary_mutation ( context , read_deleted = '<STR_LIT:yes>' ) : <EOL> instances = objects . InstanceList . get_by_filters ( <EOL> context , filters , expected_attrs = attrs , use_slave = True ) <EOL> LOG . debug ( '<STR_LIT>' , len ( instances ) ) <EOL> for instance in instances : <EOL> attempts = int ( instance . system_metadata . get ( '<STR_LIT>' , '<STR_LIT:0>' ) ) <EOL> LOG . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> { '<STR_LIT>' : attempts , <EOL> '<STR_LIT>' : CONF . maximum_instance_delete_attempts } , <EOL> instance = instance ) <EOL> if attempts < CONF . maximum_instance_delete_attempts : <EOL> success = self . driver . delete_instance_files ( instance ) <EOL> instance . system_metadata [ '<STR_LIT>' ] = str ( attempts + <NUM_LIT:1> ) <EOL> if success : <EOL> instance . cleaned = True <EOL> with utils . temporary_mutation ( context , read_deleted = '<STR_LIT:yes>' ) : <EOL> instance . save ( ) <EOL> @ periodic_task . periodic_task ( spacing = CONF . instance_delete_interval ) <EOL> def _cleanup_incomplete_migrations ( self , context ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( '<STR_LIT>' ) <EOL> migration_filters = { '<STR_LIT:host>' : CONF . host , <EOL> '<STR_LIT:status>' : '<STR_LIT:error>' } <EOL> migrations = objects . MigrationList . get_by_filters ( context , <EOL> migration_filters ) <EOL> if not migrations : <EOL> return <EOL> inst_uuid_from_migrations = set ( [ migration . instance_uuid for migration <EOL> in migrations ] ) <EOL> inst_filters = { '<STR_LIT>' : True , '<STR_LIT>' : False , <EOL> '<STR_LIT>' : inst_uuid_from_migrations , '<STR_LIT:host>' : CONF . host } <EOL> attrs = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> with utils . temporary_mutation ( context , read_deleted = '<STR_LIT:yes>' ) : <EOL> instances = objects . InstanceList . get_by_filters ( <EOL> context , inst_filters , expected_attrs = attrs , use_slave = True ) <EOL> for instance in instances : <EOL> for migration in migrations : <EOL> if instance . uuid == migration . instance_uuid : <EOL> self . driver . delete_instance_files ( instance ) <EOL> try : <EOL> migration . status = '<STR_LIT>' <EOL> with migration . obj_as_admin ( ) : <EOL> migration . save ( ) <EOL> except exception . MigrationNotFound : <EOL> LOG . warning ( _LW ( "<STR_LIT>" ) , <EOL> migration . id , context = context , <EOL> instance = instance ) <EOL> break <EOL> @ messaging . expected_exceptions ( exception . InstanceQuiesceNotSupported , <EOL> exception . QemuGuestAgentNotEnabled , <EOL> exception . NovaException , <EOL> NotImplementedError ) <EOL> @ wrap_exception ( ) <EOL> def quiesce_instance ( self , context , instance ) : <EOL> """<STR_LIT>""" <EOL> context = context . elevated ( ) <EOL> image_meta = objects . ImageMeta . from_instance ( instance ) <EOL> self . driver . quiesce ( context , instance , image_meta ) <EOL> def _wait_for_snapshots_completion ( self , context , mapping ) : <EOL> for mapping_dict in mapping : <EOL> if mapping_dict . get ( '<STR_LIT>' ) == '<STR_LIT>' : <EOL> def _wait_snapshot ( ) : <EOL> snapshot = self . volume_api . get_snapshot ( <EOL> context , mapping_dict [ '<STR_LIT>' ] ) <EOL> if snapshot . get ( '<STR_LIT:status>' ) != '<STR_LIT>' : <EOL> raise loopingcall . LoopingCallDone ( ) <EOL> timer = loopingcall . FixedIntervalLoopingCall ( _wait_snapshot ) <EOL> timer . start ( interval = <NUM_LIT:0.5> ) . wait ( ) <EOL> @ messaging . expected_exceptions ( exception . InstanceQuiesceNotSupported , <EOL> exception . QemuGuestAgentNotEnabled , <EOL> exception . NovaException , <EOL> NotImplementedError ) <EOL> @ wrap_exception ( ) <EOL> def unquiesce_instance ( self , context , instance , mapping = None ) : <EOL> """<STR_LIT>""" <EOL> context = context . elevated ( ) <EOL> if mapping : <EOL> try : <EOL> self . _wait_for_snapshots_completion ( context , mapping ) <EOL> except Exception as error : <EOL> LOG . exception ( _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> error , instance = instance ) <EOL> image_meta = objects . ImageMeta . from_instance ( instance ) <EOL> self . driver . unquiesce ( context , instance , image_meta ) </s>
