summaryrefslogtreecommitdiffstats
path: root/src/lib/Server/Plugins/ExtraGroups.py
blob: e2a9a559151e243c591c43355364c7b73494398d (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
"""
The ExtraGroups plugin allows for assigning <Client>s to <Group>s in the
Metadata/groups.xml file.

Written by Holger Weiss <holger@ZEDAT.FU-Berlin.DE>.
"""

import os
import lxml.etree
import Bcfg2.Server.Plugin

class ExtraGroups(Bcfg2.Server.Plugin.Plugin, Bcfg2.Server.Plugin.Connector):
    name = 'ExtraGroups'
    version = '1'

    def __init__(self, core, datastore):
        Bcfg2.Server.Plugin.Plugin.__init__(self, core, datastore)
        Bcfg2.Server.Plugin.Connector.__init__(self)
        self.groups_xml = os.path.dirname(self.data) + '/Metadata/groups.xml'

    def get_additional_groups(self, metadata):
        try:
            xdata = lxml.etree.parse(self.groups_xml)
        except lxml.etree.XMLSyntaxError:
            self.logger.error('Failed to parse %s' % self.groups_xml)
            return
        includes = [ent.get('href') for ent in
                    xdata.findall('./{http://www.w3.org/2001/XInclude}include')]
        if includes:
            try:
                xdata.xinclude()
            except lxml.etree.XIncludeError:
                self.logger.error('Failed to process XInclude for file %s' %
                                  self.groups_xml)
        extra_groups = []
        for group in xdata.xpath('//Groups/Group'):
            if metadata.hostname in [item.get('name')
                                     for item in group.findall('./Client')]:
                extra_groups.append(group.get('name'))
                extra_groups.extend([item.get('name')
                                     for item in group.findall('./Group')])
        return extra_groups