<s> """<STR_LIT>""" <EOL> import logging <EOL> import shutil <EOL> import socket <EOL> import tempfile <EOL> import time <EOL> from ganeti import compat <EOL> from ganeti import constants <EOL> from ganeti import errors <EOL> from ganeti import locking <EOL> from ganeti import utils <EOL> from ganeti . masterd import iallocator <EOL> from ganeti . cmdlib . base import NoHooksLU <EOL> from ganeti . cmdlib . common import ExpandInstanceUuidAndName , GetWantedNodes , GetWantedInstances <EOL> class TestSocketWrapper ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . tmpdir = None <EOL> self . sock = None <EOL> def Create ( self , max_connections = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> self . tmpdir = tempfile . mkdtemp ( ) <EOL> try : <EOL> tmpsock = utils . PathJoin ( self . tmpdir , "<STR_LIT>" ) <EOL> logging . debug ( "<STR_LIT>" , tmpsock ) <EOL> self . sock = socket . socket ( socket . AF_UNIX , socket . SOCK_STREAM ) <EOL> try : <EOL> self . sock . bind ( tmpsock ) <EOL> self . sock . listen ( max_connections ) <EOL> except : <EOL> self . sock . close ( ) <EOL> raise <EOL> except : <EOL> shutil . rmtree ( self . tmpdir ) <EOL> raise <EOL> return self . sock , tmpsock <EOL> def Destroy ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . tmpdir is None or self . sock is None : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> try : <EOL> self . sock . close ( ) <EOL> finally : <EOL> shutil . rmtree ( self . tmpdir ) <EOL> class LUTestDelay ( NoHooksLU ) : <EOL> """<STR_LIT>""" <EOL> REQ_BGL = False <EOL> def ExpandNames ( self ) : <EOL> """<STR_LIT>""" <EOL> self . needed_locks = { } <EOL> if self . op . duration <= <NUM_LIT:0> : <EOL> raise errors . OpPrereqError ( "<STR_LIT>" ) <EOL> if not self . op . no_locks and ( self . op . on_nodes or self . op . on_master ) : <EOL> self . needed_locks [ locking . LEVEL_NODE ] = [ ] <EOL> self . op . on_node_uuids = [ ] <EOL> if self . op . on_nodes : <EOL> ( self . op . on_node_uuids , self . op . on_nodes ) = GetWantedNodes ( self , self . op . on_nodes ) <EOL> master_uuid = self . cfg . GetMasterNode ( ) <EOL> if self . op . on_master and master_uuid not in self . op . on_node_uuids : <EOL> self . op . on_node_uuids . append ( master_uuid ) <EOL> self . needed_locks = { } <EOL> self . needed_locks [ locking . LEVEL_NODE ] = self . op . on_node_uuids <EOL> def _InterruptibleDelay ( self ) : <EOL> """<STR_LIT>""" <EOL> socket_wrapper = TestSocketWrapper ( ) <EOL> sock , path = socket_wrapper . Create ( ) <EOL> self . Log ( constants . ELOG_DELAY_TEST , ( path , ) ) <EOL> try : <EOL> sock . settimeout ( self . op . duration ) <EOL> start = time . time ( ) <EOL> ( conn , _ ) = sock . accept ( ) <EOL> except socket . timeout , _ : <EOL> return False <EOL> finally : <EOL> socket_wrapper . Destroy ( ) <EOL> try : <EOL> time_to_go = self . op . duration - ( time . time ( ) - start ) <EOL> self . Log ( constants . ELOG_MESSAGE , <EOL> "<STR_LIT>" % time_to_go ) <EOL> if time_to_go < <NUM_LIT:0> : <EOL> time_to_go = <NUM_LIT:0> <EOL> conn . settimeout ( time_to_go ) <EOL> conn . recv ( <NUM_LIT:1> ) <EOL> except socket . timeout , _ : <EOL> return False <EOL> finally : <EOL> conn . close ( ) <EOL> self . Log ( constants . ELOG_MESSAGE , <EOL> "<STR_LIT>" % ( time . time ( ) - start ) ) <EOL> return True <EOL> def _UninterruptibleDelay ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . op . on_node_uuids : <EOL> result = self . rpc . call_test_delay ( self . op . on_node_uuids , self . op . duration ) <EOL> for node_uuid , node_result in result . items ( ) : <EOL> node_result . Raise ( "<STR_LIT>" % <EOL> self . cfg . GetNodeName ( node_uuid ) ) <EOL> else : <EOL> if not utils . TestDelay ( self . op . duration ) [ <NUM_LIT:0> ] : <EOL> raise errors . OpExecError ( "<STR_LIT>" ) <EOL> def _TestDelay ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . op . interruptible : <EOL> return self . _InterruptibleDelay ( ) <EOL> else : <EOL> self . _UninterruptibleDelay ( ) <EOL> return False <EOL> def Exec ( self , feedback_fn ) : <EOL> """<STR_LIT>""" <EOL> if self . op . repeat == <NUM_LIT:0> : <EOL> i = self . _TestDelay ( ) <EOL> else : <EOL> top_value = self . op . repeat - <NUM_LIT:1> <EOL> for i in range ( self . op . repeat ) : <EOL> self . LogInfo ( "<STR_LIT>" , i , top_value ) <EOL> if self . _TestDelay ( ) : <EOL> break <EOL> class LUTestJqueue ( NoHooksLU ) : <EOL> """<STR_LIT>""" <EOL> REQ_BGL = False <EOL> _CLIENT_CONNECT_TIMEOUT = <NUM_LIT> <EOL> _CLIENT_CONFIRM_TIMEOUT = <NUM_LIT> <EOL> @ classmethod <EOL> def _NotifyUsingSocket ( cls , cb , errcls ) : <EOL> """<STR_LIT>""" <EOL> socket_wrapper = TestSocketWrapper ( ) <EOL> sock , path = socket_wrapper . Create ( ) <EOL> cb ( path ) <EOL> try : <EOL> sock . settimeout ( cls . _CLIENT_CONNECT_TIMEOUT ) <EOL> ( conn , _ ) = sock . accept ( ) <EOL> except socket . error , err : <EOL> raise errcls ( "<STR_LIT>" % err ) <EOL> finally : <EOL> socket_wrapper . Destroy ( ) <EOL> try : <EOL> try : <EOL> conn . settimeout ( cls . _CLIENT_CONFIRM_TIMEOUT ) <EOL> conn . recv ( <NUM_LIT:1> ) <EOL> except socket . error , err : <EOL> raise errcls ( "<STR_LIT>" % err ) <EOL> finally : <EOL> conn . close ( ) <EOL> def _SendNotification ( self , test , arg , sockname ) : <EOL> """<STR_LIT>""" <EOL> self . Log ( constants . ELOG_JQUEUE_TEST , ( sockname , test , arg ) ) <EOL> def _Notify ( self , prereq , test , arg ) : <EOL> """<STR_LIT>""" <EOL> if prereq : <EOL> errcls = errors . OpPrereqError <EOL> else : <EOL> errcls = errors . OpExecError <EOL> return self . _NotifyUsingSocket ( compat . partial ( self . _SendNotification , <EOL> test , arg ) , <EOL> errcls ) <EOL> def CheckArguments ( self ) : <EOL> self . checkargs_calls = getattr ( self , "<STR_LIT>" , <NUM_LIT:0> ) + <NUM_LIT:1> <EOL> self . expandnames_calls = <NUM_LIT:0> <EOL> def ExpandNames ( self ) : <EOL> checkargs_calls = getattr ( self , "<STR_LIT>" , <NUM_LIT:0> ) <EOL> if checkargs_calls < <NUM_LIT:1> : <EOL> raise errors . ProgrammerError ( "<STR_LIT>" ) <EOL> self . expandnames_calls += <NUM_LIT:1> <EOL> if self . op . notify_waitlock : <EOL> self . _Notify ( True , constants . JQT_EXPANDNAMES , None ) <EOL> self . LogInfo ( "<STR_LIT>" ) <EOL> self . needed_locks = { <EOL> locking . LEVEL_NODE : self . cfg . GetMasterNode ( ) , <EOL> } <EOL> def Exec ( self , feedback_fn ) : <EOL> if self . expandnames_calls < <NUM_LIT:1> : <EOL> raise errors . ProgrammerError ( "<STR_LIT>" ) <EOL> if self . op . notify_exec : <EOL> self . _Notify ( False , constants . JQT_EXEC , None ) <EOL> self . LogInfo ( "<STR_LIT>" ) <EOL> if self . op . log_messages : <EOL> self . _Notify ( False , constants . JQT_STARTMSG , len ( self . op . log_messages ) ) <EOL> for idx , msg in enumerate ( self . op . log_messages ) : <EOL> self . LogInfo ( "<STR_LIT>" , idx + <NUM_LIT:1> ) <EOL> feedback_fn ( constants . JQT_MSGPREFIX + msg ) <EOL> self . _Notify ( False , constants . JQT_LOGMSG , idx + <NUM_LIT:1> ) <EOL> if self . op . fail : <EOL> raise errors . OpExecError ( "<STR_LIT>" ) <EOL> return True <EOL> class LUTestOsParams ( NoHooksLU ) : <EOL> """<STR_LIT>""" <EOL> REQ_BGL = False <EOL> def ExpandNames ( self ) : <EOL> self . needed_locks = { } <EOL> def Exec ( self , feedback_fn ) : <EOL> if self . op . osparams_secret : <EOL> msg = "<STR_LIT>" % self . op . osparams_secret . Unprivate ( ) <EOL> feedback_fn ( msg ) <EOL> else : <EOL> raise errors . OpExecError ( "<STR_LIT>" ) <EOL> class LUTestAllocator ( NoHooksLU ) : <EOL> """<STR_LIT>""" <EOL> def CheckPrereq ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . op . mode in ( constants . IALLOCATOR_MODE_ALLOC , <EOL> constants . IALLOCATOR_MODE_MULTI_ALLOC ) : <EOL> ( self . inst_uuid , iname ) = self . cfg . ExpandInstanceName ( self . op . name ) <EOL> if iname is not None : <EOL> raise errors . OpPrereqError ( "<STR_LIT>" % <EOL> iname , errors . ECODE_EXISTS ) <EOL> for row in self . op . disks : <EOL> if ( not isinstance ( row , dict ) or <EOL> constants . IDISK_SIZE not in row or <EOL> not isinstance ( row [ constants . IDISK_SIZE ] , int ) or <EOL> constants . IDISK_MODE not in row or <EOL> row [ constants . IDISK_MODE ] not in constants . DISK_ACCESS_SET ) : <EOL> raise errors . OpPrereqError ( "<STR_LIT>" <EOL> "<STR_LIT>" , errors . ECODE_INVAL ) <EOL> if self . op . hypervisor is None : <EOL> self . op . hypervisor = self . cfg . GetHypervisorType ( ) <EOL> elif self . op . mode == constants . IALLOCATOR_MODE_RELOC : <EOL> ( self . inst_uuid , self . op . name ) = ExpandInstanceUuidAndName ( self . cfg , None , <EOL> self . op . name ) <EOL> self . relocate_from_node_uuids = list ( self . cfg . GetInstanceSecondaryNodes ( self . inst_uuid ) ) <EOL> elif self . op . mode in ( constants . IALLOCATOR_MODE_CHG_GROUP , <EOL> constants . IALLOCATOR_MODE_NODE_EVAC ) : <EOL> if not self . op . instances : <EOL> raise errors . OpPrereqError ( "<STR_LIT>" , errors . ECODE_INVAL ) <EOL> ( _ , self . op . instances ) = GetWantedInstances ( self , self . op . instances ) <EOL> else : <EOL> raise errors . OpPrereqError ( "<STR_LIT>" % <EOL> self . op . mode , errors . ECODE_INVAL ) <EOL> if self . op . direction == constants . IALLOCATOR_DIR_OUT : <EOL> if self . op . iallocator is None : <EOL> raise errors . OpPrereqError ( "<STR_LIT>" , <EOL> errors . ECODE_INVAL ) <EOL> def Exec ( self , feedback_fn ) : <EOL> """<STR_LIT>""" <EOL> if self . op . mode == constants . IALLOCATOR_MODE_ALLOC : <EOL> req = iallocator . IAReqInstanceAlloc ( name = self . op . name , <EOL> memory = self . op . memory , <EOL> disks = self . op . disks , <EOL> disk_template = self . op . disk_template , <EOL> group_name = self . op . group_name , <EOL> os = self . op . os , <EOL> tags = self . op . tags , <EOL> nics = self . op . nics , <EOL> vcpus = self . op . vcpus , <EOL> spindle_use = self . op . spindle_use , <EOL> hypervisor = self . op . hypervisor , <EOL> node_whitelist = None ) <EOL> elif self . op . mode == constants . IALLOCATOR_MODE_RELOC : <EOL> req = iallocator . IAReqRelocate ( <EOL> inst_uuid = self . inst_uuid , <EOL> relocate_from_node_uuids = list ( self . relocate_from_node_uuids ) ) <EOL> elif self . op . mode == constants . IALLOCATOR_MODE_CHG_GROUP : <EOL> req = iallocator . IAReqGroupChange ( instances = self . op . instances , <EOL> target_groups = self . op . target_groups ) <EOL> elif self . op . mode == constants . IALLOCATOR_MODE_NODE_EVAC : <EOL> req = iallocator . IAReqNodeEvac ( instances = self . op . instances , <EOL> evac_mode = self . op . evac_mode , <EOL> ignore_soft_errors = False ) <EOL> elif self . op . mode == constants . IALLOCATOR_MODE_MULTI_ALLOC : <EOL> disk_template = self . op . disk_template <EOL> insts = [ iallocator . IAReqInstanceAlloc ( name = "<STR_LIT>" % ( self . op . name , idx ) , <EOL> memory = self . op . memory , <EOL> disks = self . op . disks , <EOL> disk_template = disk_template , <EOL> group_name = self . op . group_name , <EOL> os = self . op . os , <EOL> tags = self . op . tags , <EOL> nics = self . op . nics , <EOL> vcpus = self . op . vcpus , <EOL> spindle_use = self . op . spindle_use , <EOL> hypervisor = self . op . hypervisor , <EOL> node_whitelist = None ) <EOL> for idx in range ( self . op . count ) ] <EOL> req = iallocator . IAReqMultiInstanceAlloc ( instances = insts ) <EOL> else : <EOL> raise errors . ProgrammerError ( "<STR_LIT>" <EOL> "<STR_LIT>" , self . op . mode ) <EOL> ial = iallocator . IAllocator ( self . cfg , self . rpc , req ) <EOL> if self . op . direction == constants . IALLOCATOR_DIR_IN : <EOL> result = ial . in_text <EOL> else : <EOL> ial . Run ( self . op . iallocator , validate = False ) <EOL> result = ial . out_text <EOL> return result </s>
