#!/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. # # Puppet node integration; turns an IClassify node into a puppet node. # require 'rubygems' require File.dirname(__FILE__) + '/../lib/iclassify-interface' require 'optparse' require 'highline/import' config = {} args = ARGV opts = OptionParser.new do |opts| opts.banner = "Usage: #{$0} [-s server] hostname" opts.on("-s SERVER", "--server", "iClassify Server URL") do |s| config[:server] = s 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_tail("-h", "--help", "Show this message") do puts opts exit end end opts.parse!(args) unless args.length == 1 puts "You must supply a hostname or fqdn. You supplied: " puts args.join(" ") 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 hostname = args[0] client = IClassify::Client.new(config[:server], config[:user], config[:passwd]) begin results = client.search("hostname:#{hostname} OR fqdn:#{hostname}") rescue SocketError $stderr.puts("Error: Could not connect to server.") exit 1 end if results.length > 1 STDERR.puts "Hostname #{hostname} has more than one node definition!" exit 10 end # FIXME: This should go away when real support for parents/children shows up. base_node = client.search("description:\"Base node\"") if base_node.length == 1 base_node[0].tags.each do |tag| results[0].tags << tag unless results[0].tags.detect { |t| t == tag } end end results.each do |node| puts node.to_puppet end exit 0