diff options
author | Robert Gogolok <gogo@cs.uni-sb.de> | 2007-12-30 19:26:51 +0000 |
---|---|---|
committer | Robert Gogolok <gogo@cs.uni-sb.de> | 2007-12-30 19:26:51 +0000 |
commit | e8c9ba57d10d174c79ac1aae9b53661ee8464d0f (patch) | |
tree | 65cb5f7b0c699e6c97eb365ec83c76155a023f7e /src/lib/Settings.py | |
parent | 73ba22f7e519694dbf7010810de69b3b845f634f (diff) | |
download | bcfg2-e8c9ba57d10d174c79ac1aae9b53661ee8464d0f.tar.gz bcfg2-e8c9ba57d10d174c79ac1aae9b53661ee8464d0f.tar.bz2 bcfg2-e8c9ba57d10d174c79ac1aae9b53661ee8464d0f.zip |
Settings class to remove redundant code for parsing config file.
If a module wants to access bcfg2 settings:
from Bcfg2.Settings import settings
git-svn-id: https://svn.mcs.anl.gov/repos/bcfg/trunk/bcfg2@4131 ce84e21b-d406-0410-9b95-82705330c041
Diffstat (limited to 'src/lib/Settings.py')
-rw-r--r-- | src/lib/Settings.py | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/src/lib/Settings.py b/src/lib/Settings.py new file mode 100644 index 000000000..4a47c6f8d --- /dev/null +++ b/src/lib/Settings.py @@ -0,0 +1,145 @@ +""" +Settings for bcfg2. +FIXME: simplify code! +FIXME: add statistics configuration +""" + +__revision__ = '$Revision$' + +import logging, socket, ConfigParser + +class Settings(object): + + def __init__(self): + self.CONFIG_FILE = self.default_config_file() + + self.SERVER_GENERATORS = self.default_server_generators() + self.SERVER_REPOSITORY = self.default_server_repository() + self.SERVER_STRUCTURES = self.default_server_structures() + self.SERVER_SVN = self.default_server_svn() + + self.COMMUNICATION_KEY = self.default_communication_key() + self.COMMUNICATION_PASSWORD = self.default_communication_password() + self.COMMUNICATION_PROTOCOL = self.default_communication_protocol() + self.COMMUNICATION_USER = self.default_communication_user() + + self.COMPONENTS_BCFG2 = self.default_components_bcfg2() + self.COMPONENTS_BCFG2_STATIC = self.default_components_bcfg2_static() + + + def __getattr__(self, name): + print "name = %s\n" % name + if name == '__members__': + return self.name() + return getattr(self, name) + + def read_config_file(self, filename): + + logger = logging.getLogger('bcfg2 settings') + + # set config file + if not filename: + logger.info("No config file given. Trying default config file '%s'." % self.CONFIG_FILE) + else: + logger.debug("Trying user specified config file '%s'." % filename) + self.CONFIG_FILE = filename + + # open config file + try: + cf = open(self.CONFIG_FILE) + except IOError: + logger.info("Skipping not accessable config file '%s'." % self.CONFIG_FILE) + return + + # parse config file + cfp = ConfigParser.ConfigParser() + try: + cfp.readfp(cf) + except Exception, e: + logger.error("Content of config file '%s' is not valid. Correct it!\n%s\n" % (self.CONFIG_FILE, e)) + raise SystemExit, 1 + # communication config + if cfp.has_section('communication'): + try: + self.COMMUNICATION_PROTOCOL = cfp.get('communication','protocol') + except: + pass + try: + self.COMMUNICATION_PASSWORD = cfp.get('communication','password') + except: + pass + try: + self.COMMUNICATION_KEY = cfp.get('communication','key') + except: + pass + try: + self.COMMUNICATION_USER = cfp.get('communication','user') + except: + pass + # components config + if cfp.has_section('components'): + try: + self.COMPONENTS_BCFG2 = cfp.get('components', 'bcfg2') + self.COMPONENTS_BCFG2_STATIC = True + except: + pass + # server config + if cfp.has_section('server'): + try: + self.SERVER_GENERATORS = cfp.get('server','generators').replace(' ','').split(',') + except: + pass + try: + self.SERVER_REPOSITORY = cfp.get('server','repository') + except: + pass + try: + self.SERVER_STRUCTURES = cfp.get('server','structures').replace(' ','').split(',') + except: + pass + try: + self.SERVER_SVN = cfp.get('server','svn') + except: + pass + + return + + def default_config_file(self): + return '/etc/bcfg2.conf' + + def default_server_generators(self): + return ['SSHbase', 'Cfg', 'Pkgmgr', 'Rules'] + + def default_server_structures(self): + return ['Bundler', 'Base'] + + def default_server_repository(self): + return '/var/lib/bcfg2/' + + def default_communication_key(self): + return False + + def default_communication_password(self): + return 'password' + + def default_communication_protocol(self): + return 'xmlrpc/ssl' + + def default_communication_user(self): + return 'root' + + def default_components_bcfg2(self): + return (socket.gethostname(), 0) + + def default_components_bcfg2_static(self): + return False + + def default_sendmail_path(self): + return '/usr/sbin/sendmail' + + def default_server_svn(self): + return None + + + +settings = Settings() |