[PATCH/foreman 1/2] Added deprication warning to ssh-using-foreman

Signed-off-by: Brian Gupta <brian.gupta@brandorr.com>

··· --- extras/query/ssh_using_foreman | 5 +++++ 1 files changed, 5 insertions(+), 0 deletions(-)

diff --git a/extras/query/ssh_using_foreman b/extras/query/ssh_using_foreman
index 4f4cd39…7acc0d2 100755
— a/extras/query/ssh_using_foreman
+++ b/extras/query/ssh_using_foreman
@@ -29,6 +29,9 @@ optparse = OptionParser.new do |opts|
opts.on( ‘-u’, ‘–user USER’, “User to use - defaults to #{options[:user]}” ) do |user|
options[:user] = user
end

  • opts.on( ‘-q’, ‘–quiet’, ‘Suppress deprication warning’ ) do |cmd|

  • options[:quiet] = true

  • end

    options[:facts] = {}
    opts.on( ‘-f’, ‘–facts fact=x,fact=y…’, ‘one or more facts to filter the host list’ ) do |facts|
    @@ -55,6 +58,7 @@ optparse = OptionParser.new do |opts|
    end

optparse.parse!
+puts “WARNING: The Query API is deprecated and will be retired. Please use the new Search API.” unless options[:quiet]

if options[:command].empty? or (options[:facts].empty? and options[:class].empty?)
puts optparse
@@ -66,6 +70,7 @@ unless ARGV.empty?
exit 1
end

puts "About to execute: #{options[:command]}"
query = {“state” => options[:state]}
query.merge!({“fact” => options[:facts]}) unless options[:facts].empty?

1.7.4.1

Signed-off-by: Brian Gupta <brian.gupta@brandorr.com>

··· --- extras/cli/foremancli | 181 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 181 insertions(+), 0 deletions(-) create mode 100755 extras/cli/foremancli

diff --git a/extras/cli/foremancli b/extras/cli/foremancli
new file mode 100755
index 0000000…22f0ce0
— /dev/null
+++ b/extras/cli/foremancli
@@ -0,0 +1,181 @@
+#!/usr/bin/env ruby
+# Copyright © 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 “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: API - Foreman” ) 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

1.7.4.1