Skip to content

Instantly share code, notes, and snippets.

@bgupta
Forked from ohadlevy/foreman-cli
Created May 28, 2011 07:50
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 bgupta/996702 to your computer and use it in GitHub Desktop.
Save bgupta/996702 to your computer and use it in GitHub Desktop.
foreman-cli
#!/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.
#
# v1.0 May 27, 2011 - Supports --hostlist
require "rubygems"
require "time"
require "openssl"
require "rest_client"
require "json"
require "socket"
require "open-uri"
require "optparse"
require "yaml"
foreman_url = "http://localhost:3000/"
collections = [
:architectures,
:auth_source_ldaps,
:common_parameters,
:config_templates,
:domains,
:environments,
:fact_values,
:hostgroups,
:hosts,
:hypervisors,
:lookup_keys,
:media,
:operatingsystems,
:ptables,
:puppetclasses,
:reports,
:smart_proxies,
:subnets,
:usergroups,
:users
]
collections_not_implemented = [ :dashboard, :status ]
options = {}
OptionParser.new do|opts|
opts.banner = "Usage: foreman-cli [options] ..."
options[:verbose] = false
opts.on( "-v", "--verbose", "Output more information" ) do
options[:verbose] = true
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("-h", "--help", "Display this screen" ) do
puts opts
exit
end
end.parse!
def get_list(foreman_url,path)
response = RestClient.get foreman_url + path.to_s, :accept =>:json
results = JSON.parse(response.to_str)
puts JSON.pretty_generate(results)
end
p options
options.keys.each do |collection|
get_list(foreman_url,collection) if options[collection] == true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment