summaryrefslogtreecommitdiffstats
path: root/src/lib/Server/Structures/bundler.py
blob: 3bc9cf5103dad763fec5b704ff7f36c61f5b1d7c (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
#!/usr/bin/env python
# $Id: $

from copy import deepcopy
from syslog import LOG_ERR, syslog

from Bcfg2.Server.Generator import SingleXMLFileBacked, XMLFileBacked, DirectoryBacked
from Bcfg2.Server.Structure import Structure

from elementtree.ElementTree import Element, XML, tostring

class ImageFile(SingleXMLFileBacked):
    def Index(self):
        a = XML(self.data)
        self.attr = a.attrib
        self.entries = a.getchildren()
        self.images = {}
        for child in self.entries:
            (name, package, service) = map(lambda x:child.attrib.get(x), ['name', 'package', 'service'])
            for grandchild in child.getchildren():
                self.images[grandchild.attrib['name']] = (name, package, service)

class Bundle(XMLFileBacked):
    def Index(self):
        x = XML(self.data)
        # scan self.entries to build partial bundle fragments
        self.all = []
        self.systems = {}
        self.attributes = {}
        for entry in x.getchildren():
            if entry.tag == 'System':
                self.systems[entry.attrib['name']] = entry.getchildren()
            elif entry.tag == 'Attribute':
                self.attributes["%s.%s"%(entry.attrib['scope'], entry.attrib['name'])] = entry.getchildren()
            else:
                self.all.append(entry)
        del self.data

    def BuildBundle(self,metadata, system):
        bundlename = self.name[:-4]
        b = Element('Bundle', name=bundlename)
        for entry in self.all + self.systems.get(system, []):
            b.append(deepcopy(entry))
        for attribute in metadata.attributes:
            for entry in self.attributes.get(attribute, []):
                b.append(deepcopy(entry))
        return b
            
class BundleSet(DirectoryBacked):
    '''The Bundler handles creation of dependent clauses based on bundle definitions'''
    __child__ = Bundle

class bundler(Structure):
    __name__ =  'bundler'
    __version__ = '$Id$'
    
    '''The bundler creates dependent clauses based on the bundle/translation scheme from bcfg1'''
    def __init__(self, core, datastore):
        Structure.__init__(self, core, datastore)
        self.imageinfo = ImageFile("%s/common/imageinfo.xml"%(datastore), self.core.fam)
        self.bundles = BundleSet(self.data, self.core.fam)

    def Construct(self, metadata):
        (system, package, service) = self.GetTransInfo(metadata)
        bundleset = []
        for bundlename in metadata.bundles:
            if not self.bundles.entries.has_key("%s.xml"%(bundlename)):
                syslog(LOG_ERR, "Client %s requested nonexistent bundle %s"%(metadata.hostname, bundlename))
                continue

            bundle = self.bundles.entries["%s.xml"%(bundlename)].BuildBundle(metadata, system)
            # now we need to populate service/package types
            for entry in bundle.getchildren():
                if entry.tag == 'Package':
                    entry.attrib['type'] = package
                elif entry.tag == 'Service':
                    entry.attrib['type'] = service
            bundleset.append(bundle)
        return bundleset

    def GetTransInfo(self, metadata):
        if self.imageinfo.images.has_key(metadata.image):
            return self.imageinfo.images[metadata.image]
        else:
            raise KeyError, metadata.image