foremancli 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/bin/foremancli ADDED
@@ -0,0 +1,200 @@
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
+ require "optparse"
20
+
21
+ collections_filterable = [
22
+ :architectures,
23
+ :common_parameters,
24
+ :config_templates,
25
+ :domains,
26
+ :environments,
27
+ :fact_values,
28
+ :hosts,
29
+ :hostgroups,
30
+ :media,
31
+ :puppetclasses,
32
+ :reports,
33
+ :roles,
34
+ :settings,
35
+ :lookup_keys,
36
+ :dashboard,
37
+ :operatingsystems,
38
+ :subnets,
39
+ :ptables,
40
+ :users
41
+ ]
42
+
43
+ collections = [
44
+ :auth_source_ldaps,
45
+ :hypervisors,
46
+ :lookup_values,
47
+ :smart_proxies,
48
+ :statistics,
49
+ :usergroups,
50
+ ]
51
+
52
+ collections_not_implemented = [ :audits, :bookmarks ]
53
+
54
+ @foreman_user = ENV['FOREMAN_USER'] if ENV['FOREMAN_USER']
55
+ @foreman_pass = ENV['FOREMAN_PASSWORD'] if ENV['FOREMAN_PASSWORD']
56
+ @foreman_url = ENV['FOREMAN_SERVER'] if ENV['FOREMAN_SERVER']
57
+
58
+ options = {}
59
+ OptionParser.new do |opts|
60
+ opts.banner = "Usage: " + File.basename($0) + " [options] ..."
61
+
62
+ options[:debug] = false
63
+ opts.on( "-d", "--debug", "Output more information" ) do
64
+ options[:debug] = true
65
+ end
66
+ options[:username] = false
67
+ opts.on( '-u', '--user USER', "Foreman user") do |f|
68
+ options[:username] = f
69
+ end
70
+ options[:password] = false
71
+ opts.on( "-p", "--pass PASSWORD", "Foreman password" ) do |f|
72
+ options[:password] = f
73
+ end
74
+ options[:server] = false
75
+ opts.on( "-s", "--server URL", "Foreman Server URL" ) do |f|
76
+ options[:server] = f
77
+ end
78
+ options[:json] = false
79
+ opts.on( "--json", "JSON output" ) do
80
+ options[:json] = true
81
+ end
82
+ options[:yaml] = false
83
+ opts.on( "--yaml", "YAML output" ) do
84
+ options[:yaml] = true
85
+ end
86
+ options[:status] = false
87
+ opts.on( "--status", "Foreman status" ) do
88
+ options[:status] = true
89
+ end
90
+ # options[:dashboard] = false
91
+ # opts.on( "--dashboard", "Foreman dashboard" ) do |f|
92
+ # options[:dashboard] = f
93
+ # end
94
+ options[:custom] = false
95
+ opts.on( "--custom COLLECTION", "Custom COLLECTION string, see: http://theforeman.org/projects/foreman/wiki/API" ) do |f|
96
+ options[:custom] = f
97
+ end
98
+ collections_filterable.each do |collection|
99
+ options[collection] = false
100
+ opts.on("--#{collection} [filter]", "Retreive a list of #{collection}") do |f|
101
+ options[collection] = f || true
102
+ end
103
+ end
104
+ collections.each do |collection|
105
+ options[collection] = false
106
+ opts.on("--#{collection}", "Retreive a list of #{collection}") do
107
+ options[collection] = true
108
+ end
109
+ end
110
+ collections_not_implemented.each do |collection|
111
+ options[collection] = false
112
+ opts.on("--#{collection}", "Not implemented") do
113
+ options[collection] = true
114
+ end
115
+ end
116
+ opts.on_tail("-h", "--help", "Show this message") do
117
+ puts opts
118
+ puts ""
119
+ puts " ENVIRONMENT VARIABLES:"
120
+ puts ""
121
+ puts " FOREMAN_SERVER Foreman Server URL"
122
+ puts " FOREMAN_USER Foreman user"
123
+ puts " FOREMAN_PASSWORD Foreman password"
124
+ puts ""
125
+ puts " CLI options take precendence over ENVIRONMENT VARIABLES"
126
+ puts ""
127
+ puts " FILTERS:"
128
+ puts ""
129
+ puts " Please see:"
130
+ puts " http://theforeman.org/projects/foreman/wiki/Search_API"
131
+ puts ""
132
+ puts " Examples:"
133
+ puts " --hosts \"domain = domain.com AND class = squid\""
134
+ puts " --hosts \"domain = domain.com AND facts.architecture = x86_64 AND \\"
135
+ puts " class = module::class"
136
+ puts " --classes \"name = squid\""
137
+ puts " --domains \"name = domain.com\""
138
+ puts ""
139
+ exit 1
140
+ end
141
+ end.parse!
142
+ if options.values.uniq == [false]
143
+ warn "Use -h, --help for usage."
144
+ exit 1
145
+ end
146
+
147
+ require "rubygems"
148
+ require "rest_client"
149
+ require "json"
150
+ require "yaml"
151
+
152
+ @foreman_user = options.delete(:username) if options[:username]
153
+ @foreman_pass = options.delete(:password) if options[:password]
154
+ @foreman_url = options.delete(:server) if options[:server]
155
+ @usejson = options.delete(:json) if options[:json]
156
+ @useyaml = options.delete(:yaml) if options[:yaml]
157
+ @custom = options.delete(:custom) if options[:custom]
158
+ @status = options.delete(:status) if options[:status]
159
+
160
+ RestClient.log = 'stdout' if options.delete(:debug)
161
+
162
+ def get_collection(path, options = {})
163
+ JSON.parse(RestClient::Request.new({:method => :get,
164
+ :url => "#{@foreman_url}/#{path.to_s.chomp('/')}",
165
+ :user => @foreman_user, :password => @foreman_pass,
166
+ :headers => { :accept => :json, :content_type => :json }}.merge(options)).execute.to_s)
167
+ end
168
+
169
+ def search_collection(path,search)
170
+ get_collection(path, :url => "#{@foreman_url}/#{path}?search=#{URI.encode(search)}")
171
+ end
172
+
173
+ def format
174
+ return "json" if @usejson
175
+ return "yaml" if @useyaml
176
+ end
177
+
178
+ def print_response response
179
+ puts(case format
180
+ when "json"
181
+ JSON.pretty_generate(response)
182
+ when "yaml"
183
+ YAML.dump(response)
184
+ else
185
+ (response.first.is_a?(Hash) ? response.map{|o| o.inspect} : response).join("\n")
186
+ end)
187
+ end
188
+
189
+ options.each do |collection, filter|
190
+ next unless filter
191
+ if filter == true
192
+ print_response(get_collection(collection))
193
+ else
194
+ print_response(search_collection(collection, filter))
195
+ end
196
+ end
197
+
198
+ print_response(get_collection(@custom)) if @custom
199
+
200
+ puts JSON.pretty_generate(get_collection("status")) if @status
@@ -0,0 +1,42 @@
1
+
2
+ # -*- encoding: utf-8 -*-
3
+ $:.push('lib')
4
+ require "foremancli/version"
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "foremancli"
8
+ s.version = Foremancli::VERSION.dup
9
+ s.date = "2012-01-11"
10
+ s.summary = "This is the CLI for Foreman, which is a provisioning tool and node classifier for sysadmins."
11
+ s.email = "brian.gupta@brandorr.com"
12
+ s.homepage = "https://github.com/ohadlevy/foreman/blob/master/extras/cli/foremancli"
13
+ s.authors = ['Brian Gupta']
14
+
15
+ s.description = <<-EOF
16
+ This is the CLI for Foreman, which is a provisioning tool and node classifier for sysadmins.
17
+ EOF
18
+
19
+ dependencies = [
20
+ [:runtime, "rest-client", "~> 1.4"],
21
+ [:runtime, "json", "~> 1.4"],
22
+ ]
23
+
24
+ s.files = Dir['**/*']
25
+ s.test_files = Dir['test/**/*'] + Dir['spec/**/*']
26
+ s.executables = Dir['bin/*'].map { |f| File.basename(f) }
27
+ s.require_paths = ["lib"]
28
+
29
+
30
+ ## Make sure you can build the gem on older versions of RubyGems too:
31
+ s.rubygems_version = "1.3.7"
32
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
33
+ s.specification_version = 3 if s.respond_to? :specification_version
34
+
35
+ dependencies.each do |type, name, version|
36
+ if s.respond_to?("add_#{type}_dependency")
37
+ s.send("add_#{type}_dependency", name, version)
38
+ else
39
+ s.add_dependency(name, version)
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,3 @@
1
+ module Foremancli
2
+ VERSION = '0.4.1'
3
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: foremancli
3
+ version: !ruby/object:Gem::Version
4
+ hash: 13
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 4
9
+ - 1
10
+ version: 0.4.1
11
+ platform: ruby
12
+ authors:
13
+ - Brian Gupta
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-01-11 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rest-client
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 7
30
+ segments:
31
+ - 1
32
+ - 4
33
+ version: "1.4"
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: json
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ hash: 7
45
+ segments:
46
+ - 1
47
+ - 4
48
+ version: "1.4"
49
+ type: :runtime
50
+ version_requirements: *id002
51
+ description: |
52
+ This is the CLI for Foreman, which is a provisioning tool and node classifier for sysadmins.
53
+
54
+ email: brian.gupta@brandorr.com
55
+ executables:
56
+ - foremancli
57
+ extensions: []
58
+
59
+ extra_rdoc_files: []
60
+
61
+ files:
62
+ - bin/foremancli
63
+ - foremancli-0.4.1.gem
64
+ - foremancli.gemspec
65
+ - lib/foremancli/version.rb
66
+ has_rdoc: true
67
+ homepage: https://github.com/ohadlevy/foreman/blob/master/extras/cli/foremancli
68
+ licenses: []
69
+
70
+ post_install_message:
71
+ rdoc_options: []
72
+
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ hash: 3
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ hash: 3
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ requirements: []
94
+
95
+ rubyforge_project:
96
+ rubygems_version: 1.3.7
97
+ signing_key:
98
+ specification_version: 3
99
+ summary: This is the CLI for Foreman, which is a provisioning tool and node classifier for sysadmins.
100
+ test_files: []
101
+