summaryrefslogtreecommitdiff
path: root/pykolab/cli/conf/cmd_file_apply_settings.py
blob: cfeb4814cab21a0a2964c01bc394a8af59d2454b (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
# -*- 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.
#

from augeas import Augeas
from Cheetah.Template import Template

import sys
import time

import pykolab

from pykolab import confmgmt
from pykolab import constants
from pykolab.cli import commands
from pykolab.confmgmt.db import get_db
from pykolab.translate import _

log = pykolab.getLogger('pykolab.cli')
conf = pykolab.getConf()

def __init__():
    commands.register('file_apply_settings', execute, group='conf', description=description())

def cli_options():
    my_option_group = conf.add_cli_parser_option_group(_("CLI Options"))
    my_option_group.add_option(
                '--file',
                dest    = "_file",
                action  = "store",
                default = None,
                metavar = "FILE",
                help    = _("Add file FILE")
            )

def description():
    return """Apply managed settings to file FILE."""

def execute(*args, **kw):
    if conf._file == None:
        print >> sys.stderr, _("Must specify a file.")
        sys.exit(1)

    _file = confmgmt.get_file(conf._file)

    if _file == None:
        print >> sys.stderr, _("File %s is not managed.") % (conf._file)
        sys.exit(1)

    if len(_file.settings) < 1:
        print >> sys.stderr, _("No managed settings for file %s.") % (conf._file)
        sys.exit(1)

    node = confmgmt.get_node(constants.fqdn)

    if node == None:
        print >> sys.stderr, _("This node (%s) is not managed.") % (constants.fqdn)
        sys.exit(1)

    myaugeas = Augeas()
    myaugeas.set('/augeas/load/Postfix_LDAP/lens', '@Postfix_Main')
    myaugeas.set('/augeas/load/Postfix_LDAP/incl', "/etc/postfix/ldap/*.cf")
    myaugeas.load()

    for role in node.roles:
        #print role
        for service in role.services:
            #print service
            for _file in service.files:
                #print _file
                if _file.path == conf._file:
                    _cheetah_searchlist = {}
                    for setting in _file.settings:
                        #print "node role names", [haystack.name for haystack in node.roles]
                        #print "setting role names", [needle.name for needle in setting.roles]
                        #print [needle.name for needle in setting.roles if needle.name in [haystack.name for haystack in node.roles]]
                        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):
                                if not setting.function == None:
                                    exec("retval = %s" % (setting.function))
                                    if isinstance(retval, list):
                                        new_setting = ' '.join(retval)
                                    else:
                                        new_setting = retval

                                    if _file.tech == 'augeas':
                                        current_value = myaugeas.get('/files%s/%s' % (_file.path, setting.key))
                                        if not new_setting == current_value:
                                            myaugeas.set('/files%s/%s' % (_file.path, setting.key), new_setting)
                                    elif _file.tech == 'cheetah':
                                        _cheetah_searchlist[setting.key] = new_setting

                                else:
                                    if _file.tech == 'augeas':
                                        current_value = myaugeas.get('/files%s/%s' % (_file.path, setting.key))
                                        if not setting.value == current_value:
                                            myaugeas.set('/files%s/%s' % (_file.path, setting.key), setting.value)
                                    elif _file.tech == 'cheetah':
                                        _cheetah_searchlist[setting.key] = setting.value
                        else:
                            if not setting.function == None:
                                exec("retval = %s" % (setting.function))
                                if isinstance(retval, list):
                                    new_setting = ' '.join(retval)
                                else:
                                    new_setting = retval

                                if _file.tech == 'augeas':
                                    current_value = myaugeas.get('/files%s/%s' % (_file.path, setting.key))
                                    if not new_setting == current_value:
                                        myaugeas.set('/files%s/%s' % (_file.path, setting.key), new_setting)
                                elif _file.tech == 'cheetah':
                                    _cheetah_searchlist[setting.key] = new_setting

                            else:
                                if _file.tech == 'augeas':
                                    current_value = myaugeas.get('/files%s/%s' % (_file.path, setting.key))
                                    if not setting.value == current_value:
                                        myaugeas.set('/files%s/%s' % (_file.path, setting.key), setting.value)
                                elif _file.tech == 'cheetah':
                                    _cheetah_searchlist[setting.key] = setting.value

                    if _file.tech == 'cheetah':
                        _cheetah_searchlist['conf'] = conf

                        _file_basename = os.path.basename(_file.path)
                        if os.path.isfile('/etc/kolab/templates/roundcubemail/%s.tpl' % (_file_basename)):
                            template_file = '/etc/kolab/templates/roundcubemail/%s.tpl' % (_file_basename)
                        elif os.path.isfile('/usr/share/kolab/templates/roundcubemail/%s.tpl' % (_file_basename)):
                            template_file = '/usr/share/kolab/templates/roundcubemail/%s.tpl' % (_file_basename)
                        elif os.path.isfile(os.path.abspath(os.path.join(__file__, '..', '..', '..', 'share', 'templates', 'roundcubemail', '%s.tpl' % (_file_basename)))):
                            template_file = os.path.abspath(os.path.join(__file__, '..', '..', '..', 'share', 'templates', 'roundcubemail', '%s.tpl' % (_file_basename)))

                        if not template_file == None:
                            log.debug(_("Using template file %r") % (template_file), level=8)
                            fp = open(template_file, 'r')
                            template_definition = fp.read()
                            fp.close()

                            t = Template(template_definition, searchList=[_cheetah_searchlist])

                            print t.__dict__
                            print "Settings:", t._CHEETAH_requiredCheetahMethods

                            log.debug(
                                    _("Successfully compiled template %r, writing out to %r") % (
                                            template_file,
                                            '/etc/roundcubemail/%s' % (_file_basename)
                                        ),
                                    level=8
                                )

                            fp = open('/etc/roundcubemail/%s.new' % (_file_basename), 'w')
                            fp.write(t.__str__())
                            fp.close()
                    else:
                        myaugeas.save()