<s> """<STR_LIT>""" <EOL> import time <EOL> from eventlet import event <EOL> from nova import context <EOL> from nova import db <EOL> from nova import exception <EOL> from nova import flags <EOL> from nova import log as logging <EOL> from nova import utils <EOL> from nova . virt import driver <EOL> from nova . virt . vmwareapi import error_util <EOL> from nova . virt . vmwareapi import vim <EOL> from nova . virt . vmwareapi import vim_util <EOL> from nova . virt . vmwareapi . vmops import VMWareVMOps <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> None , <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> , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> flags . DEFINE_float ( '<STR_LIT>' , <EOL> <NUM_LIT:10> , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> flags . DEFINE_string ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> TIME_BETWEEN_API_CALL_RETRIES = <NUM_LIT> <EOL> class Failure ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , details ) : <EOL> self . details = details <EOL> def __str__ ( self ) : <EOL> return str ( self . details ) <EOL> def get_connection ( _ ) : <EOL> """<STR_LIT>""" <EOL> host_ip = FLAGS . vmwareapi_host_ip <EOL> host_username = FLAGS . vmwareapi_host_username <EOL> host_password = FLAGS . vmwareapi_host_password <EOL> api_retry_count = FLAGS . vmwareapi_api_retry_count <EOL> if not host_ip or host_username is None or host_password is None : <EOL> raise Exception ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> return VMWareESXConnection ( host_ip , host_username , host_password , <EOL> api_retry_count ) <EOL> class VMWareESXConnection ( driver . ComputeDriver ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , host_ip , host_username , host_password , <EOL> api_retry_count , scheme = "<STR_LIT>" ) : <EOL> super ( VMWareESXConnection , self ) . __init__ ( ) <EOL> session = VMWareAPISession ( host_ip , host_username , host_password , <EOL> api_retry_count , scheme = scheme ) <EOL> self . _vmops = VMWareVMOps ( session ) <EOL> def init_host ( self , host ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def list_instances ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _vmops . list_instances ( ) <EOL> def spawn ( self , context , instance , network_info , <EOL> block_device_mapping = None ) : <EOL> """<STR_LIT>""" <EOL> self . _vmops . spawn ( context , instance , network_info ) <EOL> def snapshot ( self , context , instance , name ) : <EOL> """<STR_LIT>""" <EOL> self . _vmops . snapshot ( context , instance , name ) <EOL> def reboot ( self , instance , network_info ) : <EOL> """<STR_LIT>""" <EOL> self . _vmops . reboot ( instance , network_info ) <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 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 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_info ( 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 attach_volume ( self , instance_name , device_path , mountpoint ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def detach_volume ( self , instance_name , mountpoint ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def get_console_pool_info ( self , console_type ) : <EOL> """<STR_LIT>""" <EOL> return { '<STR_LIT:address>' : FLAGS . vmwareapi_host_ip , <EOL> '<STR_LIT:username>' : FLAGS . vmwareapi_host_username , <EOL> '<STR_LIT:password>' : FLAGS . vmwareapi_host_password } <EOL> def update_available_resource ( self , ctxt , host ) : <EOL> """<STR_LIT>""" <EOL> return <EOL> def host_power_action ( self , host , action ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def set_host_enabled ( self , host , enabled ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def plug_vifs ( self , instance , network_info ) : <EOL> """<STR_LIT>""" <EOL> self . _vmops . plug_vifs ( instance , network_info ) <EOL> class VMWareAPISession ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , host_ip , host_username , host_password , <EOL> api_retry_count , scheme = "<STR_LIT>" ) : <EOL> self . _host_ip = host_ip <EOL> self . _host_username = host_username <EOL> self . _host_password = host_password <EOL> self . api_retry_count = api_retry_count <EOL> self . _scheme = scheme <EOL> self . _session_id = None <EOL> self . vim = None <EOL> self . _create_session ( ) <EOL> def _get_vim_object ( self ) : <EOL> """<STR_LIT>""" <EOL> return vim . Vim ( protocol = self . _scheme , host = self . _host_ip ) <EOL> def _create_session ( self ) : <EOL> """<STR_LIT>""" <EOL> while True : <EOL> try : <EOL> self . vim = self . _get_vim_object ( ) <EOL> session = self . vim . Login ( <EOL> self . vim . get_service_content ( ) . sessionManager , <EOL> userName = self . _host_username , <EOL> password = self . _host_password ) <EOL> if self . _session_id : <EOL> try : <EOL> self . vim . TerminateSession ( <EOL> self . vim . get_service_content ( ) . sessionManager , <EOL> sessionId = [ self . _session_id ] ) <EOL> except Exception , excep : <EOL> LOG . debug ( excep ) <EOL> self . _session_id = session . key <EOL> return <EOL> except Exception , excep : <EOL> LOG . critical ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % excep ) <EOL> raise exception . Error ( excep ) <EOL> def __del__ ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . vim . Logout ( self . vim . get_service_content ( ) . sessionManager ) <EOL> except Exception , excep : <EOL> LOG . debug ( excep ) <EOL> def _is_vim_object ( self , module ) : <EOL> """<STR_LIT>""" <EOL> return isinstance ( module , vim . Vim ) <EOL> def _call_method ( self , module , method , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> args = list ( args ) <EOL> retry_count = <NUM_LIT:0> <EOL> exc = None <EOL> last_fault_list = [ ] <EOL> while True : <EOL> try : <EOL> if not self . _is_vim_object ( module ) : <EOL> if retry_count > <NUM_LIT:0> : <EOL> args = args [ <NUM_LIT:1> : ] <EOL> args = [ self . vim ] + args <EOL> retry_count += <NUM_LIT:1> <EOL> temp_module = module <EOL> for method_elem in method . split ( "<STR_LIT:.>" ) : <EOL> temp_module = getattr ( temp_module , method_elem ) <EOL> return temp_module ( * args , ** kwargs ) <EOL> except error_util . VimFaultException , excep : <EOL> exc = excep <EOL> if error_util . FAULT_NOT_AUTHENTICATED in excep . fault_list : <EOL> if error_util . FAULT_NOT_AUTHENTICATED in last_fault_list : <EOL> return [ ] <EOL> last_fault_list = excep . fault_list <EOL> self . _create_session ( ) <EOL> else : <EOL> break <EOL> except error_util . SessionOverLoadException , excep : <EOL> exc = excep <EOL> except Exception , excep : <EOL> exc = excep <EOL> break <EOL> if retry_count > self . api_retry_count : <EOL> break <EOL> time . sleep ( TIME_BETWEEN_API_CALL_RETRIES ) <EOL> LOG . critical ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % exc ) <EOL> raise <EOL> def _get_vim ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . vim is None : <EOL> self . _create_session ( ) <EOL> return self . vim <EOL> def _wait_for_task ( self , instance_id , task_ref ) : <EOL> """<STR_LIT>""" <EOL> done = event . Event ( ) <EOL> loop = utils . LoopingCall ( self . _poll_task , instance_id , task_ref , <EOL> done ) <EOL> loop . start ( FLAGS . vmwareapi_task_poll_interval , now = True ) <EOL> ret_val = done . wait ( ) <EOL> loop . stop ( ) <EOL> return ret_val <EOL> def _poll_task ( self , instance_id , task_ref , done ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> task_info = self . _call_method ( vim_util , "<STR_LIT>" , <EOL> task_ref , "<STR_LIT>" , "<STR_LIT:info>" ) <EOL> task_name = task_info . name <EOL> action = dict ( <EOL> instance_id = int ( instance_id ) , <EOL> action = task_name [ <NUM_LIT:0> : <NUM_LIT:255> ] , <EOL> error = None ) <EOL> if task_info . state in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> return <EOL> elif task_info . state == '<STR_LIT:success>' : <EOL> LOG . debug ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % locals ( ) ) <EOL> done . send ( "<STR_LIT:success>" ) <EOL> else : <EOL> error_info = str ( task_info . error . localizedMessage ) <EOL> action [ "<STR_LIT:error>" ] = error_info <EOL> LOG . warn ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % locals ( ) ) <EOL> done . send_exception ( exception . Error ( error_info ) ) <EOL> db . instance_action_create ( context . get_admin_context ( ) , action ) <EOL> except Exception , excep : <EOL> LOG . warn ( _ ( "<STR_LIT>" ) % excep ) <EOL> done . send_exception ( excep ) </s>
