<s> """<STR_LIT>""" <EOL> import os <EOL> import os . path <EOL> import logging <EOL> from ganeti import utils <EOL> from ganeti import constants <EOL> from ganeti import errors <EOL> from ganeti import objects <EOL> from ganeti import pathutils <EOL> from ganeti . hypervisor import hv_base <EOL> class FakeHypervisor ( hv_base . BaseHypervisor ) : <EOL> """<STR_LIT>""" <EOL> PARAMETERS = { <EOL> constants . HV_MIGRATION_MODE : hv_base . MIGRATION_MODE_CHECK , <EOL> } <EOL> CAN_MIGRATE = True <EOL> _ROOT_DIR = pathutils . RUN_DIR + "<STR_LIT>" <EOL> def __init__ ( self ) : <EOL> hv_base . BaseHypervisor . __init__ ( self ) <EOL> utils . EnsureDirs ( [ ( self . _ROOT_DIR , constants . RUN_DIRS_MODE ) ] ) <EOL> def ListInstances ( self , hvparams = None ) : <EOL> """<STR_LIT>""" <EOL> return os . listdir ( self . _ROOT_DIR ) <EOL> def GetInstanceInfo ( self , instance_name , hvparams = None ) : <EOL> """<STR_LIT>""" <EOL> file_name = self . _InstanceFile ( instance_name ) <EOL> if not os . path . exists ( file_name ) : <EOL> return None <EOL> try : <EOL> fh = open ( file_name , "<STR_LIT:r>" ) <EOL> try : <EOL> inst_id = fh . readline ( ) . strip ( ) <EOL> memory = utils . TryConvert ( int , fh . readline ( ) . strip ( ) ) <EOL> vcpus = utils . TryConvert ( int , fh . readline ( ) . strip ( ) ) <EOL> stat = hv_base . HvInstanceState . RUNNING <EOL> times = <NUM_LIT:0> <EOL> return ( instance_name , inst_id , memory , vcpus , stat , times ) <EOL> finally : <EOL> fh . close ( ) <EOL> except IOError , err : <EOL> raise errors . HypervisorError ( "<STR_LIT>" % <EOL> ( instance_name , err ) ) <EOL> def GetAllInstancesInfo ( self , hvparams = None ) : <EOL> """<STR_LIT>""" <EOL> data = [ ] <EOL> for file_name in os . listdir ( self . _ROOT_DIR ) : <EOL> try : <EOL> fh = open ( utils . PathJoin ( self . _ROOT_DIR , file_name ) , "<STR_LIT:r>" ) <EOL> inst_id = "<STR_LIT>" <EOL> memory = <NUM_LIT:0> <EOL> vcpus = <NUM_LIT:1> <EOL> stat = hv_base . HvInstanceState . SHUTDOWN <EOL> times = - <NUM_LIT:1> <EOL> try : <EOL> inst_id = fh . readline ( ) . strip ( ) <EOL> memory = utils . TryConvert ( int , fh . readline ( ) . strip ( ) ) <EOL> vcpus = utils . TryConvert ( int , fh . readline ( ) . strip ( ) ) <EOL> stat = hv_base . HvInstanceState . RUNNING <EOL> times = <NUM_LIT:0> <EOL> finally : <EOL> fh . close ( ) <EOL> data . append ( ( file_name , inst_id , memory , vcpus , stat , times ) ) <EOL> except IOError , err : <EOL> raise errors . HypervisorError ( "<STR_LIT>" % err ) <EOL> return data <EOL> @ classmethod <EOL> def _InstanceFile ( cls , instance_name ) : <EOL> """<STR_LIT>""" <EOL> return utils . PathJoin ( cls . _ROOT_DIR , instance_name ) <EOL> def _IsAlive ( self , instance_name ) : <EOL> """<STR_LIT>""" <EOL> file_name = self . _InstanceFile ( instance_name ) <EOL> return os . path . exists ( file_name ) <EOL> def _MarkUp ( self , instance , memory ) : <EOL> """<STR_LIT>""" <EOL> file_name = self . _InstanceFile ( instance . name ) <EOL> fh = file ( file_name , "<STR_LIT:w>" ) <EOL> try : <EOL> fh . write ( "<STR_LIT>" % <EOL> ( memory , <EOL> instance . beparams [ constants . BE_VCPUS ] ) ) <EOL> finally : <EOL> fh . close ( ) <EOL> def _MarkDown ( self , instance_name ) : <EOL> """<STR_LIT>""" <EOL> file_name = self . _InstanceFile ( instance_name ) <EOL> utils . RemoveFile ( file_name ) <EOL> def StartInstance ( self , instance , block_devices , startup_paused ) : <EOL> """<STR_LIT>""" <EOL> if self . _IsAlive ( instance . name ) : <EOL> raise errors . HypervisorError ( "<STR_LIT>" % <EOL> ( instance . name , "<STR_LIT>" ) ) <EOL> try : <EOL> self . _MarkUp ( instance , self . _InstanceStartupMemory ( instance ) ) <EOL> except IOError , err : <EOL> raise errors . HypervisorError ( "<STR_LIT>" % <EOL> ( instance . name , err ) ) <EOL> def StopInstance ( self , instance , force = False , retry = False , name = None , <EOL> timeout = None ) : <EOL> """<STR_LIT>""" <EOL> assert ( timeout is None or force is not None ) <EOL> if name is None : <EOL> name = instance . name <EOL> if not self . _IsAlive ( name ) : <EOL> raise errors . HypervisorError ( "<STR_LIT>" % <EOL> ( name , "<STR_LIT>" ) ) <EOL> self . _MarkDown ( name ) <EOL> def RebootInstance ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> return <EOL> def BalloonInstanceMemory ( self , instance , mem ) : <EOL> """<STR_LIT>""" <EOL> if not self . _IsAlive ( instance . name ) : <EOL> raise errors . HypervisorError ( "<STR_LIT>" % <EOL> ( instance . name , "<STR_LIT>" ) ) <EOL> try : <EOL> self . _MarkUp ( instance , mem ) <EOL> except EnvironmentError , err : <EOL> raise errors . HypervisorError ( "<STR_LIT>" % <EOL> ( instance . name , utils . ErrnoOrStr ( err ) ) ) <EOL> def GetNodeInfo ( self , hvparams = None ) : <EOL> """<STR_LIT>""" <EOL> result = self . GetLinuxNodeInfo ( ) <EOL> all_instances = self . GetAllInstancesInfo ( ) <EOL> result [ "<STR_LIT>" ] -= min ( result [ "<STR_LIT>" ] , <EOL> sum ( [ row [ <NUM_LIT:2> ] for row in all_instances ] ) ) <EOL> return result <EOL> @ classmethod <EOL> def GetInstanceConsole ( cls , instance , primary_node , node_group , <EOL> hvparams , beparams ) : <EOL> """<STR_LIT>""" <EOL> return objects . InstanceConsole ( instance = instance . name , <EOL> kind = constants . CONS_MESSAGE , <EOL> message = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> def Verify ( self , hvparams = None ) : <EOL> """<STR_LIT>""" <EOL> if os . path . exists ( self . _ROOT_DIR ) : <EOL> return None <EOL> else : <EOL> return "<STR_LIT>" % self . _ROOT_DIR <EOL> @ classmethod <EOL> def PowercycleNode ( cls , hvparams = None ) : <EOL> """<STR_LIT>""" <EOL> cls . LinuxPowercycle ( ) <EOL> def AcceptInstance ( self , instance , info , target ) : <EOL> """<STR_LIT>""" <EOL> if self . _IsAlive ( instance . name ) : <EOL> raise errors . HypervisorError ( "<STR_LIT>" ) <EOL> def MigrateInstance ( self , cluster_name , instance , target , live ) : <EOL> """<STR_LIT>""" <EOL> logging . debug ( "<STR_LIT>" , <EOL> instance , target , live ) <EOL> def FinalizeMigrationDst ( self , instance , info , success ) : <EOL> """<STR_LIT>""" <EOL> if success : <EOL> self . _MarkUp ( instance , self . _InstanceStartupMemory ( instance ) ) <EOL> else : <EOL> self . _MarkDown ( instance . name ) <EOL> def PostMigrationCleanup ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def FinalizeMigrationSource ( self , instance , success , live ) : <EOL> """<STR_LIT>""" <EOL> if success : <EOL> self . _MarkDown ( instance . name ) <EOL> def GetMigrationStatus ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> return objects . MigrationStatus ( status = constants . HV_MIGRATION_COMPLETED ) </s>
