foreman/extras/cli/foremancli @ 49620a31
71ce3e9b | Brian Gupta | #!/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.
|
|||
require "optparse"
|
|||
collections_filterable = [
|
|||
e9d8f482 | Brian Gupta | :architectures,
|
|
:common_parameters,
|
|||
:config_templates,
|
|||
:domains,
|
|||
71ce3e9b | Brian Gupta | :environments,
|
|
e9d8f482 | Brian Gupta | :fact_values,
|
|
:hosts,
|
|||
71ce3e9b | Brian Gupta | :hostgroups,
|
|
e9d8f482 | Brian Gupta | :media,
|
|
:puppetclasses,
|
|||
:reports,
|
|||
:roles,
|
|||
98c5e4ed | Ohad Levy | :settings,
|
|
:lookup_keys,
|
|||
:dashboard,
|
|||
:operatingsystems,
|
|||
:subnets,
|
|||
:ptables,
|
|||
:users
|
|||
71ce3e9b | Brian Gupta | ]
|
|
collections = [
|
|||
:auth_source_ldaps,
|
|||
:hypervisors,
|
|||
98c5e4ed | Ohad Levy | :lookup_values,
|
|
71ce3e9b | Brian Gupta | :smart_proxies,
|
|
e9d8f482 | Brian Gupta | :statistics,
|
|
71ce3e9b | Brian Gupta | :usergroups,
|
|
]
|
|||
e9d8f482 | Brian Gupta | collections_not_implemented = [ :audits, :bookmarks ]
|
|
71ce3e9b | Brian Gupta | ||
@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[:yaml] = false
|
|||
opts.on( "--yaml", "YAML output" ) do
|
|||
options[:yaml] = 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
|
|||
49620a31 | Brian Gupta | opts.on("--#{collection} [filter]", "Retrieve a list of #{collection}") do |f|
|
|
71ce3e9b | Brian Gupta | options[collection] = f || true
|
|
end
|
|||
end
|
|||
collections.each do |collection|
|
|||
options[collection] = false
|
|||
49620a31 | Brian Gupta | opts.on("--#{collection}", "Retrieve a list of #{collection}") do
|
|
71ce3e9b | Brian Gupta | 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 ""
|
|||
49620a31 | Brian Gupta | puts " CLI options take precedence over ENVIRONMENT VARIABLES"
|
|
71ce3e9b | Brian Gupta | puts ""
|
|
e9d8f482 | Brian Gupta | puts " FILTERS:"
|
|
puts ""
|
|||
puts " Please see:"
|
|||
puts " http://theforeman.org/projects/foreman/wiki/Search_API"
|
|||
puts ""
|
|||
puts " Examples:"
|
|||
puts " --hosts \"domain = domain.com AND class = squid\""
|
|||
puts " --hosts \"domain = domain.com AND facts.architecture = x86_64 AND \\"
|
|||
puts " class = module::class"
|
|||
puts " --classes \"name = squid\""
|
|||
puts " --domains \"name = domain.com\""
|
|||
puts ""
|
|||
71ce3e9b | Brian Gupta | exit 1
|
|
end
|
|||
end.parse!
|
|||
if options.values.uniq == [false]
|
|||
warn "Use -h, --help for usage."
|
|||
exit 1
|
|||
end
|
|||
require "rubygems"
|
|||
require "rest_client"
|
|||
require "json"
|
|||
require "yaml"
|
|||
@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]
|
|||
@useyaml = options.delete(:yaml) if options[:yaml]
|
|||
@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, options = {})
|
|||
JSON.parse(RestClient::Request.new({:method => :get,
|
|||
:url => "#{@foreman_url}/#{path.to_s.chomp('/')}",
|
|||
:user => @foreman_user, :password => @foreman_pass,
|
|||
82879fc4 | Brian Gupta | :headers => { :accept => :json, :content_type => :json }}.merge(options)).execute.to_s)
|
|
71ce3e9b | Brian Gupta | end
|
|
def search_collection(path,search)
|
|||
get_collection(path, :url => "#{@foreman_url}/#{path}?search=#{URI.encode(search)}")
|
|||
end
|
|||
def format
|
|||
return "json" if @usejson
|
|||
return "yaml" if @useyaml
|
|||
end
|
|||
def print_response response
|
|||
puts(case format
|
|||
when "json"
|
|||
JSON.pretty_generate(response)
|
|||
when "yaml"
|
|||
YAML.dump(response)
|
|||
else
|
|||
49620a31 | Brian Gupta | r = response.first
|
|
if r.is_a?(Hash)
|
|||
# we simply return host list
|
|||
if r.first[0] == "host"
|
|||
response.map{|h| h["host"]["name"]}
|
|||
else
|
|||
response.map{|o| o.inspect}
|
|||
end
|
|||
else
|
|||
response.join("\n")
|
|||
end
|
|||
71ce3e9b | Brian Gupta | end)
|
|
end
|
|||
options.each do |collection, filter|
|
|||
next unless filter
|
|||
49620a31 | Brian Gupta | if filter
|
|
71ce3e9b | Brian Gupta | print_response(get_collection(collection))
|
|
else
|
|||
print_response(search_collection(collection, filter))
|
|||
end
|
|||
end
|
|||
print_response(get_collection(@custom)) if @custom
|
|||
puts JSON.pretty_generate(get_collection("status")) if @status
|