summaryrefslogtreecommitdiffstats
path: root/src/lib/Settings.py
blob: 4a47c6f8d91eeecad9f120fdb6fe852666ea6f9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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()