Skip to content

Instantly share code, notes, and snippets.

@treydock
Created August 20, 2012 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save treydock/3409173 to your computer and use it in GitHub Desktop.
Save treydock/3409173 to your computer and use it in GitHub Desktop.
Test output of Foreman extlookup for Puppet module integration
#!/usr/bin/env ruby
##
# == Usage
#
# Perform hostgroup search for host 'host1.tld'
# +ruby foreman_extlookup.rb https://foreman-server.tld -i hostgroups -s "host = host1.tld" -u lookup -p Password1+
#
# Perform hostgroup search for host 'host1.tld' without providing password, will be prompted for password
# +ruby foreman_extlookup.rb https://foreman-server.tld -i hostgroups -s "host = host1.tld" -u lookup -p+
##
require "rubygems"
require "net/http"
require "net/https"
require "uri"
require "timeout"
require "puppet"
require 'optparse'
require 'highline/import'
@options = {}
optparse = OptionParser.new do |opts|
opts.banner = "Usage: #{File.basename(__FILE__)} foreman_url [options]"
opts.on('-i', '--item ITEM', String, 'the item to search on') do |item|
@options[:item] = item
end
opts.on('-s', '--search SEARCH', String, 'the search to run') do |search|
@options[:search] = search
end
opts.on('-u', '--user USER', String, 'user with permissions to perform search') do |user|
@options[:user] = user
end
opts.on('-p', '--password', 'user\'s password. Will be prompted for password if left blank') do
@options[:password] = ARGV.empty? ? '' : ARGV[0]
end
opts.on('-h', '--help', 'Display this screen') do
puts opts
exit
end
end
begin
optparse.parse!(ARGV)
mandatory = [:item, :search, :user, :password]
missing = mandatory.select { |param| @options[param].nil? }
unless missing.empty?
puts "Missing arguments: #{missing.join(', ')}"
puts optparse
exit
end
if ARGV.empty?
puts "Missing Foreman URL"
puts optparse
exit
end
# Prompt for password if none was provided
if @options[:password].empty?
@options[:password] = ask("Enter #{@options[:user]}'s password: ") { |q| q.echo = false }
end
rescue OptionParser::InvalidOption, OptionParser::MissingArgument
puts $!.to_s
puts optparse
exit
end
# URL to query
foreman_url = ARGV[0]
foreman_user = @options[:user]
foreman_pass = @options[:password]
item = @options[:item]
search = @options[:search]
# extend this as required
searchable_items = %w{ hosts puppetclasses fact_values environments hostgroups }
output = ''
raise ArgumentError, "Foreman: Invalid item to search on: #{item}, must be one of #{searchable_items.join(", ")}." unless searchable_items.include?(item)
uri = URI.parse(foreman_url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if uri.scheme == 'https'
path = URI.escape("/#{item}?search=#{search}")
req = Net::HTTP::Get.new(path)
req.basic_auth(foreman_user, foreman_pass)
req['Content-Type'] = 'application/json'
req['Accept'] = 'application/json'
begin
Timeout::timeout(5) { output = PSON.parse http.request(req).body }
rescue Exception => e
raise RuntimeError, "Failed to contact Foreman #{e}"
end
puts "Results: \n"
puts YAML.dump(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment