<s> """<STR_LIT>""" <EOL> import os <EOL> import re <EOL> import errno <EOL> import pwd <EOL> import time <EOL> import itertools <EOL> import select <EOL> import logging <EOL> import signal <EOL> from ganeti import errors <EOL> from ganeti import constants <EOL> from ganeti import compat <EOL> from ganeti import pathutils <EOL> from ganeti . utils . algo import * <EOL> from ganeti . utils . filelock import * <EOL> from ganeti . utils . hash import * <EOL> from ganeti . utils . io import * <EOL> from ganeti . utils . livelock import * <EOL> from ganeti . utils . log import * <EOL> from ganeti . utils . lvm import * <EOL> from ganeti . utils . mlock import * <EOL> from ganeti . utils . nodesetup import * <EOL> from ganeti . utils . process import * <EOL> from ganeti . utils . retry import * <EOL> from ganeti . utils . security import * <EOL> from ganeti . utils . storage import * <EOL> from ganeti . utils . tags import * <EOL> from ganeti . utils . text import * <EOL> from ganeti . utils . wrapper import * <EOL> from ganeti . utils . version import * <EOL> from ganeti . utils . x509 import * <EOL> from ganeti . utils . bitarrays import * <EOL> _VALID_SERVICE_NAME_RE = re . compile ( "<STR_LIT>" ) <EOL> UUID_RE = re . compile ( constants . UUID_REGEX ) <EOL> def ForceDictType ( target , key_types , allowed_values = None ) : <EOL> """<STR_LIT>""" <EOL> if allowed_values is None : <EOL> allowed_values = [ ] <EOL> if not isinstance ( target , dict ) : <EOL> msg = "<STR_LIT>" % target <EOL> raise errors . TypeEnforcementError ( msg ) <EOL> for key in target : <EOL> if key not in key_types : <EOL> msg = "<STR_LIT>" % key <EOL> raise errors . TypeEnforcementError ( msg ) <EOL> if target [ key ] in allowed_values : <EOL> continue <EOL> ktype = key_types [ key ] <EOL> if ktype not in constants . ENFORCEABLE_TYPES : <EOL> msg = "<STR_LIT>" % ( key , ktype ) <EOL> raise errors . ProgrammerError ( msg ) <EOL> if ktype in ( constants . VTYPE_STRING , constants . VTYPE_MAYBE_STRING ) : <EOL> if target [ key ] is None and ktype == constants . VTYPE_MAYBE_STRING : <EOL> pass <EOL> elif not isinstance ( target [ key ] , basestring ) : <EOL> if isinstance ( target [ key ] , bool ) and not target [ key ] : <EOL> target [ key ] = "<STR_LIT>" <EOL> else : <EOL> msg = "<STR_LIT>" % ( key , target [ key ] ) <EOL> raise errors . TypeEnforcementError ( msg ) <EOL> elif ktype == constants . VTYPE_BOOL : <EOL> if isinstance ( target [ key ] , basestring ) and target [ key ] : <EOL> if target [ key ] . lower ( ) == constants . VALUE_FALSE : <EOL> target [ key ] = False <EOL> elif target [ key ] . lower ( ) == constants . VALUE_TRUE : <EOL> target [ key ] = True <EOL> else : <EOL> msg = "<STR_LIT>" % ( key , target [ key ] ) <EOL> raise errors . TypeEnforcementError ( msg ) <EOL> elif target [ key ] : <EOL> target [ key ] = True <EOL> else : <EOL> target [ key ] = False <EOL> elif ktype == constants . VTYPE_SIZE : <EOL> try : <EOL> target [ key ] = ParseUnit ( target [ key ] ) <EOL> except errors . UnitParseError , err : <EOL> msg = "<STR_LIT>" % ( key , target [ key ] , err ) <EOL> raise errors . TypeEnforcementError ( msg ) <EOL> elif ktype == constants . VTYPE_INT : <EOL> try : <EOL> target [ key ] = int ( target [ key ] ) <EOL> except ( ValueError , TypeError ) : <EOL> msg = "<STR_LIT>" % ( key , target [ key ] ) <EOL> raise errors . TypeEnforcementError ( msg ) <EOL> elif ktype == constants . VTYPE_FLOAT : <EOL> try : <EOL> target [ key ] = float ( target [ key ] ) <EOL> except ( ValueError , TypeError ) : <EOL> msg = "<STR_LIT>" % ( key , target [ key ] ) <EOL> raise errors . TypeEnforcementError ( msg ) <EOL> def ValidateServiceName ( name ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> numport = int ( name ) <EOL> except ( ValueError , TypeError ) : <EOL> valid = _VALID_SERVICE_NAME_RE . match ( name ) <EOL> else : <EOL> valid = ( numport >= <NUM_LIT:0> and numport < ( <NUM_LIT:1> << <NUM_LIT:16> ) ) <EOL> if not valid : <EOL> raise errors . OpPrereqError ( "<STR_LIT>" % name , <EOL> errors . ECODE_INVAL ) <EOL> return name <EOL> def _ComputeMissingKeys ( key_path , options , defaults ) : <EOL> """<STR_LIT>""" <EOL> defaults_keys = frozenset ( defaults . keys ( ) ) <EOL> invalid = [ ] <EOL> for key , value in options . items ( ) : <EOL> if key_path : <EOL> new_path = "<STR_LIT>" % ( key_path , key ) <EOL> else : <EOL> new_path = key <EOL> if key not in defaults_keys : <EOL> invalid . append ( new_path ) <EOL> elif isinstance ( value , dict ) : <EOL> invalid . extend ( _ComputeMissingKeys ( new_path , value , defaults [ key ] ) ) <EOL> return invalid <EOL> def VerifyDictOptions ( options , defaults ) : <EOL> """<STR_LIT>""" <EOL> invalid = _ComputeMissingKeys ( "<STR_LIT>" , options , defaults ) <EOL> if invalid : <EOL> raise errors . OpPrereqError ( "<STR_LIT>" % <EOL> CommaJoin ( invalid ) , errors . ECODE_INVAL ) <EOL> def ListVolumeGroups ( ) : <EOL> """<STR_LIT>""" <EOL> command = "<STR_LIT>" <EOL> result = RunCmd ( command ) <EOL> retval = { } <EOL> if result . failed : <EOL> return retval <EOL> for line in result . stdout . splitlines ( ) : <EOL> try : <EOL> name , size = line . split ( ) <EOL> size = int ( float ( size ) ) <EOL> except ( IndexError , ValueError ) , err : <EOL> logging . error ( "<STR_LIT>" , err , line ) <EOL> continue <EOL> retval [ name ] = size <EOL> return retval <EOL> def BridgeExists ( bridge ) : <EOL> """<STR_LIT>""" <EOL> return os . path . isdir ( "<STR_LIT>" % bridge ) <EOL> def TryConvert ( fn , val ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> nv = fn ( val ) <EOL> except ( ValueError , TypeError ) : <EOL> nv = val <EOL> return nv <EOL> def ParseCpuMask ( cpu_mask ) : <EOL> """<STR_LIT>""" <EOL> if not cpu_mask : <EOL> return [ ] <EOL> cpu_list = [ ] <EOL> for range_def in cpu_mask . split ( "<STR_LIT:U+002C>" ) : <EOL> if range_def == constants . CPU_PINNING_ALL : <EOL> return [ constants . CPU_PINNING_ALL_VAL ] <EOL> boundaries = range_def . split ( "<STR_LIT:->" ) <EOL> n_elements = len ( boundaries ) <EOL> if n_elements > <NUM_LIT:2> : <EOL> raise errors . ParseError ( "<STR_LIT>" <EOL> "<STR_LIT>" % range_def ) <EOL> try : <EOL> lower = int ( boundaries [ <NUM_LIT:0> ] ) <EOL> except ( ValueError , TypeError ) , err : <EOL> raise errors . ParseError ( "<STR_LIT>" <EOL> "<STR_LIT>" % str ( err ) ) <EOL> try : <EOL> higher = int ( boundaries [ - <NUM_LIT:1> ] ) <EOL> except ( ValueError , TypeError ) , err : <EOL> raise errors . ParseError ( "<STR_LIT>" <EOL> "<STR_LIT>" % str ( err ) ) <EOL> if lower > higher : <EOL> raise errors . ParseError ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( lower , higher , range_def ) ) <EOL> cpu_list . extend ( range ( lower , higher + <NUM_LIT:1> ) ) <EOL> return cpu_list <EOL> def ParseMultiCpuMask ( cpu_mask ) : <EOL> """<STR_LIT>""" <EOL> if not cpu_mask : <EOL> return [ ] <EOL> cpu_list = [ ] <EOL> for range_def in cpu_mask . split ( constants . CPU_PINNING_SEP ) : <EOL> cpu_list . append ( sorted ( set ( ParseCpuMask ( range_def ) ) ) ) <EOL> return cpu_list <EOL> def GetHomeDir ( user , default = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if isinstance ( user , basestring ) : <EOL> result = pwd . getpwnam ( user ) <EOL> elif isinstance ( user , ( int , long ) ) : <EOL> result = pwd . getpwuid ( user ) <EOL> else : <EOL> raise errors . ProgrammerError ( "<STR_LIT>" % <EOL> type ( user ) ) <EOL> except KeyError : <EOL> return default <EOL> return result . pw_dir <EOL> def FirstFree ( seq , base = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> for idx , elem in enumerate ( seq ) : <EOL> assert elem >= base , "<STR_LIT>" <EOL> if elem > idx + base : <EOL> return idx + base <EOL> return None <EOL> def SingleWaitForFdCondition ( fdobj , event , timeout ) : <EOL> """<STR_LIT>""" <EOL> check = ( event | select . POLLPRI | <EOL> select . POLLNVAL | select . POLLHUP | select . POLLERR ) <EOL> if timeout is not None : <EOL> timeout *= <NUM_LIT:1000> <EOL> poller = select . poll ( ) <EOL> poller . register ( fdobj , event ) <EOL> try : <EOL> io_events = poller . poll ( timeout ) <EOL> except select . error , err : <EOL> if err [ <NUM_LIT:0> ] != errno . EINTR : <EOL> raise <EOL> io_events = [ ] <EOL> if io_events and io_events [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] & check : <EOL> return io_events [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> else : <EOL> return None <EOL> class FdConditionWaiterHelper ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , timeout ) : <EOL> self . timeout = timeout <EOL> def Poll ( self , fdobj , event ) : <EOL> result = SingleWaitForFdCondition ( fdobj , event , self . timeout ) <EOL> if result is None : <EOL> raise RetryAgain ( ) <EOL> else : <EOL> return result <EOL> def UpdateTimeout ( self , timeout ) : <EOL> self . timeout = timeout <EOL> def WaitForFdCondition ( fdobj , event , timeout ) : <EOL> """<STR_LIT>""" <EOL> if timeout is not None : <EOL> retrywaiter = FdConditionWaiterHelper ( timeout ) <EOL> try : <EOL> result = Retry ( retrywaiter . Poll , RETRY_REMAINING_TIME , timeout , <EOL> args = ( fdobj , event ) , wait_fn = retrywaiter . UpdateTimeout ) <EOL> except RetryTimeout : <EOL> result = None <EOL> else : <EOL> result = None <EOL> while result is None : <EOL> result = SingleWaitForFdCondition ( fdobj , event , timeout ) <EOL> return result <EOL> def EnsureDaemon ( name ) : <EOL> """<STR_LIT>""" <EOL> result = RunCmd ( [ pathutils . DAEMON_UTIL , "<STR_LIT>" , name ] ) <EOL> if result . failed : <EOL> logging . error ( "<STR_LIT>" , <EOL> name , result . fail_reason , result . output ) <EOL> return False <EOL> return True <EOL> def StopDaemon ( name ) : <EOL> """<STR_LIT>""" <EOL> result = RunCmd ( [ pathutils . DAEMON_UTIL , "<STR_LIT>" , name ] ) <EOL> if result . failed : <EOL> logging . error ( "<STR_LIT>" , <EOL> name , result . fail_reason , result . output ) <EOL> return False <EOL> return True <EOL> def SplitTime ( value ) : <EOL> """<STR_LIT>""" <EOL> ( seconds , microseconds ) = divmod ( int ( value * <NUM_LIT> ) , <NUM_LIT> ) <EOL> assert <NUM_LIT:0> <= seconds , "<STR_LIT>" % seconds <EOL> assert <NUM_LIT:0> <= microseconds <= <NUM_LIT> , "<STR_LIT>" % microseconds <EOL> return ( int ( seconds ) , int ( microseconds ) ) <EOL> def MergeTime ( timetuple ) : <EOL> """<STR_LIT>""" <EOL> ( seconds , microseconds ) = timetuple <EOL> assert <NUM_LIT:0> <= seconds , "<STR_LIT>" % seconds <EOL> assert <NUM_LIT:0> <= microseconds <= <NUM_LIT> , "<STR_LIT>" % microseconds <EOL> return float ( seconds ) + ( float ( microseconds ) * <NUM_LIT> ) <EOL> def EpochNano ( ) : <EOL> """<STR_LIT>""" <EOL> return int ( time . time ( ) * <NUM_LIT> ) <EOL> def FindMatch ( data , name ) : <EOL> """<STR_LIT>""" <EOL> if name in data : <EOL> return ( data [ name ] , [ ] ) <EOL> for key , value in data . items ( ) : <EOL> if hasattr ( key , "<STR_LIT>" ) : <EOL> m = key . match ( name ) <EOL> if m : <EOL> return ( value , list ( m . groups ( ) ) ) <EOL> return None <EOL> def GetMounts ( filename = constants . PROC_MOUNTS ) : <EOL> """<STR_LIT>""" <EOL> data = [ ] <EOL> mountlines = ReadFile ( filename ) . splitlines ( ) <EOL> for line in mountlines : <EOL> device , mountpoint , fstype , options , _ = line . split ( None , <NUM_LIT:4> ) <EOL> data . append ( ( device , mountpoint , fstype , options ) ) <EOL> return data <EOL> def SignalHandled ( signums ) : <EOL> """<STR_LIT>""" <EOL> def wrap ( fn ) : <EOL> def sig_function ( * args , ** kwargs ) : <EOL> assert "<STR_LIT>" not in kwargs or kwargs [ "<STR_LIT>" ] is None or isinstance ( kwargs [ "<STR_LIT>" ] , dict ) , "<STR_LIT>" <EOL> if "<STR_LIT>" in kwargs and kwargs [ "<STR_LIT>" ] is not None : <EOL> signal_handlers = kwargs [ "<STR_LIT>" ] <EOL> else : <EOL> signal_handlers = { } <EOL> kwargs [ "<STR_LIT>" ] = signal_handlers <EOL> sighandler = SignalHandler ( signums ) <EOL> try : <EOL> for sig in signums : <EOL> signal_handlers [ sig ] = sighandler <EOL> return fn ( * args , ** kwargs ) <EOL> finally : <EOL> sighandler . Reset ( ) <EOL> return sig_function <EOL> return wrap <EOL> def TimeoutExpired ( epoch , timeout , _time_fn = time . time ) : <EOL> """<STR_LIT>""" <EOL> return _time_fn ( ) > ( epoch + timeout ) <EOL> class SignalWakeupFd ( object ) : <EOL> try : <EOL> _set_wakeup_fd_fn = signal . set_wakeup_fd <EOL> except AttributeError : <EOL> def _SetWakeupFd ( self , _ ) : <EOL> return - <NUM_LIT:1> <EOL> else : <EOL> def _SetWakeupFd ( self , fd ) : <EOL> return self . _set_wakeup_fd_fn ( fd ) <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> ( read_fd , write_fd ) = os . pipe ( ) <EOL> self . _read_fh = os . fdopen ( read_fd , "<STR_LIT:r>" , <NUM_LIT:0> ) <EOL> self . _write_fh = os . fdopen ( write_fd , "<STR_LIT:w>" , <NUM_LIT:0> ) <EOL> self . _previous = self . _SetWakeupFd ( self . _write_fh . fileno ( ) ) <EOL> self . fileno = self . _read_fh . fileno <EOL> self . read = self . _read_fh . read <EOL> def Reset ( self ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( self , "<STR_LIT>" ) and self . _previous is not None : <EOL> self . _SetWakeupFd ( self . _previous ) <EOL> self . _previous = None <EOL> def Notify ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _write_fh . write ( chr ( <NUM_LIT:0> ) ) <EOL> def __del__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . Reset ( ) <EOL> class SignalHandler ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , signum , handler_fn = None , wakeup = None ) : <EOL> """<STR_LIT>""" <EOL> assert handler_fn is None or callable ( handler_fn ) <EOL> self . signum = set ( signum ) <EOL> self . called = False <EOL> self . _handler_fn = handler_fn <EOL> self . _wakeup = wakeup <EOL> self . _previous = { } <EOL> try : <EOL> for signum in self . signum : <EOL> prev_handler = signal . signal ( signum , self . _HandleSignal ) <EOL> try : <EOL> self . _previous [ signum ] = prev_handler <EOL> except : <EOL> signal . signal ( signum , prev_handler ) <EOL> raise <EOL> except : <EOL> self . Reset ( ) <EOL> raise <EOL> def __del__ ( self ) : <EOL> self . Reset ( ) <EOL> def Reset ( self ) : <EOL> """<STR_LIT>""" <EOL> for signum , prev_handler in self . _previous . items ( ) : <EOL> signal . signal ( signum , prev_handler ) <EOL> del self . _previous [ signum ] <EOL> def Clear ( self ) : <EOL> """<STR_LIT>""" <EOL> self . called = False <EOL> def _HandleSignal ( self , signum , frame ) : <EOL> """<STR_LIT>""" <EOL> self . called = True <EOL> if self . _wakeup : <EOL> self . _wakeup . Notify ( ) <EOL> if self . _handler_fn : <EOL> self . _handler_fn ( signum , frame ) <EOL> class FieldSet ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * items ) : <EOL> self . items = [ re . compile ( "<STR_LIT>" % value ) for value in items ] <EOL> def Extend ( self , other_set ) : <EOL> """<STR_LIT>""" <EOL> self . items . extend ( other_set . items ) <EOL> def Matches ( self , field ) : <EOL> """<STR_LIT>""" <EOL> for m in itertools . ifilter ( None , ( val . match ( field ) for val in self . items ) ) : <EOL> return m <EOL> return None <EOL> def NonMatching ( self , items ) : <EOL> """<STR_LIT>""" <EOL> return [ val for val in items if not self . Matches ( val ) ] <EOL> def ValidateDeviceNames ( kind , container ) : <EOL> """<STR_LIT>""" <EOL> valid = [ ] <EOL> for device in container : <EOL> if isinstance ( device , dict ) : <EOL> if kind == "<STR_LIT>" : <EOL> name = device . get ( constants . INIC_NAME , None ) <EOL> elif kind == "<STR_LIT>" : <EOL> name = device . get ( constants . IDISK_NAME , None ) <EOL> else : <EOL> raise errors . OpPrereqError ( "<STR_LIT>" % kind , <EOL> errors . ECODE_INVAL ) <EOL> else : <EOL> name = device . name <EOL> valid . append ( device . uuid ) <EOL> try : <EOL> int ( name ) <EOL> except ( ValueError , TypeError ) : <EOL> pass <EOL> else : <EOL> raise errors . OpPrereqError ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( name , kind ) , <EOL> errors . ECODE_INVAL ) <EOL> if name is not None and name . lower ( ) != constants . VALUE_NONE : <EOL> if name in valid : <EOL> raise errors . OpPrereqError ( "<STR_LIT>" % ( kind , name ) , <EOL> errors . ECODE_NOTUNIQUE ) <EOL> else : <EOL> valid . append ( name ) <EOL> def AllDiskOfType ( disks_info , dev_types ) : <EOL> """<STR_LIT>""" <EOL> assert not isinstance ( dev_types , str ) <EOL> if not disks_info and constants . DT_DISKLESS not in dev_types : <EOL> return False <EOL> for disk in disks_info : <EOL> if disk . dev_type not in dev_types : <EOL> return False <EOL> return True <EOL> def AnyDiskOfType ( disks_info , dev_types ) : <EOL> """<STR_LIT>""" <EOL> assert not isinstance ( dev_types , str ) <EOL> if not disks_info and constants . DT_DISKLESS in dev_types : <EOL> return True <EOL> for disk in disks_info : <EOL> if disk . dev_type in dev_types : <EOL> return True <EOL> return False <EOL> def GetDiskTemplateString ( disk_types ) : <EOL> """<STR_LIT>""" <EOL> disk_types = set ( dev_type for dev_type in disk_types ) <EOL> if not disk_types : <EOL> return constants . DT_DISKLESS <EOL> elif len ( disk_types ) > <NUM_LIT:1> : <EOL> return constants . DT_MIXED <EOL> else : <EOL> return disk_types . pop ( ) <EOL> def GetDiskTemplate ( disks_info ) : <EOL> """<STR_LIT>""" <EOL> return GetDiskTemplateString ( d . dev_type for d in disks_info ) </s>
