<s> """<STR_LIT>""" <EOL> import base64 <EOL> import functools <EOL> import time <EOL> import zlib <EOL> import eventlet <EOL> from eventlet import greenthread <EOL> import netaddr <EOL> from oslo_log import log as logging <EOL> from oslo_serialization import jsonutils <EOL> from oslo_utils import excutils <EOL> from oslo_utils import importutils <EOL> from oslo_utils import netutils <EOL> from oslo_utils import strutils <EOL> from oslo_utils import timeutils <EOL> from oslo_utils import units <EOL> import six <EOL> from nova import block_device <EOL> from nova import compute <EOL> from nova . compute import power_state <EOL> from nova . compute import task_states <EOL> from nova . compute import vm_mode <EOL> from nova . compute import vm_states <EOL> import nova . conf <EOL> from nova . console import type as ctype <EOL> from nova import context as nova_context <EOL> from nova import exception <EOL> from nova . i18n import _ , _LE , _LI , _LW <EOL> from nova import objects <EOL> from nova . objects import migrate_data as migrate_data_obj <EOL> from nova . pci import manager as pci_manager <EOL> from nova import utils <EOL> from nova . virt import configdrive <EOL> from nova . virt import driver as virt_driver <EOL> from nova . virt import firewall <EOL> from nova . virt . xenapi import agent as xapi_agent <EOL> from nova . virt . xenapi import pool_states <EOL> from nova . virt . xenapi import vm_utils <EOL> from nova . virt . xenapi import volume_utils <EOL> from nova . virt . xenapi import volumeops <EOL> LOG = logging . getLogger ( __name__ ) <EOL> CONF = nova . conf . CONF <EOL> CONF . import_opt ( '<STR_LIT:host>' , '<STR_LIT>' ) <EOL> DEFAULT_FIREWALL_DRIVER = "<STR_LIT>" % ( <EOL> firewall . __name__ , <EOL> firewall . IptablesFirewallDriver . __name__ ) <EOL> RESIZE_TOTAL_STEPS = <NUM_LIT:5> <EOL> DEVICE_ROOT = '<STR_LIT:0>' <EOL> DEVICE_RESCUE = '<STR_LIT:1>' <EOL> DEVICE_SWAP = '<STR_LIT:2>' <EOL> DEVICE_CONFIGDRIVE = '<STR_LIT:3>' <EOL> DEVICE_EPHEMERAL = '<STR_LIT:4>' <EOL> DEVICE_CD = '<STR_LIT:1>' <EOL> def make_step_decorator ( context , instance , update_instance_progress , <EOL> total_offset = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> step_info = dict ( total = total_offset , current = <NUM_LIT:0> ) <EOL> def bump_progress ( ) : <EOL> step_info [ '<STR_LIT>' ] += <NUM_LIT:1> <EOL> update_instance_progress ( context , instance , <EOL> step_info [ '<STR_LIT>' ] , step_info [ '<STR_LIT>' ] ) <EOL> def step_decorator ( f ) : <EOL> step_info [ '<STR_LIT>' ] += <NUM_LIT:1> <EOL> @ functools . wraps ( f ) <EOL> def inner ( * args , ** kwargs ) : <EOL> rv = f ( * args , ** kwargs ) <EOL> bump_progress ( ) <EOL> return rv <EOL> return inner <EOL> return step_decorator <EOL> class VMOps ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , session , virtapi ) : <EOL> self . compute_api = compute . API ( ) <EOL> self . _session = session <EOL> self . _virtapi = virtapi <EOL> self . _volumeops = volumeops . VolumeOps ( self . _session ) <EOL> self . firewall_driver = firewall . load_driver ( <EOL> DEFAULT_FIREWALL_DRIVER , <EOL> xenapi_session = self . _session ) <EOL> vif_impl = importutils . import_class ( CONF . xenserver . vif_driver ) <EOL> self . vif_driver = vif_impl ( xenapi_session = self . _session ) <EOL> self . default_root_dev = '<STR_LIT>' <EOL> LOG . debug ( "<STR_LIT>" , <EOL> CONF . xenserver . image_upload_handler ) <EOL> self . image_upload_handler = importutils . import_object ( <EOL> CONF . xenserver . image_upload_handler ) <EOL> def agent_enabled ( self , instance ) : <EOL> if CONF . xenserver . disable_agent : <EOL> return False <EOL> return xapi_agent . should_use_agent ( instance ) <EOL> def _get_agent ( self , instance , vm_ref ) : <EOL> if self . agent_enabled ( instance ) : <EOL> return xapi_agent . XenAPIBasedAgent ( self . _session , self . _virtapi , <EOL> instance , vm_ref ) <EOL> raise exception . NovaException ( _ ( "<STR_LIT>" ) ) <EOL> def instance_exists ( self , name_label ) : <EOL> return vm_utils . lookup ( self . _session , name_label ) is not None <EOL> def list_instances ( self ) : <EOL> """<STR_LIT>""" <EOL> name_labels = [ ] <EOL> for vm_ref , vm_rec in vm_utils . list_vms ( self . _session ) : <EOL> name_labels . append ( vm_rec [ "<STR_LIT>" ] ) <EOL> return name_labels <EOL> def list_instance_uuids ( self ) : <EOL> """<STR_LIT>""" <EOL> nova_uuids = [ ] <EOL> for vm_ref , vm_rec in vm_utils . list_vms ( self . _session ) : <EOL> other_config = vm_rec [ '<STR_LIT>' ] <EOL> nova_uuid = other_config . get ( '<STR_LIT>' ) <EOL> if nova_uuid : <EOL> nova_uuids . append ( nova_uuid ) <EOL> return nova_uuids <EOL> def confirm_migration ( self , migration , instance , network_info ) : <EOL> self . _destroy_orig_vm ( instance , network_info ) <EOL> def _destroy_orig_vm ( self , instance , network_info ) : <EOL> name_label = self . _get_orig_vm_name_label ( instance ) <EOL> vm_ref = vm_utils . lookup ( self . _session , name_label ) <EOL> return self . _destroy ( instance , vm_ref , network_info = network_info ) <EOL> def _attach_mapped_block_devices ( self , instance , block_device_info ) : <EOL> block_device_mapping = virt_driver . block_device_info_get_mapping ( <EOL> block_device_info ) <EOL> for vol in block_device_mapping : <EOL> if vol [ '<STR_LIT>' ] == instance [ '<STR_LIT>' ] : <EOL> continue <EOL> connection_info = vol [ '<STR_LIT>' ] <EOL> mount_device = vol [ '<STR_LIT>' ] . rpartition ( "<STR_LIT:/>" ) [ <NUM_LIT:2> ] <EOL> self . _volumeops . attach_volume ( connection_info , <EOL> instance [ '<STR_LIT:name>' ] , <EOL> mount_device , <EOL> hotplug = False ) <EOL> def finish_revert_migration ( self , context , instance , <EOL> block_device_info = None , <EOL> power_on = True ) : <EOL> self . _restore_orig_vm_and_cleanup_orphan ( instance , block_device_info , <EOL> power_on ) <EOL> def _restore_orig_vm_and_cleanup_orphan ( self , instance , <EOL> block_device_info = None , <EOL> power_on = True ) : <EOL> name_label = self . _get_orig_vm_name_label ( instance ) <EOL> vm_ref = vm_utils . lookup ( self . _session , name_label ) <EOL> new_ref = vm_utils . lookup ( self . _session , instance [ '<STR_LIT:name>' ] ) <EOL> if vm_ref is not None : <EOL> if new_ref is not None : <EOL> self . _destroy ( instance , new_ref ) <EOL> name_label = instance [ '<STR_LIT:name>' ] <EOL> vm_utils . set_vm_name_label ( self . _session , vm_ref , name_label ) <EOL> self . _attach_mapped_block_devices ( instance , block_device_info ) <EOL> elif new_ref is not None : <EOL> vm_ref = new_ref <EOL> if power_on and vm_utils . is_vm_shutdown ( self . _session , vm_ref ) : <EOL> self . _start ( instance , vm_ref ) <EOL> def finish_migration ( self , context , migration , instance , disk_info , <EOL> network_info , image_meta , resize_instance , <EOL> block_device_info = None , power_on = True ) : <EOL> def null_step_decorator ( f ) : <EOL> return f <EOL> def create_disks_step ( undo_mgr , disk_image_type , image_meta , <EOL> name_label ) : <EOL> import_root = True <EOL> root_vol_vdi = None <EOL> if block_device_info : <EOL> LOG . debug ( "<STR_LIT>" , <EOL> block_device_info , instance = instance ) <EOL> for bdm in block_device_info [ '<STR_LIT>' ] : <EOL> if bdm [ '<STR_LIT>' ] == instance [ '<STR_LIT>' ] : <EOL> connection_info = bdm [ '<STR_LIT>' ] <EOL> _sr , root_vol_vdi = self . _volumeops . connect_volume ( <EOL> connection_info ) <EOL> import_root = False <EOL> break <EOL> vdis = vm_utils . import_all_migrated_disks ( self . _session , instance , <EOL> import_root = import_root ) <EOL> if root_vol_vdi : <EOL> vol_vdi_ref = self . _session . call_xenapi ( '<STR_LIT>' , <EOL> root_vol_vdi ) <EOL> vdis [ '<STR_LIT:root>' ] = dict ( uuid = root_vol_vdi , file = None , <EOL> ref = vol_vdi_ref , osvol = True ) <EOL> def undo_create_disks ( ) : <EOL> eph_vdis = vdis [ '<STR_LIT>' ] <EOL> root_vdi = vdis [ '<STR_LIT:root>' ] <EOL> vdi_refs = [ vdi [ '<STR_LIT>' ] for vdi in eph_vdis . values ( ) ] <EOL> if not root_vdi . get ( '<STR_LIT>' , False ) : <EOL> vdi_refs . append ( root_vdi [ '<STR_LIT>' ] ) <EOL> else : <EOL> self . _volumeops . safe_cleanup_from_vdis ( root_vdi [ '<STR_LIT>' ] ) <EOL> vm_utils . safe_destroy_vdis ( self . _session , vdi_refs ) <EOL> undo_mgr . undo_with ( undo_create_disks ) <EOL> return vdis <EOL> def completed_callback ( ) : <EOL> self . _update_instance_progress ( context , instance , <EOL> step = <NUM_LIT:5> , <EOL> total_steps = RESIZE_TOTAL_STEPS ) <EOL> self . _spawn ( context , instance , image_meta , null_step_decorator , <EOL> create_disks_step , first_boot = False , injected_files = None , <EOL> admin_password = None , network_info = network_info , <EOL> block_device_info = block_device_info , name_label = None , <EOL> rescue = False , power_on = power_on , resize = resize_instance , <EOL> completed_callback = completed_callback ) <EOL> def _start ( self , instance , vm_ref = None , bad_volumes_callback = None , <EOL> start_pause = False ) : <EOL> """<STR_LIT>""" <EOL> vm_ref = vm_ref or self . _get_vm_opaque_ref ( instance ) <EOL> LOG . debug ( "<STR_LIT>" , instance = instance ) <EOL> if bad_volumes_callback : <EOL> bad_devices = self . _volumeops . find_bad_volumes ( vm_ref ) <EOL> for device_name in bad_devices : <EOL> self . _volumeops . detach_volume ( <EOL> None , instance [ '<STR_LIT:name>' ] , device_name ) <EOL> self . _session . call_xenapi ( '<STR_LIT>' , vm_ref , <EOL> self . _session . host_ref , <EOL> start_pause , False ) <EOL> if bad_volumes_callback and bad_devices : <EOL> bad_volumes_callback ( bad_devices ) <EOL> def _get_vdis_for_instance ( self , context , instance , name_label , <EOL> image_meta , image_type , block_device_info ) : <EOL> """<STR_LIT>""" <EOL> vdis = self . _connect_cinder_volumes ( instance , block_device_info ) <EOL> if '<STR_LIT:root>' not in vdis : <EOL> create_image_vdis = vm_utils . create_image ( context , self . _session , <EOL> instance , name_label , image_meta . id , image_type ) <EOL> vdis . update ( create_image_vdis ) <EOL> for vdi in six . itervalues ( vdis ) : <EOL> vdi [ '<STR_LIT>' ] = self . _session . call_xenapi ( '<STR_LIT>' , <EOL> vdi [ '<STR_LIT>' ] ) <EOL> return vdis <EOL> def _connect_cinder_volumes ( self , instance , block_device_info ) : <EOL> """<STR_LIT>""" <EOL> vdis = { } <EOL> if block_device_info : <EOL> msg = "<STR_LIT>" % block_device_info <EOL> LOG . debug ( strutils . mask_password ( msg ) , instance = instance ) <EOL> root_device_name = block_device_info [ '<STR_LIT>' ] <EOL> for bdm in block_device_info [ '<STR_LIT>' ] : <EOL> if ( block_device . strip_prefix ( bdm [ '<STR_LIT>' ] ) == <EOL> block_device . strip_prefix ( root_device_name ) ) : <EOL> type_ = '<STR_LIT:root>' <EOL> else : <EOL> type_ = bdm [ '<STR_LIT>' ] <EOL> conn_info = bdm [ '<STR_LIT>' ] <EOL> _sr , vdi_uuid = self . _volumeops . connect_volume ( conn_info ) <EOL> if vdi_uuid : <EOL> vdis [ type_ ] = dict ( uuid = vdi_uuid , file = None , osvol = True ) <EOL> return vdis <EOL> def _update_last_dom_id ( self , vm_ref ) : <EOL> other_config = self . _session . VM . get_other_config ( vm_ref ) <EOL> other_config [ '<STR_LIT>' ] = self . _session . VM . get_domid ( vm_ref ) <EOL> self . _session . VM . set_other_config ( vm_ref , other_config ) <EOL> def spawn ( self , context , instance , image_meta , injected_files , <EOL> admin_password , network_info = None , block_device_info = None , <EOL> name_label = None , rescue = False ) : <EOL> if block_device_info : <EOL> LOG . debug ( "<STR_LIT>" , <EOL> block_device_info , instance = instance ) <EOL> if block_device_info and not block_device_info [ '<STR_LIT>' ] : <EOL> block_device_info [ '<STR_LIT>' ] = self . default_root_dev <EOL> step = make_step_decorator ( context , instance , <EOL> self . _update_instance_progress ) <EOL> @ step <EOL> def create_disks_step ( undo_mgr , disk_image_type , image_meta , <EOL> name_label ) : <EOL> vdis = self . _get_vdis_for_instance ( context , instance , name_label , <EOL> image_meta , disk_image_type , <EOL> block_device_info ) <EOL> def undo_create_disks ( ) : <EOL> vdi_refs = [ vdi [ '<STR_LIT>' ] for vdi in vdis . values ( ) <EOL> if not vdi . get ( '<STR_LIT>' ) ] <EOL> vm_utils . safe_destroy_vdis ( self . _session , vdi_refs ) <EOL> vol_vdi_refs = [ vdi [ '<STR_LIT>' ] for vdi in vdis . values ( ) <EOL> if vdi . get ( '<STR_LIT>' ) ] <EOL> self . _volumeops . safe_cleanup_from_vdis ( vol_vdi_refs ) <EOL> undo_mgr . undo_with ( undo_create_disks ) <EOL> return vdis <EOL> self . _spawn ( context , instance , image_meta , step , create_disks_step , <EOL> True , injected_files , admin_password , <EOL> network_info , block_device_info , name_label , rescue ) <EOL> def _spawn ( self , context , instance , image_meta , step , create_disks_step , <EOL> first_boot , injected_files = None , admin_password = None , <EOL> network_info = None , block_device_info = None , <EOL> name_label = None , rescue = False , power_on = True , resize = True , <EOL> completed_callback = None ) : <EOL> if name_label is None : <EOL> name_label = instance [ '<STR_LIT:name>' ] <EOL> self . _ensure_instance_name_unique ( name_label ) <EOL> self . _ensure_enough_free_mem ( instance ) <EOL> def attach_disks ( undo_mgr , vm_ref , vdis , disk_image_type ) : <EOL> if image_meta . properties . get ( '<STR_LIT>' , False ) : <EOL> if '<STR_LIT>' in vdis : <EOL> vm_utils . handle_ipxe_iso ( <EOL> self . _session , instance , vdis [ '<STR_LIT>' ] , network_info ) <EOL> else : <EOL> LOG . warning ( _LW ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , instance = instance ) <EOL> if resize : <EOL> self . _resize_up_vdis ( instance , vdis ) <EOL> self . _attach_disks ( instance , image_meta , vm_ref , name_label , vdis , <EOL> disk_image_type , network_info , rescue , <EOL> admin_password , injected_files ) <EOL> if not first_boot : <EOL> self . _attach_mapped_block_devices ( instance , <EOL> block_device_info ) <EOL> def attach_pci_devices ( undo_mgr , vm_ref ) : <EOL> dev_to_passthrough = "<STR_LIT>" <EOL> devices = pci_manager . get_instance_pci_devs ( instance ) <EOL> for d in devices : <EOL> pci_address = d [ "<STR_LIT:address>" ] <EOL> if pci_address . count ( "<STR_LIT::>" ) == <NUM_LIT:1> : <EOL> pci_address = "<STR_LIT>" + pci_address <EOL> dev_to_passthrough += "<STR_LIT>" + pci_address <EOL> if dev_to_passthrough : <EOL> vm_utils . set_other_config_pci ( self . _session , <EOL> vm_ref , <EOL> dev_to_passthrough [ <NUM_LIT:1> : ] ) <EOL> @ step <EOL> def determine_disk_image_type_step ( undo_mgr ) : <EOL> return vm_utils . determine_disk_image_type ( image_meta ) <EOL> @ step <EOL> def create_kernel_ramdisk_step ( undo_mgr ) : <EOL> kernel_file , ramdisk_file = vm_utils . create_kernel_and_ramdisk ( <EOL> context , self . _session , instance , name_label ) <EOL> def undo_create_kernel_ramdisk ( ) : <EOL> vm_utils . destroy_kernel_ramdisk ( self . _session , instance , <EOL> kernel_file , ramdisk_file ) <EOL> undo_mgr . undo_with ( undo_create_kernel_ramdisk ) <EOL> return kernel_file , ramdisk_file <EOL> @ step <EOL> def create_vm_record_step ( undo_mgr , disk_image_type , <EOL> kernel_file , ramdisk_file ) : <EOL> vm_ref = self . _create_vm_record ( context , instance , name_label , <EOL> disk_image_type , kernel_file , <EOL> ramdisk_file , image_meta , rescue ) <EOL> def undo_create_vm ( ) : <EOL> self . _destroy ( instance , vm_ref , network_info = network_info ) <EOL> undo_mgr . undo_with ( undo_create_vm ) <EOL> return vm_ref <EOL> @ step <EOL> def attach_devices_step ( undo_mgr , vm_ref , vdis , disk_image_type ) : <EOL> attach_disks ( undo_mgr , vm_ref , vdis , disk_image_type ) <EOL> attach_pci_devices ( undo_mgr , vm_ref ) <EOL> if rescue : <EOL> @ step <EOL> def attach_orig_disks_step ( undo_mgr , vm_ref ) : <EOL> vbd_refs = self . _attach_orig_disks ( instance , vm_ref ) <EOL> def undo_attach_orig_disks ( ) : <EOL> for vbd_ref in vbd_refs : <EOL> vm_utils . destroy_vbd ( self . _session , vbd_ref ) <EOL> undo_mgr . undo_with ( undo_attach_orig_disks ) <EOL> @ step <EOL> def inject_instance_data_step ( undo_mgr , vm_ref , vdis ) : <EOL> self . _inject_instance_metadata ( instance , vm_ref ) <EOL> self . _inject_auto_disk_config ( instance , vm_ref ) <EOL> if first_boot : <EOL> self . _inject_hostname ( instance , vm_ref , rescue ) <EOL> self . _file_inject_vm_settings ( instance , vm_ref , vdis , network_info ) <EOL> self . inject_network_info ( instance , network_info , vm_ref ) <EOL> @ step <EOL> def setup_network_step ( undo_mgr , vm_ref ) : <EOL> self . _create_vifs ( instance , vm_ref , network_info ) <EOL> self . _prepare_instance_filter ( instance , network_info ) <EOL> @ step <EOL> def start_paused_step ( undo_mgr , vm_ref ) : <EOL> if power_on : <EOL> self . _start ( instance , vm_ref , start_pause = True ) <EOL> @ step <EOL> def boot_and_configure_instance_step ( undo_mgr , vm_ref ) : <EOL> self . _unpause_and_wait ( vm_ref , instance , power_on ) <EOL> if first_boot : <EOL> self . _configure_new_instance_with_agent ( instance , vm_ref , <EOL> injected_files , admin_password ) <EOL> self . _remove_hostname ( instance , vm_ref ) <EOL> @ step <EOL> def apply_security_group_filters_step ( undo_mgr ) : <EOL> self . firewall_driver . apply_instance_filter ( instance , network_info ) <EOL> undo_mgr = utils . UndoManager ( ) <EOL> try : <EOL> disk_image_type = determine_disk_image_type_step ( undo_mgr ) <EOL> vdis = create_disks_step ( undo_mgr , disk_image_type , image_meta , <EOL> name_label ) <EOL> kernel_file , ramdisk_file = create_kernel_ramdisk_step ( undo_mgr ) <EOL> vm_ref = create_vm_record_step ( undo_mgr , disk_image_type , <EOL> kernel_file , ramdisk_file ) <EOL> attach_devices_step ( undo_mgr , vm_ref , vdis , disk_image_type ) <EOL> inject_instance_data_step ( undo_mgr , vm_ref , vdis ) <EOL> timeout = CONF . vif_plugging_timeout <EOL> events = self . _get_neutron_events ( network_info , <EOL> power_on , first_boot ) <EOL> try : <EOL> with self . _virtapi . wait_for_instance_event ( <EOL> instance , events , deadline = timeout , <EOL> error_callback = self . _neutron_failed_callback ) : <EOL> LOG . debug ( "<STR_LIT>" , events ) <EOL> setup_network_step ( undo_mgr , vm_ref ) <EOL> if rescue : <EOL> attach_orig_disks_step ( undo_mgr , vm_ref ) <EOL> start_paused_step ( undo_mgr , vm_ref ) <EOL> except eventlet . timeout . Timeout : <EOL> self . _handle_neutron_event_timeout ( instance , undo_mgr ) <EOL> apply_security_group_filters_step ( undo_mgr ) <EOL> boot_and_configure_instance_step ( undo_mgr , vm_ref ) <EOL> if completed_callback : <EOL> completed_callback ( ) <EOL> except Exception : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> undo_mgr . rollback_and_reraise ( msg = msg , instance = instance ) <EOL> def _handle_neutron_event_timeout ( self , instance , undo_mgr ) : <EOL> LOG . warn ( _LW ( '<STR_LIT>' ) , <EOL> instance = instance ) <EOL> if CONF . vif_plugging_is_fatal : <EOL> raise exception . VirtualInterfaceCreateException ( ) <EOL> def _unpause_and_wait ( self , vm_ref , instance , power_on ) : <EOL> if power_on : <EOL> LOG . debug ( "<STR_LIT>" , instance = instance ) <EOL> self . _session . VM . unpause ( vm_ref ) <EOL> self . _wait_for_instance_to_start ( instance , vm_ref ) <EOL> self . _update_last_dom_id ( vm_ref ) <EOL> def _neutron_failed_callback ( self , event_name , instance ) : <EOL> LOG . warn ( _LW ( '<STR_LIT>' ) , <EOL> { '<STR_LIT>' : event_name } , instance = instance ) <EOL> if CONF . vif_plugging_is_fatal : <EOL> raise exception . VirtualInterfaceCreateException ( ) <EOL> def _get_neutron_events ( self , network_info , power_on , first_boot ) : <EOL> timeout = CONF . vif_plugging_timeout <EOL> if ( utils . is_neutron ( ) and power_on and timeout and first_boot ) : <EOL> return [ ( '<STR_LIT>' , vif [ '<STR_LIT:id>' ] ) <EOL> for vif in network_info if vif . get ( '<STR_LIT>' , True ) is False ] <EOL> else : <EOL> return [ ] <EOL> def _attach_orig_disks ( self , instance , vm_ref ) : <EOL> orig_vm_ref = vm_utils . lookup ( self . _session , instance [ '<STR_LIT:name>' ] ) <EOL> orig_vdi_refs = self . _find_vdi_refs ( orig_vm_ref , <EOL> exclude_volumes = True ) <EOL> root_vdi_ref = orig_vdi_refs . get ( DEVICE_ROOT ) <EOL> if not root_vdi_ref : <EOL> raise exception . NotFound ( _ ( "<STR_LIT>" ) ) <EOL> vbd_ref = vm_utils . create_vbd ( self . _session , vm_ref , root_vdi_ref , <EOL> DEVICE_RESCUE , bootable = False ) <EOL> vbd_refs = [ vbd_ref ] <EOL> swap_vdi_ref = orig_vdi_refs . get ( DEVICE_SWAP ) <EOL> if swap_vdi_ref : <EOL> vbd_ref = vm_utils . create_vbd ( self . _session , vm_ref , swap_vdi_ref , <EOL> DEVICE_SWAP , bootable = False ) <EOL> vbd_refs . append ( vbd_ref ) <EOL> for userdevice , vdi_ref in six . iteritems ( orig_vdi_refs ) : <EOL> if userdevice >= DEVICE_EPHEMERAL : <EOL> vbd_ref = vm_utils . create_vbd ( self . _session , vm_ref , vdi_ref , <EOL> userdevice , bootable = False ) <EOL> vbd_refs . append ( vbd_ref ) <EOL> return vbd_refs <EOL> def _file_inject_vm_settings ( self , instance , vm_ref , vdis , network_info ) : <EOL> if CONF . flat_injected : <EOL> vm_utils . preconfigure_instance ( self . _session , instance , <EOL> vdis [ '<STR_LIT:root>' ] [ '<STR_LIT>' ] , network_info ) <EOL> def _ensure_instance_name_unique ( self , name_label ) : <EOL> vm_ref = vm_utils . lookup ( self . _session , name_label ) <EOL> if vm_ref is not None : <EOL> raise exception . InstanceExists ( name = name_label ) <EOL> def _ensure_enough_free_mem ( self , instance ) : <EOL> if not vm_utils . is_enough_free_mem ( self . _session , instance ) : <EOL> raise exception . InsufficientFreeMemory ( uuid = instance [ '<STR_LIT>' ] ) <EOL> def _create_vm_record ( self , context , instance , name_label , disk_image_type , <EOL> kernel_file , ramdisk_file , image_meta , rescue = False ) : <EOL> """<STR_LIT>""" <EOL> mode = vm_utils . determine_vm_mode ( instance , disk_image_type ) <EOL> if rescue : <EOL> rescue_vm_mode = image_meta . properties . get ( '<STR_LIT>' , None ) <EOL> if rescue_vm_mode is None : <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , mode , instance = instance ) <EOL> else : <EOL> mode = vm_mode . canonicalize ( rescue_vm_mode ) <EOL> if instance . vm_mode != mode : <EOL> instance . vm_mode = mode <EOL> instance . save ( ) <EOL> device_id = vm_utils . get_vm_device_id ( self . _session , image_meta ) <EOL> use_pv_kernel = ( mode == vm_mode . XEN ) <EOL> LOG . debug ( "<STR_LIT>" , use_pv_kernel , instance = instance ) <EOL> vm_ref = vm_utils . create_vm ( self . _session , instance , name_label , <EOL> kernel_file , ramdisk_file , <EOL> use_pv_kernel , device_id ) <EOL> return vm_ref <EOL> def _attach_disks ( self , instance , image_meta , vm_ref , name_label , vdis , <EOL> disk_image_type , network_info , rescue = False , <EOL> admin_password = None , files = None ) : <EOL> flavor = instance . get_flavor ( ) <EOL> if disk_image_type == vm_utils . ImageType . DISK_ISO : <EOL> root_disk_size = flavor . root_gb <EOL> if root_disk_size > <NUM_LIT:0> : <EOL> vm_utils . generate_iso_blank_root_disk ( self . _session , instance , <EOL> vm_ref , DEVICE_ROOT , name_label , root_disk_size ) <EOL> cd_vdi = vdis . pop ( '<STR_LIT>' ) <EOL> vm_utils . attach_cd ( self . _session , vm_ref , cd_vdi [ '<STR_LIT>' ] , <EOL> DEVICE_CD ) <EOL> else : <EOL> root_vdi = vdis [ '<STR_LIT:root>' ] <EOL> auto_disk_config = instance [ '<STR_LIT>' ] <EOL> if rescue : <EOL> if not image_meta . properties . obj_attr_is_set ( <EOL> "<STR_LIT>" ) : <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> auto_disk_config , instance = instance ) <EOL> else : <EOL> auto_disk_config = strutils . bool_from_string ( <EOL> image_meta . properties . hw_auto_disk_config ) <EOL> if auto_disk_config : <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , instance = instance ) <EOL> vm_utils . try_auto_configure_disk ( self . _session , <EOL> root_vdi [ '<STR_LIT>' ] , <EOL> flavor . root_gb ) <EOL> vm_utils . create_vbd ( self . _session , vm_ref , root_vdi [ '<STR_LIT>' ] , <EOL> DEVICE_ROOT , bootable = True , <EOL> osvol = root_vdi . get ( '<STR_LIT>' ) ) <EOL> for type_ , vdi_info in vdis . items ( ) : <EOL> if not type_ . startswith ( '<STR_LIT>' ) : <EOL> continue <EOL> userdevice = ord ( block_device . strip_prefix ( type_ ) ) - ord ( '<STR_LIT:a>' ) <EOL> vm_utils . create_vbd ( self . _session , vm_ref , vdi_info [ '<STR_LIT>' ] , <EOL> userdevice , bootable = False , <EOL> osvol = vdi_info . get ( '<STR_LIT>' ) ) <EOL> swap_mb = flavor . swap <EOL> if not rescue and swap_mb : <EOL> vm_utils . generate_swap ( self . _session , instance , vm_ref , <EOL> DEVICE_SWAP , name_label , swap_mb ) <EOL> ephemeral_gb = flavor . ephemeral_gb <EOL> if not rescue and ephemeral_gb : <EOL> ephemeral_vdis = vdis . get ( '<STR_LIT>' ) <EOL> if ephemeral_vdis : <EOL> for userdevice , ephemeral_vdi in six . iteritems ( ephemeral_vdis ) : <EOL> vm_utils . create_vbd ( self . _session , vm_ref , <EOL> ephemeral_vdi [ '<STR_LIT>' ] , <EOL> userdevice , bootable = False ) <EOL> else : <EOL> vm_utils . generate_ephemeral ( self . _session , instance , vm_ref , <EOL> DEVICE_EPHEMERAL , name_label , <EOL> ephemeral_gb ) <EOL> if configdrive . required_by ( instance ) : <EOL> vm_utils . generate_configdrive ( self . _session , instance , vm_ref , <EOL> DEVICE_CONFIGDRIVE , <EOL> network_info , <EOL> admin_password = admin_password , <EOL> files = files ) <EOL> def _wait_for_instance_to_start ( self , instance , vm_ref ) : <EOL> LOG . debug ( '<STR_LIT>' , <EOL> instance = instance ) <EOL> expiration = time . time ( ) + CONF . xenserver . running_timeout <EOL> while time . time ( ) < expiration : <EOL> state = vm_utils . get_power_state ( self . _session , vm_ref ) <EOL> if state == power_state . RUNNING : <EOL> break <EOL> greenthread . sleep ( <NUM_LIT:0.5> ) <EOL> def _configure_new_instance_with_agent ( self , instance , vm_ref , <EOL> injected_files , admin_password ) : <EOL> if not self . agent_enabled ( instance ) : <EOL> LOG . debug ( "<STR_LIT>" , instance = instance ) <EOL> return <EOL> agent = self . _get_agent ( instance , vm_ref ) <EOL> version = agent . get_version ( ) <EOL> if not version : <EOL> LOG . debug ( "<STR_LIT>" , <EOL> instance = instance ) <EOL> return <EOL> LOG . debug ( '<STR_LIT>' , version , instance = instance ) <EOL> agent . inject_ssh_key ( ) <EOL> if injected_files : <EOL> agent . inject_files ( injected_files ) <EOL> if admin_password : <EOL> agent . set_admin_password ( admin_password ) <EOL> agent . resetnetwork ( ) <EOL> agent . update_if_needed ( version ) <EOL> def _prepare_instance_filter ( self , instance , network_info ) : <EOL> try : <EOL> self . firewall_driver . setup_basic_filtering ( <EOL> instance , network_info ) <EOL> except NotImplementedError : <EOL> pass <EOL> self . firewall_driver . prepare_instance_filter ( instance , <EOL> network_info ) <EOL> def _get_vm_opaque_ref ( self , instance , check_rescue = False ) : <EOL> """<STR_LIT>""" <EOL> vm_ref = vm_utils . lookup ( self . _session , instance [ '<STR_LIT:name>' ] , check_rescue ) <EOL> if vm_ref is None : <EOL> raise exception . InstanceNotFound ( instance_id = instance [ '<STR_LIT:name>' ] ) <EOL> return vm_ref <EOL> def _acquire_bootlock ( self , vm ) : <EOL> """<STR_LIT>""" <EOL> self . _session . call_xenapi ( <EOL> "<STR_LIT>" , <EOL> vm , <EOL> { "<STR_LIT:start>" : "<STR_LIT>" } ) <EOL> def _release_bootlock ( self , vm ) : <EOL> """<STR_LIT>""" <EOL> self . _session . call_xenapi ( <EOL> "<STR_LIT>" , <EOL> vm , <EOL> "<STR_LIT:start>" ) <EOL> def snapshot ( self , context , instance , image_id , update_task_state ) : <EOL> """<STR_LIT>""" <EOL> vm_ref = self . _get_vm_opaque_ref ( instance ) <EOL> label = "<STR_LIT>" % instance [ '<STR_LIT:name>' ] <EOL> start_time = timeutils . utcnow ( ) <EOL> with vm_utils . snapshot_attached_here ( <EOL> self . _session , instance , vm_ref , label , <EOL> post_snapshot_callback = update_task_state ) as vdi_uuids : <EOL> update_task_state ( task_state = task_states . IMAGE_UPLOADING , <EOL> expected_state = task_states . IMAGE_PENDING_UPLOAD ) <EOL> self . image_upload_handler . upload_image ( context , <EOL> self . _session , <EOL> instance , <EOL> image_id , <EOL> vdi_uuids , <EOL> ) <EOL> duration = timeutils . delta_seconds ( start_time , timeutils . utcnow ( ) ) <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> { '<STR_LIT>' : image_id , '<STR_LIT>' : duration } , <EOL> instance = instance ) <EOL> def post_interrupted_snapshot_cleanup ( self , context , instance ) : <EOL> """<STR_LIT>""" <EOL> vm_ref = self . _get_vm_opaque_ref ( instance ) <EOL> vm_utils . remove_old_snapshots ( self . _session , instance , vm_ref ) <EOL> def _get_orig_vm_name_label ( self , instance ) : <EOL> return instance [ '<STR_LIT:name>' ] + '<STR_LIT>' <EOL> def _update_instance_progress ( self , context , instance , step , total_steps ) : <EOL> """<STR_LIT>""" <EOL> progress = round ( float ( step ) / total_steps * <NUM_LIT:100> ) <EOL> LOG . debug ( "<STR_LIT>" , progress , <EOL> instance = instance ) <EOL> instance . progress = progress <EOL> instance . save ( ) <EOL> def _resize_ensure_vm_is_shutdown ( self , instance , vm_ref ) : <EOL> if vm_utils . is_vm_shutdown ( self . _session , vm_ref ) : <EOL> LOG . debug ( "<STR_LIT>" , instance = instance ) <EOL> return <EOL> if not vm_utils . clean_shutdown_vm ( self . _session , instance , vm_ref ) : <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , instance = instance ) <EOL> if not vm_utils . hard_shutdown_vm ( self . _session , instance , vm_ref ) : <EOL> raise exception . ResizeError ( <EOL> reason = _ ( "<STR_LIT>" ) ) <EOL> def _migrate_disk_resizing_down ( self , context , instance , dest , <EOL> flavor , vm_ref , sr_path ) : <EOL> step = make_step_decorator ( context , instance , <EOL> self . _update_instance_progress , <EOL> total_offset = <NUM_LIT:1> ) <EOL> @ step <EOL> def fake_step_to_match_resizing_up ( ) : <EOL> pass <EOL> @ step <EOL> def rename_and_power_off_vm ( undo_mgr ) : <EOL> self . _resize_ensure_vm_is_shutdown ( instance , vm_ref ) <EOL> self . _apply_orig_vm_name_label ( instance , vm_ref ) <EOL> def restore_orig_vm ( ) : <EOL> self . _restore_orig_vm_and_cleanup_orphan ( instance ) <EOL> undo_mgr . undo_with ( restore_orig_vm ) <EOL> @ step <EOL> def create_copy_vdi_and_resize ( undo_mgr , old_vdi_ref ) : <EOL> new_vdi_ref , new_vdi_uuid = vm_utils . resize_disk ( self . _session , <EOL> instance , old_vdi_ref , flavor ) <EOL> def cleanup_vdi_copy ( ) : <EOL> vm_utils . destroy_vdi ( self . _session , new_vdi_ref ) <EOL> undo_mgr . undo_with ( cleanup_vdi_copy ) <EOL> return new_vdi_ref , new_vdi_uuid <EOL> @ step <EOL> def transfer_vhd_to_dest ( new_vdi_ref , new_vdi_uuid ) : <EOL> vm_utils . migrate_vhd ( self . _session , instance , new_vdi_uuid , <EOL> dest , sr_path , <NUM_LIT:0> ) <EOL> vm_utils . destroy_vdi ( self . _session , new_vdi_ref ) <EOL> undo_mgr = utils . UndoManager ( ) <EOL> try : <EOL> fake_step_to_match_resizing_up ( ) <EOL> rename_and_power_off_vm ( undo_mgr ) <EOL> old_vdi_ref , _ignore = vm_utils . get_vdi_for_vm_safely ( <EOL> self . _session , vm_ref ) <EOL> new_vdi_ref , new_vdi_uuid = create_copy_vdi_and_resize ( <EOL> undo_mgr , old_vdi_ref ) <EOL> transfer_vhd_to_dest ( new_vdi_ref , new_vdi_uuid ) <EOL> except Exception as error : <EOL> LOG . exception ( _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> instance = instance ) <EOL> undo_mgr . _rollback ( ) <EOL> raise exception . InstanceFaultRollback ( error ) <EOL> def _migrate_disk_resizing_up ( self , context , instance , dest , vm_ref , <EOL> sr_path ) : <EOL> step = make_step_decorator ( context , <EOL> instance , <EOL> self . _update_instance_progress , <EOL> total_offset = <NUM_LIT:1> ) <EOL> """<STR_LIT>""" <EOL> @ step <EOL> def fake_step_to_show_snapshot_complete ( ) : <EOL> pass <EOL> @ step <EOL> def transfer_immutable_vhds ( root_vdi_uuids ) : <EOL> immutable_root_vdi_uuids = root_vdi_uuids [ <NUM_LIT:1> : ] <EOL> for vhd_num , vdi_uuid in enumerate ( immutable_root_vdi_uuids , <EOL> start = <NUM_LIT:1> ) : <EOL> vm_utils . migrate_vhd ( self . _session , instance , vdi_uuid , dest , <EOL> sr_path , vhd_num ) <EOL> LOG . debug ( "<STR_LIT>" , instance = instance ) <EOL> def _process_ephemeral_chain_recursive ( ephemeral_chains , <EOL> active_vdi_uuids ) : <EOL> number_of_chains = len ( ephemeral_chains ) <EOL> if number_of_chains == <NUM_LIT:0> : <EOL> LOG . debug ( "<STR_LIT>" , instance = instance ) <EOL> return power_down_and_transfer_leaf_vhds ( <EOL> active_root_vdi_uuid , <EOL> active_vdi_uuids ) <EOL> remaining_chains = [ ] <EOL> if number_of_chains > <NUM_LIT:1> : <EOL> remaining_chains = ephemeral_chains [ <NUM_LIT:1> : ] <EOL> ephemeral_disk_index = len ( active_vdi_uuids ) <EOL> userdevice = int ( DEVICE_EPHEMERAL ) + ephemeral_disk_index <EOL> with vm_utils . snapshot_attached_here ( self . _session , instance , <EOL> vm_ref , label , str ( userdevice ) ) as chain_vdi_uuids : <EOL> vdi_ref , vm_vdi_rec = vm_utils . get_vdi_for_vm_safely ( <EOL> self . _session , vm_ref , str ( userdevice ) ) <EOL> active_uuid = vm_vdi_rec [ '<STR_LIT>' ] <EOL> active_vdi_uuids . append ( active_uuid ) <EOL> inactive_vdi_uuids = chain_vdi_uuids [ <NUM_LIT:1> : ] <EOL> ephemeral_disk_number = ephemeral_disk_index + <NUM_LIT:1> <EOL> for seq_num , vdi_uuid in enumerate ( inactive_vdi_uuids , <EOL> start = <NUM_LIT:1> ) : <EOL> vm_utils . migrate_vhd ( self . _session , instance , vdi_uuid , <EOL> dest , sr_path , seq_num , <EOL> ephemeral_disk_number ) <EOL> LOG . debug ( "<STR_LIT>" , userdevice , <EOL> instance = instance ) <EOL> return _process_ephemeral_chain_recursive ( remaining_chains , <EOL> active_vdi_uuids ) <EOL> @ step <EOL> def transfer_ephemeral_disks_then_all_leaf_vdis ( ) : <EOL> ephemeral_chains = vm_utils . get_all_vdi_uuids_for_vm ( <EOL> self . _session , vm_ref , <EOL> min_userdevice = int ( DEVICE_EPHEMERAL ) ) <EOL> if ephemeral_chains : <EOL> ephemeral_chains = list ( ephemeral_chains ) <EOL> else : <EOL> ephemeral_chains = [ ] <EOL> _process_ephemeral_chain_recursive ( ephemeral_chains , [ ] ) <EOL> @ step <EOL> def power_down_and_transfer_leaf_vhds ( root_vdi_uuid , <EOL> ephemeral_vdi_uuids = None ) : <EOL> self . _resize_ensure_vm_is_shutdown ( instance , vm_ref ) <EOL> if root_vdi_uuid is not None : <EOL> vm_utils . migrate_vhd ( self . _session , instance , root_vdi_uuid , <EOL> dest , sr_path , <NUM_LIT:0> ) <EOL> if ephemeral_vdi_uuids : <EOL> for ephemeral_disk_number , ephemeral_vdi_uuid in enumerate ( <EOL> ephemeral_vdi_uuids , start = <NUM_LIT:1> ) : <EOL> vm_utils . migrate_vhd ( self . _session , instance , <EOL> ephemeral_vdi_uuid , dest , <EOL> sr_path , <NUM_LIT:0> , ephemeral_disk_number ) <EOL> self . _apply_orig_vm_name_label ( instance , vm_ref ) <EOL> try : <EOL> label = "<STR_LIT>" % instance [ '<STR_LIT:name>' ] <EOL> if volume_utils . is_booted_from_volume ( self . _session , vm_ref ) : <EOL> LOG . debug ( '<STR_LIT>' , <EOL> instance = instance ) <EOL> fake_step_to_show_snapshot_complete ( ) <EOL> fake_step_to_show_snapshot_complete ( ) <EOL> active_root_vdi_uuid = None <EOL> transfer_ephemeral_disks_then_all_leaf_vdis ( ) <EOL> return <EOL> with vm_utils . snapshot_attached_here ( <EOL> self . _session , instance , vm_ref , label ) as root_vdi_uuids : <EOL> fake_step_to_show_snapshot_complete ( ) <EOL> transfer_immutable_vhds ( root_vdi_uuids ) <EOL> vdi_ref , vm_vdi_rec = vm_utils . get_vdi_for_vm_safely ( <EOL> self . _session , vm_ref ) <EOL> active_root_vdi_uuid = vm_vdi_rec [ '<STR_LIT>' ] <EOL> transfer_ephemeral_disks_then_all_leaf_vdis ( ) <EOL> except Exception as error : <EOL> LOG . exception ( _LE ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , error , <EOL> instance = instance ) <EOL> try : <EOL> self . _restore_orig_vm_and_cleanup_orphan ( instance ) <EOL> except Exception as rollback_error : <EOL> LOG . warning ( _LW ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , rollback_error , <EOL> instance = instance ) <EOL> raise exception . InstanceFaultRollback ( error ) <EOL> def _apply_orig_vm_name_label ( self , instance , vm_ref ) : <EOL> name_label = self . _get_orig_vm_name_label ( instance ) <EOL> vm_utils . set_vm_name_label ( self . _session , vm_ref , name_label ) <EOL> def _ensure_not_resize_down_ephemeral ( self , instance , flavor ) : <EOL> old_gb = instance [ "<STR_LIT>" ] <EOL> new_gb = flavor . ephemeral_gb <EOL> if old_gb > new_gb : <EOL> reason = _ ( "<STR_LIT>" ) <EOL> raise exception . ResizeError ( reason ) <EOL> def migrate_disk_and_power_off ( self , context , instance , dest , <EOL> flavor , block_device_info ) : <EOL> """<STR_LIT>""" <EOL> self . _ensure_not_resize_down_ephemeral ( instance , flavor ) <EOL> self . _update_instance_progress ( context , instance , <EOL> step = <NUM_LIT:0> , <EOL> total_steps = RESIZE_TOTAL_STEPS ) <EOL> old_gb = instance [ '<STR_LIT>' ] <EOL> new_gb = flavor . root_gb <EOL> resize_down = old_gb > new_gb <EOL> if new_gb == <NUM_LIT:0> and old_gb != <NUM_LIT:0> : <EOL> reason = _ ( "<STR_LIT>" ) <EOL> raise exception . ResizeError ( reason = reason ) <EOL> vm_ref = self . _get_vm_opaque_ref ( instance ) <EOL> sr_path = vm_utils . get_sr_path ( self . _session ) <EOL> if resize_down : <EOL> self . _migrate_disk_resizing_down ( <EOL> context , instance , dest , flavor , vm_ref , sr_path ) <EOL> else : <EOL> self . _migrate_disk_resizing_up ( <EOL> context , instance , dest , vm_ref , sr_path ) <EOL> self . _detach_block_devices_from_orig_vm ( instance , block_device_info ) <EOL> disk_info = { } <EOL> return disk_info <EOL> def _detach_block_devices_from_orig_vm ( self , instance , block_device_info ) : <EOL> block_device_mapping = virt_driver . block_device_info_get_mapping ( <EOL> block_device_info ) <EOL> name_label = self . _get_orig_vm_name_label ( instance ) <EOL> for vol in block_device_mapping : <EOL> connection_info = vol [ '<STR_LIT>' ] <EOL> mount_device = vol [ '<STR_LIT>' ] . rpartition ( "<STR_LIT:/>" ) [ <NUM_LIT:2> ] <EOL> self . _volumeops . detach_volume ( connection_info , name_label , <EOL> mount_device ) <EOL> def _resize_up_vdis ( self , instance , vdis ) : <EOL> new_root_gb = instance [ '<STR_LIT>' ] <EOL> root_vdi = vdis . get ( '<STR_LIT:root>' ) <EOL> if new_root_gb and root_vdi : <EOL> if root_vdi . get ( '<STR_LIT>' , False ) : <EOL> LOG . debug ( "<STR_LIT>" , <EOL> instance = instance ) <EOL> else : <EOL> vdi_ref = root_vdi [ '<STR_LIT>' ] <EOL> vm_utils . update_vdi_virtual_size ( self . _session , instance , <EOL> vdi_ref , new_root_gb ) <EOL> ephemeral_vdis = vdis . get ( '<STR_LIT>' ) <EOL> if not ephemeral_vdis : <EOL> return <EOL> total_ephemeral_gb = instance [ '<STR_LIT>' ] <EOL> if total_ephemeral_gb : <EOL> sizes = vm_utils . get_ephemeral_disk_sizes ( total_ephemeral_gb ) <EOL> for userdevice , new_size in enumerate ( sizes , <EOL> start = int ( DEVICE_EPHEMERAL ) ) : <EOL> vdi = ephemeral_vdis . get ( str ( userdevice ) ) <EOL> if vdi : <EOL> vdi_ref = vdi [ '<STR_LIT>' ] <EOL> vm_utils . update_vdi_virtual_size ( self . _session , instance , <EOL> vdi_ref , new_size ) <EOL> else : <EOL> LOG . debug ( "<STR_LIT>" , <EOL> userdevice , instance = instance ) <EOL> vdi_ref = vm_utils . generate_single_ephemeral ( <EOL> self . _session , instance , None , userdevice , new_size ) <EOL> vdis [ str ( userdevice ) ] = { '<STR_LIT>' : vdi_ref , '<STR_LIT>' : True } <EOL> def reboot ( self , instance , reboot_type , bad_volumes_callback = None ) : <EOL> """<STR_LIT>""" <EOL> vm_ref = self . _get_vm_opaque_ref ( instance , check_rescue = True ) <EOL> try : <EOL> if reboot_type == "<STR_LIT>" : <EOL> self . _session . call_xenapi ( '<STR_LIT>' , vm_ref ) <EOL> else : <EOL> self . _session . call_xenapi ( '<STR_LIT>' , vm_ref ) <EOL> except self . _session . XenAPI . Failure as exc : <EOL> details = exc . details <EOL> if ( details [ <NUM_LIT:0> ] == '<STR_LIT>' and <EOL> details [ - <NUM_LIT:1> ] == '<STR_LIT>' ) : <EOL> LOG . info ( _LI ( "<STR_LIT>" ) , <EOL> instance = instance ) <EOL> self . _start ( instance , vm_ref = vm_ref , <EOL> bad_volumes_callback = bad_volumes_callback ) <EOL> return <EOL> elif details [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> LOG . warning ( _LW ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> instance = instance ) <EOL> self . _start ( instance , vm_ref = vm_ref , <EOL> bad_volumes_callback = bad_volumes_callback ) <EOL> return <EOL> else : <EOL> raise <EOL> def set_admin_password ( self , instance , new_pass ) : <EOL> """<STR_LIT>""" <EOL> if self . agent_enabled ( instance ) : <EOL> vm_ref = self . _get_vm_opaque_ref ( instance ) <EOL> agent = self . _get_agent ( instance , vm_ref ) <EOL> agent . set_admin_password ( new_pass ) <EOL> else : <EOL> raise NotImplementedError ( ) <EOL> def inject_file ( self , instance , path , contents ) : <EOL> """<STR_LIT>""" <EOL> if self . agent_enabled ( instance ) : <EOL> vm_ref = self . _get_vm_opaque_ref ( instance ) <EOL> agent = self . _get_agent ( instance , vm_ref ) <EOL> agent . inject_file ( path , contents ) <EOL> else : <EOL> raise NotImplementedError ( ) <EOL> @ staticmethod <EOL> def _sanitize_xenstore_key ( key ) : <EOL> """<STR_LIT>""" <EOL> allowed_chars = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return '<STR_LIT>' . join ( [ x in allowed_chars and x or '<STR_LIT:_>' for x in key ] ) <EOL> def _inject_instance_metadata ( self , instance , vm_ref ) : <EOL> """<STR_LIT>""" <EOL> @ utils . synchronized ( '<STR_LIT>' + instance [ '<STR_LIT>' ] ) <EOL> def store_meta ( topdir , data_dict ) : <EOL> for key , value in data_dict . items ( ) : <EOL> key = self . _sanitize_xenstore_key ( key ) <EOL> value = value or '<STR_LIT>' <EOL> self . _add_to_param_xenstore ( vm_ref , '<STR_LIT>' % ( topdir , key ) , <EOL> jsonutils . dumps ( value ) ) <EOL> store_meta ( '<STR_LIT>' , utils . instance_meta ( instance ) ) <EOL> def _inject_auto_disk_config ( self , instance , vm_ref ) : <EOL> """<STR_LIT>""" <EOL> @ utils . synchronized ( '<STR_LIT>' + instance [ '<STR_LIT>' ] ) <EOL> def store_auto_disk_config ( key , value ) : <EOL> value = value and True or False <EOL> self . _add_to_param_xenstore ( vm_ref , key , str ( value ) ) <EOL> store_auto_disk_config ( '<STR_LIT>' , <EOL> instance [ '<STR_LIT>' ] ) <EOL> def change_instance_metadata ( self , instance , diff ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> vm_ref = self . _get_vm_opaque_ref ( instance ) <EOL> except exception . NotFound : <EOL> LOG . warning ( _LW ( "<STR_LIT>" ) , <EOL> instance = instance , exc_info = True ) <EOL> return <EOL> def process_change ( location , change ) : <EOL> if change [ <NUM_LIT:0> ] == '<STR_LIT:->' : <EOL> self . _remove_from_param_xenstore ( vm_ref , location ) <EOL> try : <EOL> self . _delete_from_xenstore ( instance , location , <EOL> vm_ref = vm_ref ) <EOL> except exception . InstanceNotFound : <EOL> pass <EOL> elif change [ <NUM_LIT:0> ] == '<STR_LIT:+>' : <EOL> self . _add_to_param_xenstore ( vm_ref , location , <EOL> jsonutils . dumps ( change [ <NUM_LIT:1> ] ) ) <EOL> try : <EOL> self . _write_to_xenstore ( instance , location , change [ <NUM_LIT:1> ] , <EOL> vm_ref = vm_ref ) <EOL> except exception . InstanceNotFound : <EOL> pass <EOL> @ utils . synchronized ( '<STR_LIT>' + instance [ '<STR_LIT>' ] ) <EOL> def update_meta ( ) : <EOL> for key , change in diff . items ( ) : <EOL> key = self . _sanitize_xenstore_key ( key ) <EOL> location = '<STR_LIT>' % key <EOL> process_change ( location , change ) <EOL> update_meta ( ) <EOL> def _find_vdi_refs ( self , vm_ref , exclude_volumes = False ) : <EOL> """<STR_LIT>""" <EOL> if not vm_ref : <EOL> return { } <EOL> vdi_refs = { } <EOL> for vbd_ref in self . _session . call_xenapi ( "<STR_LIT>" , vm_ref ) : <EOL> vbd = self . _session . call_xenapi ( "<STR_LIT>" , vbd_ref ) <EOL> if not exclude_volumes or '<STR_LIT>' not in vbd [ '<STR_LIT>' ] : <EOL> vdi_refs [ vbd [ '<STR_LIT>' ] ] = vbd [ '<STR_LIT>' ] <EOL> return vdi_refs <EOL> def _destroy_vdis ( self , instance , vm_ref ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( "<STR_LIT>" , instance = instance ) <EOL> vdi_refs = vm_utils . lookup_vm_vdis ( self . _session , vm_ref ) <EOL> if not vdi_refs : <EOL> return <EOL> for vdi_ref in vdi_refs : <EOL> try : <EOL> vm_utils . destroy_vdi ( self . _session , vdi_ref ) <EOL> except exception . StorageError as exc : <EOL> LOG . error ( exc ) <EOL> def _destroy_kernel_ramdisk ( self , instance , vm_ref ) : <EOL> """<STR_LIT>""" <EOL> instance_uuid = instance [ '<STR_LIT>' ] <EOL> if not instance [ '<STR_LIT>' ] and not instance [ '<STR_LIT>' ] : <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , instance = instance ) <EOL> return <EOL> if not ( instance [ '<STR_LIT>' ] and instance [ '<STR_LIT>' ] ) : <EOL> raise exception . InstanceUnacceptable ( instance_id = instance_uuid , <EOL> reason = _ ( "<STR_LIT>" ) ) <EOL> ( kernel , ramdisk ) = vm_utils . lookup_kernel_ramdisk ( self . _session , <EOL> vm_ref ) <EOL> if kernel or ramdisk : <EOL> vm_utils . destroy_kernel_ramdisk ( self . _session , instance , <EOL> kernel , ramdisk ) <EOL> LOG . debug ( "<STR_LIT>" , instance = instance ) <EOL> def _destroy_rescue_instance ( self , rescue_vm_ref , original_vm_ref ) : <EOL> """<STR_LIT>""" <EOL> state = vm_utils . get_power_state ( self . _session , rescue_vm_ref ) <EOL> if state != power_state . SHUTDOWN : <EOL> self . _session . call_xenapi ( "<STR_LIT>" , rescue_vm_ref ) <EOL> vdi_refs = vm_utils . lookup_vm_vdis ( self . _session , rescue_vm_ref ) <EOL> orig_vdi_refs = self . _find_vdi_refs ( original_vm_ref ) <EOL> vdi_refs = set ( vdi_refs ) - set ( orig_vdi_refs . values ( ) ) <EOL> vm_utils . safe_destroy_vdis ( self . _session , vdi_refs ) <EOL> self . _session . call_xenapi ( "<STR_LIT>" , rescue_vm_ref ) <EOL> def destroy ( self , instance , network_info , block_device_info = None , <EOL> destroy_disks = True ) : <EOL> """<STR_LIT>""" <EOL> LOG . info ( _LI ( "<STR_LIT>" ) , instance = instance ) <EOL> vm_ref = vm_utils . lookup ( self . _session , instance [ '<STR_LIT:name>' ] ) <EOL> rescue_vm_ref = vm_utils . lookup ( self . _session , <EOL> "<STR_LIT>" % instance [ '<STR_LIT:name>' ] ) <EOL> if rescue_vm_ref : <EOL> self . _destroy_rescue_instance ( rescue_vm_ref , vm_ref ) <EOL> return self . _destroy ( instance , vm_ref , network_info = network_info , <EOL> destroy_disks = destroy_disks , <EOL> block_device_info = block_device_info ) <EOL> def _destroy ( self , instance , vm_ref , network_info = None , <EOL> destroy_disks = True , block_device_info = None ) : <EOL> """<STR_LIT>""" <EOL> if vm_ref is None : <EOL> LOG . warning ( _LW ( "<STR_LIT>" ) , <EOL> instance = instance ) <EOL> bdms = block_device_info [ '<STR_LIT>' ] or [ ] <EOL> if not bdms : <EOL> return <EOL> for bdm in bdms : <EOL> volume_id = bdm [ '<STR_LIT>' ] [ '<STR_LIT:data>' ] [ '<STR_LIT>' ] <EOL> sr_uuid = '<STR_LIT>' % volume_id <EOL> sr_ref = None <EOL> try : <EOL> sr_ref = volume_utils . find_sr_by_uuid ( self . _session , <EOL> sr_uuid ) <EOL> if not sr_ref : <EOL> connection_data = bdm [ '<STR_LIT>' ] [ '<STR_LIT:data>' ] <EOL> ( sr_uuid , _ , _ ) = volume_utils . parse_sr_info ( <EOL> connection_data ) <EOL> sr_ref = volume_utils . find_sr_by_uuid ( self . _session , <EOL> sr_uuid ) <EOL> except Exception : <EOL> LOG . exception ( _LE ( '<STR_LIT>' ) , <EOL> volume_id , instance = instance ) <EOL> try : <EOL> if sr_ref : <EOL> volume_utils . forget_sr ( self . _session , sr_ref ) <EOL> else : <EOL> LOG . error ( _LE ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , volume_id , <EOL> instance = instance ) <EOL> except Exception : <EOL> LOG . exception ( _LE ( '<STR_LIT>' ) , <EOL> volume_id , instance = instance ) <EOL> return <EOL> if block_device_info and block_device_info [ '<STR_LIT>' ] : <EOL> if not vm_utils . clean_shutdown_vm ( self . _session , instance , vm_ref ) : <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , instance = instance ) <EOL> vm_utils . hard_shutdown_vm ( self . _session , instance , vm_ref ) <EOL> else : <EOL> vm_utils . hard_shutdown_vm ( self . _session , instance , vm_ref ) <EOL> if destroy_disks : <EOL> self . _volumeops . detach_all ( vm_ref ) <EOL> self . _destroy_vdis ( instance , vm_ref ) <EOL> self . _destroy_kernel_ramdisk ( instance , vm_ref ) <EOL> self . unplug_vifs ( instance , network_info , vm_ref ) <EOL> self . firewall_driver . unfilter_instance ( <EOL> instance , network_info = network_info ) <EOL> vm_utils . destroy_vm ( self . _session , instance , vm_ref ) <EOL> def pause ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> vm_ref = self . _get_vm_opaque_ref ( instance ) <EOL> self . _session . call_xenapi ( '<STR_LIT>' , vm_ref ) <EOL> def unpause ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> vm_ref = self . _get_vm_opaque_ref ( instance ) <EOL> self . _session . call_xenapi ( '<STR_LIT>' , vm_ref ) <EOL> def suspend ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> vm_ref = self . _get_vm_opaque_ref ( instance ) <EOL> self . _acquire_bootlock ( vm_ref ) <EOL> self . _session . call_xenapi ( '<STR_LIT>' , vm_ref ) <EOL> def resume ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> vm_ref = self . _get_vm_opaque_ref ( instance ) <EOL> self . _release_bootlock ( vm_ref ) <EOL> self . _session . call_xenapi ( '<STR_LIT>' , vm_ref , False , True ) <EOL> def rescue ( self , context , instance , network_info , image_meta , <EOL> rescue_password ) : <EOL> """<STR_LIT>""" <EOL> rescue_name_label = '<STR_LIT>' % instance . name <EOL> rescue_vm_ref = vm_utils . lookup ( self . _session , rescue_name_label ) <EOL> if rescue_vm_ref : <EOL> raise RuntimeError ( _ ( "<STR_LIT>" ) <EOL> % instance . name ) <EOL> vm_ref = self . _get_vm_opaque_ref ( instance ) <EOL> vm_utils . hard_shutdown_vm ( self . _session , instance , vm_ref ) <EOL> self . _acquire_bootlock ( vm_ref ) <EOL> self . spawn ( context , instance , image_meta , [ ] , rescue_password , <EOL> network_info , name_label = rescue_name_label , rescue = True ) <EOL> def set_bootable ( self , instance , is_bootable ) : <EOL> """<STR_LIT>""" <EOL> vm_ref = self . _get_vm_opaque_ref ( instance ) <EOL> if is_bootable : <EOL> self . _release_bootlock ( vm_ref ) <EOL> else : <EOL> self . _acquire_bootlock ( vm_ref ) <EOL> def unrescue ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> rescue_vm_ref = vm_utils . lookup ( self . _session , <EOL> "<STR_LIT>" % instance . name ) <EOL> if not rescue_vm_ref : <EOL> raise exception . InstanceNotInRescueMode ( <EOL> instance_id = instance . uuid ) <EOL> original_vm_ref = self . _get_vm_opaque_ref ( instance ) <EOL> self . _destroy_rescue_instance ( rescue_vm_ref , original_vm_ref ) <EOL> self . _release_bootlock ( original_vm_ref ) <EOL> self . _start ( instance , original_vm_ref ) <EOL> def soft_delete ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> vm_ref = self . _get_vm_opaque_ref ( instance ) <EOL> except exception . NotFound : <EOL> LOG . warning ( _LW ( "<STR_LIT>" ) , <EOL> instance = instance ) <EOL> else : <EOL> vm_utils . hard_shutdown_vm ( self . _session , instance , vm_ref ) <EOL> self . _acquire_bootlock ( vm_ref ) <EOL> def restore ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> vm_ref = self . _get_vm_opaque_ref ( instance ) <EOL> self . _release_bootlock ( vm_ref ) <EOL> self . _start ( instance , vm_ref ) <EOL> def power_off ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> vm_ref = self . _get_vm_opaque_ref ( instance ) <EOL> vm_utils . hard_shutdown_vm ( self . _session , instance , vm_ref ) <EOL> def power_on ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> vm_ref = self . _get_vm_opaque_ref ( instance ) <EOL> self . _start ( instance , vm_ref ) <EOL> def _cancel_stale_tasks ( self , timeout , task ) : <EOL> """<STR_LIT>""" <EOL> task_refs = self . _session . call_xenapi ( "<STR_LIT>" , task ) <EOL> for task_ref in task_refs : <EOL> task_rec = self . _session . call_xenapi ( "<STR_LIT>" , task_ref ) <EOL> task_created = timeutils . parse_strtime ( task_rec [ "<STR_LIT>" ] . value , <EOL> "<STR_LIT>" ) <EOL> if timeutils . is_older_than ( task_created , timeout ) : <EOL> self . _session . call_xenapi ( "<STR_LIT>" , task_ref ) <EOL> def poll_rebooting_instances ( self , timeout , instances ) : <EOL> """<STR_LIT>""" <EOL> self . _cancel_stale_tasks ( timeout , '<STR_LIT>' ) <EOL> ctxt = nova_context . get_admin_context ( ) <EOL> instances_info = dict ( instance_count = len ( instances ) , <EOL> timeout = timeout ) <EOL> if instances_info [ "<STR_LIT>" ] > <NUM_LIT:0> : <EOL> LOG . info ( _LI ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , instances_info ) <EOL> for instance in instances : <EOL> LOG . info ( _LI ( "<STR_LIT>" ) , instance = instance ) <EOL> self . compute_api . reboot ( ctxt , instance , "<STR_LIT>" ) <EOL> def get_info ( self , instance , vm_ref = None ) : <EOL> """<STR_LIT>""" <EOL> vm_ref = vm_ref or self . _get_vm_opaque_ref ( instance ) <EOL> return vm_utils . compile_info ( self . _session , vm_ref ) <EOL> def get_diagnostics ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> vm_ref = self . _get_vm_opaque_ref ( instance ) <EOL> vm_rec = self . _session . call_xenapi ( "<STR_LIT>" , vm_ref ) <EOL> return vm_utils . compile_diagnostics ( vm_rec ) <EOL> def get_instance_diagnostics ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> vm_ref = self . _get_vm_opaque_ref ( instance ) <EOL> vm_rec = self . _session . VM . get_record ( vm_ref ) <EOL> return vm_utils . compile_instance_diagnostics ( instance , vm_rec ) <EOL> def _get_vif_device_map ( self , vm_rec ) : <EOL> vif_map = { } <EOL> for vif in [ self . _session . call_xenapi ( "<STR_LIT>" , vrec ) <EOL> for vrec in vm_rec [ '<STR_LIT>' ] ] : <EOL> vif_map [ vif [ '<STR_LIT>' ] ] = vif [ '<STR_LIT>' ] <EOL> return vif_map <EOL> def get_all_bw_counters ( self ) : <EOL> """<STR_LIT>""" <EOL> counters = vm_utils . fetch_bandwidth ( self . _session ) <EOL> bw = { } <EOL> for vm_ref , vm_rec in vm_utils . list_vms ( self . _session ) : <EOL> vif_map = self . _get_vif_device_map ( vm_rec ) <EOL> name = vm_rec [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' not in vm_rec [ '<STR_LIT>' ] : <EOL> continue <EOL> dom = vm_rec . get ( '<STR_LIT>' ) <EOL> if dom is None or dom not in counters : <EOL> continue <EOL> vifs_bw = bw . setdefault ( name , { } ) <EOL> for vif_num , vif_data in six . iteritems ( counters [ dom ] ) : <EOL> mac = vif_map [ vif_num ] <EOL> vif_data [ '<STR_LIT>' ] = mac <EOL> vifs_bw [ mac ] = vif_data <EOL> return bw <EOL> def get_console_output ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> dom_id = self . _get_last_dom_id ( instance , check_rescue = True ) <EOL> try : <EOL> raw_console_data = self . _session . call_plugin ( '<STR_LIT>' , <EOL> '<STR_LIT>' , { '<STR_LIT>' : dom_id } ) <EOL> except self . _session . XenAPI . Failure : <EOL> LOG . exception ( _LE ( "<STR_LIT>" ) ) <EOL> raise exception . ConsoleNotAvailable ( ) <EOL> return zlib . decompress ( base64 . b64decode ( raw_console_data ) ) <EOL> def get_vnc_console ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> if instance . vm_state == vm_states . RESCUED : <EOL> name = '<STR_LIT>' % instance . name <EOL> vm_ref = vm_utils . lookup ( self . _session , name ) <EOL> if vm_ref is None : <EOL> raise exception . InstanceNotReady ( instance_id = instance . uuid ) <EOL> else : <EOL> vm_ref = vm_utils . lookup ( self . _session , instance . name ) <EOL> if vm_ref is None : <EOL> raise exception . InstanceNotFound ( instance_id = instance . uuid ) <EOL> session_id = self . _session . get_session_id ( ) <EOL> path = "<STR_LIT>" % ( str ( vm_ref ) , session_id ) <EOL> return ctype . ConsoleVNC ( <EOL> host = CONF . vnc . vncserver_proxyclient_address , <EOL> port = <NUM_LIT> , <EOL> internal_access_path = path ) <EOL> def _vif_xenstore_data ( self , vif ) : <EOL> """<STR_LIT>""" <EOL> def get_ip ( ip ) : <EOL> if not ip : <EOL> return None <EOL> return ip [ '<STR_LIT:address>' ] <EOL> def fixed_ip_dict ( ip , subnet ) : <EOL> if ip [ '<STR_LIT:version>' ] == <NUM_LIT:4> : <EOL> netmask = str ( subnet . as_netaddr ( ) . netmask ) <EOL> else : <EOL> netmask = subnet . as_netaddr ( ) . _prefixlen <EOL> return { '<STR_LIT>' : ip [ '<STR_LIT:address>' ] , <EOL> '<STR_LIT>' : '<STR_LIT:1>' , <EOL> '<STR_LIT>' : netmask , <EOL> '<STR_LIT>' : get_ip ( subnet [ '<STR_LIT>' ] ) } <EOL> def convert_route ( route ) : <EOL> return { '<STR_LIT>' : str ( netaddr . IPNetwork ( route [ '<STR_LIT>' ] ) . network ) , <EOL> '<STR_LIT>' : str ( netaddr . IPNetwork ( route [ '<STR_LIT>' ] ) . netmask ) , <EOL> '<STR_LIT>' : get_ip ( route [ '<STR_LIT>' ] ) } <EOL> network = vif [ '<STR_LIT>' ] <EOL> v4_subnets = [ subnet for subnet in network [ '<STR_LIT>' ] <EOL> if subnet [ '<STR_LIT:version>' ] == <NUM_LIT:4> ] <EOL> v6_subnets = [ subnet for subnet in network [ '<STR_LIT>' ] <EOL> if subnet [ '<STR_LIT:version>' ] == <NUM_LIT:6> ] <EOL> routes = [ convert_route ( route ) for subnet in network [ '<STR_LIT>' ] <EOL> for route in subnet [ '<STR_LIT>' ] ] <EOL> dns = [ get_ip ( ip ) for subnet in network [ '<STR_LIT>' ] <EOL> for ip in subnet [ '<STR_LIT>' ] ] <EOL> info_dict = { '<STR_LIT:label>' : network [ '<STR_LIT:label>' ] , <EOL> '<STR_LIT>' : vif [ '<STR_LIT:address>' ] } <EOL> if v4_subnets : <EOL> info_dict [ '<STR_LIT>' ] = get_ip ( v4_subnets [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) <EOL> info_dict [ '<STR_LIT>' ] = str ( v4_subnets [ <NUM_LIT:0> ] . as_netaddr ( ) . broadcast ) <EOL> info_dict [ '<STR_LIT>' ] = [ fixed_ip_dict ( ip , subnet ) <EOL> for subnet in v4_subnets <EOL> for ip in subnet [ '<STR_LIT>' ] ] <EOL> if v6_subnets : <EOL> info_dict [ '<STR_LIT>' ] = get_ip ( v6_subnets [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) <EOL> info_dict [ '<STR_LIT>' ] = [ fixed_ip_dict ( ip , subnet ) <EOL> for subnet in v6_subnets <EOL> for ip in subnet [ '<STR_LIT>' ] ] <EOL> if routes : <EOL> info_dict [ '<STR_LIT>' ] = routes <EOL> if dns : <EOL> info_dict [ '<STR_LIT>' ] = list ( set ( dns ) ) <EOL> return info_dict <EOL> def inject_network_info ( self , instance , network_info , vm_ref = None ) : <EOL> """<STR_LIT>""" <EOL> vm_ref = vm_ref or self . _get_vm_opaque_ref ( instance ) <EOL> LOG . debug ( "<STR_LIT>" , instance = instance ) <EOL> @ utils . synchronized ( '<STR_LIT>' + instance [ '<STR_LIT>' ] ) <EOL> def update_nwinfo ( ) : <EOL> for vif in network_info : <EOL> xs_data = self . _vif_xenstore_data ( vif ) <EOL> location = ( '<STR_LIT>' % <EOL> vif [ '<STR_LIT:address>' ] . replace ( '<STR_LIT::>' , '<STR_LIT>' ) ) <EOL> self . _add_to_param_xenstore ( vm_ref , <EOL> location , <EOL> jsonutils . dumps ( xs_data ) ) <EOL> try : <EOL> self . _write_to_xenstore ( instance , location , xs_data , <EOL> vm_ref = vm_ref ) <EOL> except exception . InstanceNotFound : <EOL> pass <EOL> update_nwinfo ( ) <EOL> def _create_vifs ( self , instance , vm_ref , network_info ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( "<STR_LIT>" , instance = instance ) <EOL> self . _session . call_xenapi ( "<STR_LIT>" , vm_ref ) <EOL> for device , vif in enumerate ( network_info ) : <EOL> LOG . debug ( '<STR_LIT>' , vif , instance = instance ) <EOL> self . vif_driver . plug ( instance , vif , vm_ref = vm_ref , device = device ) <EOL> def plug_vifs ( self , instance , network_info ) : <EOL> """<STR_LIT>""" <EOL> for device , vif in enumerate ( network_info ) : <EOL> self . vif_driver . plug ( instance , vif , device = device ) <EOL> def unplug_vifs ( self , instance , network_info , vm_ref ) : <EOL> if network_info : <EOL> for vif in network_info : <EOL> self . vif_driver . unplug ( instance , vif , vm_ref ) <EOL> def reset_network ( self , instance , rescue = False ) : <EOL> """<STR_LIT>""" <EOL> if self . agent_enabled ( instance ) : <EOL> vm_ref = self . _get_vm_opaque_ref ( instance ) <EOL> agent = self . _get_agent ( instance , vm_ref ) <EOL> self . _inject_hostname ( instance , vm_ref , rescue ) <EOL> agent . resetnetwork ( ) <EOL> self . _remove_hostname ( instance , vm_ref ) <EOL> else : <EOL> raise NotImplementedError ( ) <EOL> def _inject_hostname ( self , instance , vm_ref , rescue ) : <EOL> """<STR_LIT>""" <EOL> hostname = instance [ '<STR_LIT>' ] <EOL> if rescue : <EOL> hostname = '<STR_LIT>' % hostname <EOL> if instance [ '<STR_LIT>' ] == "<STR_LIT>" : <EOL> hostname = hostname [ : <NUM_LIT:15> ] <EOL> LOG . debug ( "<STR_LIT>" , hostname , <EOL> instance = instance ) <EOL> @ utils . synchronized ( '<STR_LIT>' + instance [ '<STR_LIT>' ] ) <EOL> def update_hostname ( ) : <EOL> self . _add_to_param_xenstore ( vm_ref , '<STR_LIT>' , hostname ) <EOL> update_hostname ( ) <EOL> def _remove_hostname ( self , instance , vm_ref ) : <EOL> LOG . debug ( "<STR_LIT>" , instance = instance ) <EOL> @ utils . synchronized ( '<STR_LIT>' + instance [ '<STR_LIT>' ] ) <EOL> def update_hostname ( ) : <EOL> self . _remove_from_param_xenstore ( vm_ref , '<STR_LIT>' ) <EOL> update_hostname ( ) <EOL> def _write_to_xenstore ( self , instance , path , value , vm_ref = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _make_plugin_call ( '<STR_LIT>' , '<STR_LIT>' , instance , <EOL> vm_ref = vm_ref , path = path , <EOL> value = jsonutils . dumps ( value ) ) <EOL> def _read_from_xenstore ( self , instance , path , ignore_missing_path = True , <EOL> vm_ref = None ) : <EOL> """<STR_LIT>""" <EOL> if ignore_missing_path : <EOL> ignore_missing = '<STR_LIT:True>' <EOL> else : <EOL> ignore_missing = '<STR_LIT:False>' <EOL> return self . _make_plugin_call ( '<STR_LIT>' , '<STR_LIT>' , instance , <EOL> vm_ref = vm_ref , path = path , <EOL> ignore_missing_path = ignore_missing ) <EOL> def _delete_from_xenstore ( self , instance , path , vm_ref = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _make_plugin_call ( '<STR_LIT>' , '<STR_LIT>' , instance , <EOL> vm_ref = vm_ref , path = path ) <EOL> def _make_plugin_call ( self , plugin , method , instance = None , vm_ref = None , <EOL> ** addl_args ) : <EOL> """<STR_LIT>""" <EOL> args = { } <EOL> if instance or vm_ref : <EOL> args [ '<STR_LIT>' ] = self . _get_dom_id ( instance , vm_ref ) <EOL> args . update ( addl_args ) <EOL> try : <EOL> return self . _session . call_plugin ( plugin , method , args ) <EOL> except self . _session . XenAPI . Failure as e : <EOL> err_msg = e . details [ - <NUM_LIT:1> ] . splitlines ( ) [ - <NUM_LIT:1> ] <EOL> if '<STR_LIT>' in err_msg : <EOL> LOG . error ( _LE ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> { '<STR_LIT>' : method , '<STR_LIT:args>' : args } , instance = instance ) <EOL> return { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:message>' : err_msg } <EOL> elif '<STR_LIT>' in err_msg : <EOL> LOG . error ( _LE ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> { '<STR_LIT>' : method , '<STR_LIT:args>' : args } , instance = instance ) <EOL> return { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:message>' : err_msg } <EOL> else : <EOL> LOG . error ( _LE ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> { '<STR_LIT>' : method , '<STR_LIT:args>' : args , '<STR_LIT:e>' : e } , <EOL> instance = instance ) <EOL> return { '<STR_LIT>' : '<STR_LIT:error>' , '<STR_LIT:message>' : err_msg } <EOL> def _get_dom_id ( self , instance = None , vm_ref = None , check_rescue = False ) : <EOL> vm_ref = vm_ref or self . _get_vm_opaque_ref ( instance , check_rescue ) <EOL> domid = self . _session . call_xenapi ( "<STR_LIT>" , vm_ref ) <EOL> if not domid or domid == - <NUM_LIT:1> : <EOL> raise exception . InstanceNotFound ( instance_id = instance [ '<STR_LIT:name>' ] ) <EOL> return domid <EOL> def _get_last_dom_id ( self , instance = None , vm_ref = None , check_rescue = False ) : <EOL> vm_ref = vm_ref or self . _get_vm_opaque_ref ( instance , check_rescue ) <EOL> other_config = self . _session . call_xenapi ( "<STR_LIT>" , vm_ref ) <EOL> if '<STR_LIT>' not in other_config : <EOL> raise exception . InstanceNotFound ( instance_id = instance [ '<STR_LIT:name>' ] ) <EOL> return other_config [ '<STR_LIT>' ] <EOL> def _add_to_param_xenstore ( self , vm_ref , key , val ) : <EOL> """<STR_LIT>""" <EOL> self . _remove_from_param_xenstore ( vm_ref , key ) <EOL> self . _session . call_xenapi ( '<STR_LIT>' , vm_ref , key , val ) <EOL> def _remove_from_param_xenstore ( self , vm_ref , key ) : <EOL> """<STR_LIT>""" <EOL> self . _session . call_xenapi ( '<STR_LIT>' , vm_ref , key ) <EOL> def refresh_security_group_rules ( self , security_group_id ) : <EOL> """<STR_LIT>""" <EOL> self . firewall_driver . refresh_security_group_rules ( security_group_id ) <EOL> def refresh_instance_security_rules ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> self . firewall_driver . refresh_instance_security_rules ( instance ) <EOL> def unfilter_instance ( self , instance_ref , network_info ) : <EOL> """<STR_LIT>""" <EOL> self . firewall_driver . unfilter_instance ( instance_ref , <EOL> network_info = network_info ) <EOL> def _get_host_uuid_from_aggregate ( self , context , hostname ) : <EOL> aggregate_list = objects . AggregateList . get_by_host ( <EOL> context , CONF . host , key = pool_states . POOL_FLAG ) <EOL> reason = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % hostname <EOL> if len ( aggregate_list ) == <NUM_LIT:0> : <EOL> raise exception . MigrationPreCheckError ( reason = reason ) <EOL> if hostname not in aggregate_list [ <NUM_LIT:0> ] . metadata : <EOL> raise exception . MigrationPreCheckError ( reason = reason ) <EOL> return aggregate_list [ <NUM_LIT:0> ] . metadata [ hostname ] <EOL> def _ensure_host_in_aggregate ( self , context , hostname ) : <EOL> self . _get_host_uuid_from_aggregate ( context , hostname ) <EOL> def _get_host_opaque_ref ( self , context , hostname ) : <EOL> host_uuid = self . _get_host_uuid_from_aggregate ( context , hostname ) <EOL> return self . _session . call_xenapi ( "<STR_LIT>" , host_uuid ) <EOL> def _migrate_receive ( self , ctxt ) : <EOL> destref = self . _session . host_ref <EOL> expr = '<STR_LIT>' <EOL> pifs = self . _session . call_xenapi ( '<STR_LIT>' , <EOL> expr ) <EOL> if len ( pifs ) != <NUM_LIT:1> : <EOL> msg = _ ( '<STR_LIT>' ) <EOL> raise exception . MigrationPreCheckError ( reason = msg ) <EOL> pifkey = list ( pifs . keys ( ) ) [ <NUM_LIT:0> ] <EOL> if not ( netutils . is_valid_ipv4 ( pifs [ pifkey ] [ '<STR_LIT>' ] ) or <EOL> netutils . is_valid_ipv6 ( pifs [ pifkey ] [ '<STR_LIT>' ] ) ) : <EOL> msg = ( _ ( '<STR_LIT>' ) <EOL> % pifs [ pifkey ] [ '<STR_LIT>' ] ) <EOL> raise exception . MigrationPreCheckError ( reason = msg ) <EOL> nwref = pifs [ list ( pifs . keys ( ) ) [ <NUM_LIT:0> ] ] [ '<STR_LIT>' ] <EOL> try : <EOL> options = { } <EOL> migrate_data = self . _session . call_xenapi ( "<STR_LIT>" , <EOL> destref , <EOL> nwref , <EOL> options ) <EOL> except self . _session . XenAPI . Failure : <EOL> LOG . exception ( _LE ( '<STR_LIT>' ) ) <EOL> msg = _ ( '<STR_LIT>' ) <EOL> raise exception . MigrationPreCheckError ( reason = msg ) <EOL> return migrate_data <EOL> def _get_iscsi_srs ( self , ctxt , instance_ref ) : <EOL> vm_ref = self . _get_vm_opaque_ref ( instance_ref ) <EOL> vbd_refs = self . _session . call_xenapi ( "<STR_LIT>" , vm_ref ) <EOL> iscsi_srs = [ ] <EOL> for vbd_ref in vbd_refs : <EOL> vdi_ref = self . _session . call_xenapi ( "<STR_LIT>" , vbd_ref ) <EOL> sr_ref = self . _session . call_xenapi ( "<STR_LIT>" , vdi_ref ) <EOL> if self . _session . call_xenapi ( "<STR_LIT>" , sr_ref ) == '<STR_LIT>' : <EOL> iscsi_srs . append ( sr_ref ) <EOL> return iscsi_srs <EOL> def check_can_live_migrate_destination ( self , ctxt , instance_ref , <EOL> block_migration = False , <EOL> disk_over_commit = False ) : <EOL> """<STR_LIT>""" <EOL> dest_check_data = objects . XenapiLiveMigrateData ( ) <EOL> if block_migration is None : <EOL> src = instance_ref [ '<STR_LIT:host>' ] <EOL> try : <EOL> self . _ensure_host_in_aggregate ( ctxt , src ) <EOL> except exception . MigrationPreCheckError : <EOL> block_migration = True <EOL> else : <EOL> sr_ref = vm_utils . safe_find_sr ( self . _session ) <EOL> sr_rec = self . _session . get_rec ( '<STR_LIT>' , sr_ref ) <EOL> block_migration = not sr_rec [ "<STR_LIT>" ] <EOL> if block_migration : <EOL> dest_check_data . block_migration = True <EOL> dest_check_data . migrate_send_data = self . _migrate_receive ( ctxt ) <EOL> dest_check_data . destination_sr_ref = vm_utils . safe_find_sr ( <EOL> self . _session ) <EOL> else : <EOL> dest_check_data . block_migration = False <EOL> src = instance_ref [ '<STR_LIT:host>' ] <EOL> self . _ensure_host_in_aggregate ( ctxt , src ) <EOL> return dest_check_data <EOL> def _is_xsm_sr_check_relaxed ( self ) : <EOL> try : <EOL> return self . cached_xsm_sr_relaxed <EOL> except AttributeError : <EOL> config_value = None <EOL> try : <EOL> config_value = self . _make_plugin_call ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> key = '<STR_LIT>' ) <EOL> except Exception : <EOL> LOG . exception ( _LE ( '<STR_LIT>' ) ) <EOL> self . cached_xsm_sr_relaxed = config_value == "<STR_LIT:true>" <EOL> return self . cached_xsm_sr_relaxed <EOL> def check_can_live_migrate_source ( self , ctxt , instance_ref , <EOL> dest_check_data ) : <EOL> """<STR_LIT>""" <EOL> if len ( self . _get_iscsi_srs ( ctxt , instance_ref ) ) > <NUM_LIT:0> : <EOL> if not self . _is_xsm_sr_check_relaxed ( ) : <EOL> raise exception . MigrationError ( reason = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> if not isinstance ( dest_check_data , migrate_data_obj . LiveMigrateData ) : <EOL> obj = objects . XenapiLiveMigrateData ( ) <EOL> obj . from_legacy_dict ( dest_check_data ) <EOL> dest_check_data = obj <EOL> if ( '<STR_LIT>' in dest_check_data and <EOL> dest_check_data . block_migration ) : <EOL> vm_ref = self . _get_vm_opaque_ref ( instance_ref ) <EOL> try : <EOL> self . _call_live_migrate_command ( <EOL> "<STR_LIT>" , vm_ref , dest_check_data ) <EOL> except self . _session . XenAPI . Failure as exc : <EOL> reason = exc . details [ <NUM_LIT:0> ] <EOL> msg = _ ( '<STR_LIT>' ) % reason <EOL> LOG . debug ( msg , exc_info = True ) <EOL> raise exception . MigrationPreCheckError ( reason = msg ) <EOL> return dest_check_data <EOL> def _ensure_pv_driver_info_for_live_migration ( self , instance , vm_ref ) : <EOL> """<STR_LIT>""" <EOL> if self . _pv_driver_version_reported ( instance , vm_ref ) : <EOL> return <EOL> if self . _pv_device_reported ( instance , vm_ref ) : <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> instance = instance ) <EOL> self . _write_fake_pv_version ( instance , vm_ref ) <EOL> else : <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> instance = instance ) <EOL> def _pv_driver_version_reported ( self , instance , vm_ref ) : <EOL> xs_path = "<STR_LIT>" <EOL> major_version = self . _read_from_xenstore ( instance , xs_path , <EOL> vm_ref = vm_ref ) <EOL> LOG . debug ( "<STR_LIT>" , major_version , <EOL> instance = instance ) <EOL> if major_version == '<STR_LIT>' : <EOL> return False <EOL> else : <EOL> return True <EOL> def _pv_device_reported ( self , instance , vm_ref ) : <EOL> vm_rec = self . _session . VM . get_record ( vm_ref ) <EOL> vif_list = [ self . _session . call_xenapi ( "<STR_LIT>" , vrec ) <EOL> for vrec in vm_rec [ '<STR_LIT>' ] ] <EOL> net_devices = [ vif [ '<STR_LIT>' ] for vif in vif_list ] <EOL> for device in net_devices : <EOL> xs_path = "<STR_LIT>" % device <EOL> ret = self . _read_from_xenstore ( instance , xs_path , vm_ref = vm_ref ) <EOL> LOG . debug ( "<STR_LIT>" , <EOL> { '<STR_LIT>' : device , '<STR_LIT>' : ret } , instance = instance ) <EOL> if strutils . is_int_like ( ret ) and int ( ret ) == <NUM_LIT:4> : <EOL> return True <EOL> return False <EOL> def _write_fake_pv_version ( self , instance , vm_ref ) : <EOL> version = self . _session . product_version <EOL> LOG . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , { '<STR_LIT>' : version [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' : version [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' : version [ <NUM_LIT:2> ] } , <EOL> instance = instance ) <EOL> major_ver = "<STR_LIT>" <EOL> self . _write_to_xenstore ( instance , major_ver , version [ <NUM_LIT:0> ] , vm_ref = vm_ref ) <EOL> minor_ver = "<STR_LIT>" <EOL> self . _write_to_xenstore ( instance , minor_ver , version [ <NUM_LIT:1> ] , vm_ref = vm_ref ) <EOL> micro_ver = "<STR_LIT>" <EOL> self . _write_to_xenstore ( instance , micro_ver , version [ <NUM_LIT:2> ] , vm_ref = vm_ref ) <EOL> xs_path = "<STR_LIT>" <EOL> self . _write_to_xenstore ( instance , xs_path , "<STR_LIT:1>" , vm_ref = vm_ref ) <EOL> def _generate_vdi_map ( self , destination_sr_ref , vm_ref , sr_ref = None ) : <EOL> """<STR_LIT>""" <EOL> if sr_ref is None : <EOL> sr_ref = vm_utils . safe_find_sr ( self . _session ) <EOL> vm_vdis = vm_utils . get_instance_vdis_for_sr ( self . _session , <EOL> vm_ref , sr_ref ) <EOL> return { vdi : destination_sr_ref for vdi in vm_vdis } <EOL> def _call_live_migrate_command ( self , command_name , vm_ref , migrate_data ) : <EOL> """<STR_LIT>""" <EOL> migrate_send_data = None <EOL> if '<STR_LIT>' in migrate_data : <EOL> migrate_send_data = migrate_data . migrate_send_data <EOL> if not migrate_send_data : <EOL> raise exception . InvalidParameterValue ( <EOL> '<STR_LIT>' ) <EOL> migrate_send_data = dict ( migrate_send_data ) <EOL> destination_sr_ref = migrate_data . destination_sr_ref <EOL> vdi_map = self . _generate_vdi_map ( destination_sr_ref , vm_ref ) <EOL> sr_uuid_map = None <EOL> if "<STR_LIT>" in migrate_data : <EOL> sr_uuid_map = migrate_data . sr_uuid_map <EOL> if sr_uuid_map : <EOL> for sr_uuid in sr_uuid_map : <EOL> sr_ref = self . _session . call_xenapi ( "<STR_LIT>" , sr_uuid ) <EOL> vdi_map . update ( <EOL> self . _generate_vdi_map ( <EOL> sr_uuid_map [ sr_uuid ] , vm_ref , sr_ref ) ) <EOL> vif_map = { } <EOL> options = { } <EOL> self . _session . call_xenapi ( command_name , vm_ref , <EOL> migrate_send_data , True , <EOL> vdi_map , vif_map , options ) <EOL> def pre_live_migration ( self , context , instance , block_device_info , <EOL> network_info , disk_info , migrate_data ) : <EOL> if migrate_data is None : <EOL> raise exception . InvalidParameterValue ( <EOL> '<STR_LIT>' ) <EOL> migrate_data . sr_uuid_map = self . connect_block_device_volumes ( <EOL> block_device_info ) <EOL> return migrate_data <EOL> def live_migrate ( self , context , instance , destination_hostname , <EOL> post_method , recover_method , block_migration , <EOL> migrate_data = None ) : <EOL> try : <EOL> vm_ref = self . _get_vm_opaque_ref ( instance ) <EOL> try : <EOL> self . _ensure_pv_driver_info_for_live_migration ( instance , <EOL> vm_ref ) <EOL> except Exception as e : <EOL> LOG . warning ( e ) <EOL> if migrate_data is not None : <EOL> ( kernel , ramdisk ) = vm_utils . lookup_kernel_ramdisk ( <EOL> self . _session , vm_ref ) <EOL> migrate_data . kernel_file = kernel <EOL> migrate_data . ramdisk_file = ramdisk <EOL> if migrate_data is not None and migrate_data . block_migration : <EOL> iscsi_srs = self . _get_iscsi_srs ( context , instance ) <EOL> try : <EOL> self . _call_live_migrate_command ( <EOL> "<STR_LIT>" , vm_ref , migrate_data ) <EOL> except self . _session . XenAPI . Failure : <EOL> LOG . exception ( _LE ( '<STR_LIT>' ) ) <EOL> raise exception . MigrationError ( <EOL> reason = _ ( '<STR_LIT>' ) ) <EOL> for sr_ref in iscsi_srs : <EOL> volume_utils . forget_sr ( self . _session , sr_ref ) <EOL> else : <EOL> host_ref = self . _get_host_opaque_ref ( context , <EOL> destination_hostname ) <EOL> self . _session . call_xenapi ( "<STR_LIT>" , vm_ref , <EOL> host_ref , { "<STR_LIT>" : "<STR_LIT:true>" } ) <EOL> post_method ( context , instance , destination_hostname , <EOL> block_migration , migrate_data ) <EOL> except Exception : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> recover_method ( context , instance , destination_hostname , <EOL> block_migration ) <EOL> def post_live_migration ( self , context , instance , migrate_data = None ) : <EOL> if migrate_data is not None : <EOL> vm_utils . destroy_kernel_ramdisk ( self . _session , instance , <EOL> migrate_data . kernel_file , <EOL> migrate_data . ramdisk_file ) <EOL> def post_live_migration_at_destination ( self , context , instance , <EOL> network_info , block_migration , <EOL> block_device_info ) : <EOL> self . _prepare_instance_filter ( instance , network_info ) <EOL> self . firewall_driver . apply_instance_filter ( instance , network_info ) <EOL> vm_utils . create_kernel_and_ramdisk ( context , self . _session , instance , <EOL> instance [ '<STR_LIT:name>' ] ) <EOL> vm_ref = self . _get_vm_opaque_ref ( instance ) <EOL> vm_utils . strip_base_mirror_from_vdis ( self . _session , vm_ref ) <EOL> def rollback_live_migration_at_destination ( self , instance , <EOL> block_device_info ) : <EOL> bdms = block_device_info [ '<STR_LIT>' ] or [ ] <EOL> for bdm in bdms : <EOL> conn_data = bdm [ '<STR_LIT>' ] [ '<STR_LIT:data>' ] <EOL> uuid , label , params = volume_utils . parse_sr_info ( conn_data ) <EOL> try : <EOL> sr_ref = volume_utils . find_sr_by_uuid ( self . _session , <EOL> uuid ) <EOL> if sr_ref : <EOL> volume_utils . forget_sr ( self . _session , sr_ref ) <EOL> except Exception : <EOL> LOG . exception ( _LE ( '<STR_LIT>' ) , <EOL> params [ '<STR_LIT:id>' ] , instance = instance ) <EOL> def get_per_instance_usage ( self ) : <EOL> """<STR_LIT>""" <EOL> usage = { } <EOL> def _is_active ( vm_rec ) : <EOL> power_state = vm_rec [ '<STR_LIT>' ] . lower ( ) <EOL> return power_state in [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def _get_uuid ( vm_rec ) : <EOL> other_config = vm_rec [ '<STR_LIT>' ] <EOL> return other_config . get ( '<STR_LIT>' , None ) <EOL> for vm_ref , vm_rec in vm_utils . list_vms ( self . _session ) : <EOL> uuid = _get_uuid ( vm_rec ) <EOL> if _is_active ( vm_rec ) and uuid is not None : <EOL> memory_mb = int ( vm_rec [ '<STR_LIT>' ] ) / units . Mi <EOL> usage [ uuid ] = { '<STR_LIT>' : memory_mb , '<STR_LIT>' : uuid } <EOL> return usage <EOL> def connect_block_device_volumes ( self , block_device_info ) : <EOL> sr_uuid_map = { } <EOL> try : <EOL> if block_device_info is not None : <EOL> for block_device_map in block_device_info [ <EOL> '<STR_LIT>' ] : <EOL> sr_uuid , _ = self . _volumeops . connect_volume ( <EOL> block_device_map [ '<STR_LIT>' ] ) <EOL> sr_ref = self . _session . call_xenapi ( '<STR_LIT>' , <EOL> sr_uuid ) <EOL> sr_uuid_map [ sr_uuid ] = sr_ref <EOL> except Exception : <EOL> with excutils . save_and_reraise_exception ( ) : <EOL> for sr_ref in six . itervalues ( sr_uuid_map ) : <EOL> volume_utils . forget_sr ( self . _session , sr_ref ) <EOL> return sr_uuid_map </s>
