summaryrefslogtreecommitdiffstats
path: root/src/lib/Bcfg2/Server/Plugins/Bundler.py
blob: 5eeb542ee5d1ead776ed3ac8775e23980c81c7be (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""This provides bundle clauses with translation functionality."""

import os
import re
import sys
import Bcfg2.Server
import Bcfg2.Server.Plugin
import Bcfg2.Server.Lint
from genshi.template import TemplateError


class BundleFile(Bcfg2.Server.Plugin.StructFile):
    """ Representation of a bundle XML file """
    bundle_name_re = re.compile('^(?P<name>.*)\.(xml|genshi)$')

    def __init__(self, filename, should_monitor=False):
        Bcfg2.Server.Plugin.StructFile.__init__(self, filename,
                                                should_monitor=should_monitor)
        if self.name.endswith(".genshi"):
            self.logger.warning("Bundler: Bundle filenames ending with "
                                ".genshi are deprecated; add the Genshi XML "
                                "namespace to a .xml bundle instead")
    __init__.__doc__ = Bcfg2.Server.Plugin.StructFile.__init__.__doc__

    def Index(self):
        Bcfg2.Server.Plugin.StructFile.Index(self)
        if self.xdata.get("name"):
            self.logger.warning("Bundler: Explicitly specifying bundle names "
                                "is deprecated")
    Index.__doc__ = Bcfg2.Server.Plugin.StructFile.Index.__doc__

    @property
    def bundle_name(self):
        """ The name of the bundle, as determined from the filename """
        return self.bundle_name_re.match(
            os.path.basename(self.name)).group("name")


class Bundler(Bcfg2.Server.Plugin.Plugin,
              Bcfg2.Server.Plugin.Structure,
              Bcfg2.Server.Plugin.XMLDirectoryBacked):
    """ The bundler creates dependent clauses based on the
    bundle/translation scheme from Bcfg1. """
    __author__ = 'bcfg-dev@mcs.anl.gov'
    __child__ = BundleFile

    def __init__(self, core, datastore):
        Bcfg2.Server.Plugin.Plugin.__init__(self, core, datastore)
        Bcfg2.Server.Plugin.Structure.__init__(self)
        Bcfg2.Server.Plugin.XMLDirectoryBacked.__init__(self, self.data)
        #: Bundles by bundle name, rather than filename
        self.bundles = dict()
    __init__.__doc__ = Bcfg2.Server.Plugin.Plugin.__init__.__doc__

    def HandleEvent(self, event):
        Bcfg2.Server.Plugin.XMLDirectoryBacked.HandleEvent(self, event)

        self.bundles = dict()
        for bundle in self.entries.values():
            self.bundles[bundle.bundle_name] = bundle
    HandleEvent.__doc__ = \
        Bcfg2.Server.Plugin.XMLDirectoryBacked.HandleEvent.__doc__

    def BuildStructures(self, metadata):
        bundleset = []
        for bundlename in metadata.bundles:
            try:
                bundle = self.bundles[bundlename]
            except KeyError:
                self.logger.error("Bundler: Bundle %s does not exist" %
                                  bundlename)
                continue
            try:
                bundleset.append(bundle.XMLMatch(metadata))
            except TemplateError:
                err = sys.exc_info()[1]
                self.logger.error("Bundler: Failed to render templated bundle "
                                  "%s: %s" % (bundlename, err))
            except:
                self.logger.error("Bundler: Unexpected bundler error for %s" %
                                  bundlename, exc_info=1)
        return bundleset
    BuildStructures.__doc__ = \
        Bcfg2.Server.Plugin.Structure.BuildStructures.__doc__


class BundlerLint(Bcfg2.Server.Lint.ServerPlugin):
    """ Perform various bundle checks """

    def Run(self):
        """ run plugin """
        self.missing_bundles()
        for bundle in self.core.plugins['Bundler'].entries.values():
            if self.HandlesFile(bundle.name):
                self.bundle_names(bundle)

    @classmethod
    def Errors(cls):
        return {"bundle-not-found": "error",
                "unused-bundle": "warning",
                "explicit-bundle-name": "error",
                "genshi-extension-bundle": "error"}

    def missing_bundles(self):
        """ find bundles listed in Metadata but not implemented in Bundler """
        if self.files is None:
            # when given a list of files on stdin, this check is
            # useless, so skip it
            groupdata = self.metadata.groups_xml.xdata
            ref_bundles = set([b.get("name")
                               for b in groupdata.findall("//Bundle")])

            allbundles = self.core.plugins['Bundler'].bundles.keys()
            for bundle in ref_bundles:
                if bundle not in allbundles:
                    self.LintError("bundle-not-found",
                                   "Bundle %s referenced, but does not exist" %
                                   bundle)

            for bundle in allbundles:
                if bundle not in ref_bundles:
                    self.LintError("unused-bundle",
                                   "Bundle %s defined, but is not referenced "
                                   "in Metadata" % bundle)

    def bundle_names(self, bundle):
        """ Verify that deprecated bundle .genshi bundles and explicit
        bundle names aren't used """
        if bundle.xdata.get('name'):
            self.LintError("explicit-bundle-name",
                           "Deprecated explicit bundle name in %s" %
                           bundle.name)

        if bundle.name.endswith(".genshi"):
            self.LintError("genshi-extension-bundle",
                           "Bundle %s uses deprecated .genshi extension" %
                           bundle.name)