summaryrefslogtreecommitdiffstats
path: root/src/lib/Server/Plugins/Editor.py
blob: 76a03a325b2ae85239a16ec8725dc49933472a3d (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
import Bcfg2.Server.Plugin
import re
import lxml.etree


def linesub(pattern, repl, filestring):
    """Substitutes instances of pattern with repl in filestring."""
    if filestring == None:
        filestring = ''
    output = list()
    fileread = filestring.split('\n')
    for line in fileread:
        output.append(re.sub(pattern, repl, filestring))
    return '\n'.join(output)


class EditDirectives(Bcfg2.Server.Plugin.SpecificData):
    """This object handles the editing directives."""
    def ProcessDirectives(self, input):
        """Processes a list of edit directives on input."""
        temp = input
        for directive in self.data.split('\n'):
            directive = directive.split(',')
            temp = linesub(directive[0], directive[1], temp)
        return temp


class EditEntrySet(Bcfg2.Server.Plugin.EntrySet):
    def __init__(self, basename, path, entry_type, encoding):
        self.ignore = re.compile("^(\.#.*|.*~|\\..*\\.(tmp|sw[px])|%s\.H_.*)$" % path.split('/')[-1])
        Bcfg2.Server.Plugin.EntrySet.__init__(self,
                                              basename,
                                              path,
                                              entry_type,
                                              encoding)
        self.inputs = dict()

    def bind_entry(self, entry, metadata):
        client = metadata.hostname
        filename = entry.get('name')
        permdata = {'owner': 'root',
                    'group': 'root',
                    'perms': '0644'}
        [entry.attrib.__setitem__(key, permdata[key]) for key in permdata]
        entry.text = self.entries['edits'].ProcessDirectives(self.get_client_data(client))
        if not entry.text:
            entry.set('empty', 'true')
        try:
            f = open('%s/%s.H_%s' % (self.path, filename.split('/')[-1], client), 'w')
            f.write(entry.text)
            f.close()
        except:
            pass

    def get_client_data(self, client):
        return self.inputs[client]


class Editor(Bcfg2.Server.Plugin.GroupSpool,
             Bcfg2.Server.Plugin.Probing):
    name = 'Editor'
    __version__ = '$Id$'
    __author__ = 'bcfg2-dev@mcs.anl.gov'
    filename_pattern = 'edits'
    es_child_cls = EditDirectives
    es_cls = EditEntrySet

    def GetProbes(self, _):
        '''Return a set of probes for execution on client'''
        probelist = list()
        for name in list(self.entries.keys()):
            probe = lxml.etree.Element('probe')
            probe.set('name', name)
            probe.set('source', "Editor")
            probe.text = "cat %s" % name
            probelist.append(probe)
        return probelist

    def ReceiveData(self, client, datalist):
        for data in datalist:
            self.entries[data.get('name')].inputs[client.hostname] = data.text