<s> """<STR_LIT>""" <EOL> import json <EOL> import random <EOL> import sys <EOL> import urlparse <EOL> import xmlrpclib <EOL> from eventlet import event <EOL> from eventlet import tpool <EOL> from eventlet import timeout <EOL> from nova import context <EOL> from nova import db <EOL> from nova import exception <EOL> from nova import utils <EOL> from nova import flags <EOL> from nova import log as logging <EOL> from nova . virt import driver <EOL> from nova . virt . xenapi import vm_utils <EOL> from nova . virt . xenapi . vmops import VMOps <EOL> from nova . virt . xenapi . volumeops import VolumeOps <EOL> LOG = logging . getLogger ( "<STR_LIT>" ) <EOL> FLAGS = flags . FLAGS <EOL> flags . DEFINE_string ( '<STR_LIT>' , <EOL> None , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> flags . DEFINE_string ( '<STR_LIT>' , <EOL> '<STR_LIT:root>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> flags . DEFINE_string ( '<STR_LIT>' , <EOL> None , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> flags . DEFINE_float ( '<STR_LIT>' , <EOL> <NUM_LIT:0.5> , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> flags . DEFINE_float ( '<STR_LIT>' , <EOL> <NUM_LIT> , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> flags . DEFINE_integer ( '<STR_LIT>' , <EOL> <NUM_LIT:5> , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> flags . DEFINE_string ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> flags . DEFINE_string ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> flags . DEFINE_string ( '<STR_LIT>' , <EOL> None , <EOL> '<STR_LIT>' ) <EOL> flags . DEFINE_string ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> flags . DEFINE_string ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> flags . DEFINE_bool ( '<STR_LIT>' , False , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> flags . DEFINE_string ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> flags . DEFINE_integer ( '<STR_LIT>' , <EOL> <NUM_LIT:10> , <EOL> '<STR_LIT>' ) <EOL> def get_connection ( _ ) : <EOL> """<STR_LIT>""" <EOL> url = FLAGS . xenapi_connection_url <EOL> username = FLAGS . xenapi_connection_username <EOL> password = FLAGS . xenapi_connection_password <EOL> if not url or password is None : <EOL> raise Exception ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> return XenAPIConnection ( url , username , password ) <EOL> class XenAPIConnection ( driver . ComputeDriver ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , url , user , pw ) : <EOL> super ( XenAPIConnection , self ) . __init__ ( ) <EOL> self . _session = XenAPISession ( url , user , pw ) <EOL> self . _vmops = VMOps ( self . _session ) <EOL> self . _volumeops = VolumeOps ( self . _session ) <EOL> self . _host_state = None <EOL> @ property <EOL> def HostState ( self ) : <EOL> if not self . _host_state : <EOL> self . _host_state = HostState ( self . _session ) <EOL> return self . _host_state <EOL> def init_host ( self , host ) : <EOL> pass <EOL> def list_instances ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _vmops . list_instances ( ) <EOL> def list_instances_detail ( self ) : <EOL> return self . _vmops . list_instances_detail ( ) <EOL> def spawn ( self , context , instance , <EOL> network_info = None , block_device_info = None ) : <EOL> """<STR_LIT>""" <EOL> self . _vmops . spawn ( context , instance , network_info ) <EOL> def revert_migration ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> self . _vmops . revert_migration ( instance ) <EOL> def finish_migration ( self , context , instance , disk_info , network_info , <EOL> resize_instance = False ) : <EOL> """<STR_LIT>""" <EOL> self . _vmops . finish_migration ( context , instance , disk_info , <EOL> network_info , resize_instance ) <EOL> def snapshot ( self , context , instance , image_id ) : <EOL> """<STR_LIT>""" <EOL> self . _vmops . snapshot ( context , instance , image_id ) <EOL> def reboot ( self , instance , network_info ) : <EOL> """<STR_LIT>""" <EOL> self . _vmops . reboot ( instance ) <EOL> def set_admin_password ( self , instance , new_pass ) : <EOL> """<STR_LIT>""" <EOL> self . _vmops . set_admin_password ( instance , new_pass ) <EOL> def inject_file ( self , instance , b64_path , b64_contents ) : <EOL> """<STR_LIT>""" <EOL> self . _vmops . inject_file ( instance , b64_path , b64_contents ) <EOL> def destroy ( self , instance , network_info , cleanup = True ) : <EOL> """<STR_LIT>""" <EOL> self . _vmops . destroy ( instance , network_info ) <EOL> def pause ( self , instance , callback ) : <EOL> """<STR_LIT>""" <EOL> self . _vmops . pause ( instance , callback ) <EOL> def unpause ( self , instance , callback ) : <EOL> """<STR_LIT>""" <EOL> self . _vmops . unpause ( instance , callback ) <EOL> def migrate_disk_and_power_off ( self , instance , dest ) : <EOL> """<STR_LIT>""" <EOL> return self . _vmops . migrate_disk_and_power_off ( instance , dest ) <EOL> def suspend ( self , instance , callback ) : <EOL> """<STR_LIT>""" <EOL> self . _vmops . suspend ( instance , callback ) <EOL> def resume ( self , instance , callback ) : <EOL> """<STR_LIT>""" <EOL> self . _vmops . resume ( instance , callback ) <EOL> def rescue ( self , context , instance , _callback , network_info ) : <EOL> """<STR_LIT>""" <EOL> self . _vmops . rescue ( context , instance , _callback , network_info ) <EOL> def unrescue ( self , instance , _callback , network_info ) : <EOL> """<STR_LIT>""" <EOL> self . _vmops . unrescue ( instance , _callback ) <EOL> def poll_rescued_instances ( self , timeout ) : <EOL> """<STR_LIT>""" <EOL> self . _vmops . poll_rescued_instances ( timeout ) <EOL> def reset_network ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> self . _vmops . reset_network ( instance ) <EOL> def inject_network_info ( self , instance , network_info ) : <EOL> """<STR_LIT>""" <EOL> self . _vmops . inject_network_info ( instance , network_info ) <EOL> def plug_vifs ( self , instance_ref , network_info ) : <EOL> self . _vmops . plug_vifs ( instance_ref , network_info ) <EOL> def get_info ( self , instance_id ) : <EOL> """<STR_LIT>""" <EOL> return self . _vmops . get_info ( instance_id ) <EOL> def get_diagnostics ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> return self . _vmops . get_diagnostics ( instance ) <EOL> def get_console_output ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> return self . _vmops . get_console_output ( instance ) <EOL> def get_ajax_console ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> return self . _vmops . get_ajax_console ( instance ) <EOL> def get_host_ip_addr ( self ) : <EOL> xs_url = urlparse . urlparse ( FLAGS . xenapi_connection_url ) <EOL> return xs_url . netloc <EOL> def attach_volume ( self , instance_name , device_path , mountpoint ) : <EOL> """<STR_LIT>""" <EOL> return self . _volumeops . attach_volume ( instance_name , <EOL> device_path , <EOL> mountpoint ) <EOL> def detach_volume ( self , instance_name , mountpoint ) : <EOL> """<STR_LIT>""" <EOL> return self . _volumeops . detach_volume ( instance_name , mountpoint ) <EOL> def get_console_pool_info ( self , console_type ) : <EOL> xs_url = urlparse . urlparse ( FLAGS . xenapi_connection_url ) <EOL> return { '<STR_LIT:address>' : xs_url . netloc , <EOL> '<STR_LIT:username>' : FLAGS . xenapi_connection_username , <EOL> '<STR_LIT:password>' : FLAGS . xenapi_connection_password } <EOL> def update_available_resource ( self , ctxt , host ) : <EOL> """<STR_LIT>""" <EOL> return <EOL> def compare_cpu ( self , xml ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> def ensure_filtering_rules_for_instance ( self , instance_ref , network_info ) : <EOL> """<STR_LIT>""" <EOL> return <EOL> def live_migration ( self , context , instance_ref , dest , <EOL> post_method , recover_method , block_migration = False ) : <EOL> """<STR_LIT>""" <EOL> return <EOL> def unfilter_instance ( self , instance_ref , network_info ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> def update_host_status ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . HostState . update_status ( ) <EOL> def get_host_stats ( self , refresh = False ) : <EOL> """<STR_LIT>""" <EOL> return self . HostState . get_host_stats ( refresh = refresh ) <EOL> def host_power_action ( self , host , action ) : <EOL> """<STR_LIT>""" <EOL> if action in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> return self . _vmops . host_power_action ( host , action ) <EOL> else : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise NotImplementedError ( msg ) <EOL> def set_host_enabled ( self , host , enabled ) : <EOL> """<STR_LIT>""" <EOL> return self . _vmops . set_host_enabled ( host , enabled ) <EOL> class XenAPISession ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , url , user , pw ) : <EOL> self . XenAPI = self . get_imported_xenapi ( ) <EOL> self . _session = self . _create_session ( url ) <EOL> exception = self . XenAPI . Failure ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> with timeout . Timeout ( FLAGS . xenapi_login_timeout , exception ) : <EOL> self . _session . login_with_password ( user , pw ) <EOL> def get_imported_xenapi ( self ) : <EOL> """<STR_LIT>""" <EOL> return __import__ ( '<STR_LIT>' ) <EOL> def get_xenapi ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _session . xenapi <EOL> def get_xenapi_host ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _session . xenapi . session . get_this_host ( self . _session . handle ) <EOL> def call_xenapi ( self , method , * args ) : <EOL> """<STR_LIT>""" <EOL> f = self . _session . xenapi <EOL> for m in method . split ( '<STR_LIT:.>' ) : <EOL> f = f . __getattr__ ( m ) <EOL> return tpool . execute ( f , * args ) <EOL> def call_xenapi_request ( self , method , * args ) : <EOL> """<STR_LIT>""" <EOL> f = self . _session . xenapi_request <EOL> return tpool . execute ( f , method , * args ) <EOL> def async_call_plugin ( self , plugin , fn , args ) : <EOL> """<STR_LIT>""" <EOL> return tpool . execute ( self . _unwrap_plugin_exceptions , <EOL> self . _session . xenapi . Async . host . call_plugin , <EOL> self . get_xenapi_host ( ) , plugin , fn , args ) <EOL> def wait_for_task ( self , task , id = None ) : <EOL> """<STR_LIT>""" <EOL> done = event . Event ( ) <EOL> loop = utils . LoopingCall ( f = None ) <EOL> def _poll_task ( ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> name = self . _session . xenapi . task . get_name_label ( task ) <EOL> status = self . _session . xenapi . task . get_status ( task ) <EOL> action = dict ( action = name [ : <NUM_LIT:255> ] , error = None ) <EOL> if id : <EOL> action [ "<STR_LIT>" ] = int ( id ) <EOL> if status == "<STR_LIT>" : <EOL> return <EOL> elif status == "<STR_LIT:success>" : <EOL> result = self . _session . xenapi . task . get_result ( task ) <EOL> LOG . info ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % locals ( ) ) <EOL> done . send ( _parse_xmlrpc_value ( result ) ) <EOL> else : <EOL> error_info = self . _session . xenapi . task . get_error_info ( task ) <EOL> action [ "<STR_LIT:error>" ] = str ( error_info ) <EOL> LOG . warn ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % locals ( ) ) <EOL> done . send_exception ( self . XenAPI . Failure ( error_info ) ) <EOL> if id : <EOL> db . instance_action_create ( context . get_admin_context ( ) , <EOL> action ) <EOL> except self . XenAPI . Failure , exc : <EOL> LOG . warn ( exc ) <EOL> done . send_exception ( * sys . exc_info ( ) ) <EOL> loop . stop ( ) <EOL> loop . f = _poll_task <EOL> loop . start ( FLAGS . xenapi_task_poll_interval , now = True ) <EOL> return done . wait ( ) <EOL> def _create_session ( self , url ) : <EOL> """<STR_LIT>""" <EOL> return self . XenAPI . Session ( url ) <EOL> def _unwrap_plugin_exceptions ( self , func , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return func ( * args , ** kwargs ) <EOL> except self . XenAPI . Failure , exc : <EOL> LOG . debug ( _ ( "<STR_LIT>" ) , exc ) <EOL> if ( len ( exc . details ) == <NUM_LIT:4> and <EOL> exc . details [ <NUM_LIT:0> ] == '<STR_LIT>' and <EOL> exc . details [ <NUM_LIT:2> ] == '<STR_LIT>' ) : <EOL> params = None <EOL> try : <EOL> params = eval ( exc . details [ <NUM_LIT:3> ] ) <EOL> except Exception : <EOL> raise exc <EOL> raise self . XenAPI . Failure ( params ) <EOL> else : <EOL> raise <EOL> except xmlrpclib . ProtocolError , exc : <EOL> LOG . debug ( _ ( "<STR_LIT>" ) , exc ) <EOL> raise <EOL> class HostState ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , session ) : <EOL> super ( HostState , self ) . __init__ ( ) <EOL> self . _session = session <EOL> self . _stats = { } <EOL> self . update_status ( ) <EOL> def get_host_stats ( self , refresh = False ) : <EOL> """<STR_LIT>""" <EOL> if refresh : <EOL> self . update_status ( ) <EOL> return self . _stats <EOL> def update_status ( self ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( _ ( "<STR_LIT>" ) ) <EOL> task_id = random . randint ( - <NUM_LIT> , - <NUM_LIT> ) <EOL> task = self . _session . async_call_plugin ( "<STR_LIT>" , "<STR_LIT>" , { } ) <EOL> task_result = self . _session . wait_for_task ( task , task_id ) <EOL> if not task_result : <EOL> task_result = json . dumps ( "<STR_LIT>" ) <EOL> try : <EOL> data = json . loads ( task_result ) <EOL> except ValueError as e : <EOL> LOG . error ( _ ( "<STR_LIT>" ) % e ) <EOL> return <EOL> try : <EOL> sr_ref = vm_utils . safe_find_sr ( self . _session ) <EOL> except exception . NotFound as e : <EOL> LOG . error ( _ ( "<STR_LIT>" ) % e ) <EOL> return <EOL> sr_rec = self . _session . get_xenapi ( ) . SR . get_record ( sr_ref ) <EOL> total = int ( sr_rec [ "<STR_LIT>" ] ) <EOL> used = int ( sr_rec [ "<STR_LIT>" ] ) <EOL> data [ "<STR_LIT>" ] = total <EOL> data [ "<STR_LIT>" ] = used <EOL> data [ "<STR_LIT>" ] = total - used <EOL> host_memory = data . get ( '<STR_LIT>' , None ) <EOL> if host_memory : <EOL> data [ "<STR_LIT>" ] = host_memory . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> data [ "<STR_LIT>" ] = host_memory . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> data [ "<STR_LIT>" ] = host_memory . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> data [ "<STR_LIT>" ] = host_memory . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> del data [ '<STR_LIT>' ] <EOL> self . _stats = data <EOL> def _parse_xmlrpc_value ( val ) : <EOL> """<STR_LIT>""" <EOL> if not val : <EOL> return val <EOL> x = xmlrpclib . loads ( <EOL> '<STR_LIT>' + <EOL> val + <EOL> '<STR_LIT>' ) <EOL> return x [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] </s>
