#!/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 -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) ''' 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)