Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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, :status ]
- options = {}
- OptionParser.new do|opts|
- opts.banner = "Usage: " + File.basename($0) + " [options] ..."
- options[:verbose] = false
- opts.on( "-v", "--verbose", "Output more information" ) do
- options[:verbose] = 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|f|
- options[:json] = 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
- 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]
- RestClient.log = 'stdout' if options.delete(:verbose)
- 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
Add Comment
Please, Sign In to add comment