summaryrefslogtreecommitdiffstats
path: root/src/lib/Server/Plugins/BB.py
blob: 18719f2be4e0494ba8e1e832a27e7a002d4f9f65 (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
import lxml.etree 
import Bcfg2.Server.Plugin
import glob
import os
import socket

#manage boot symlinks
  #add statistics check to do build->boot mods

#map profiles: first array is not empty we replace the -p with a determined profile.
logger = Bcfg2.Server.Plugin.logger

class BBfile(Bcfg2.Server.Plugin.XMLFileBacked):
    '''Class for bb files'''
    def Index(self):
        '''Build data into an xml object'''

        try:
            self.data = lxml.etree.XML(self.data)
        except lxml.etree.XMLSyntaxError:
            Bcfg2.Server.Plugin.logger.error("Failed to parse %s" % self.name)
            return
        self.tftppath = self.data.get('tftp', '/tftpboot')
        self.macs = {}
        self.users = {}
        self.actions = {}
        self.bootlinks = []

        for node in self.data.findall('Node'):
            iface = node.find('Interface')
            if iface != None:
                mac = "01-%s" % (iface.get('mac'.replace(':','-').lower()))
                self.actions[node.get('name')] = node.get('action')
                self.bootlinks.append((mac, node.get('action')))
                try:
                    ip = socket.gethostbyname(node.get('name'))
                except:
                    logger.error("failed host resolution for %s" % node.get('name'))

                self.macs[node.get('name')] = (iface.get('mac'), ip)
            else:
                logger.error("%s" % lxml.etree.tostring(node))
            self.users[node.get('name')] = node.get('user',"").split(':')
        
    def enforce_bootlinks(self):
        for mac, target in self.bootlinks:
            path = self.tftppath + '/' + mac
            if not os.path.islink(path):
                logger.error("Boot file %s not a link" % path)
            if target != os.readlink(path):
                try:
                    os.unlink(path)
                    os.symlink(target, path)
                except:
                    logger.error("Failed to modify link %s" % path)
        
class BBDirectoryBacked(Bcfg2.Server.Plugin.DirectoryBacked):
    __child__ = BBfile


class BB(Bcfg2.Server.Plugin.Plugin,
         Bcfg2.Server.Plugin.Connector):
    '''The BB plugin maps users to machines and metadata to machines'''
    name = 'BB'
    version = '$Revision$'
    experimental = True

    def __init__(self, core, datastore):
        Bcfg2.Server.Plugin.Plugin.__init__(self, core, datastore)
        Bcfg2.Server.Plugin.Connector.__init__(self)
        self.store = BBDirectoryBacked(self.data, core.fam)

    def get_additional_data(self, metadata):
        
        users = {}
        for user in self.store.entries['bb.xml'].users.get(metadata.hostname.split(".")[0], []):
            pubkeys = []
            for fname in glob.glob('/home/%s/.ssh/*.pub'%user):
                pubkeys.append(open(fname).read())
            
            users[user] = pubkeys            
        
        return dict([('users', users), 
                      ('macs', self.store.entries['bb.xml'].macs)])