diff options
Diffstat (limited to 'pykolab/cli/conf/cmd_file_apply_settings.py')
-rw-r--r-- | pykolab/cli/conf/cmd_file_apply_settings.py | 172 |
1 files changed, 172 insertions, 0 deletions
diff --git a/pykolab/cli/conf/cmd_file_apply_settings.py b/pykolab/cli/conf/cmd_file_apply_settings.py new file mode 100644 index 0000000..cfeb481 --- /dev/null +++ b/pykolab/cli/conf/cmd_file_apply_settings.py @@ -0,0 +1,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() |