<s> """<STR_LIT>""" <EOL> import sys <EOL> import errno <EOL> import logging <EOL> from ganeti import errors <EOL> from ganeti import constants <EOL> from ganeti import utils <EOL> from ganeti import netutils <EOL> from ganeti import pathutils <EOL> SSCONF_LOCK_TIMEOUT = <NUM_LIT:10> <EOL> _VALID_KEYS = constants . VALID_SS_KEYS <EOL> _MAX_SIZE = <NUM_LIT> * <NUM_LIT> <EOL> def ReadSsconfFile ( filename ) : <EOL> """<STR_LIT>""" <EOL> statcb = utils . FileStatHelper ( ) <EOL> data = utils . ReadFile ( filename , size = _MAX_SIZE , preread = statcb ) <EOL> if statcb . st . st_size > _MAX_SIZE : <EOL> msg = ( "<STR_LIT>" % <EOL> ( filename , statcb . st . st_size , _MAX_SIZE ) ) <EOL> raise RuntimeError ( msg ) <EOL> return data . rstrip ( "<STR_LIT:\n>" ) <EOL> class SimpleStore ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cfg_location = None , _lockfile = pathutils . SSCONF_LOCK_FILE ) : <EOL> if cfg_location is None : <EOL> self . _cfg_dir = pathutils . DATA_DIR <EOL> else : <EOL> self . _cfg_dir = cfg_location <EOL> self . _lockfile = _lockfile <EOL> def KeyToFilename ( self , key ) : <EOL> """<STR_LIT>""" <EOL> if key not in _VALID_KEYS : <EOL> raise errors . ProgrammerError ( "<STR_LIT>" <EOL> % str ( key ) ) <EOL> filename = self . _cfg_dir + "<STR_LIT:/>" + constants . SSCONF_FILEPREFIX + key <EOL> return filename <EOL> def _ReadFile ( self , key , default = None ) : <EOL> """<STR_LIT>""" <EOL> filename = self . KeyToFilename ( key ) <EOL> try : <EOL> return ReadSsconfFile ( filename ) <EOL> except EnvironmentError , err : <EOL> if err . errno == errno . ENOENT and default is not None : <EOL> return default <EOL> raise errors . ConfigurationError ( "<STR_LIT>" % <EOL> ( filename , str ( err ) ) ) <EOL> def ReadAll ( self ) : <EOL> """<STR_LIT>""" <EOL> result = [ ] <EOL> for key in _VALID_KEYS : <EOL> try : <EOL> value = self . _ReadFile ( key ) <EOL> except errors . ConfigurationError : <EOL> pass <EOL> else : <EOL> result . append ( ( key , value ) ) <EOL> return dict ( result ) <EOL> def WriteFiles ( self , values , dry_run = False ) : <EOL> """<STR_LIT>""" <EOL> ssconf_lock = utils . FileLock . Open ( self . _lockfile ) <EOL> ssconf_lock . Exclusive ( blocking = True , timeout = SSCONF_LOCK_TIMEOUT ) <EOL> try : <EOL> for name , value in values . iteritems ( ) : <EOL> if isinstance ( value , ( list , tuple ) ) : <EOL> value = "<STR_LIT:\n>" . join ( value ) <EOL> if value and not value . endswith ( "<STR_LIT:\n>" ) : <EOL> value += "<STR_LIT:\n>" <EOL> if len ( value ) > _MAX_SIZE : <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( name , len ( value ) , _MAX_SIZE ) ) <EOL> raise errors . ConfigurationError ( msg ) <EOL> utils . WriteFile ( self . KeyToFilename ( name ) , data = value , <EOL> mode = constants . SS_FILE_PERMS , <EOL> dry_run = dry_run ) <EOL> finally : <EOL> ssconf_lock . Unlock ( ) <EOL> def GetFileList ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ self . KeyToFilename ( key ) for key in _VALID_KEYS ] <EOL> def GetClusterName ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _ReadFile ( constants . SS_CLUSTER_NAME ) <EOL> def GetFileStorageDir ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _ReadFile ( constants . SS_FILE_STORAGE_DIR ) <EOL> def GetSharedFileStorageDir ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _ReadFile ( constants . SS_SHARED_FILE_STORAGE_DIR ) <EOL> def GetGlusterStorageDir ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _ReadFile ( constants . SS_GLUSTER_STORAGE_DIR ) <EOL> def GetMasterCandidates ( self ) : <EOL> """<STR_LIT>""" <EOL> data = self . _ReadFile ( constants . SS_MASTER_CANDIDATES ) <EOL> nl = data . splitlines ( False ) <EOL> return nl <EOL> def GetMasterCandidatesIPList ( self ) : <EOL> """<STR_LIT>""" <EOL> data = self . _ReadFile ( constants . SS_MASTER_CANDIDATES_IPS ) <EOL> nl = data . splitlines ( False ) <EOL> return nl <EOL> def _GetDictOfSsconfMap ( self , ss_file_key ) : <EOL> """<STR_LIT>""" <EOL> data = self . _ReadFile ( ss_file_key ) <EOL> lines = data . splitlines ( False ) <EOL> mapping = { } <EOL> for line in lines : <EOL> ( key , value ) = line . split ( "<STR_LIT:=>" ) <EOL> mapping [ key ] = value <EOL> return mapping <EOL> def GetMasterCandidatesCertMap ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _GetDictOfSsconfMap ( constants . SS_MASTER_CANDIDATES_CERTS ) <EOL> def GetSshPortMap ( self ) : <EOL> """<STR_LIT>""" <EOL> return dict ( [ ( node_name , int ( ssh_port ) ) for <EOL> node_name , ssh_port in <EOL> self . _GetDictOfSsconfMap ( constants . SS_SSH_PORTS ) . items ( ) ] ) <EOL> def GetMasterIP ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _ReadFile ( constants . SS_MASTER_IP ) <EOL> def GetMasterNetdev ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _ReadFile ( constants . SS_MASTER_NETDEV ) <EOL> def GetMasterNetmask ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . _ReadFile ( constants . SS_MASTER_NETMASK ) <EOL> except errors . ConfigurationError : <EOL> family = self . GetPrimaryIPFamily ( ) <EOL> ipcls = netutils . IPAddress . GetClassFromIpFamily ( family ) <EOL> return ipcls . iplen <EOL> def GetMasterNode ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _ReadFile ( constants . SS_MASTER_NODE ) <EOL> def GetNodeList ( self ) : <EOL> """<STR_LIT>""" <EOL> data = self . _ReadFile ( constants . SS_NODE_LIST ) <EOL> nl = data . splitlines ( False ) <EOL> return nl <EOL> def GetOnlineNodeList ( self ) : <EOL> """<STR_LIT>""" <EOL> data = self . _ReadFile ( constants . SS_ONLINE_NODES ) <EOL> nl = data . splitlines ( False ) <EOL> return nl <EOL> def GetNodePrimaryIPList ( self ) : <EOL> """<STR_LIT>""" <EOL> data = self . _ReadFile ( constants . SS_NODE_PRIMARY_IPS ) <EOL> nl = data . splitlines ( False ) <EOL> return nl <EOL> def GetNodeSecondaryIPList ( self ) : <EOL> """<STR_LIT>""" <EOL> data = self . _ReadFile ( constants . SS_NODE_SECONDARY_IPS ) <EOL> nl = data . splitlines ( False ) <EOL> return nl <EOL> def GetNodesVmCapable ( self ) : <EOL> """<STR_LIT>""" <EOL> data = self . _ReadFile ( constants . SS_NODE_VM_CAPABLE ) <EOL> vm_capable = { } <EOL> for line in data . splitlines ( False ) : <EOL> ( node_uuid , node_vm_capable ) = line . split ( "<STR_LIT:=>" ) <EOL> vm_capable [ node_uuid ] = node_vm_capable == "<STR_LIT:True>" <EOL> return vm_capable <EOL> def GetNodegroupList ( self ) : <EOL> """<STR_LIT>""" <EOL> data = self . _ReadFile ( constants . SS_NODEGROUPS ) <EOL> nl = data . splitlines ( False ) <EOL> return nl <EOL> def GetNetworkList ( self ) : <EOL> """<STR_LIT>""" <EOL> data = self . _ReadFile ( constants . SS_NETWORKS ) <EOL> nl = data . splitlines ( False ) <EOL> return nl <EOL> def GetClusterTags ( self ) : <EOL> """<STR_LIT>""" <EOL> data = self . _ReadFile ( constants . SS_CLUSTER_TAGS ) <EOL> nl = data . splitlines ( False ) <EOL> return nl <EOL> def GetHypervisorList ( self ) : <EOL> """<STR_LIT>""" <EOL> data = self . _ReadFile ( constants . SS_HYPERVISOR_LIST ) <EOL> nl = data . splitlines ( False ) <EOL> return nl <EOL> def GetHvparamsForHypervisor ( self , hvname ) : <EOL> """<STR_LIT>""" <EOL> return self . _GetDictOfSsconfMap ( constants . SS_HVPARAMS_PREF + hvname ) <EOL> def GetHvparams ( self ) : <EOL> """<STR_LIT>""" <EOL> all_hvparams = { } <EOL> for hv in constants . HYPER_TYPES : <EOL> all_hvparams [ hv ] = self . GetHvparamsForHypervisor ( hv ) <EOL> return all_hvparams <EOL> def GetMaintainNodeHealth ( self ) : <EOL> """<STR_LIT>""" <EOL> data = self . _ReadFile ( constants . SS_MAINTAIN_NODE_HEALTH ) <EOL> return data == "<STR_LIT:True>" <EOL> def GetUidPool ( self ) : <EOL> """<STR_LIT>""" <EOL> data = self . _ReadFile ( constants . SS_UID_POOL ) <EOL> return data <EOL> def GetPrimaryIPFamily ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return int ( self . _ReadFile ( constants . SS_PRIMARY_IP_FAMILY , <EOL> default = netutils . IP4Address . family ) ) <EOL> except ( ValueError , TypeError ) , err : <EOL> raise errors . ConfigurationError ( "<STR_LIT>" <EOL> "<STR_LIT>" % err ) <EOL> def GetEnabledUserShutdown ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _ReadFile ( constants . SS_ENABLED_USER_SHUTDOWN ) == "<STR_LIT:True>" <EOL> def WriteSsconfFiles ( values , dry_run = False ) : <EOL> """<STR_LIT>""" <EOL> SimpleStore ( ) . WriteFiles ( values , dry_run = dry_run ) <EOL> def GetMasterAndMyself ( ss = None ) : <EOL> """<STR_LIT>""" <EOL> if ss is None : <EOL> ss = SimpleStore ( ) <EOL> return ss . GetMasterNode ( ) , netutils . Hostname . GetSysName ( ) <EOL> def CheckMaster ( debug , ss = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> master_name , myself = GetMasterAndMyself ( ss ) <EOL> except errors . ConfigurationError , err : <EOL> print "<STR_LIT>" % str ( err ) <EOL> sys . exit ( constants . EXIT_NODESETUP_ERROR ) <EOL> except errors . ResolverError , err : <EOL> sys . stderr . write ( "<STR_LIT>" % err . args [ <NUM_LIT:0> ] ) <EOL> sys . exit ( constants . EXIT_NODESETUP_ERROR ) <EOL> if myself != master_name : <EOL> if debug : <EOL> sys . stderr . write ( "<STR_LIT>" ) <EOL> sys . exit ( constants . EXIT_NOTMASTER ) <EOL> def VerifyClusterName ( name , _cfg_location = None ) : <EOL> """<STR_LIT>""" <EOL> sstore = SimpleStore ( cfg_location = _cfg_location ) <EOL> try : <EOL> local_name = sstore . GetClusterName ( ) <EOL> except errors . ConfigurationError , err : <EOL> logging . debug ( "<STR_LIT>" , err ) <EOL> else : <EOL> if name != local_name : <EOL> raise errors . GenericError ( "<STR_LIT>" % local_name ) <EOL> def VerifyKeys ( keys ) : <EOL> """<STR_LIT>""" <EOL> invalid = frozenset ( keys ) - _VALID_KEYS <EOL> if invalid : <EOL> raise errors . GenericError ( "<STR_LIT>" % <EOL> utils . CommaJoin ( sorted ( invalid ) ) ) </s>
