summaryrefslogtreecommitdiffstats
path: root/src/lib/Server/Plugins/Pinning.py
blob: 474ece41b8a83c003f8703ce8be2456b5d58a2a9 (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
import os
import re
import sys
import copy
import logging
import lxml.etree
import Bcfg2.Server.Plugin

logger = logging.getLogger('Bcfg2.Plugins.Pinnings')

class PinningFile(Bcfg2.Server.Plugin.StructFile):
    """Class for pinning files."""
    def get_additional_data(self, meta):
        data = self.Match(meta)
        results = {}
        for d in data:
            name = d.get('name', '')
            src = d.get('src', '')
            results[name] = src
        return results

class PinDirectoryBacked(Bcfg2.Server.Plugin.DirectoryBacked):
    __child__ = PinningFile
    patterns = re.compile(r'.*\.xml$')

    def get_additional_data(self, meta):
        results = {}
        for files in self.entries:
            results.update(self.entries[files].get_additional_data(meta))
        return results

class Pinning(Bcfg2.Server.Plugin.Plugin,
                 Bcfg2.Server.Plugin.Connector):
    name = 'Pinning'
    version = '$Revision$'

    def __init__(self, core, datastore):
        Bcfg2.Server.Plugin.Plugin.__init__(self, core, datastore)
        Bcfg2.Server.Plugin.Connector.__init__(self)
        try:
            self.store = PinDirectoryBacked(self.data, core.fam)
        except OSError:
            e = sys.exc_info()[1]
            self.logger.error("Error while creating Pinning store: %s %s" %
                              (e.strerror, e.filename))
            raise Bcfg2.Server.Plugin.PluginInitError

    def get_additional_data(self, meta):
        return self.store.get_additional_data(meta)