Guest User

Untitled

a guest
Jan 13th, 2018
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. # Copyright (C) 2011 Vijay Brian Gupta brian.gupta@brandorr.com
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  17. # USA.
  18. #
  19. # v0.1 May 27, 2011 - Supports --hostlist
  20. # v0.9 Oct 7, 2011 - Supports filters, and SSL and basicauth, and url, user and password as options.
  21.  
  22. require "rubygems"
  23. require "time"
  24. require "openssl"
  25. require "rest_client"
  26. require "json"
  27. require "socket"
  28. require "open-uri"
  29. require "optparse"
  30. require "yaml"
  31.  
  32. collections_filterable = [
  33. :hosts,
  34. :puppetclasses,
  35. :fact_values,
  36. :environments,
  37. :hostgroups,
  38. ]
  39.  
  40. collections = [
  41. :architectures,
  42. :auth_source_ldaps,
  43. :common_parameters,
  44. :config_templates,
  45. :domains,
  46. :hypervisors,
  47. :lookup_keys,
  48. :media,
  49. :operatingsystems,
  50. :ptables,
  51. :reports,
  52. :smart_proxies,
  53. :subnets,
  54. :usergroups,
  55. :users
  56. ]
  57.  
  58. collections_not_implemented = [ :dashboard, :status ]
  59.  
  60. options = {}
  61. OptionParser.new do|opts|
  62. opts.banner = "Usage: " + File.basename($0) + " [options] ..."
  63.  
  64. options[:verbose] = false
  65. opts.on( "-v", "--verbose", "Output more information" ) do
  66. options[:verbose] = true
  67. end
  68. options[:username] = false
  69. opts.on( '-u', '--user USER', "Foreman user") do|f|
  70. options[:username] = f
  71. end
  72. options[:password] = false
  73. opts.on( "-p", "--pass PASSWORD", "Foreman password" ) do|f|
  74. options[:password] = f
  75. end
  76. options[:server] = false
  77. opts.on( "-s", "--server URL", "Foreman Server URL" ) do|f|
  78. options[:server] = f
  79. end
  80. options[:json] = false
  81. opts.on( "--json", "JSON output" ) do|f|
  82. options[:json] = f
  83. end
  84. collections_filterable.each do |collection|
  85. options[collection] = false
  86. opts.on("--#{collection} [filter]", "Retreive a list of #{collection}") do|f|
  87. options[collection] = f || true
  88. end
  89. end
  90. collections.each do |collection|
  91. options[collection] = false
  92. opts.on("--#{collection}", "Retreive a list of #{collection}") do
  93. options[collection] = true
  94. end
  95. end
  96. collections_not_implemented.each do |collection|
  97. options[collection] = false
  98. opts.on("--#{collection}", "Not implemented") do
  99. options[collection] = true
  100. end
  101. end
  102. opts.on_tail("-h", "--help", "Show this message") do
  103. puts opts
  104. exit
  105. end
  106. end.parse!
  107.  
  108. puts "Use -h, --help for usage." if options.values.uniq == [false]
  109.  
  110. @foreman_user = options.delete(:username) if options[:username]
  111. @foreman_pass = options.delete(:password) if options[:password]
  112. @foreman_url = options.delete(:server) if options[:server]
  113. @usejson = options.delete(:json) if options[:json]
  114.  
  115. RestClient.log = 'stdout' if options.delete(:verbose)
  116.  
  117. def get_collection(path)
  118. response = RestClient::Request.new(:method => :get,
  119. :url => @foreman_url + "/" + path.to_s,
  120. :user => @foreman_user, :password => @foreman_pass,
  121. :headers => { :accept => :json, :content_type => :json }).execute
  122. results = JSON.parse(response.to_str)
  123. end
  124.  
  125. def search_collection(path,search)
  126. response = RestClient::Request.new(:method => :get,
  127. :url => @foreman_url + "/" + path.to_s + "?search=" + search,
  128. :user => @foreman_user, :password => @foreman_pass,
  129. :headers => { :accept => :json, :content_type => :json }).execute
  130. results = JSON.parse(response.to_str)
  131. end
  132.  
  133. options.keys.each do |collection|
  134. if options[collection]
  135. if options[collection] == true
  136. if @usejson
  137. puts JSON.pretty_generate(get_collection(collection))
  138. else
  139. puts get_collection(collection)
  140. end
  141. else
  142. if @usejson
  143. puts JSON.pretty_generate(search_collection(collection,options[collection]))
  144. else
  145. puts search_collection(collection,options[collection])
  146. end
  147. end
  148. end
  149. end
Add Comment
Please, Sign In to add comment