<s> """<STR_LIT>""" <EOL> import os <EOL> import socket <EOL> import sys <EOL> import tempfile <EOL> import time <EOL> import functools <EOL> from eventlet import greenthread <EOL> import nova . context <EOL> from nova import block_device <EOL> from nova import exception <EOL> from nova import flags <EOL> import nova . image <EOL> from nova import log as logging <EOL> from nova import manager <EOL> from nova import network <EOL> from nova import rpc <EOL> from nova import utils <EOL> from nova import volume <EOL> from nova import db <EOL> from nova . compute import power_state <EOL> from nova . compute import task_states <EOL> from nova . compute import vm_states <EOL> from nova . notifier import api as notifier <EOL> from nova . compute . utils import terminate_volumes <EOL> from nova . virt import driver <EOL> FLAGS = flags . FLAGS <EOL> flags . DEFINE_string ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> flags . DEFINE_string ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> flags . DEFINE_string ( '<STR_LIT>' , False , <EOL> '<STR_LIT>' ) <EOL> flags . DEFINE_integer ( '<STR_LIT>' , <NUM_LIT:12> , <EOL> '<STR_LIT>' ) <EOL> flags . DEFINE_string ( '<STR_LIT>' , socket . gethostname ( ) , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> flags . DEFINE_integer ( '<STR_LIT>' , <NUM_LIT:30> , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> flags . DEFINE_integer ( "<STR_LIT>" , <NUM_LIT:0> , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> flags . DEFINE_integer ( '<STR_LIT>' , <NUM_LIT> , <EOL> '<STR_LIT>' ) <EOL> LOG = logging . getLogger ( '<STR_LIT>' ) <EOL> def publisher_id ( host = None ) : <EOL> return notifier . publisher_id ( "<STR_LIT>" , host ) <EOL> def checks_instance_lock ( function ) : <EOL> """<STR_LIT>""" <EOL> @ functools . wraps ( function ) <EOL> def decorated_function ( self , context , instance_id , * args , ** kwargs ) : <EOL> LOG . info ( _ ( "<STR_LIT>" ) , function , <EOL> context = context ) <EOL> LOG . info ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % locals ( ) , context = context ) <EOL> locked = self . get_lock ( context , instance_id ) <EOL> admin = context . is_admin <EOL> LOG . info ( _ ( "<STR_LIT>" ) , locked , <EOL> context = context ) <EOL> LOG . info ( _ ( "<STR_LIT>" ) , admin , <EOL> context = context ) <EOL> if admin or not locked : <EOL> LOG . info ( _ ( "<STR_LIT>" ) , function , <EOL> context = context ) <EOL> function ( self , context , instance_id , * args , ** kwargs ) <EOL> else : <EOL> LOG . error ( _ ( "<STR_LIT>" ) , <EOL> function , context = context ) <EOL> return False <EOL> return decorated_function <EOL> class ComputeManager ( manager . SchedulerDependentManager ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , compute_driver = None , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not compute_driver : <EOL> compute_driver = FLAGS . compute_driver <EOL> try : <EOL> self . driver = utils . check_isinstance ( <EOL> utils . import_object ( compute_driver ) , <EOL> driver . ComputeDriver ) <EOL> except ImportError as e : <EOL> LOG . error ( _ ( "<STR_LIT>" ) % ( e ) ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> self . network_api = network . API ( ) <EOL> self . network_manager = utils . import_object ( FLAGS . network_manager ) <EOL> self . volume_manager = utils . import_object ( FLAGS . volume_manager ) <EOL> self . _last_host_check = <NUM_LIT:0> <EOL> super ( ComputeManager , self ) . __init__ ( service_name = "<STR_LIT>" , <EOL> * args , ** kwargs ) <EOL> def _instance_update ( self , context , instance_id , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return self . db . instance_update ( context , instance_id , kwargs ) <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 = self . db . instance_get_all_by_host ( context , self . host ) <EOL> for instance in instances : <EOL> inst_name = instance [ '<STR_LIT:name>' ] <EOL> db_state = instance [ '<STR_LIT>' ] <EOL> drv_state = self . _get_power_state ( context , instance ) <EOL> expect_running = db_state == power_state . RUNNING and drv_state != db_state <EOL> LOG . debug ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , locals ( ) ) <EOL> if ( expect_running and FLAGS . resume_guests_state_on_host_boot ) or FLAGS . start_guests_on_host_boot : <EOL> LOG . info ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , locals ( ) ) <EOL> self . reboot_instance ( context , instance [ '<STR_LIT:id>' ] ) <EOL> elif drv_state == power_state . RUNNING : <EOL> try : <EOL> net_info = self . _get_instance_nw_info ( context , instance ) <EOL> self . driver . ensure_filtering_rules_for_instance ( instance , <EOL> net_info ) <EOL> except NotImplementedError : <EOL> LOG . warning ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> def _get_power_state ( self , context , instance ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( _ ( '<STR_LIT>' ) , instance [ '<STR_LIT:name>' ] ) <EOL> try : <EOL> return self . driver . get_info ( instance [ '<STR_LIT:name>' ] ) [ "<STR_LIT:state>" ] <EOL> except exception . NotFound : <EOL> return power_state . FAILED <EOL> def get_console_topic ( self , context , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return self . db . queue_get_for ( context , <EOL> FLAGS . console_topic , <EOL> FLAGS . console_host ) <EOL> def get_console_pool_info ( self , context , console_type ) : <EOL> return self . driver . get_console_pool_info ( console_type ) <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> def refresh_security_group_rules ( self , context , security_group_id , <EOL> ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return self . driver . refresh_security_group_rules ( security_group_id ) <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> def refresh_security_group_members ( self , context , <EOL> security_group_id , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return self . driver . refresh_security_group_members ( security_group_id ) <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> def refresh_provider_fw_rules ( self , context , ** _kwargs ) : <EOL> """<STR_LIT>""" <EOL> return self . driver . refresh_provider_fw_rules ( ) <EOL> def _get_instance_nw_info ( self , context , instance ) : <EOL> """<STR_LIT>""" <EOL> network_info = [ ] <EOL> if not FLAGS . stub_network : <EOL> network_info = self . network_api . get_instance_nw_info ( context , <EOL> instance ) <EOL> return network_info <EOL> def _setup_block_device_mapping ( self , context , instance_id ) : <EOL> """<STR_LIT>""" <EOL> volume_api = volume . API ( ) <EOL> block_device_mapping = [ ] <EOL> swap = None <EOL> ephemerals = [ ] <EOL> for bdm in self . db . block_device_mapping_get_all_by_instance ( <EOL> context , instance_id ) : <EOL> LOG . debug ( _ ( "<STR_LIT>" ) , bdm ) <EOL> if bdm [ '<STR_LIT>' ] : <EOL> continue <EOL> if bdm [ '<STR_LIT>' ] : <EOL> virtual_name = bdm [ '<STR_LIT>' ] <EOL> device_name = bdm [ '<STR_LIT>' ] <EOL> assert block_device . is_swap_or_ephemeral ( virtual_name ) <EOL> if virtual_name == '<STR_LIT>' : <EOL> swap = { '<STR_LIT>' : device_name , <EOL> '<STR_LIT>' : bdm [ '<STR_LIT>' ] } <EOL> elif block_device . is_ephemeral ( virtual_name ) : <EOL> eph = { '<STR_LIT>' : block_device . ephemeral_num ( virtual_name ) , <EOL> '<STR_LIT>' : virtual_name , <EOL> '<STR_LIT>' : device_name , <EOL> '<STR_LIT:size>' : bdm [ '<STR_LIT>' ] } <EOL> ephemerals . append ( eph ) <EOL> continue <EOL> if ( ( bdm [ '<STR_LIT>' ] is not None ) and <EOL> ( bdm [ '<STR_LIT>' ] is None ) ) : <EOL> vol = volume_api . create ( context , bdm [ '<STR_LIT>' ] , <EOL> bdm [ '<STR_LIT>' ] , '<STR_LIT>' , '<STR_LIT>' ) <EOL> volume_api . wait_creation ( context , vol [ '<STR_LIT:id>' ] ) <EOL> self . db . block_device_mapping_update ( <EOL> context , bdm [ '<STR_LIT:id>' ] , { '<STR_LIT>' : vol [ '<STR_LIT:id>' ] } ) <EOL> bdm [ '<STR_LIT>' ] = vol [ '<STR_LIT:id>' ] <EOL> if not ( ( bdm [ '<STR_LIT>' ] is None ) or <EOL> ( bdm [ '<STR_LIT>' ] is not None ) ) : <EOL> LOG . error ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) % <EOL> { '<STR_LIT:id>' : bdm [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT>' : bdm [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : bdm [ '<STR_LIT>' ] } ) <EOL> raise exception . ApiError ( _ ( '<STR_LIT>' ) % <EOL> bdm [ '<STR_LIT:id>' ] ) <EOL> if bdm [ '<STR_LIT>' ] is not None : <EOL> volume_api . check_attach ( context , <EOL> volume_id = bdm [ '<STR_LIT>' ] ) <EOL> dev_path = self . _attach_volume_boot ( context , instance_id , <EOL> bdm [ '<STR_LIT>' ] , <EOL> bdm [ '<STR_LIT>' ] ) <EOL> block_device_mapping . append ( { '<STR_LIT>' : dev_path , <EOL> '<STR_LIT>' : <EOL> bdm [ '<STR_LIT>' ] } ) <EOL> return ( swap , ephemerals , block_device_mapping ) <EOL> def _run_instance ( self , context , instance_id , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> def _check_image_size ( ) : <EOL> """<STR_LIT>""" <EOL> image_href = instance [ '<STR_LIT>' ] <EOL> image_service , image_id = nova . image . get_image_service ( context , <EOL> image_href ) <EOL> image_meta = image_service . show ( context , image_id ) <EOL> try : <EOL> size_bytes = image_meta [ '<STR_LIT:size>' ] <EOL> except KeyError : <EOL> return <EOL> instance_type_id = instance [ '<STR_LIT>' ] <EOL> instance_type = self . db . instance_type_get ( context , <EOL> instance_type_id ) <EOL> allowed_size_gb = instance_type [ '<STR_LIT>' ] <EOL> if allowed_size_gb == <NUM_LIT:0> : <EOL> return <EOL> allowed_size_bytes = allowed_size_gb * <NUM_LIT> * <NUM_LIT> * <NUM_LIT> <EOL> LOG . debug ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) % locals ( ) ) <EOL> if size_bytes > allowed_size_bytes : <EOL> LOG . info ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> % locals ( ) ) <EOL> raise exception . ImageTooLarge ( ) <EOL> def _make_network_info ( ) : <EOL> if FLAGS . stub_network : <EOL> network_info = [ ] <EOL> else : <EOL> network_info = self . network_api . allocate_for_instance ( context , <EOL> instance , vpn = is_vpn , <EOL> requested_networks = requested_networks ) <EOL> LOG . debug ( _ ( "<STR_LIT>" ) , network_info ) <EOL> return network_info <EOL> def _make_block_device_info ( ) : <EOL> ( swap , ephemerals , <EOL> block_device_mapping ) = self . _setup_block_device_mapping ( <EOL> context , instance_id ) <EOL> block_device_info = { <EOL> '<STR_LIT>' : instance [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : swap , <EOL> '<STR_LIT>' : ephemerals , <EOL> '<STR_LIT>' : block_device_mapping } <EOL> return block_device_info <EOL> def _deallocate_network ( ) : <EOL> if not FLAGS . stub_network : <EOL> LOG . debug ( _ ( "<STR_LIT>" ) , <EOL> instance [ '<STR_LIT:id>' ] ) <EOL> self . network_api . deallocate_for_instance ( context , <EOL> instance ) <EOL> context = context . elevated ( ) <EOL> instance = self . db . instance_get ( context , instance_id ) <EOL> requested_networks = kwargs . get ( '<STR_LIT>' , None ) <EOL> if instance [ '<STR_LIT:name>' ] in self . driver . list_instances ( ) : <EOL> raise exception . Error ( _ ( "<STR_LIT>" ) ) <EOL> _check_image_size ( ) <EOL> LOG . audit ( _ ( "<STR_LIT>" ) , instance_id , <EOL> context = context ) <EOL> updates = { } <EOL> updates [ '<STR_LIT:host>' ] = self . host <EOL> updates [ '<STR_LIT>' ] = self . host <EOL> updates [ '<STR_LIT>' ] = vm_states . BUILDING <EOL> updates [ '<STR_LIT>' ] = task_states . NETWORKING <EOL> instance = self . db . instance_update ( context , instance_id , updates ) <EOL> instance [ '<STR_LIT>' ] = kwargs . get ( '<STR_LIT>' , [ ] ) <EOL> instance [ '<STR_LIT>' ] = kwargs . get ( '<STR_LIT>' , None ) <EOL> is_vpn = instance [ '<STR_LIT>' ] == str ( FLAGS . vpn_image_id ) <EOL> network_info = _make_network_info ( ) <EOL> def _inner_run_instance ( ) : <EOL> try : <EOL> self . _instance_update ( context , <EOL> instance_id , <EOL> vm_state = vm_states . BUILDING , <EOL> task_state = task_states . BLOCK_DEVICE_MAPPING ) <EOL> block_device_info = _make_block_device_info ( ) <EOL> self . _instance_update ( context , <EOL> instance_id , <EOL> vm_state = vm_states . BUILDING , <EOL> task_state = task_states . SPAWNING ) <EOL> try : <EOL> self . driver . spawn ( context , instance , <EOL> network_info , block_device_info ) <EOL> except Exception as ex : <EOL> msg = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) % locals ( ) <EOL> LOG . exception ( msg ) <EOL> _deallocate_network ( ) <EOL> return <EOL> current_power_state = self . _get_power_state ( context , instance ) <EOL> self . _instance_update ( context , <EOL> instance_id , <EOL> power_state = current_power_state , <EOL> vm_state = vm_states . ACTIVE , <EOL> task_state = None , <EOL> launched_at = utils . utcnow ( ) ) <EOL> usage_info = utils . usage_from_instance ( instance ) <EOL> notifier . notify ( '<STR_LIT>' % self . host , <EOL> '<STR_LIT>' , <EOL> notifier . INFO , usage_info ) <EOL> except exception . InstanceNotFound : <EOL> pass <EOL> except : <EOL> with utils . save_and_reraise_exception ( ) : <EOL> _deallocate_network ( ) <EOL> greenthread . spawn ( _inner_run_instance ) <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> def run_instance ( self , context , instance_id , ** kwargs ) : <EOL> self . _run_instance ( context , instance_id , ** kwargs ) <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> def start_instance ( self , context , instance_id ) : <EOL> """<STR_LIT>""" <EOL> def _inner_start_instance ( ) : <EOL> instance = self . db . instance_get ( context , instance_id ) <EOL> self . driver . start ( context , instance ) <EOL> self . _instance_update ( context , <EOL> instance_id , <EOL> vm_state = vm_states . ACTIVE , <EOL> task_state = None ) <EOL> greenthread . spawn ( _inner_start_instance ) <EOL> def _shutdown_instance ( self , context , instance_id , action_str ) : <EOL> """<STR_LIT>""" <EOL> context = context . elevated ( ) <EOL> instance = self . db . instance_get ( context , instance_id ) <EOL> LOG . audit ( _ ( "<STR_LIT>" ) % <EOL> { '<STR_LIT>' : action_str , '<STR_LIT>' : instance_id } , <EOL> context = context ) <EOL> network_info = self . _get_instance_nw_info ( context , instance ) <EOL> if not FLAGS . stub_network : <EOL> self . network_api . deallocate_for_instance ( context , instance ) <EOL> return self . driver . destroy ( context , instance , network_info ) <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> @ checks_instance_lock <EOL> def terminate_instance ( self , context , instance_id ) : <EOL> """<STR_LIT>""" <EOL> def _inner_terminate_instance ( ) : <EOL> try : <EOL> self . _instance_update ( context , <EOL> instance_id , <EOL> vm_state = vm_states . DELETED , <EOL> task_state = None , <EOL> terminated_at = utils . utcnow ( ) ) <EOL> bmm = self . _shutdown_instance ( context , instance_id , '<STR_LIT>' ) <EOL> instance = self . db . instance_get ( context . elevated ( ) , instance_id ) <EOL> instance_new = db . instance_create ( context , <EOL> { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : instance [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : instance [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : instance [ "<STR_LIT>" ] , <EOL> "<STR_LIT:host>" : instance [ "<STR_LIT:host>" ] , <EOL> "<STR_LIT>" : instance [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : instance [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : instance [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : vm_states . BUILDING , <EOL> "<STR_LIT>" : FLAGS . dodai_default_image } ) <EOL> self . db . instance_destroy ( context , instance_id ) <EOL> usage_info = utils . usage_from_instance ( instance ) <EOL> notifier . notify ( '<STR_LIT>' % self . host , <EOL> '<STR_LIT>' , <EOL> notifier . INFO , usage_info ) <EOL> self . driver . add_to_resource_pool ( context , instance_new , bmm ) <EOL> except Exception as ex : <EOL> LOG . exception ( ex ) <EOL> greenthread . spawn ( _inner_terminate_instance ) <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> def stop_instance ( self , context , instance_id ) : <EOL> """<STR_LIT>""" <EOL> def _inner_stop_instance ( ) : <EOL> instance = self . db . instance_get ( context , instance_id ) <EOL> self . _instance_update ( context , <EOL> instance_id , <EOL> vm_state = vm_states . STOPPED , <EOL> task_state = None ) <EOL> self . driver . stop ( context , instance ) <EOL> greenthread . spawn ( _inner_stop_instance ) <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> @ checks_instance_lock <EOL> def rebuild_instance ( self , context , instance_id , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> context = context . elevated ( ) <EOL> instance_ref = self . db . instance_get ( context , instance_id ) <EOL> LOG . audit ( _ ( "<STR_LIT>" ) , instance_id , context = context ) <EOL> current_power_state = self . _get_power_state ( context , instance_ref ) <EOL> self . _instance_update ( context , <EOL> instance_id , <EOL> power_state = current_power_state , <EOL> vm_state = vm_states . REBUILDING , <EOL> task_state = None ) <EOL> network_info = self . _get_instance_nw_info ( context , instance_ref ) <EOL> self . driver . destroy ( instance_ref , network_info ) <EOL> self . _instance_update ( context , <EOL> instance_id , <EOL> vm_state = vm_states . REBUILDING , <EOL> task_state = task_states . BLOCK_DEVICE_MAPPING ) <EOL> instance_ref . injected_files = kwargs . get ( '<STR_LIT>' , [ ] ) <EOL> network_info = self . network_api . get_instance_nw_info ( context , <EOL> instance_ref ) <EOL> bd_mapping = self . _setup_block_device_mapping ( context , instance_id ) <EOL> self . _instance_update ( context , <EOL> instance_id , <EOL> vm_state = vm_states . REBUILDING , <EOL> task_state = task_states . SPAWNING ) <EOL> instance_ref . admin_pass = kwargs . get ( '<STR_LIT>' , <EOL> utils . generate_password ( FLAGS . password_length ) ) <EOL> self . driver . spawn ( context , instance_ref , network_info , bd_mapping ) <EOL> current_power_state = self . _get_power_state ( context , instance_ref ) <EOL> self . _instance_update ( context , <EOL> instance_id , <EOL> power_state = current_power_state , <EOL> vm_state = vm_states . ACTIVE , <EOL> task_state = None , <EOL> launched_at = utils . utcnow ( ) ) <EOL> usage_info = utils . usage_from_instance ( instance_ref ) <EOL> notifier . notify ( '<STR_LIT>' % self . host , <EOL> '<STR_LIT>' , <EOL> notifier . INFO , <EOL> usage_info ) <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> @ checks_instance_lock <EOL> def reboot_instance ( self , context , instance_id ) : <EOL> """<STR_LIT>""" <EOL> LOG . audit ( _ ( "<STR_LIT>" ) , instance_id , context = context ) <EOL> context = context . elevated ( ) <EOL> instance_ref = self . db . instance_get ( context , instance_id ) <EOL> current_power_state = self . _get_power_state ( context , instance_ref ) <EOL> self . _instance_update ( context , <EOL> instance_id , <EOL> power_state = current_power_state , <EOL> vm_state = vm_states . ACTIVE , <EOL> task_state = task_states . REBOOTING ) <EOL> if instance_ref [ '<STR_LIT>' ] != power_state . RUNNING : <EOL> state = instance_ref [ '<STR_LIT>' ] <EOL> running = power_state . RUNNING <EOL> LOG . warn ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) % locals ( ) , <EOL> context = context ) <EOL> network_info = self . _get_instance_nw_info ( context , instance_ref ) <EOL> self . driver . reboot ( instance_ref , network_info ) <EOL> current_power_state = self . _get_power_state ( context , instance_ref ) <EOL> self . _instance_update ( context , <EOL> instance_id , <EOL> power_state = current_power_state , <EOL> vm_state = vm_states . ACTIVE , <EOL> task_state = None ) <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> def snapshot_instance ( self , context , instance_id , image_id , <EOL> image_type = '<STR_LIT>' , backup_type = None , <EOL> rotation = None ) : <EOL> """<STR_LIT>""" <EOL> if image_type == "<STR_LIT>" : <EOL> task_state = task_states . IMAGE_SNAPSHOT <EOL> elif image_type == "<STR_LIT>" : <EOL> task_state = task_states . IMAGE_BACKUP <EOL> else : <EOL> raise Exception ( _ ( '<STR_LIT>' ) % image_type ) <EOL> context = context . elevated ( ) <EOL> instance_ref = self . db . instance_get ( context , instance_id ) <EOL> current_power_state = self . _get_power_state ( context , instance_ref ) <EOL> self . _instance_update ( context , <EOL> instance_id , <EOL> power_state = current_power_state , <EOL> vm_state = vm_states . ACTIVE , <EOL> task_state = task_state ) <EOL> LOG . audit ( _ ( '<STR_LIT>' ) , instance_id , <EOL> context = context ) <EOL> if instance_ref [ '<STR_LIT>' ] != power_state . RUNNING : <EOL> state = instance_ref [ '<STR_LIT>' ] <EOL> running = power_state . RUNNING <EOL> LOG . warn ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) % locals ( ) ) <EOL> self . driver . snapshot ( context , instance_ref , image_id ) <EOL> self . _instance_update ( context , instance_id , task_state = None ) <EOL> if image_type == '<STR_LIT>' and rotation : <EOL> raise exception . ImageRotationNotAllowed ( ) <EOL> elif image_type == '<STR_LIT>' and rotation : <EOL> instance_uuid = instance_ref [ '<STR_LIT>' ] <EOL> self . rotate_backups ( context , instance_uuid , backup_type , rotation ) <EOL> elif image_type == '<STR_LIT>' : <EOL> raise exception . RotationRequiredForBackup ( ) <EOL> def rotate_backups ( self , context , instance_uuid , backup_type , rotation ) : <EOL> """<STR_LIT>""" <EOL> def fetch_images ( ) : <EOL> images = [ ] <EOL> marker = None <EOL> while True : <EOL> batch = image_service . detail ( context , filters = filters , <EOL> marker = marker , sort_key = '<STR_LIT>' , sort_dir = '<STR_LIT>' ) <EOL> if not batch : <EOL> break <EOL> images += batch <EOL> marker = batch [ - <NUM_LIT:1> ] [ '<STR_LIT:id>' ] <EOL> return images <EOL> image_service = nova . image . get_default_image_service ( ) <EOL> filters = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : backup_type , <EOL> '<STR_LIT>' : instance_uuid } <EOL> images = fetch_images ( ) <EOL> num_images = len ( images ) <EOL> LOG . debug ( _ ( "<STR_LIT>" <EOL> % locals ( ) ) ) <EOL> if num_images > rotation : <EOL> excess = len ( images ) - rotation <EOL> LOG . debug ( _ ( "<STR_LIT>" % excess ) ) <EOL> for i in xrange ( excess ) : <EOL> image = images . pop ( ) <EOL> image_id = image [ '<STR_LIT:id>' ] <EOL> LOG . debug ( _ ( "<STR_LIT>" % image_id ) ) <EOL> image_service . delete ( context , image_id ) <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> @ checks_instance_lock <EOL> def set_admin_password ( self , context , instance_id , new_pass = None ) : <EOL> """<STR_LIT>""" <EOL> context = context . elevated ( ) <EOL> if new_pass is None : <EOL> new_pass = utils . generate_password ( FLAGS . password_length ) <EOL> max_tries = <NUM_LIT:10> <EOL> for i in xrange ( max_tries ) : <EOL> instance_ref = self . db . instance_get ( context , instance_id ) <EOL> instance_id = instance_ref [ "<STR_LIT:id>" ] <EOL> instance_state = instance_ref [ "<STR_LIT>" ] <EOL> expected_state = power_state . RUNNING <EOL> if instance_state != expected_state : <EOL> raise exception . Error ( _ ( '<STR_LIT>' ) ) <EOL> else : <EOL> try : <EOL> self . driver . set_admin_password ( instance_ref , new_pass ) <EOL> LOG . audit ( _ ( "<STR_LIT>" ) , <EOL> instance_ref [ "<STR_LIT:name>" ] ) <EOL> break <EOL> except NotImplementedError : <EOL> LOG . warn ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> break <EOL> except Exception , e : <EOL> LOG . exception ( e ) <EOL> if i == max_tries - <NUM_LIT:1> : <EOL> raise exception . Error ( _ ( '<STR_LIT>' ) ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> continue <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> @ checks_instance_lock <EOL> def inject_file ( self , context , instance_id , path , file_contents ) : <EOL> """<STR_LIT>""" <EOL> context = context . elevated ( ) <EOL> instance_ref = self . db . instance_get ( context , instance_id ) <EOL> instance_id = instance_ref [ '<STR_LIT:id>' ] <EOL> instance_state = instance_ref [ '<STR_LIT>' ] <EOL> expected_state = power_state . RUNNING <EOL> if instance_state != expected_state : <EOL> LOG . warn ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) % locals ( ) ) <EOL> nm = instance_ref [ '<STR_LIT:name>' ] <EOL> msg = _ ( '<STR_LIT>' ) % locals ( ) <EOL> LOG . audit ( msg ) <EOL> self . driver . inject_file ( instance_ref , path , file_contents ) <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> @ checks_instance_lock <EOL> def agent_update ( self , context , instance_id , url , md5hash ) : <EOL> """<STR_LIT>""" <EOL> context = context . elevated ( ) <EOL> instance_ref = self . db . instance_get ( context , instance_id ) <EOL> instance_id = instance_ref [ '<STR_LIT:id>' ] <EOL> instance_state = instance_ref [ '<STR_LIT>' ] <EOL> expected_state = power_state . RUNNING <EOL> if instance_state != expected_state : <EOL> LOG . warn ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) % locals ( ) ) <EOL> nm = instance_ref [ '<STR_LIT:name>' ] <EOL> msg = _ ( '<STR_LIT>' ) % locals ( ) <EOL> LOG . audit ( msg ) <EOL> self . driver . agent_update ( instance_ref , url , md5hash ) <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> @ checks_instance_lock <EOL> def rescue_instance ( self , context , instance_id ) : <EOL> """<STR_LIT>""" <EOL> LOG . audit ( _ ( '<STR_LIT>' ) , instance_id , context = context ) <EOL> context = context . elevated ( ) <EOL> instance_ref = self . db . instance_get ( context , instance_id ) <EOL> network_info = self . _get_instance_nw_info ( context , instance_ref ) <EOL> self . driver . rescue ( context , instance_ref , None , network_info ) <EOL> current_power_state = self . _get_power_state ( context , instance_ref ) <EOL> self . _instance_update ( context , <EOL> instance_id , <EOL> vm_state = vm_states . RESCUED , <EOL> task_state = None , <EOL> power_state = current_power_state ) <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> @ checks_instance_lock <EOL> def unrescue_instance ( self , context , instance_id ) : <EOL> """<STR_LIT>""" <EOL> LOG . audit ( _ ( '<STR_LIT>' ) , instance_id , context = context ) <EOL> context = context . elevated ( ) <EOL> instance_ref = self . db . instance_get ( context , instance_id ) <EOL> network_info = self . _get_instance_nw_info ( context , instance_ref ) <EOL> self . driver . unrescue ( instance_ref , None , network_info ) <EOL> current_power_state = self . _get_power_state ( context , instance_ref ) <EOL> self . _instance_update ( context , <EOL> instance_id , <EOL> vm_state = vm_states . ACTIVE , <EOL> task_state = None , <EOL> power_state = current_power_state ) <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> @ checks_instance_lock <EOL> def confirm_resize ( self , context , instance_id , migration_id ) : <EOL> """<STR_LIT>""" <EOL> migration_ref = self . db . migration_get ( context , migration_id ) <EOL> instance_ref = self . db . instance_get_by_uuid ( context , <EOL> migration_ref . instance_uuid ) <EOL> network_info = self . _get_instance_nw_info ( context , instance_ref ) <EOL> self . driver . destroy ( instance_ref , network_info ) <EOL> usage_info = utils . usage_from_instance ( instance_ref ) <EOL> notifier . notify ( '<STR_LIT>' % self . host , <EOL> '<STR_LIT>' , <EOL> notifier . INFO , <EOL> usage_info ) <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> @ checks_instance_lock <EOL> def revert_resize ( self , context , instance_id , migration_id ) : <EOL> """<STR_LIT>""" <EOL> migration_ref = self . db . migration_get ( context , migration_id ) <EOL> instance_ref = self . db . instance_get_by_uuid ( context , <EOL> migration_ref . instance_uuid ) <EOL> network_info = self . _get_instance_nw_info ( context , instance_ref ) <EOL> self . driver . destroy ( instance_ref , network_info ) <EOL> topic = self . db . queue_get_for ( context , FLAGS . compute_topic , <EOL> instance_ref [ '<STR_LIT:host>' ] ) <EOL> rpc . cast ( context , topic , <EOL> { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:args>' : { '<STR_LIT>' : instance_ref [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : migration_ref [ '<STR_LIT:id>' ] } , <EOL> } ) <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> @ checks_instance_lock <EOL> def finish_revert_resize ( self , context , instance_id , migration_id ) : <EOL> """<STR_LIT>""" <EOL> migration_ref = self . db . migration_get ( context , migration_id ) <EOL> instance_ref = self . db . instance_get_by_uuid ( context , <EOL> migration_ref . instance_uuid ) <EOL> instance_type = self . db . instance_type_get ( context , <EOL> migration_ref [ '<STR_LIT>' ] ) <EOL> self . _instance_update ( context , <EOL> instance_ref [ "<STR_LIT>" ] , <EOL> memory_mb = instance_type [ '<STR_LIT>' ] , <EOL> vcpus = instance_type [ '<STR_LIT>' ] , <EOL> local_gb = instance_type [ '<STR_LIT>' ] , <EOL> instance_type_id = instance_type [ '<STR_LIT:id>' ] ) <EOL> self . driver . revert_migration ( instance_ref ) <EOL> self . db . migration_update ( context , migration_id , <EOL> { '<STR_LIT:status>' : '<STR_LIT>' } ) <EOL> usage_info = utils . usage_from_instance ( instance_ref ) <EOL> notifier . notify ( '<STR_LIT>' % self . host , <EOL> '<STR_LIT>' , <EOL> notifier . INFO , <EOL> usage_info ) <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> @ checks_instance_lock <EOL> def prep_resize ( self , context , instance_id , instance_type_id ) : <EOL> """<STR_LIT>""" <EOL> context = context . elevated ( ) <EOL> instance_ref = self . db . instance_get_by_uuid ( context , instance_id ) <EOL> if instance_ref [ '<STR_LIT:host>' ] == FLAGS . host : <EOL> self . _instance_update ( context , <EOL> instance_id , <EOL> vm_state = vm_states . ERROR ) <EOL> msg = _ ( '<STR_LIT>' ) <EOL> raise exception . Error ( msg ) <EOL> old_instance_type = self . db . instance_type_get ( context , <EOL> instance_ref [ '<STR_LIT>' ] ) <EOL> new_instance_type = self . db . instance_type_get ( context , <EOL> instance_type_id ) <EOL> migration_ref = self . db . migration_create ( context , <EOL> { '<STR_LIT>' : instance_ref [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : instance_ref [ '<STR_LIT:host>' ] , <EOL> '<STR_LIT>' : FLAGS . host , <EOL> '<STR_LIT>' : self . driver . get_host_ip_addr ( ) , <EOL> '<STR_LIT>' : old_instance_type [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT>' : instance_type_id , <EOL> '<STR_LIT:status>' : '<STR_LIT>' } ) <EOL> LOG . audit ( _ ( '<STR_LIT>' ) , instance_ref [ '<STR_LIT>' ] , <EOL> context = context ) <EOL> topic = self . db . queue_get_for ( context , FLAGS . compute_topic , <EOL> instance_ref [ '<STR_LIT:host>' ] ) <EOL> rpc . cast ( context , topic , <EOL> { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:args>' : { '<STR_LIT>' : instance_ref [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : migration_ref [ '<STR_LIT:id>' ] } } ) <EOL> usage_info = utils . usage_from_instance ( instance_ref , <EOL> new_instance_type = new_instance_type [ '<STR_LIT:name>' ] , <EOL> new_instance_type_id = new_instance_type [ '<STR_LIT:id>' ] ) <EOL> notifier . notify ( '<STR_LIT>' % self . host , <EOL> '<STR_LIT>' , <EOL> notifier . INFO , <EOL> usage_info ) <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> @ checks_instance_lock <EOL> def resize_instance ( self , context , instance_id , migration_id ) : <EOL> """<STR_LIT>""" <EOL> migration_ref = self . db . migration_get ( context , migration_id ) <EOL> instance_ref = self . db . instance_get_by_uuid ( context , <EOL> migration_ref . instance_uuid ) <EOL> self . db . migration_update ( context , <EOL> migration_id , <EOL> { '<STR_LIT:status>' : '<STR_LIT>' } ) <EOL> disk_info = self . driver . migrate_disk_and_power_off ( <EOL> instance_ref , migration_ref [ '<STR_LIT>' ] ) <EOL> self . db . migration_update ( context , <EOL> migration_id , <EOL> { '<STR_LIT:status>' : '<STR_LIT>' } ) <EOL> service = self . db . service_get_by_host_and_topic ( <EOL> context , migration_ref [ '<STR_LIT>' ] , FLAGS . compute_topic ) <EOL> topic = self . db . queue_get_for ( context , <EOL> FLAGS . compute_topic , <EOL> migration_ref [ '<STR_LIT>' ] ) <EOL> params = { '<STR_LIT>' : migration_id , <EOL> '<STR_LIT>' : disk_info , <EOL> '<STR_LIT>' : instance_ref [ '<STR_LIT>' ] } <EOL> rpc . cast ( context , topic , { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:args>' : params } ) <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> @ checks_instance_lock <EOL> def finish_resize ( self , context , instance_id , migration_id , disk_info ) : <EOL> """<STR_LIT>""" <EOL> migration_ref = self . db . migration_get ( context , migration_id ) <EOL> resize_instance = False <EOL> instance_ref = self . db . instance_get_by_uuid ( context , <EOL> migration_ref . instance_uuid ) <EOL> if migration_ref [ '<STR_LIT>' ] != migration_ref [ '<STR_LIT>' ] : <EOL> instance_type = self . db . instance_type_get ( context , <EOL> migration_ref [ '<STR_LIT>' ] ) <EOL> self . db . instance_update ( context , instance_ref . uuid , <EOL> dict ( instance_type_id = instance_type [ '<STR_LIT:id>' ] , <EOL> memory_mb = instance_type [ '<STR_LIT>' ] , <EOL> vcpus = instance_type [ '<STR_LIT>' ] , <EOL> local_gb = instance_type [ '<STR_LIT>' ] ) ) <EOL> resize_instance = True <EOL> instance_ref = self . db . instance_get_by_uuid ( context , <EOL> instance_ref . uuid ) <EOL> network_info = self . _get_instance_nw_info ( context , instance_ref ) <EOL> self . driver . finish_migration ( context , instance_ref , disk_info , <EOL> network_info , resize_instance ) <EOL> self . _instance_update ( context , <EOL> instance_id , <EOL> vm_state = vm_states . ACTIVE , <EOL> task_state = task_states . RESIZE_VERIFY ) <EOL> self . db . migration_update ( context , migration_id , <EOL> { '<STR_LIT:status>' : '<STR_LIT>' , } ) <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> @ checks_instance_lock <EOL> def add_fixed_ip_to_instance ( self , context , instance_id , network_id ) : <EOL> """<STR_LIT>""" <EOL> self . network_api . add_fixed_ip_to_instance ( context , instance_id , <EOL> self . host , network_id ) <EOL> self . inject_network_info ( context , instance_id ) <EOL> self . reset_network ( context , instance_id ) <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> @ checks_instance_lock <EOL> def remove_fixed_ip_from_instance ( self , context , instance_id , address ) : <EOL> """<STR_LIT>""" <EOL> self . network_api . remove_fixed_ip_from_instance ( context , instance_id , <EOL> address ) <EOL> self . inject_network_info ( context , instance_id ) <EOL> self . reset_network ( context , instance_id ) <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> @ checks_instance_lock <EOL> def pause_instance ( self , context , instance_id ) : <EOL> """<STR_LIT>""" <EOL> LOG . audit ( _ ( '<STR_LIT>' ) , instance_id , context = context ) <EOL> context = context . elevated ( ) <EOL> instance_ref = self . db . instance_get ( context , instance_id ) <EOL> self . driver . pause ( instance_ref , lambda result : None ) <EOL> current_power_state = self . _get_power_state ( context , instance_ref ) <EOL> self . _instance_update ( context , <EOL> instance_id , <EOL> power_state = current_power_state , <EOL> vm_state = vm_states . PAUSED , <EOL> task_state = None ) <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> @ checks_instance_lock <EOL> def unpause_instance ( self , context , instance_id ) : <EOL> """<STR_LIT>""" <EOL> LOG . audit ( _ ( '<STR_LIT>' ) , instance_id , context = context ) <EOL> context = context . elevated ( ) <EOL> instance_ref = self . db . instance_get ( context , instance_id ) <EOL> self . driver . unpause ( instance_ref , lambda result : None ) <EOL> current_power_state = self . _get_power_state ( context , instance_ref ) <EOL> self . _instance_update ( context , <EOL> instance_id , <EOL> power_state = current_power_state , <EOL> vm_state = vm_states . ACTIVE , <EOL> task_state = None ) <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> def host_power_action ( self , context , host = None , action = None ) : <EOL> """<STR_LIT>""" <EOL> return self . driver . host_power_action ( host , action ) <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> def set_host_enabled ( self , context , host = None , enabled = None ) : <EOL> """<STR_LIT>""" <EOL> return self . driver . set_host_enabled ( host , enabled ) <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> def get_diagnostics ( self , context , instance_id ) : <EOL> """<STR_LIT>""" <EOL> instance_ref = self . db . instance_get ( context , instance_id ) <EOL> if instance_ref [ "<STR_LIT>" ] == power_state . RUNNING : <EOL> LOG . audit ( _ ( "<STR_LIT>" ) , instance_id , <EOL> context = context ) <EOL> return self . driver . get_diagnostics ( instance_ref ) <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> @ checks_instance_lock <EOL> def suspend_instance ( self , context , instance_id ) : <EOL> """<STR_LIT>""" <EOL> LOG . audit ( _ ( '<STR_LIT>' ) , instance_id , context = context ) <EOL> context = context . elevated ( ) <EOL> instance_ref = self . db . instance_get ( context , instance_id ) <EOL> self . driver . suspend ( instance_ref , lambda result : None ) <EOL> current_power_state = self . _get_power_state ( context , instance_ref ) <EOL> self . _instance_update ( context , <EOL> instance_id , <EOL> power_state = current_power_state , <EOL> vm_state = vm_states . SUSPENDED , <EOL> task_state = None ) <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> @ checks_instance_lock <EOL> def resume_instance ( self , context , instance_id ) : <EOL> """<STR_LIT>""" <EOL> LOG . audit ( _ ( '<STR_LIT>' ) , instance_id , context = context ) <EOL> context = context . elevated ( ) <EOL> instance_ref = self . db . instance_get ( context , instance_id ) <EOL> self . driver . resume ( instance_ref , lambda result : None ) <EOL> current_power_state = self . _get_power_state ( context , instance_ref ) <EOL> self . _instance_update ( context , <EOL> instance_id , <EOL> power_state = current_power_state , <EOL> vm_state = vm_states . ACTIVE , <EOL> task_state = None ) <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> def lock_instance ( self , context , instance_id ) : <EOL> """<STR_LIT>""" <EOL> context = context . elevated ( ) <EOL> LOG . debug ( _ ( '<STR_LIT>' ) , instance_id , context = context ) <EOL> self . db . instance_update ( context , instance_id , { '<STR_LIT>' : True } ) <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> def unlock_instance ( self , context , instance_id ) : <EOL> """<STR_LIT>""" <EOL> context = context . elevated ( ) <EOL> LOG . debug ( _ ( '<STR_LIT>' ) , instance_id , context = context ) <EOL> self . db . instance_update ( context , instance_id , { '<STR_LIT>' : False } ) <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> def get_lock ( self , context , instance_id ) : <EOL> """<STR_LIT>""" <EOL> context = context . elevated ( ) <EOL> LOG . debug ( _ ( '<STR_LIT>' ) , instance_id , <EOL> context = context ) <EOL> if utils . is_uuid_like ( instance_id ) : <EOL> uuid = instance_id <EOL> instance_ref = self . db . instance_get_by_uuid ( context , uuid ) <EOL> else : <EOL> instance_ref = self . db . instance_get ( context , instance_id ) <EOL> return instance_ref [ '<STR_LIT>' ] <EOL> @ checks_instance_lock <EOL> def reset_network ( self , context , instance_id ) : <EOL> """<STR_LIT>""" <EOL> instance = self . db . instance_get ( context , instance_id ) <EOL> LOG . debug ( _ ( '<STR_LIT>' ) , instance_id , <EOL> context = context ) <EOL> self . driver . reset_network ( instance ) <EOL> @ checks_instance_lock <EOL> def inject_network_info ( self , context , instance_id ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( _ ( '<STR_LIT>' ) , instance_id , <EOL> context = context ) <EOL> instance = self . db . instance_get ( context , instance_id ) <EOL> network_info = self . _get_instance_nw_info ( context , instance ) <EOL> LOG . debug ( _ ( "<STR_LIT>" ) , network_info ) <EOL> self . driver . inject_network_info ( instance , network_info ) <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> def get_console_output ( self , context , instance_id ) : <EOL> """<STR_LIT>""" <EOL> context = context . elevated ( ) <EOL> instance_ref = self . db . instance_get ( context , instance_id ) <EOL> LOG . audit ( _ ( "<STR_LIT>" ) , instance_id , <EOL> context = context ) <EOL> output = self . driver . get_console_output ( instance_ref ) <EOL> return output . decode ( '<STR_LIT:utf-8>' , '<STR_LIT:replace>' ) . encode ( '<STR_LIT:ascii>' , '<STR_LIT:replace>' ) <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> def get_ajax_console ( self , context , instance_id ) : <EOL> """<STR_LIT>""" <EOL> context = context . elevated ( ) <EOL> LOG . debug ( _ ( "<STR_LIT>" ) , instance_id ) <EOL> instance_ref = self . db . instance_get ( context , instance_id ) <EOL> return self . driver . get_ajax_console ( instance_ref ) <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> def get_vnc_console ( self , context , instance_id ) : <EOL> """<STR_LIT>""" <EOL> context = context . elevated ( ) <EOL> LOG . debug ( _ ( "<STR_LIT>" ) , instance_id ) <EOL> instance_ref = self . db . instance_get ( context , instance_id ) <EOL> return self . driver . get_vnc_console ( instance_ref ) <EOL> def _attach_volume_boot ( self , context , instance_id , volume_id , mountpoint ) : <EOL> """<STR_LIT>""" <EOL> volume . API ( ) . check_attach ( context , volume_id ) <EOL> context = context . elevated ( ) <EOL> LOG . audit ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % <EOL> locals ( ) , context = context ) <EOL> dev_path = self . volume_manager . setup_compute_volume ( context , volume_id ) <EOL> self . db . volume_attached ( context , volume_id , instance_id , mountpoint ) <EOL> return dev_path <EOL> @ checks_instance_lock <EOL> def attach_volume ( self , context , instance_id , volume_id , mountpoint ) : <EOL> """<STR_LIT>""" <EOL> context = context . elevated ( ) <EOL> instance_ref = self . db . instance_get ( context , instance_id ) <EOL> LOG . audit ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % locals ( ) , context = context ) <EOL> dev_path = self . volume_manager . setup_compute_volume ( context , <EOL> volume_id ) <EOL> try : <EOL> self . driver . attach_volume ( instance_ref [ '<STR_LIT:name>' ] , <EOL> dev_path , <EOL> mountpoint ) <EOL> self . db . volume_attached ( context , <EOL> volume_id , <EOL> instance_id , <EOL> mountpoint ) <EOL> values = { <EOL> '<STR_LIT>' : instance_id , <EOL> '<STR_LIT>' : mountpoint , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : volume_id , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : None } <EOL> self . db . block_device_mapping_create ( context , values ) <EOL> except Exception as exc : <EOL> LOG . exception ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % locals ( ) , context = context ) <EOL> self . volume_manager . remove_compute_volume ( context , <EOL> volume_id ) <EOL> raise exc <EOL> return True <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> @ checks_instance_lock <EOL> def _detach_volume ( self , context , instance_id , volume_id , destroy_bdm ) : <EOL> """<STR_LIT>""" <EOL> context = context . elevated ( ) <EOL> instance_ref = self . db . instance_get ( context , instance_id ) <EOL> volume_ref = self . db . volume_get ( context , volume_id ) <EOL> mp = volume_ref [ '<STR_LIT>' ] <EOL> LOG . audit ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % locals ( ) , context = context ) <EOL> if instance_ref [ '<STR_LIT:name>' ] not in self . driver . list_instances ( ) : <EOL> LOG . warn ( _ ( "<STR_LIT>" ) , <EOL> instance_id , context = context ) <EOL> else : <EOL> self . driver . detach_volume ( instance_ref [ '<STR_LIT:name>' ] , <EOL> volume_ref [ '<STR_LIT>' ] ) <EOL> self . volume_manager . remove_compute_volume ( context , volume_id ) <EOL> self . db . volume_detached ( context , volume_id ) <EOL> if destroy_bdm : <EOL> self . db . block_device_mapping_destroy_by_instance_and_volume ( <EOL> context , instance_id , volume_id ) <EOL> return True <EOL> def detach_volume ( self , context , instance_id , volume_id ) : <EOL> """<STR_LIT>""" <EOL> return self . _detach_volume ( context , instance_id , volume_id , True ) <EOL> def remove_volume ( self , context , volume_id ) : <EOL> """<STR_LIT>""" <EOL> self . volume_manager . remove_compute_volume ( context , volume_id ) <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> def compare_cpu ( self , context , cpu_info ) : <EOL> """<STR_LIT>""" <EOL> return self . driver . compare_cpu ( cpu_info ) <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> def create_shared_storage_test_file ( self , context ) : <EOL> """<STR_LIT>""" <EOL> dirpath = FLAGS . instances_path <EOL> fd , tmp_file = tempfile . mkstemp ( dir = dirpath ) <EOL> LOG . debug ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) % tmp_file ) <EOL> os . close ( fd ) <EOL> return os . path . basename ( tmp_file ) <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> def check_shared_storage_test_file ( self , context , filename ) : <EOL> """<STR_LIT>""" <EOL> tmp_file = os . path . join ( FLAGS . instances_path , filename ) <EOL> if not os . path . exists ( tmp_file ) : <EOL> return False <EOL> else : <EOL> return True <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> def cleanup_shared_storage_test_file ( self , context , filename ) : <EOL> """<STR_LIT>""" <EOL> tmp_file = os . path . join ( FLAGS . instances_path , filename ) <EOL> os . remove ( tmp_file ) <EOL> @ exception . wrap_exception ( notifier = notifier , publisher_id = publisher_id ( ) ) <EOL> def update_available_resource ( self , context ) : <EOL> """<STR_LIT>""" <EOL> return self . driver . update_available_resource ( context , self . host ) <EOL> def pre_live_migration ( self , context , instance_id , time = None , <EOL> block_migration = False , disk = None ) : <EOL> """<STR_LIT>""" <EOL> if not time : <EOL> time = greenthread <EOL> instance_ref = self . db . instance_get ( context , instance_id ) <EOL> hostname = instance_ref [ '<STR_LIT>' ] <EOL> fixed_ips = self . db . instance_get_fixed_addresses ( context , instance_id ) <EOL> if not fixed_ips : <EOL> raise exception . FixedIpNotFoundForInstance ( instance_id = instance_id ) <EOL> if not instance_ref [ '<STR_LIT>' ] : <EOL> LOG . info ( _ ( "<STR_LIT>" ) , hostname ) <EOL> else : <EOL> for v in instance_ref [ '<STR_LIT>' ] : <EOL> self . volume_manager . setup_compute_volume ( context , v [ '<STR_LIT:id>' ] ) <EOL> network_info = self . _get_instance_nw_info ( context , instance_ref ) <EOL> max_retry = FLAGS . live_migration_retry_count <EOL> for cnt in range ( max_retry ) : <EOL> try : <EOL> self . driver . plug_vifs ( instance_ref , network_info ) <EOL> break <EOL> except exception . ProcessExecutionError : <EOL> if cnt == max_retry - <NUM_LIT:1> : <EOL> raise <EOL> else : <EOL> LOG . warn ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> % locals ( ) ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> self . driver . ensure_filtering_rules_for_instance ( instance_ref , <EOL> network_info ) <EOL> if block_migration : <EOL> self . driver . pre_block_migration ( context , <EOL> instance_ref , <EOL> disk ) <EOL> def live_migration ( self , context , instance_id , <EOL> dest , block_migration = False ) : <EOL> """<STR_LIT>""" <EOL> instance_ref = self . db . instance_get ( context , instance_id ) <EOL> try : <EOL> if instance_ref [ '<STR_LIT>' ] : <EOL> rpc . call ( context , <EOL> FLAGS . volume_topic , <EOL> { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:args>" : { '<STR_LIT>' : instance_id } } ) <EOL> if block_migration : <EOL> disk = self . driver . get_instance_disk_info ( context , <EOL> instance_ref ) <EOL> else : <EOL> disk = None <EOL> rpc . call ( context , <EOL> self . db . queue_get_for ( context , FLAGS . compute_topic , dest ) , <EOL> { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:args>" : { '<STR_LIT>' : instance_id , <EOL> '<STR_LIT>' : block_migration , <EOL> '<STR_LIT>' : disk } } ) <EOL> except Exception : <EOL> i_name = instance_ref . name <EOL> msg = _ ( "<STR_LIT>" ) <EOL> LOG . error ( msg % locals ( ) ) <EOL> self . rollback_live_migration ( context , instance_ref , <EOL> dest , block_migration ) <EOL> raise <EOL> self . driver . live_migration ( context , instance_ref , dest , <EOL> self . post_live_migration , <EOL> self . rollback_live_migration , <EOL> block_migration ) <EOL> def post_live_migration ( self , ctxt , instance_ref , <EOL> dest , block_migration = False ) : <EOL> """<STR_LIT>""" <EOL> LOG . info ( _ ( '<STR_LIT>' ) ) <EOL> instance_id = instance_ref [ '<STR_LIT:id>' ] <EOL> try : <EOL> for vol in self . db . volume_get_all_by_instance ( ctxt , instance_id ) : <EOL> self . volume_manager . remove_compute_volume ( ctxt , vol [ '<STR_LIT:id>' ] ) <EOL> except exception . NotFound : <EOL> pass <EOL> network_info = self . _get_instance_nw_info ( ctxt , instance_ref ) <EOL> self . driver . unfilter_instance ( instance_ref , network_info ) <EOL> i_name = instance_ref . name <EOL> try : <EOL> floating_ip = self . db . instance_get_floating_address ( ctxt , <EOL> instance_id ) <EOL> if not floating_ip : <EOL> LOG . info ( _ ( '<STR_LIT>' ) , i_name ) <EOL> else : <EOL> floating_ip_ref = self . db . floating_ip_get_by_address ( ctxt , <EOL> floating_ip ) <EOL> self . db . floating_ip_update ( ctxt , <EOL> floating_ip_ref [ '<STR_LIT:address>' ] , <EOL> { '<STR_LIT:host>' : dest } ) <EOL> except exception . NotFound : <EOL> LOG . info ( _ ( '<STR_LIT>' ) , i_name ) <EOL> except Exception , e : <EOL> LOG . error ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) % ( locals ( ) ) ) <EOL> rpc . call ( ctxt , <EOL> self . db . queue_get_for ( ctxt , FLAGS . compute_topic , dest ) , <EOL> { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:args>" : { '<STR_LIT>' : instance_ref . id , <EOL> '<STR_LIT>' : block_migration } } ) <EOL> current_power_state = self . _get_power_state ( ctxt , instance_ref ) <EOL> self . _instance_update ( ctxt , <EOL> instance_ref [ "<STR_LIT:id>" ] , <EOL> host = dest , <EOL> power_state = current_power_state , <EOL> vm_state = vm_states . ACTIVE , <EOL> task_state = None ) <EOL> for volume_ref in instance_ref [ '<STR_LIT>' ] : <EOL> volume_id = volume_ref [ '<STR_LIT:id>' ] <EOL> self . db . volume_update ( ctxt , volume_id , { '<STR_LIT:status>' : '<STR_LIT>' } ) <EOL> if block_migration : <EOL> self . driver . destroy ( instance_ref , network_info ) <EOL> LOG . info ( _ ( '<STR_LIT>' ) <EOL> % locals ( ) ) <EOL> LOG . info ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> def post_live_migration_at_destination ( self , context , <EOL> instance_id , block_migration = False ) : <EOL> """<STR_LIT>""" <EOL> instance_ref = self . db . instance_get ( context , instance_id ) <EOL> LOG . info ( _ ( '<STR_LIT>' ) <EOL> % instance_ref . name ) <EOL> network_info = self . _get_instance_nw_info ( context , instance_ref ) <EOL> self . driver . post_live_migration_at_destination ( context , <EOL> instance_ref , <EOL> network_info , <EOL> block_migration ) <EOL> def rollback_live_migration ( self , context , instance_ref , <EOL> dest , block_migration ) : <EOL> """<STR_LIT>""" <EOL> host = instance_ref [ '<STR_LIT:host>' ] <EOL> self . _instance_update ( context , <EOL> instance_ref [ '<STR_LIT:id>' ] , <EOL> host = host , <EOL> vm_state = vm_states . ACTIVE , <EOL> task_state = None ) <EOL> for volume_ref in instance_ref [ '<STR_LIT>' ] : <EOL> volume_id = volume_ref [ '<STR_LIT:id>' ] <EOL> self . db . volume_update ( context , volume_id , { '<STR_LIT:status>' : '<STR_LIT>' } ) <EOL> volume . API ( ) . remove_from_compute ( context , volume_id , dest ) <EOL> if block_migration : <EOL> rpc . cast ( context , <EOL> self . db . queue_get_for ( context , FLAGS . compute_topic , dest ) , <EOL> { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:args>" : { '<STR_LIT>' : instance_ref [ '<STR_LIT:id>' ] } } ) <EOL> def rollback_live_migration_at_destination ( self , context , instance_id ) : <EOL> """<STR_LIT>""" <EOL> instances_ref = self . db . instance_get ( context , instance_id ) <EOL> network_info = self . _get_instance_nw_info ( context , instances_ref ) <EOL> self . driver . destroy ( instances_ref , network_info ) <EOL> def periodic_tasks ( self , context = None ) : <EOL> """<STR_LIT>""" <EOL> error_list = super ( ComputeManager , self ) . periodic_tasks ( context ) <EOL> if error_list is None : <EOL> error_list = [ ] <EOL> try : <EOL> if FLAGS . rescue_timeout > <NUM_LIT:0> : <EOL> self . driver . poll_rescued_instances ( FLAGS . rescue_timeout ) <EOL> except Exception as ex : <EOL> LOG . warning ( _ ( "<STR_LIT>" ) , <EOL> unicode ( ex ) ) <EOL> error_list . append ( ex ) <EOL> try : <EOL> self . _report_driver_status ( ) <EOL> except Exception as ex : <EOL> LOG . warning ( _ ( "<STR_LIT>" ) , <EOL> unicode ( ex ) ) <EOL> error_list . append ( ex ) <EOL> try : <EOL> self . _sync_power_states ( context ) <EOL> except Exception as ex : <EOL> LOG . warning ( _ ( "<STR_LIT>" ) , unicode ( ex ) ) <EOL> error_list . append ( ex ) <EOL> return error_list <EOL> def _report_driver_status ( self ) : <EOL> curr_time = time . time ( ) <EOL> if curr_time - self . _last_host_check > FLAGS . host_state_interval : <EOL> self . _last_host_check = curr_time <EOL> LOG . info ( _ ( "<STR_LIT>" ) ) <EOL> self . update_service_capabilities ( <EOL> self . driver . get_host_stats ( refresh = True ) ) <EOL> def _sync_power_states ( self , context ) : <EOL> """<STR_LIT>""" <EOL> vm_instances = self . driver . list_instances_detail ( context ) <EOL> vm_instances = dict ( ( vm . name , vm ) for vm in vm_instances ) <EOL> db_instances = self . db . instance_get_all_by_host ( context , self . host ) <EOL> num_vm_instances = len ( vm_instances ) <EOL> num_db_instances = len ( db_instances ) <EOL> if num_vm_instances != num_db_instances : <EOL> LOG . info ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % locals ( ) ) <EOL> for db_instance in db_instances : <EOL> name = db_instance [ "<STR_LIT:name>" ] <EOL> db_power_state = db_instance [ '<STR_LIT>' ] <EOL> vm_instance = vm_instances . get ( name ) <EOL> if vm_instance is None : <EOL> vm_power_state = power_state . NOSTATE <EOL> else : <EOL> vm_power_state = vm_instance . state <EOL> if vm_power_state == db_power_state : <EOL> continue <EOL> self . _instance_update ( context , <EOL> db_instance [ "<STR_LIT:id>" ] , <EOL> power_state = vm_power_state ) </s>
