From e3364097371ec3673c18a12a56254e776be22d7b Mon Sep 17 00:00:00 2001 From: Narayan Desai Date: Tue, 19 Feb 2008 01:18:14 +0000 Subject: bcfg2-query patch from SolJ git-svn-id: https://svn.mcs.anl.gov/repos/bcfg/trunk/bcfg2@4384 ce84e21b-d406-0410-9b95-82705330c041 --- man/bcfg2-query.8 | 25 +++++++++---- src/lib/Server/Plugins/Metadata.py | 5 +++ src/sbin/bcfg2-query | 76 +++++++++++++++++++++----------------- 3 files changed, 65 insertions(+), 41 deletions(-) diff --git a/man/bcfg2-query.8 b/man/bcfg2-query.8 index cdcc0ede1..a306ba049 100644 --- a/man/bcfg2-query.8 +++ b/man/bcfg2-query.8 @@ -3,8 +3,7 @@ bcfg2-query \- Print client names by characteristic .SH SYNOPSIS .B bcfg2-query -.I [\-d] -.I [\-u] +.I [\-g group] .I [\-p profile] .I [\-a] @@ -19,17 +18,29 @@ on client characteristics. .RS Display all clients .RE -.B "\-u" +.B "\-c" .RS -Display all up clients +Displays nodes in a comma delimited list .RE -.B "\-d" +.B "\-s" .RS -Display all down clients +Display nodes in a space delimited list +.RE +.B "\-n" +.RS +Display nodes in a newline delimited list +.RE +.B "\-v" +.RS +Turns on debugging .RE .B "\-p " .RS -Display all clients that are a part of the specified profile. +Display all clients that are a part of the specified profile +.RE +.B "\-g " +.RS +Display all clients that are members of the specified group .RE .SH "SEE ALSO" .BR bcfg2(1), diff --git a/src/lib/Server/Plugins/Metadata.py b/src/lib/Server/Plugins/Metadata.py index d2e7ad7c7..8f899f09f 100644 --- a/src/lib/Server/Plugins/Metadata.py +++ b/src/lib/Server/Plugins/Metadata.py @@ -417,3 +417,8 @@ class Metadata(Bcfg2.Server.Plugin.Plugin): '''Return a list of clients that are in a given group''' return [client for client in self.clients \ if group in self.groups[self.clients[client]][1]] + + def GetClientByProfile(self, profile): + '''Return a list of clients that are members of a given profile''' + return [client for client in self.clients \ + if self.clients[client] == profile] diff --git a/src/sbin/bcfg2-query b/src/sbin/bcfg2-query index 3f0209192..61fd37aee 100755 --- a/src/sbin/bcfg2-query +++ b/src/sbin/bcfg2-query @@ -1,41 +1,49 @@ #!/usr/bin/python -import lxml.etree, sys, ConfigParser +import Bcfg2.Server.Core, Bcfg2.Logging +import lxml.etree, sys, ConfigParser, time -CP = ConfigParser.ConfigParser() -CP.read(['/etc/bcfg2.conf']) -try: - prefix = CP.get('server', 'repository') -except: - prefix = "/var/lib/bcfg2" +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 -d|u|p " - print "\t -d\t\t shows the clients that are currently down" - print "\t -u\t\t shows the clients that are currently up" - 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 -p \t shows all the clients of that profile" - print "\t -a\t shows all clients" - sys.exit(1) + if len(sys.argv) < 2: + print "Usage bcfg2-query -a|-c|-s|-n|-g -p " + 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 \t shows all the clients that are members of that group" + print "\t -p \t shows all the clients of that profile" + sys.exit(1) -xml = lxml.etree.parse('%s/Metadata/clients.xml'%prefix) -if '-p' in sys.argv: - profile = sys.argv[sys.argv.index('-p') + 1] - clients = xml.xpath(".//Client[@profile='%s']" % (profile)) -elif '-d' in sys.argv: - clients = xml.xpath(".//Client[@pingable='N']") -elif '-u' in sys.argv: - clients = xml.xpath(".//Client[@pingable='Y']") -elif '-a' in sys.argv: - clients = xml.xpath(".//Client") + ''' 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'] -cnames = [client.get('name') for client in clients] -if '-c' in sys.argv: - print ",".join(cnames) -elif '-s' in sys.argv: - print " ".join(cnames) -else: - print "\n".join(cnames) + ''' 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) -- cgit v1.2.3-1-g7c22