Skip to content

Instantly share code, notes, and snippets.

@bgupta
Created October 8, 2011 01:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bgupta/1271734 to your computer and use it in GitHub Desktop.
Save bgupta/1271734 to your computer and use it in GitHub Desktop.
Basic CLI for Querying Foreman
#!/usr/bin/env ruby
# Copyright (C) 2011 Vijay Brian Gupta brian.gupta@brandorr.com
#
# 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.
#
# v0.1 May 27, 2011 - Supports --hostlist
# v0.9 Oct 7, 2011 - Supports filters, and SSL and basicauth, and url, user and password as options.
require "rubygems"
require "time"
require "openssl"
require "rest_client"
require "json"
require "socket"
require "open-uri"
require "optparse"
require "yaml"
collections_filterable = [
:hosts,
:puppetclasses,
:fact_values,
:environments,
:hostgroups,
]
collections = [
:architectures,
:auth_source_ldaps,
:common_parameters,
:config_templates,
:domains,
:hypervisors,
:lookup_keys,
:media,
:operatingsystems,
:ptables,
:reports,
:smart_proxies,
:subnets,
:usergroups,
:users
]
collections_not_implemented = [ :dashboard ]
@foreman_user = ENV['FOREMAN_USER'] if ENV['FOREMAN_USER']
@foreman_pass = ENV['FOREMAN_PASSWORD'] if ENV['FOREMAN_PASSWORD']
@foreman_url = ENV['FOREMAN_SERVER'] if ENV['FOREMAN_SERVER']
options = {}
OptionParser.new do|opts|
opts.banner = "Usage: " + File.basename($0) + " [options] ..."
options[:debug] = false
opts.on( "-d", "--debug", "Output more information" ) do
options[:debug] = true
end
options[:username] = false
opts.on( '-u', '--user USER', "Foreman user") do|f|
options[:username] = f
end
options[:password] = false
opts.on( "-p", "--pass PASSWORD", "Foreman password" ) do|f|
options[:password] = f
end
options[:server] = false
opts.on( "-s", "--server URL", "Foreman Server URL" ) do|f|
options[:server] = f
end
options[:json] = false
opts.on( "--json", "JSON output" ) do
options[:json] = true
end
options[:status] = false
opts.on( "--status", "Foreman status" ) do
options[:status] = true
end
# options[:dashboard] = false
# opts.on( "--dashboard", "Foreman dashboard" ) do|f|
# options[:dashboard] = f
# end
options[:custom] = false
opts.on( "--custom COLLECTION", "Custom COLLECTION string, see: http://theforeman.org/projects/foreman/wiki/API" ) do|f|
options[:custom] = f
end
collections_filterable.each do |collection|
options[collection] = false
opts.on("--#{collection} [filter]", "Retreive a list of #{collection}") do|f|
options[collection] = f || true
end
end
collections.each do |collection|
options[collection] = false
opts.on("--#{collection}", "Retreive a list of #{collection}") do
options[collection] = true
end
end
collections_not_implemented.each do |collection|
options[collection] = false
opts.on("--#{collection}", "Not implemented") do
options[collection] = true
end
end
opts.on_tail("-h", "--help", "Show this message") do
puts opts
puts ""
puts " ENVIRONMENT VARIABLES:"
puts ""
puts " FOREMAN_SERVER Foreman Server URL"
puts " FOREMAN_USER Foreman user"
puts " FOREMAN_PASSWORD Foreman password"
puts ""
puts " CLI options take precendence over ENVIRONMENT VARIABLES"
puts ""
exit
end
end.parse!
puts "Use -h, --help for usage." if options.values.uniq == [false]
@foreman_user = options.delete(:username) if options[:username]
@foreman_pass = options.delete(:password) if options[:password]
@foreman_url = options.delete(:server) if options[:server]
@usejson = options.delete(:json) if options[:json]
@custom = options.delete(:custom) if options[:custom]
@status = options.delete(:status) if options[:status]
RestClient.log = 'stdout' if options.delete(:debug)
def get_collection(path)
response = RestClient::Request.new(:method => :get,
:url => @foreman_url + "/" + path.to_s,
:user => @foreman_user, :password => @foreman_pass,
:headers => { :accept => :json, :content_type => :json }).execute
results = JSON.parse(response.to_str)
end
def search_collection(path,search)
response = RestClient::Request.new(:method => :get,
:url => @foreman_url + "/" + path.to_s + "?search=" + search,
:user => @foreman_user, :password => @foreman_pass,
:headers => { :accept => :json, :content_type => :json }).execute
results = JSON.parse(response.to_str)
end
options.keys.each do |collection|
if options[collection]
if options[collection] == true
if @usejson
puts JSON.pretty_generate(get_collection(collection))
else
puts get_collection(collection)
end
else
if @usejson
puts JSON.pretty_generate(search_collection(collection,options[collection]))
else
puts search_collection(collection,options[collection])
end
end
end
end
if @custom
if @usejson
puts JSON.pretty_generate(get_collection(@custom))
else
puts get_collection(@custom)
end
end
puts JSON.pretty_generate(get_collection("status")) if @status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment