summaryrefslogtreecommitdiffstats
path: root/src/lib/Server/Plugins/Ohai.py
blob: b202187922c5c1de9d70a68a3800206b91bea842 (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
import json
import lxml.etree
import os
import Bcfg2.Server.Plugin

class OhaiCache(object):
    def __init__(self, dirname):
        self.dirname = dirname
        self.cache = dict()

    def __setitem__(self, item, value):
        self.cache[item] = json.loads(value)
        file("%s/%s.json" % (self.dirname, item), 'w').write(value)

    def __getitem__(self, item):
        if item not in self.cache:
            try:
                data = open("%s/%s.json" % (self.dirname, item)).read()
            except:
                raise KeyError, item
            self.cache[item] = json.loads(data)
        return self.cache[item]

    def __iter__(self):
        data = self.cache.keys()
        data.extend([x[:-5] for x in os.listdir(self.dirname)])
        return data.__iter__()

class Ohai(Bcfg2.Server.Plugin.Plugin,
           Bcfg2.Server.Plugin.Probing,
           Bcfg2.Server.Plugin.Connector):
    name = 'Ohai'
    experimental = True

    def __init__(self, core, datastore):
        Bcfg2.Server.Plugin.Plugin.__init__(self, core, datastore)
        Bcfg2.Server.Plugin.Probing.__init__(self)
        Bcfg2.Server.Plugin.Connector.__init__(self)
        self.probe = lxml.etree.Element('probe', name='Ohai', source='Ohai',
                                        interpreter='/bin/sh')
        self.probe.text = 'ohai'
        try:
            os.stat(self.data)
        except:
            self.make_path(self.data)
        self.cache = OhaiCache(self.data)

    def GetProbes(self, meta, force=False):
        return [self.probe]

    def ReceiveData(self, meta, datalist):
        self.cache[meta.hostname] = datalist[0].text

    def get_additional_data(self, meta):
        if meta.hostname in self.cache:
            return self.cache[meta.hostname]
        return dict()