#!/usr/bin/env ruby # # iClassify - A node classification service. # Copyright (C) 2007 HJK Solutions and Adam Jacob () # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # # A very simple search utility for iclassify. # require 'rubygems' require File.dirname(__FILE__) + '/../lib/iclassify' require 'optparse' require 'highline/import' config = { :server => 'https://iclassify', :user => ENV.has_key?('USER') ? ENV['USER'] : 'puppet', :only => nil, :quiet => nil } opts = OptionParser.new do |opts| opts.banner = "Usage: #{$0} (options) query" opts.on("-a one,two,three", "--attrib one,two,three", Array, "Attributes to print") do |a| config[:attribs] = a end opts.on("-t", "--tags", "Print tags or not, if attribs specified") do |t| config[:tags] = t end opts.on("-u user", "--user user", "User to authenticate with, defaults to USER env variable") do |u| config[:user] = u end opts.on("-p passwd", "--passwd passwd", "Password to authenticate with") do |p| config[:passwd] = p end opts.on("-s server", "--server server", "iClassify Server URL") do |s| config[:server] = s end opts.on("-o", "--only", "Print only the attributes specified on the command line") do |o| config[:only] = o end opts.on("-q", "--quiet", "Do not print the header") do |q| config[:quiet] = q end opts.on_tail("-h", "--help", "Show this message") do puts opts exit end end args = ARGV opts.parse!(args) if args.length != 1 puts "You must specify a single query." puts opts.help exit 1 end unless config[:passwd] config[:passwd] = HighLine.ask("Password: ") { |q| q.echo = "*" } end unless config[:user] && config[:passwd] puts "You must provide a username and password." puts opts.help exit 1 end query = args[0] client = IClassify::Client.new(config[:server], config[:user], config[:passwd]) begin results = client.search(query, config[:attribs] ? config[:attribs] : []) rescue SocketError $stderr.puts("Error: Could not connect to server.") exit 1 end if config.has_key?(:attribs) header = "# " if config[:only] header += "#{config[:attribs].join(',')}" else header += "description,uuid,#{config[:attribs].join(',')}" end header << ",tags" if config.has_key?(:tags) puts header unless config[:quiet] results.each do |node| line = Array.new line << "#{node.description}" unless config[:only] line << "#{node.uuid}" unless config[:only] config[:attribs].each do |attrib| na = node.attribs.detect { |a| a[:name] == attrib } if na line << "#{na[:values].join(':')}" else line << "" end end line << "\"#{node.tags.join(' ')}\"" if config.has_key?(:tags) puts line.join(",") end else results.each do |node| puts "#\n# Node #{node.uuid}\n#" puts node.to_s end end