summaryrefslogtreecommitdiffstats
path: root/src/lib/Server/Admin/Query.py
blob: 0c0659213e4d97a4600ce365493d9acf3ccee7ae (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
import Bcfg2.Server.Admin, Bcfg2.Logging, logging

class Query(Bcfg2.Server.Admin.Mode):
    __shorthelp__ = 'bcfg2-admin query [-n] [-c] g=group p=profile'
    __longhelp__ = __shorthelp__ + '\n\tQuery clients'
    def __init__(self, cfile):
        logging.root.setLevel(100)
        Bcfg2.Logging.setup_logging(100, to_console=False, to_syslog=False)
        Bcfg2.Server.Admin.Mode.__init__(self, cfile)
        try:
            self.bcore = Bcfg2.Server.Core.Core(self.get_repo_path(),
                                                [], ['Metadata'],
                                                'foo', False)
        except Bcfg2.Server.Core.CoreInitError, msg:
            self.errExit("Core load failed because %s" % msg)
        [self.bcore.fam.Service() for _ in range(1)]
        self.meta = self.bcore.metadata
        self.meta.load_probedata()
        while self.bcore.fam.Service():
            pass

    def __call__(self, args):
        Bcfg2.Server.Admin.Mode.__call__(self, args)
        clients = self.meta.clients.keys()
        for arg in args:
            if arg in ['-n', '-c']:
                continue
            try:
                k, v = arg.split('=')
            except:
                print "Unknown argument %s" % arg
                continue
            if k == 'p':
                nc = [c for c, p in self.meta.clients.iteritems() if p == v]
            elif k == 'g':
                nc = [c for c in self.meta.clients if v in
                      self.meta.groups[self.meta.clients[c]][1] or
                      v in self.meta.cgroups.get(c, [])]
            clients = [c for c in clients if c in nc]

        if '-n' in args:
            for client in clients:
                print client
        else:
            print ','.join(clients)