<s> """<STR_LIT>""" <EOL> import abc <EOL> import os . path <EOL> import threading <EOL> import jinja2 <EOL> from perfkitbenchmarker import data <EOL> from perfkitbenchmarker import disk <EOL> from perfkitbenchmarker import errors <EOL> from perfkitbenchmarker import flags <EOL> from perfkitbenchmarker import resource <EOL> from perfkitbenchmarker import vm_util <EOL> from perfkitbenchmarker . configs import option_decoders <EOL> from perfkitbenchmarker . configs import spec <EOL> FLAGS = flags . FLAGS <EOL> DEFAULT_USERNAME = '<STR_LIT>' <EOL> _VM_SPEC_REGISTRY = { } <EOL> _VM_REGISTRY = { } <EOL> def GetVmSpecClass ( cloud ) : <EOL> """<STR_LIT>""" <EOL> return _VM_SPEC_REGISTRY . get ( cloud , BaseVmSpec ) <EOL> def GetVmClass ( cloud , os_type ) : <EOL> """<STR_LIT>""" <EOL> return _VM_REGISTRY . get ( ( cloud , os_type ) ) <EOL> class AutoRegisterVmSpecMeta ( spec . BaseSpecMetaClass ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( cls , name , bases , dct ) : <EOL> super ( AutoRegisterVmSpecMeta , cls ) . __init__ ( name , bases , dct ) <EOL> if cls . CLOUD in _VM_SPEC_REGISTRY : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> _VM_SPEC_REGISTRY [ cls . CLOUD ] = cls <EOL> class AutoRegisterVmMeta ( abc . ABCMeta ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( cls , name , bases , dct ) : <EOL> if hasattr ( cls , '<STR_LIT>' ) and hasattr ( cls , '<STR_LIT>' ) : <EOL> if cls . CLOUD is None : <EOL> raise Exception ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> elif cls . OS_TYPE is None : <EOL> raise Exception ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> else : <EOL> _VM_REGISTRY [ ( cls . CLOUD , cls . OS_TYPE ) ] = cls <EOL> super ( AutoRegisterVmMeta , cls ) . __init__ ( name , bases , dct ) <EOL> class BaseVmSpec ( spec . BaseSpec ) : <EOL> """<STR_LIT>""" <EOL> __metaclass__ = AutoRegisterVmSpecMeta <EOL> CLOUD = None <EOL> @ classmethod <EOL> def _ApplyFlags ( cls , config_values , flag_values ) : <EOL> """<STR_LIT>""" <EOL> super ( BaseVmSpec , cls ) . _ApplyFlags ( config_values , flag_values ) <EOL> if flag_values [ '<STR_LIT:image>' ] . present : <EOL> config_values [ '<STR_LIT:image>' ] = flag_values . image <EOL> if flag_values [ '<STR_LIT>' ] . present : <EOL> config_values [ '<STR_LIT>' ] = flag_values . install_packages <EOL> if flag_values [ '<STR_LIT>' ] . present : <EOL> config_values [ '<STR_LIT>' ] = flag_values . machine_type <EOL> if flag_values [ '<STR_LIT>' ] . present : <EOL> config_values [ '<STR_LIT>' ] = ( <EOL> flag_values . background_cpu_threads ) <EOL> if flag_values [ '<STR_LIT>' ] . present : <EOL> config_values [ '<STR_LIT>' ] = ( <EOL> flag_values . background_network_mbits_per_sec ) <EOL> if flag_values [ '<STR_LIT>' ] . present : <EOL> config_values [ '<STR_LIT>' ] = ( <EOL> flag_values . background_network_ip_type ) <EOL> @ classmethod <EOL> def _GetOptionDecoderConstructions ( cls ) : <EOL> """<STR_LIT>""" <EOL> result = super ( BaseVmSpec , cls ) . _GetOptionDecoderConstructions ( ) <EOL> result . update ( { <EOL> '<STR_LIT:image>' : ( option_decoders . StringDecoder , { '<STR_LIT>' : True , <EOL> '<STR_LIT:default>' : None } ) , <EOL> '<STR_LIT>' : ( option_decoders . BooleanDecoder , { '<STR_LIT:default>' : True } ) , <EOL> '<STR_LIT>' : ( option_decoders . StringDecoder , { '<STR_LIT>' : True , <EOL> '<STR_LIT:default>' : None } ) , <EOL> '<STR_LIT>' : ( option_decoders . StringDecoder , { '<STR_LIT>' : True , <EOL> '<STR_LIT:default>' : None } ) , <EOL> '<STR_LIT>' : ( option_decoders . IntDecoder , { <EOL> '<STR_LIT>' : True , '<STR_LIT:default>' : None } ) , <EOL> '<STR_LIT>' : ( option_decoders . EnumDecoder , { <EOL> '<STR_LIT:default>' : vm_util . IpAddressSubset . EXTERNAL , <EOL> '<STR_LIT>' : [ vm_util . IpAddressSubset . EXTERNAL , <EOL> vm_util . IpAddressSubset . INTERNAL ] } ) , <EOL> '<STR_LIT>' : ( option_decoders . IntDecoder , { <EOL> '<STR_LIT>' : True , '<STR_LIT:default>' : None } ) } ) <EOL> return result <EOL> class BaseVirtualMachine ( resource . BaseResource ) : <EOL> """<STR_LIT>""" <EOL> __metaclass__ = AutoRegisterVmMeta <EOL> is_static = False <EOL> CLOUD = None <EOL> _instance_counter_lock = threading . Lock ( ) <EOL> _instance_counter = <NUM_LIT:0> <EOL> def __init__ ( self , vm_spec ) : <EOL> """<STR_LIT>""" <EOL> super ( BaseVirtualMachine , self ) . __init__ ( ) <EOL> with self . _instance_counter_lock : <EOL> self . instance_number = self . _instance_counter <EOL> self . name = '<STR_LIT>' % ( FLAGS . run_uri , self . instance_number ) <EOL> BaseVirtualMachine . _instance_counter += <NUM_LIT:1> <EOL> self . zone = vm_spec . zone <EOL> self . machine_type = vm_spec . machine_type <EOL> self . image = vm_spec . image <EOL> self . install_packages = vm_spec . install_packages <EOL> self . ip_address = None <EOL> self . internal_ip = None <EOL> self . user_name = DEFAULT_USERNAME <EOL> self . password = None <EOL> self . ssh_public_key = vm_util . GetPublicKeyPath ( ) <EOL> self . ssh_private_key = vm_util . GetPrivateKeyPath ( ) <EOL> self . disk_specs = [ ] <EOL> self . scratch_disks = [ ] <EOL> self . max_local_disks = <NUM_LIT:0> <EOL> self . local_disk_counter = <NUM_LIT:0> <EOL> self . remote_disk_counter = <NUM_LIT:0> <EOL> self . background_cpu_threads = vm_spec . background_cpu_threads <EOL> self . background_network_mbits_per_sec = ( <EOL> vm_spec . background_network_mbits_per_sec ) <EOL> self . background_network_ip_type = vm_spec . background_network_ip_type <EOL> self . network = None <EOL> self . firewall = None <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' . format ( <EOL> self . ip_address , self . internal_ip ) <EOL> def __str__ ( self ) : <EOL> if self . ip_address : <EOL> return self . ip_address <EOL> return super ( BaseVirtualMachine , self ) . __str__ ( ) <EOL> def CreateScratchDisk ( self , disk_spec ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def DeleteScratchDisks ( self ) : <EOL> """<STR_LIT>""" <EOL> for scratch_disk in self . scratch_disks : <EOL> if scratch_disk . disk_type != disk . LOCAL : <EOL> scratch_disk . Delete ( ) <EOL> def GetScratchDir ( self , disk_num = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if disk_num >= len ( self . scratch_disks ) : <EOL> raise errors . Error ( <EOL> '<STR_LIT>' % ( <EOL> disk_num , len ( self . scratch_disks ) ) ) <EOL> return self . scratch_disks [ disk_num ] . mount_point <EOL> def GetLocalDisks ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ ] <EOL> def AllowPort ( self , port ) : <EOL> """<STR_LIT>""" <EOL> if self . firewall : <EOL> self . firewall . AllowPort ( self , port ) <EOL> def AllowRemoteAccessPorts ( self ) : <EOL> """<STR_LIT>""" <EOL> for port in self . remote_access_ports : <EOL> self . AllowPort ( port ) <EOL> def AddMetadata ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def GetMachineTypeDict ( self ) : <EOL> """<STR_LIT>""" <EOL> result = { } <EOL> if self . machine_type is not None : <EOL> result [ '<STR_LIT>' ] = self . machine_type <EOL> return result <EOL> class BaseOsMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> __metaclass__ = abc . ABCMeta <EOL> OS_TYPE = None <EOL> def __init__ ( self ) : <EOL> super ( BaseOsMixin , self ) . __init__ ( ) <EOL> self . _installed_packages = set ( ) <EOL> self . bootable_time = None <EOL> self . hostname = None <EOL> self . remote_access_ports = [ ] <EOL> self . _reachable = { } <EOL> self . _total_memory_kb = None <EOL> self . _num_cpus = None <EOL> @ abc . abstractmethod <EOL> def RemoteCommand ( self , command , should_log = False , ignore_failure = False , <EOL> suppress_warning = False , timeout = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> @ abc . abstractmethod <EOL> def RemoteCopy ( self , file_path , remote_path = '<STR_LIT>' , copy_to = True ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> @ abc . abstractmethod <EOL> def WaitForBootCompletion ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def OnStartup ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def PrepareVMEnvironment ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> @ abc . abstractmethod <EOL> def Install ( self , package_name ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> @ abc . abstractmethod <EOL> def Uninstall ( self , package_name ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> @ abc . abstractmethod <EOL> def PackageCleanup ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def SetupLocalDisks ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def PushFile ( self , source_path , remote_path = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> self . RemoteCopy ( source_path , remote_path ) <EOL> def PullFile ( self , local_path , remote_path ) : <EOL> """<STR_LIT>""" <EOL> self . RemoteCopy ( local_path , remote_path , copy_to = False ) <EOL> def PushDataFile ( self , data_file , remote_path = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> file_path = data . ResourcePath ( data_file ) <EOL> self . PushFile ( file_path , remote_path ) <EOL> def RenderTemplate ( self , template_path , remote_path , context ) : <EOL> """<STR_LIT>""" <EOL> with open ( template_path ) as fp : <EOL> template_contents = fp . read ( ) <EOL> environment = jinja2 . Environment ( undefined = jinja2 . StrictUndefined ) <EOL> template = environment . from_string ( template_contents ) <EOL> prefix = '<STR_LIT>' + os . path . basename ( template_path ) <EOL> with vm_util . NamedTemporaryFile ( prefix = prefix , dir = vm_util . GetTempDir ( ) , <EOL> delete = False ) as tf : <EOL> tf . write ( template . render ( vm = self , ** context ) ) <EOL> tf . close ( ) <EOL> self . RemoteCopy ( tf . name , remote_path ) <EOL> @ abc . abstractmethod <EOL> def _CreateScratchDiskFromDisks ( self , disk_spec , disks ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> @ property <EOL> def num_cpus ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _num_cpus is None : <EOL> self . _num_cpus = self . _GetNumCpus ( ) <EOL> return self . _num_cpus <EOL> @ abc . abstractmethod <EOL> def _GetNumCpus ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> @ property <EOL> def total_memory_kb ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _total_memory_kb : <EOL> self . _total_memory_kb = self . _GetTotalMemoryKb ( ) <EOL> return self . _total_memory_kb <EOL> @ abc . abstractmethod <EOL> def _GetTotalMemoryKb ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def IsReachable ( self , target_vm ) : <EOL> """<STR_LIT>""" <EOL> if target_vm not in self . _reachable : <EOL> if target_vm . internal_ip : <EOL> self . _reachable [ target_vm ] = self . _TestReachable ( target_vm . internal_ip ) <EOL> else : <EOL> self . _reachable [ target_vm ] = False <EOL> return self . _reachable [ target_vm ] <EOL> @ abc . abstractmethod <EOL> def _TestReachable ( self , ip ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def StartBackgroundWorkload ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . background_cpu_threads or self . background_network_mbits_per_sec : <EOL> raise NotImplementedError ( ) <EOL> def StopBackgroundWorkload ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . background_cpu_threads or self . background_network_mbits_per_sec : <EOL> raise NotImplementedError ( ) <EOL> def PrepareBackgroundWorkload ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . background_cpu_threads or self . background_network_mbits_per_sec : <EOL> raise NotImplementedError ( ) </s>
