summaryrefslogtreecommitdiffstats
path: root/src/sbin/bcfg2-query
blob: 61fd37aee8ffd8a3db64e9b5a2652e9d9682b379 (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
#!/usr/bin/python

import Bcfg2.Server.Core, Bcfg2.Logging
import lxml.etree, sys, ConfigParser, time

if __name__ == "__main__":
    CP = ConfigParser.ConfigParser()
    CP.read(['/etc/bcfg2.conf'])
    try:
        prefix = CP.get('server', 'repository')
    except:
        prefix = "/var/lib/bcfg2"

    if len(sys.argv) < 2:
        print "Usage bcfg2-query -a|-c|-s|-n|-g <group name> -p <profile name>"
        print "\t -a\t\t shows all clients"
        print "\t -c\t\t prints node names in a comma delimited list"
        print "\t -s\t\t prints node names in a space delimited list"
        print "\t -n\t\t prints node names in a newline delimited list (default)"
        print "\t -v\t\t turn on debugging messages"
        print "\t -g <group name>\t shows all the clients that are members of that group"
        print "\t -p <profile name>\t shows all the clients of that profile"
        sys.exit(1)

    ''' Create the metadata object '''
    bcore = Bcfg2.Server.Core.Core('%s'%prefix, [], ['Metadata'], None, False)
    while(bcore.fam.Service()):
        time.sleep(1)
    mdata = bcore.plugins['Metadata']

    ''' Turn on debugging '''
    if '-v' in sys.argv:
        Bcfg2.Logging.setup_logging(0, to_console=True)

    if '-a' in sys.argv:
        cnames = [key for key in mdata.clients.keys()]
    elif '-g' in sys.argv:
        group = sys.argv[sys.argv.index('-g') + 1]
        cnames = mdata.GetClientByGroup('%s' % group)
    elif '-p' in sys.argv:
        profile = sys.argv[sys.argv.index('-p') + 1]
        cnames = mdata.GetClientByProfile('%s' % profile)

    if '-c' in sys.argv:
        print ",".join(cnames)
    elif '-s' in sys.argv:
        print " ".join(cnames)
    else:
        print "\n".join(cnames)