Skip to content

Instantly share code, notes, and snippets.

@bgupta
Created May 27, 2011 07:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bgupta/994819 to your computer and use it in GitHub Desktop.
Save bgupta/994819 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" }
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
options[:architectures] = false
opts.on('--architectures', 'Retreive a list of architectures') do
options[:architectures] = true
end
options[:auth_source_ldaps] = false
opts.on( '--auth_source_ldaps', 'Retrieve a list of Auth source ldaps') do
options[:auth_source_ldaps] = true
end
options[:common_parameters] = false
opts.on( '--common_parameters', 'Retrieve a list of') do
options[:common_parameters] = true
end
options[:config_templates] = false
opts.on( '--config_templates', 'Retrieve a list of') do
options[:config_templates] = true
end
options[:domains] = false
opts.on( '--domains', 'Retrieve a list of') do
options[:domains] = true
end
options[:environments] = false
opts.on( '--environments', 'Retrieve a list of') do
options[:environments] = true
end
options[:fact_values] = false
opts.on( '--fact_values', 'Retrieve a list of') do
options[:fact_values] = true
end
options[:hostgroups] = false
opts.on( '--hostgroups', 'Retrieve a list of') do
options[:hostgroups] = true
end
options[:hosts] = false
opts.on( '--hosts', 'Retrieve a list of') do
options[:hosts] = true
end
options[:hypervisors] = false
opts.on( '--hypervisors', 'Retrieve a list of') do
options[:hypervisors] = true
end
options[:lookup_keys] = false
opts.on( '--lookup_keys', 'Retrieve a list of') do
options[:lookup_keys] = true
end
options[:media] = false
opts.on( '--media', 'Retrieve a list of') do
options[:media] = true
end
options[:operatingsystems] = false
opts.on( '--operatingsystems', 'Retrieve a list of') do
options[:operatingsystems] = true
end
options[:ptables] = false
opts.on( '--ptables', 'Retrieve a list of') do
options[:ptables] = true
end
options[:puppetclasses] = false
opts.on( '--puppetclasses', 'Retrieve a list of') do
options[:puppetclasses] = true
end
options[:reports] = false
opts.on( '--reports', 'Retrieve a list of') do
options[:reports] = true
end
options[:smart_proxies] = false
opts.on( '--smart_proxies', 'Retrieve a list of') do
options[:smart_proxies] = true
end
options[:subnets] = false
opts.on( '--subnets', 'Retrieve a list of') do
options[:subnets] = true
end
options[:usergroups] = false
opts.on( '--usergroups', 'Retrieve a list of ') do
options[:usergroups] = true
end
options[:users] = false
opts.on( '--users', 'Retrieve a list of users') do
options[:users] = true
end
options[:dashboard] = false
opts.on( '--dashboard', 'Not implemented') do
options[:dashboard] = true
end
options[:status] = false
opts.on( '--status', 'Not implemented') do
options[:status] = true
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,
:accept =>:json
results = JSON.parse(response.to_str)
puts JSON.pretty_generate(results)
end
if options[:architectures]
get_list(foreman_url,"architectures")
exit
end
if options[:auth_source_ldaps]
get_list(foreman_url, "auth_source_ldaps")
exit
end
if options[:common_parameter]
get_list(foreman_url, "common_parameters")
exit
end
if options[:config_templates]
get_list(foreman_url, "config_templates")
exit
end
if options[:domains]
get_list(foreman_url, "domains")
exit
end
if options[:environments]
get_list(foreman_url, "environments")
exit
end
if options[:fact_values]
get_list(foreman_url, "fact_values")
exit
end
if options[:hostgroups]
get_list(foreman_url, "hostsgroups")
exit
end
if options[:hosts]
get_list(foreman_url, "hosts")
exit
end
if options[:hypervisors]
get_list(foreman_url, "hypervisors")
exit
end
if options[:lookup_keys]
get_list(foreman_url, "lookup_keys")
exit
end
if options[:media]
get_list(foreman_url, "media")
exit
end
if options[:models]
get_list(foreman_url, "models")
exit
end
if options[:operatingsystems]
get_list(foreman_url, "operatingsystems")
exit
end
if options[:ptables]
get_list(foreman_url, "ptables")
exit
end
if options[:puppetclasses]
get_list(foreman_url, "puppetclasses")
exit
end
if options[:reports]
get_list(foreman_url, "reports")
exit
end
if options[:roles]
get_list(foreman_url, "roles")
exit
end
if options[:smart_proxies]
get_list(foreman_url, "smart_proxies")
exit
end
if options[:subnets]
get_list(foreman_url, "subnets")
exit
end
if options[:usergroups]
get_list(foreman_url, "usergroups")
exit
end
if options[:users]
get_list(foreman_url, "users")
exit
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment