summaryrefslogtreecommitdiff
path: root/pykolab/confmgmt/__init__.py
blob: 8958ead8cbbb0b6082e50248e796bd7548f0fb26 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# -*- coding: utf-8 -*-
#
# Copyright 2010-2012 Kolab Systems AG (http://www.kolabsys.com)
#
# Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen a kolabsys.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 3 or, at your option, any later version
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

import pykolab

log = pykolab.getLogger('pykolab.confmgmt')

from pykolab.confmgmt.db import get_db

from pykolab.confmgmt.model import Environment
from pykolab.confmgmt.model import File
from pykolab.confmgmt.model import Node
from pykolab.confmgmt.model import Package
from pykolab.confmgmt.model import Role
from pykolab.confmgmt.model import Service
from pykolab.confmgmt.model import Setting

def add_environment(name=None):
    db = get_db()

    if not name == None:
        environment = db.query(Environment).filter_by(name=name).first()
        if not environment == None:
            return environment

    environments = db.query(Environment).all()

    if len(environments) == 0 and name == None:
        return add_environment('development')
    else:
        db.add(Environment(name=name))
        db.commit()

    environment = db.query(Environment).filter_by(name=name).first()

    return environment

def add_file(file_path, tech=None):
    log.debug("Adding file %r" % (file_path))
    db = get_db()

    _file = db.query(File).filter_by(path=file_path).first()

    if _file == None:
        db.add(File(path=file_path, tech=tech))
        db.commit()

    _file = db.query(File).filter_by(path=file_path).first()

    return _file

def add_file_setting(_file, setting):
    db = get_db()
    _file.settings.append(setting)
    db.commit()

def add_node(node_fqdn):
    log.debug("Adding node %r" % (node_fqdn))
    db = get_db()
    node = db.query(Node).filter_by(fqdn=node_fqdn).first()

    if node == None:
        db.add(Node(fqdn=node_fqdn))
        db.commit()

    node = db.query(Node).filter_by(fqdn=node_fqdn).first()

    node.environment = add_environment()

    db.commit()

    return node

def add_node_role(node, role):
    log.debug("Adding node %r role %r" % (node.fqdn, role.name))
    db = get_db()
    node.roles.append(role)
    db.commit()

def add_package():
    pass

def add_package_file():
    pass

def add_package_service():
    pass

def add_role(role):
    log.debug("Adding role %r" % (role))
    db = get_db()

    _role = db.query(Role).filter_by(name=role).first()
    if _role == None:
        db.add(Role(name=role))
        db.commit()

    _role = db.query(Role).filter_by(name=role).first()

    return _role

def add_role_service(_role,_service):
    log.debug("Adding role %r service %r" % (_role.name, _service.name))
    db = get_db()

    _role.services.append(_service)

    db.commit()

def add_role_setting():
    pass

def add_service(service):
    db = get_db()

    _service = db.query(Service).filter_by(name=service).first()

    if _service == None:
        db.add(Service(name=service))
        db.commit()

    _service = db.query(Service).filter_by(name=service).first()

    return _service

def add_service_file(service, _file):
    db = get_db()
    service.files.append(_file)
    db.commit()

def add_setting(key, value, function):
    db = get_db()

    _setting = db.query(Setting).filter_by(key=key, value=value, function=function).first()

    if _setting == None:
        db.add(Setting(key=key, value=value, function=function))
        db.commit()

    _setting = db.query(Setting).filter_by(key=key, value=value, function=function).first()

    return _setting

def _add_setting(file_path, key, value=None, function=None, service_name=None, role_name=None, tech=None):
    if not service_name == None:
        log.debug(_("Searching for service with name %s") % (service_name), level=8)
        service = add_service(service_name)
        if not service == None:
            log.debug(_("Found service %s") % (service_name), level=8)
    else:
        service = None

    if not role_name == None:
        log.debug(_("Searching for role with name %s") % (role_name), level=8)
        role = add_role(role_name)
        if not role == None:
            log.debug(_("Found role %s") % (role_name), level=8)
    else:
        role = None

    log.debug(_("Searching for file with path %s") % (file_path), level=8)
    _file = add_file(file_path, tech)

    if not service_name == None:
        log.debug(_("Adding file with path %s to service with name %s") % (file_path, service_name), level=8)
        add_service_file(service, _file)

    log.debug(_("Adding setting with key %s (%r, %r)") % (key, value, function), level=8)
    setting = add_setting(key=key, value=value, function=function)

    log.debug(_("Adding setting %s to file with path %s") % (key, file_path), level=8)
    add_file_setting(_file, setting)

    if not role == None:
        log.debug(_("Adding role with name %s to setting with key %s") %(role_name, key), level=8)
        setting.roles.append(role)

def commit():
    db = get_db()
    db.commit()

def get_environment(name):
    """
        Return the environment record for the environment with name 'name'.
    """
    db = get_db()
    environment = db.query(Environment).filter_by(name=name).first()
    return environment

def get_file(path):
    """
        Return a file record for the file with path 'path'.
    """
    db = get_db()
    _file = db.query(File).filter_by(path=path).first()
    return _file

def get_node(fqdn):
    """
        Return a node record for the node with FQDN 'fqdn'.
    """
    db = get_db()
    node = db.query(Node).filter_by(fqdn=fqdn).first()
    return node

def get_node_files(fqdn):
    """
        Get a list of file records that are relevant for the node with FQDN 'fqdn'.
    """
    files = []

    node = get_node(fqdn)

    if node == None:
        return files

    for role in node.roles:
        for service in role.services:
            for _file in service.files:
                _settings = []

                for setting in _file.settings:
                    if len(setting.roles) > 0:
                        if (len([needle.name for needle in setting.roles if needle.name in [haystack.name for haystack in node.roles]]) > 0):
                            _settings.append(setting.key)
                    else:
                        _settings.append(setting.key)

                if len(_settings) > 0:
                    files.append(_file)

    return files

def get_role(name):
    """
        Return a role record for the role with name 'name'.
    """
    db = get_db()
    role = db.query(Role).filter_by(name=name).first()
    return role

def get_service(name):
    """
        Return the service record for the service with name 'name'.
    """
    db = get_db()
    service = db.query(Service).filter_by(name=name).first()
    return service

def get_setting(key, path, roles=[]):
    """
        Return the setting record for setting 'key' in file 'file_path'.
    """
    db = get_db()
    _file = db.query(File).filter_by(path=path).first()
    setting = db.query(Setting).filter_by(key=key).filter(Setting.files.contains(_file)).first()
    return setting

def list_environments():
    """
        List environments.
    """
    db = get_db()
    environments = db.query(Environment).all()
    return environments

def list_files():
    """
        List files.
    """
    db = get_db()
    files = db.query(File).all()
    return files

def list_nodes():
    """
        List nodes.
    """
    db = get_db()
    nodes = db.query(Node).all()
    return nodes

def list_roles():
    """
        List roles.
    """
    db = get_db()
    roles = db.query(Role).all()
    return roles

def list_services():
    """
        List services.
    """
    db = get_db()
    services = db.query(Service).all()
    return services

def remove_environment(environment):
    db = get_db()

    db.delete(environment)
    db.commit()

def remove_node_role(node, role):
    log.debug("Removing node %r role %r" % (node.fqdn, role.name))
    db = get_db()
    node.roles.pop(node.roles.index(role))
    db.commit()

def remove_role(role):
    db = get_db()

    db.delete(role)
    db.commit()