summaryrefslogtreecommitdiffstats
path: root/src/lib/Server/Plugins/Base.py
blob: 1cdd7599c6e280d5f6bd3cb778941c8bfb20ff45 (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
'''This module sets up a base list of configuration entries'''
__revision__ = '$Revision$'

from copy import deepcopy
from lxml.etree import Element, XML, XMLSyntaxError, _Comment

from Bcfg2.Server.Plugin import Plugin, PluginInitError, SingleXMLFileBacked

class Base(Plugin, SingleXMLFileBacked):
    '''This Structure is good for the pile of independent configs needed for most actual systems'''
    __name__ =  'Base'
    __version__ = '$Id$'
    __author__ = 'bcfg-dev@mcs.anl.gov'
    
    '''base creates independent clauses based on client metadata'''
    def __init__(self, core, datastore):
        Plugin.__init__(self, core, datastore)
        self.store = {'all':[], 'Class':{'all':[]}, 'Image':{'all':[]}, 'all':[]}
        try:
            SingleXMLFileBacked.__init__(self, "%s/etc/base.xml"%(datastore), self.core.fam)
        except OSError:
            self.LogError("Failed to load base.xml")
            raise PluginInitError
        
    def Index(self):
        '''Store XML data in reasonable structures'''
        try:
            xdata = XML(self.data)
        except XMLSyntaxError:
            self.LogError("Failed to parse base.xml")
            return
        self.store = {'all':[], 'Class':{'all':[]}, 'Image':{'all':[]}, 'all':[]}
        for entry in [ent for ent in xdata.getchildren() if not isinstance(ent, _Comment)]:
            if entry.tag in ['Image', 'Class']:
                if not self.store[entry.tag].has_key(entry.get('name')):
                    self.store[entry.tag][entry.get('name')] = {'all':[], 'Class':{}, 'Image':{}}
                for child in [ent for ent in entry.getchildren() if not isinstance(ent, _Comment)]:
                    if child.tag in ['Image', 'Class']:
                        self.store[entry.tag][entry.get('name')][child.tag][child.get('name')] = \
                                                                                               [ent for ent in child.getchildren() if \
                                                                                                not isinstance(ent, _Comment)]
                    else:
                        self.store[entry.tag][entry.get('name')]['all'].append(child)
            else:
                self.store['all'].append(child)

    def BuildStructures(self, metadata):
        '''Build structures for client described by metadata'''
        ret = Element("Independant", version='2.0')
        [ret.append(deepcopy(entry)) for entry in self.store['all']]
        idata = self.store['Image'].get(metadata.image, {'all':[], 'Class':{}})
        for entry in idata['all']:
            ret.append(deepcopy(entry))
        for cls in metadata.classes:
            for entry in idata['Class'].get(cls, []):
                ret.append(deepcopy(entry))
            cdata = self.store['Class'].get(cls, {'all':[], 'Image':{}})
            for entry in cdata['all']:
                ret.append(deepcopy(entry))
            for entry in cdata['Image'].get(metadata.image, []):
                ret.append(deepcopy(entry))
        return [ret]