summaryrefslogtreecommitdiffstats
path: root/src/lib/Server/Generators/servicemgr.py
blob: 0fb7ec3e60b2bedc064452004c6088d33337b8f3 (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
#!/usr/bin/env python

from Bcfg2.Server.Generator import Generator, SingleXMLFileBacked

class ServiceList(SingleXMLFileBacked):
    def Index(self):
        SingleXMLFileBacked.Index(self)
        self.services = {}
        for e in self.entries:
            m = (e.tag, e.attrib['name'])
            for s in e.findall('Service'):
                bs = self.services.get(s.attrib['name'],[])
                bs.append((m,s))
                self.services[s.attrib['name']] = bs
        # now we need to build the index to point __provides__ at
        self.__provides__ = {'Service':{}}
        for s in self.services.keys():
            self.__provides__['Service'][s] = self.GetService
            self.services[s].sort(self.Sort)

    def GetService(self, entry, metadata):
        s = self.services[entry.attrib['name']]
        useful = filter(lambda x:self.MatchMetadata(x[0], metadata), s)
        return useful[-1][1]
        
    def MatchMetadata(self, m, metadata):
        if m[0] == 'Global':
            return True
        elif m[0] == 'Image':
            if m[1] == metadata.image:
                return True
        elif m[0] == 'Class':
            if m[1] in metadata.classes:
                return True
        elif m[0] == 'Host':
            if m[1] == metadata.hostname:
                return True
        return False

    def Sort(self, m1, m2):
        d = {('Global','Host'):-1,('Global','Image'):-1,("Global",'Class'):-1,
             ('Image', 'Global'):1, ('Image', 'Image'):0, ('Image', 'Host'):1, ('Image', 'Class'):-1,
             ('Class','Global'):1, ('Class', 'Image'):1, ('Class','Class'):0, ('Class', 'Host'): -1,
             ('Host', 'Global'):1, ('Host', 'Image'):1, ('Host','Class'):1, ('Host','Host'):0}
        if d.has_key((m1[0][0], m2[0][0])):
            return d[(m1[0][0],m2[0][0])]

class servicemgr(Generator):
    '''This is a generator that handles service assignments'''
    __name__ = 'servicemgr'
    __version__ = '$Id$'
    __author__ = 'bcfg-dev@mcs.anl.gov'

    def __setup__(self):
        self.srvinfo = ServiceList("%s/packages.xml"%(self.data))
        self.__provides__ = self.srvinfo.__provides__

    def GetService(self,entry,metadata):
        # for now sshd is on
        if entry.attrib['name'] == 'sshd':
            entry.attrib['status'] = 'on'