[bcfg2] On créer un nouveau plugin Bcfg2 'GlobalPython' à partir du plugin Python.
Ce nouveau plugin permet en plus du plugin Python d'avoir accès aux metadata de tous les clients Bcfg2. Ça peut être utile automatiser des configurations comme celles de Nagios ou babar pour lesquels on a pas envie de redécrire tout le réseau et les rôles des serveurs alors que c'est déjà dans Bcfg2. GlobalPython est a priori compatible avec Python (c'est à dire que pour les scripts passés au plugin Python, on aurait le même résultat avec GlobalPython) mais comme ça m'avait l'air dangereux de trop toucher au plugin Python (et pas facile à tester aussi), j'ai préféré le forker.
This commit is contained in:
parent
264df1f392
commit
328743f789
2 changed files with 771 additions and 0 deletions
88
bcfg2/plugins/GlobalPython.py
Normal file
88
bcfg2/plugins/GlobalPython.py
Normal file
|
@ -0,0 +1,88 @@
|
|||
# -*- mode: python; coding: utf-8 -*-
|
||||
#
|
||||
# GlobalPython.py: extension du plugin bcfg2 Python rajoutant des informations sur la configuration de bcfg2
|
||||
# ---------
|
||||
#
|
||||
# Copyright (C) 2013 Raphael Cauderlier <cauderlier@crans.org>
|
||||
#
|
||||
# This file 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; either version 2 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This file 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 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 Street #330, Boston, MA 02111-1307, USA.
|
||||
|
||||
""" Plugin pour bcfg2 pour générer des fichiers de conf en prenant la sortie d'un
|
||||
script python.
|
||||
Ce plugin est une extension du plugin Python rajoutant des informations sur la configuration de bcfg2.
|
||||
Les scripts python de génération de conf utilisant ce plugin peuvent par exemple lister les noms des clients
|
||||
appartenant à un groupe donné. C'est très pratique pour générer la conf de clients qui ont un rôle de serveur
|
||||
pour tous les autres clients en différenciant suivant leurs rôles.
|
||||
Exemples : serveur de sauvegarde, monitoring.
|
||||
"""
|
||||
|
||||
__all__ = ["GlobalPython"]
|
||||
|
||||
import Python
|
||||
import Bcfg2.Server.Plugins.Metadata
|
||||
import sys, binascii
|
||||
|
||||
sys.path.append('/usr/scripts/bcfg2')
|
||||
import pygen
|
||||
|
||||
class GlobalPython(Python.Python):
|
||||
"""The GlobalPython generator implements a templating mechanism for configuration files
|
||||
extending the PythonGenerator by access to metadata of all the clients"""
|
||||
name = 'Python'
|
||||
__version__ = '1.0'
|
||||
__author__ = 'cauderlier@crans.org'
|
||||
|
||||
def __init__(self, core, datastore):
|
||||
Python.Python.__init__(self, core, datastore)
|
||||
global_metadata = Bcfg2.Server.Plugins.Metadata.Metadata(core, datastore)
|
||||
|
||||
|
||||
def BuildEntry(self, entry, metadata):
|
||||
'''Construit le fichier'''
|
||||
code = self.codes[entry.get('name')]
|
||||
fname = entry.get('realname', entry.get('name'))
|
||||
debug("building config file: %s" % fname, 'blue')
|
||||
env = pygen.Environment()
|
||||
env["global_metadata"] = self.global_metadata
|
||||
env["metadata"] = metadata
|
||||
env["properties"] = self.properties
|
||||
env["include"] = lambda incfile: Python.include(env, incfile)
|
||||
env["dump"] = lambda incfile: Python.dump(env, incfile)
|
||||
env["info"] = { 'owner': 'root',
|
||||
'group': 'root',
|
||||
'perms': 0644 }
|
||||
env.included = set([])
|
||||
try:
|
||||
Python.include(env, "common")
|
||||
Python.include(env, "global_common")
|
||||
text = pygen.generate(code, env, logger)
|
||||
except Exception, e:
|
||||
Python.log_traceback(fname, 'exec', e)
|
||||
raise Bcfg2.Server.Plugin.PluginExecutionError
|
||||
info = env["info"]
|
||||
if info.get('encoding', '') == 'base64':
|
||||
text = binascii.b2a_base64(text)
|
||||
# lxml n'accepte que de l'ascii ou de l'unicode
|
||||
try:
|
||||
entry.text = text.decode("UTF-8")
|
||||
except:
|
||||
# solution de fallback
|
||||
entry.text = text.decode("ISO8859-15")
|
||||
Python.debug(entry.text)
|
||||
entry.attrib['owner'] = info.get('owner', 'root')
|
||||
entry.attrib['group'] = info.get('group', 'root')
|
||||
entry.attrib['perms'] = oct(info.get('perms', 0644))
|
||||
if 'encoding' in info:
|
||||
entry.attrib['encoding'] = info['encoding']
|
Loading…
Add table
Add a link
Reference in a new issue