<s> """<STR_LIT>""" <EOL> import os <EOL> import time <EOL> from nova import exception <EOL> from nova import flags <EOL> from nova import log as logging <EOL> from nova . compute import power_state <EOL> from nova . virt import driver <EOL> from nova . virt import images <EOL> wmi = None <EOL> FLAGS = flags . FLAGS <EOL> LOG = logging . getLogger ( '<STR_LIT>' ) <EOL> HYPERV_POWER_STATE = { <EOL> <NUM_LIT:3> : power_state . SHUTDOWN , <EOL> <NUM_LIT:2> : power_state . RUNNING , <EOL> <NUM_LIT> : power_state . PAUSED , <EOL> } <EOL> REQ_POWER_STATE = { <EOL> '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : <NUM_LIT:3> , <EOL> '<STR_LIT>' : <NUM_LIT:10> , <EOL> '<STR_LIT>' : <NUM_LIT:11> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> } <EOL> WMI_JOB_STATUS_STARTED = <NUM_LIT> <EOL> WMI_JOB_STATE_RUNNING = <NUM_LIT:4> <EOL> WMI_JOB_STATE_COMPLETED = <NUM_LIT:7> <EOL> def get_connection ( _ ) : <EOL> global wmi <EOL> if wmi is None : <EOL> wmi = __import__ ( '<STR_LIT>' ) <EOL> return HyperVConnection ( ) <EOL> class HyperVConnection ( driver . ComputeDriver ) : <EOL> def __init__ ( self ) : <EOL> super ( HyperVConnection , self ) . __init__ ( ) <EOL> self . _conn = wmi . WMI ( moniker = '<STR_LIT>' ) <EOL> self . _cim_conn = wmi . WMI ( moniker = '<STR_LIT>' ) <EOL> def init_host ( self , host ) : <EOL> LOG . debug ( _ ( '<STR_LIT>' ) ) <EOL> pass <EOL> def list_instances ( self ) : <EOL> """<STR_LIT>""" <EOL> vms = [ v . ElementName for v in self . _conn . Msvm_ComputerSystem ( [ '<STR_LIT>' ] ) ] <EOL> return vms <EOL> def list_instances_detail ( self ) : <EOL> instance_infos = [ ] <EOL> for instance_name in self . list_instances ( ) : <EOL> info = self . get_info ( instance_name ) <EOL> state = info [ '<STR_LIT:state>' ] <EOL> instance_info = driver . InstanceInfo ( instance_name , state ) <EOL> instance_infos . append ( instance_info ) <EOL> return instance_infos <EOL> def spawn ( self , context , instance , <EOL> network_info = None , block_device_info = None ) : <EOL> """<STR_LIT>""" <EOL> vm = self . _lookup ( instance . name ) <EOL> if vm is not None : <EOL> raise exception . InstanceExists ( name = instance . name ) <EOL> base_vhd_filename = os . path . join ( FLAGS . instances_path , <EOL> instance . name ) <EOL> vhdfile = "<STR_LIT>" % ( base_vhd_filename ) <EOL> images . fetch ( instance [ '<STR_LIT>' ] , vhdfile , <EOL> instance [ '<STR_LIT>' ] , instance [ '<STR_LIT>' ] ) <EOL> try : <EOL> self . _create_vm ( instance ) <EOL> self . _create_disk ( instance [ '<STR_LIT:name>' ] , vhdfile ) <EOL> mac_address = None <EOL> if instance [ '<STR_LIT>' ] : <EOL> mac_address = instance [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT:address>' ] <EOL> self . _create_nic ( instance [ '<STR_LIT:name>' ] , mac_address ) <EOL> LOG . debug ( _ ( '<STR_LIT>' ) , instance . name ) <EOL> self . _set_vm_state ( instance [ '<STR_LIT:name>' ] , '<STR_LIT>' ) <EOL> LOG . info ( _ ( '<STR_LIT>' ) , instance . name ) <EOL> except Exception as exn : <EOL> LOG . exception ( _ ( '<STR_LIT>' ) , exn ) <EOL> self . destroy ( instance ) <EOL> def _create_vm ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> vs_man_svc = self . _conn . Msvm_VirtualSystemManagementService ( ) [ <NUM_LIT:0> ] <EOL> vs_gs_data = self . _conn . Msvm_VirtualSystemGlobalSettingData . new ( ) <EOL> vs_gs_data . ElementName = instance [ '<STR_LIT:name>' ] <EOL> ( job , ret_val ) = vs_man_svc . DefineVirtualSystem ( <EOL> [ ] , None , vs_gs_data . GetText_ ( <NUM_LIT:1> ) ) [ <NUM_LIT:1> : ] <EOL> if ret_val == WMI_JOB_STATUS_STARTED : <EOL> success = self . _check_job_status ( job ) <EOL> else : <EOL> success = ( ret_val == <NUM_LIT:0> ) <EOL> if not success : <EOL> raise Exception ( _ ( '<STR_LIT>' ) , instance . name ) <EOL> LOG . debug ( _ ( '<STR_LIT>' ) , instance . name ) <EOL> vm = self . _conn . Msvm_ComputerSystem ( ElementName = instance . name ) [ <NUM_LIT:0> ] <EOL> vmsettings = vm . associators ( <EOL> wmi_result_class = '<STR_LIT>' ) <EOL> vmsetting = [ s for s in vmsettings <EOL> if s . SettingType == <NUM_LIT:3> ] [ <NUM_LIT:0> ] <EOL> memsetting = vmsetting . associators ( <EOL> wmi_result_class = '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> mem = long ( str ( instance [ '<STR_LIT>' ] ) ) <EOL> memsetting . VirtualQuantity = mem <EOL> memsetting . Reservation = mem <EOL> memsetting . Limit = mem <EOL> ( job , ret_val ) = vs_man_svc . ModifyVirtualSystemResources ( <EOL> vm . path_ ( ) , [ memsetting . GetText_ ( <NUM_LIT:1> ) ] ) <EOL> LOG . debug ( _ ( '<STR_LIT>' ) , instance . name ) <EOL> procsetting = vmsetting . associators ( <EOL> wmi_result_class = '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> vcpus = long ( instance [ '<STR_LIT>' ] ) <EOL> procsetting . VirtualQuantity = vcpus <EOL> procsetting . Reservation = vcpus <EOL> procsetting . Limit = <NUM_LIT> <EOL> ( job , ret_val ) = vs_man_svc . ModifyVirtualSystemResources ( <EOL> vm . path_ ( ) , [ procsetting . GetText_ ( <NUM_LIT:1> ) ] ) <EOL> LOG . debug ( _ ( '<STR_LIT>' ) , instance . name ) <EOL> def _create_disk ( self , vm_name , vhdfile ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) % locals ( ) ) <EOL> vms = self . _conn . MSVM_ComputerSystem ( ElementName = vm_name ) <EOL> vm = vms [ <NUM_LIT:0> ] <EOL> vmsettings = vm . associators ( <EOL> wmi_result_class = '<STR_LIT>' ) <EOL> rasds = vmsettings [ <NUM_LIT:0> ] . associators ( <EOL> wmi_result_class = '<STR_LIT>' ) <EOL> ctrller = [ r for r in rasds <EOL> if r . ResourceSubType == '<STR_LIT>' and r . Address == "<STR_LIT:0>" ] <EOL> diskdflt = self . _conn . query ( <EOL> "<STR_LIT>" ) [ <NUM_LIT:0> ] <EOL> diskdrive = self . _clone_wmi_obj ( <EOL> '<STR_LIT>' , diskdflt ) <EOL> diskdrive . Parent = ctrller [ <NUM_LIT:0> ] . path_ ( ) <EOL> diskdrive . Address = <NUM_LIT:0> <EOL> new_resources = self . _add_virt_resource ( diskdrive , vm ) <EOL> if new_resources is None : <EOL> raise Exception ( _ ( '<STR_LIT>' ) , <EOL> vm_name ) <EOL> diskdrive_path = new_resources [ <NUM_LIT:0> ] <EOL> LOG . debug ( _ ( '<STR_LIT>' ) , diskdrive_path ) <EOL> vhddefault = self . _conn . query ( <EOL> "<STR_LIT>" ) [ <NUM_LIT:0> ] <EOL> vhddisk = self . _clone_wmi_obj ( <EOL> '<STR_LIT>' , vhddefault ) <EOL> vhddisk . Parent = diskdrive_path <EOL> vhddisk . Connection = [ vhdfile ] <EOL> new_resources = self . _add_virt_resource ( vhddisk , vm ) <EOL> if new_resources is None : <EOL> raise Exception ( _ ( '<STR_LIT>' ) , <EOL> vm_name ) <EOL> LOG . info ( _ ( '<STR_LIT>' ) , vm_name ) <EOL> def _create_nic ( self , vm_name , mac ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( _ ( '<STR_LIT>' ) , vm_name ) <EOL> vms = self . _conn . Msvm_ComputerSystem ( ElementName = vm_name ) <EOL> extswitch = self . _find_external_network ( ) <EOL> vm = vms [ <NUM_LIT:0> ] <EOL> switch_svc = self . _conn . Msvm_VirtualSwitchManagementService ( ) [ <NUM_LIT:0> ] <EOL> emulatednics_data = self . _conn . Msvm_EmulatedEthernetPortSettingData ( ) <EOL> default_nic_data = [ n for n in emulatednics_data <EOL> if n . InstanceID . rfind ( '<STR_LIT>' ) > <NUM_LIT:0> ] <EOL> new_nic_data = self . _clone_wmi_obj ( <EOL> '<STR_LIT>' , <EOL> default_nic_data [ <NUM_LIT:0> ] ) <EOL> ( new_port , ret_val ) = switch_svc . CreateSwitchPort ( vm_name , vm_name , <EOL> "<STR_LIT>" , extswitch . path_ ( ) ) <EOL> if ret_val != <NUM_LIT:0> : <EOL> LOG . error ( _ ( '<STR_LIT>' ) ) <EOL> raise Exception ( _ ( '<STR_LIT>' ) , <EOL> vm_name ) <EOL> ext_path = extswitch . path_ ( ) <EOL> LOG . debug ( _ ( "<STR_LIT>" ) <EOL> % locals ( ) ) <EOL> new_nic_data . Connection = [ new_port ] <EOL> new_nic_data . ElementName = vm_name + '<STR_LIT>' <EOL> new_nic_data . Address = '<STR_LIT>' . join ( mac . split ( '<STR_LIT::>' ) ) <EOL> new_nic_data . StaticMacAddress = '<STR_LIT>' <EOL> new_resources = self . _add_virt_resource ( new_nic_data , vm ) <EOL> if new_resources is None : <EOL> raise Exception ( _ ( '<STR_LIT>' ) , <EOL> vm_name ) <EOL> LOG . info ( _ ( "<STR_LIT>" ) , vm_name ) <EOL> def _add_virt_resource ( self , res_setting_data , target_vm ) : <EOL> """<STR_LIT>""" <EOL> vs_man_svc = self . _conn . Msvm_VirtualSystemManagementService ( ) [ <NUM_LIT:0> ] <EOL> ( job , new_resources , ret_val ) = vs_man_svc . AddVirtualSystemResources ( [ res_setting_data . GetText_ ( <NUM_LIT:1> ) ] , <EOL> target_vm . path_ ( ) ) <EOL> success = True <EOL> if ret_val == WMI_JOB_STATUS_STARTED : <EOL> success = self . _check_job_status ( job ) <EOL> else : <EOL> success = ( ret_val == <NUM_LIT:0> ) <EOL> if success : <EOL> return new_resources <EOL> else : <EOL> return None <EOL> def _check_job_status ( self , jobpath ) : <EOL> """<STR_LIT>""" <EOL> inst_id = jobpath . split ( '<STR_LIT:=>' ) [ <NUM_LIT:1> ] . strip ( '<STR_LIT:">' ) <EOL> jobs = self . _conn . Msvm_ConcreteJob ( InstanceID = inst_id ) <EOL> if len ( jobs ) == <NUM_LIT:0> : <EOL> return False <EOL> job = jobs [ <NUM_LIT:0> ] <EOL> while job . JobState == WMI_JOB_STATE_RUNNING : <EOL> time . sleep ( <NUM_LIT:0.1> ) <EOL> job = self . _conn . Msvm_ConcreteJob ( InstanceID = inst_id ) [ <NUM_LIT:0> ] <EOL> if job . JobState != WMI_JOB_STATE_COMPLETED : <EOL> LOG . debug ( _ ( "<STR_LIT>" ) , job . ErrorSummaryDescription ) <EOL> return False <EOL> desc = job . Description <EOL> elap = job . ElapsedTime <EOL> LOG . debug ( _ ( "<STR_LIT>" ) <EOL> % locals ( ) ) <EOL> return True <EOL> def _find_external_network ( self ) : <EOL> """<STR_LIT>""" <EOL> bound = self . _conn . Msvm_ExternalEthernetPort ( IsBound = '<STR_LIT>' ) <EOL> if len ( bound ) == <NUM_LIT:0> : <EOL> return None <EOL> return self . _conn . Msvm_ExternalEthernetPort ( IsBound = '<STR_LIT>' ) [ <NUM_LIT:0> ] . associators ( wmi_result_class = '<STR_LIT>' ) [ <NUM_LIT:0> ] . associators ( wmi_result_class = '<STR_LIT>' ) [ <NUM_LIT:0> ] . associators ( wmi_result_class = '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> def _clone_wmi_obj ( self , wmi_class , wmi_obj ) : <EOL> """<STR_LIT>""" <EOL> cl = self . _conn . __getattr__ ( wmi_class ) <EOL> newinst = cl . new ( ) <EOL> for prop in wmi_obj . _properties : <EOL> newinst . Properties_ . Item ( prop ) . Value = wmi_obj . Properties_ . Item ( prop ) . Value <EOL> return newinst <EOL> def reboot ( self , instance , network_info ) : <EOL> """<STR_LIT>""" <EOL> vm = self . _lookup ( instance . name ) <EOL> if vm is None : <EOL> raise exception . InstanceNotFound ( instance_id = instance . id ) <EOL> self . _set_vm_state ( instance . name , '<STR_LIT>' ) <EOL> def destroy ( self , instance , network_info , cleanup = True ) : <EOL> """<STR_LIT>""" <EOL> LOG . debug ( _ ( "<STR_LIT>" ) , instance . name ) <EOL> vm = self . _lookup ( instance . name ) <EOL> if vm is None : <EOL> return <EOL> vm = self . _conn . Msvm_ComputerSystem ( ElementName = instance . name ) [ <NUM_LIT:0> ] <EOL> vs_man_svc = self . _conn . Msvm_VirtualSystemManagementService ( ) [ <NUM_LIT:0> ] <EOL> self . _set_vm_state ( instance . name , '<STR_LIT>' ) <EOL> vmsettings = vm . associators ( <EOL> wmi_result_class = '<STR_LIT>' ) <EOL> rasds = vmsettings [ <NUM_LIT:0> ] . associators ( <EOL> wmi_result_class = '<STR_LIT>' ) <EOL> disks = [ r for r in rasds if r . ResourceSubType == '<STR_LIT>' ] <EOL> diskfiles = [ ] <EOL> for disk in disks : <EOL> diskfiles . extend ( [ c for c in disk . Connection ] ) <EOL> ( job , ret_val ) = vs_man_svc . DestroyVirtualSystem ( vm . path_ ( ) ) <EOL> if ret_val == WMI_JOB_STATUS_STARTED : <EOL> success = self . _check_job_status ( job ) <EOL> elif ret_val == <NUM_LIT:0> : <EOL> success = True <EOL> if not success : <EOL> raise Exception ( _ ( '<STR_LIT>' ) % instance . name ) <EOL> for disk in diskfiles : <EOL> vhdfile = self . _cim_conn . CIM_DataFile ( Name = disk ) <EOL> for vf in vhdfile : <EOL> vf . Delete ( ) <EOL> instance_name = instance . name <EOL> LOG . debug ( _ ( "<STR_LIT>" ) <EOL> % locals ( ) ) <EOL> def get_info ( self , instance_id ) : <EOL> """<STR_LIT>""" <EOL> vm = self . _lookup ( instance_id ) <EOL> if vm is None : <EOL> raise exception . InstanceNotFound ( instance_id = instance_id ) <EOL> vm = self . _conn . Msvm_ComputerSystem ( ElementName = instance_id ) [ <NUM_LIT:0> ] <EOL> vs_man_svc = self . _conn . Msvm_VirtualSystemManagementService ( ) [ <NUM_LIT:0> ] <EOL> vmsettings = vm . associators ( <EOL> wmi_result_class = '<STR_LIT>' ) <EOL> settings_paths = [ v . path_ ( ) for v in vmsettings ] <EOL> summary_info = vs_man_svc . GetSummaryInformation ( <EOL> [ <NUM_LIT:4> , <NUM_LIT:100> , <NUM_LIT> , <NUM_LIT> ] , settings_paths ) [ <NUM_LIT:1> ] <EOL> info = summary_info [ <NUM_LIT:0> ] <EOL> state = str ( HYPERV_POWER_STATE [ info . EnabledState ] ) <EOL> memusage = str ( info . MemoryUsage ) <EOL> numprocs = str ( info . NumberOfProcessors ) <EOL> uptime = str ( info . UpTime ) <EOL> LOG . debug ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) % locals ( ) ) <EOL> return { '<STR_LIT:state>' : HYPERV_POWER_STATE [ info . EnabledState ] , <EOL> '<STR_LIT>' : info . MemoryUsage , <EOL> '<STR_LIT>' : info . MemoryUsage , <EOL> '<STR_LIT>' : info . NumberOfProcessors , <EOL> '<STR_LIT>' : info . UpTime } <EOL> def _lookup ( self , i ) : <EOL> vms = self . _conn . Msvm_ComputerSystem ( ElementName = i ) <EOL> n = len ( vms ) <EOL> if n == <NUM_LIT:0> : <EOL> return None <EOL> elif n > <NUM_LIT:1> : <EOL> raise Exception ( _ ( '<STR_LIT>' ) % i ) <EOL> else : <EOL> return vms [ <NUM_LIT:0> ] . ElementName <EOL> def _set_vm_state ( self , vm_name , req_state ) : <EOL> """<STR_LIT>""" <EOL> vms = self . _conn . Msvm_ComputerSystem ( ElementName = vm_name ) <EOL> if len ( vms ) == <NUM_LIT:0> : <EOL> return False <EOL> ( job , ret_val ) = vms [ <NUM_LIT:0> ] . RequestStateChange ( REQ_POWER_STATE [ req_state ] ) <EOL> success = False <EOL> if ret_val == WMI_JOB_STATUS_STARTED : <EOL> success = self . _check_job_status ( job ) <EOL> elif ret_val == <NUM_LIT:0> : <EOL> success = True <EOL> elif ret_val == <NUM_LIT> : <EOL> success = True <EOL> if success : <EOL> LOG . info ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % locals ( ) ) <EOL> else : <EOL> msg = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % locals ( ) <EOL> LOG . error ( msg ) <EOL> raise Exception ( msg ) <EOL> def attach_volume ( self , instance_name , device_path , mountpoint ) : <EOL> vm = self . _lookup ( instance_name ) <EOL> if vm is None : <EOL> raise exception . InstanceNotFound ( instance_id = instance_name ) <EOL> def detach_volume ( self , instance_name , mountpoint ) : <EOL> vm = self . _lookup ( instance_name ) <EOL> if vm is None : <EOL> raise exception . InstanceNotFound ( instance_id = instance_name ) <EOL> def poll_rescued_instances ( self , timeout ) : <EOL> pass <EOL> def update_available_resource ( self , ctxt , host ) : <EOL> """<STR_LIT>""" <EOL> return <EOL> def update_host_status ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def get_host_stats ( self , refresh = False ) : <EOL> """<STR_LIT>""" <EOL> pass <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 </s>
