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

class Minestruct(Bcfg2.Server.Admin.StructureMode):
    '''Pull extra entries out of statistics'''
    __shorthelp__ = 'bcfg2-admin minestruct <client> [-f file-name] [-g groups]'
    __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)")
        if "-h" in args:
            print "Usage:"
            print self.__shorthelp__
            raise SystemExit(1)
        write_to_file = False
        file_arg = False
        output_file = None
        client = None
        groups_arg = False
        groups = []
        for arg in args:
            if arg == "-f":
                file_arg = True
                groups_arg = False
                continue
            elif arg == "-g":
                groups_arg = True
                file_arg = False
                continue                           
            elif file_arg == True:
                output_file = arg
                file_arg = False
                write_to_file = True
                continue
            elif groups_arg == True:
                groups.append(arg)
                continue
            else:
                client = arg
        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)
        if write_to_file == True:
            try:
                f = open(output_file, 'w')
            except IOError:
                self.log.info("Failed to write to file: %s" % (output_file))
                raise SystemExit(1)
            tree.write(f, pretty_print=True)
        else:
            print lxml.etree.tostring(tree, pretty_print=True)