summaryrefslogtreecommitdiffstats
path: root/src/lib/Server/Admin/Minestruct.py
blob: 83eaa2c0aa456b0dcda185e0c6b6f2872905d592 (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
'''Minestruct Admin Mode'''
import Bcfg2.Server.Admin
import lxml.etree, sys, getopt

class Minestruct(Bcfg2.Server.Admin.StructureMode):
    '''Pull extra entries out of statistics'''
    __shorthelp__ = 'bcfg2-admin minestruct [-f file-name] [-g groups] client'
    __longhelp__ = __shorthelp__ + '\n\tExtract extra entry lists from statistics'

    def __call__(self, args):
        Bcfg2.Server.Admin.Mode.__call__(self, args)
        if len(args) == 0:
            self.errExit("No hostname specified (see bcfg2-admin minestruct -h for help)")
        try:
            (opts, args) = getopt.getopt(args, 'f:g:h')
        except:
            self.log.error(self.__shorthelp__)
            raise SystemExit(1)
        if "-h" in args or not args:
            print "Usage:"
            print self.__shorthelp__
            raise SystemExit(1)
        
        client = args[0]
        output = sys.stdout
        groups = []

        for (opt, optarg) in opts:
            if opt == '-f':
                try:
                    output = open(optarg, 'w')
                except IOError:
                    self.log.error("Failed to open file: %s" % (optarg))
                    raise SystemExit(1)
            elif opt == '-g':
                groups = optarg.split(':')

        extra = self.statistics.GetExtra(client)
        root = lxml.etree.Element("Base")
        self.log.info("Found %d extra entries" % (len(extra)))
        add_point = root
        for g in groups:
            add_point = lxml.etree.SubElement(add_point, "Group", name=g)
        for tag, name in extra:
            self.log.info("%s: %s" % (tag, name))
            lxml.etree.SubElement(add_point, tag, name=name)

        tree = lxml.etree.ElementTree(root)
        tree.write(output, pretty_print=True)