onering-client 0.0.29 → 0.0.30
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/onering +41 -4
- data/lib/onering.rb +1 -0
- data/lib/onering/api.rb +2 -2
- data/lib/onering/plugins/reporter.rb +152 -0
- data/lib/onering/util.rb +8 -6
- metadata +22 -10
data/bin/onering
CHANGED
@@ -150,7 +150,7 @@ subcommand :devices, "Operations related to Onering's assets database" do |devic
|
|
150
150
|
json = ::JSON.load(STDIN.read)
|
151
151
|
raise "Input document must specify an ID" if sc[:args].empty? and not json['id']
|
152
152
|
|
153
|
-
print_format(api.save((sc[:args].first || json['id']), json))
|
153
|
+
print_format(api.save((sc[:args].first || json['id']), json), :json)
|
154
154
|
rescue Exception => e
|
155
155
|
STDERR.puts "#{e.class.name}: #{e.message}"
|
156
156
|
exit 1
|
@@ -232,7 +232,7 @@ subcommand :call, "Call generic Onering API endpoints" do |call|
|
|
232
232
|
:fields => (Hash[call[:opts].collect{|i| i.split(':',2) }] rescue {})
|
233
233
|
})
|
234
234
|
|
235
|
-
print_format(rv, call[:format]) unless rv.nil? or rv.to_s.strip.chomp.empty?
|
235
|
+
print_format(rv, call[:format] || :json) unless rv.nil? or rv.to_s.strip.chomp.empty?
|
236
236
|
end
|
237
237
|
end
|
238
238
|
|
@@ -241,6 +241,7 @@ subcommand :fact, "Retrieve a system fact" do |fact|
|
|
241
241
|
fact.opt :format, '-t', '--format FORMAT', "Return the results as FORMAT"
|
242
242
|
|
243
243
|
fact.exec do
|
244
|
+
Onering::Reporter.setup()
|
244
245
|
rv = []
|
245
246
|
|
246
247
|
fact[:args].each_index do |i|
|
@@ -250,13 +251,49 @@ subcommand :fact, "Retrieve a system fact" do |fact|
|
|
250
251
|
default = fact[:args][i]
|
251
252
|
end
|
252
253
|
|
253
|
-
rv << Onering::
|
254
|
+
rv << Onering::Reporter.fact(name, default)
|
254
255
|
end
|
255
256
|
|
256
257
|
rv.compact!
|
257
258
|
rv = rv.first if rv.length == 1
|
258
259
|
|
259
|
-
print_format(rv, fact[:format]) unless
|
260
|
+
print_format(rv, fact[:format]) unless rv.nil? or rv.empty?
|
261
|
+
end
|
262
|
+
end
|
263
|
+
|
264
|
+
|
265
|
+
subcommand :report, "Collect and output system information" do |report|
|
266
|
+
report.usage = "onering report"
|
267
|
+
report.opt :plugin_path, '-p', '--plugin PATH', 'Add the named path to the plugin search path'
|
268
|
+
report.opt :status, '-S', '--status STATUS', 'Set the status to report'
|
269
|
+
report.opt :tags, '-T', '--tag TAG[,...]', 'Add a tag to the report output'
|
270
|
+
report.opt :aliases, '-A', '--alias [,...]', 'Add a tag to the report output'
|
271
|
+
report.opt :id, '-I', '--id ID', 'Override the auto-detected hardware ID'
|
272
|
+
report.opt :format, '-t', '--format', 'Format the output'
|
273
|
+
|
274
|
+
report.exec do
|
275
|
+
config = {}
|
276
|
+
%w{
|
277
|
+
plugin_path
|
278
|
+
status
|
279
|
+
tags
|
280
|
+
aliases
|
281
|
+
id
|
282
|
+
}.each do |a|
|
283
|
+
a = a.to_sym
|
284
|
+
next if report[a].nil?
|
285
|
+
|
286
|
+
if [:tags, :aliases].include?(a)
|
287
|
+
config[a] = report[a].split(',')
|
288
|
+
else
|
289
|
+
config[a] = report[a]
|
290
|
+
end
|
291
|
+
end
|
292
|
+
|
293
|
+
Onering::Reporter.setup(config)
|
294
|
+
|
295
|
+
rv = Onering::Reporter.report()
|
296
|
+
print_format(rv, report[:format] || :json) unless rv.nil?
|
260
297
|
end
|
261
298
|
end
|
262
299
|
|
data/lib/onering.rb
CHANGED
data/lib/onering/api.rb
CHANGED
@@ -61,8 +61,8 @@ module Onering
|
|
61
61
|
if @_pemfile
|
62
62
|
@_pem = File.read(File.expand_path(@_pemfile))
|
63
63
|
@_http = Net::HTTP.new(@_uri.host, (@_uri.port || 443))
|
64
|
-
@_http.open_timeout =
|
65
|
-
@_http.read_timeout =
|
64
|
+
@_http.open_timeout = 30
|
65
|
+
@_http.read_timeout = 120
|
66
66
|
@_http.use_ssl = true
|
67
67
|
@_http.cert = OpenSSL::X509::Certificate.new(@_pem)
|
68
68
|
@_http.key = OpenSSL::PKey::RSA.new(@_pem)
|
@@ -0,0 +1,152 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'rubygems'
|
3
|
+
require 'optparse'
|
4
|
+
require 'json'
|
5
|
+
require 'hashlib'
|
6
|
+
|
7
|
+
def report(&block)
|
8
|
+
Onering::Reporter.add(&block)
|
9
|
+
end
|
10
|
+
|
11
|
+
module Onering
|
12
|
+
class Reporter
|
13
|
+
DEFAULT_PLUGIN_GEMNAMES=[
|
14
|
+
'onering-report-plugins'
|
15
|
+
]
|
16
|
+
|
17
|
+
DEFAULT_PLUGIN_PATH = [
|
18
|
+
'/var/lib/onering/reporter'
|
19
|
+
]
|
20
|
+
|
21
|
+
DEFAULT_FACTER_PATH = [
|
22
|
+
'/etc/facter'
|
23
|
+
]
|
24
|
+
|
25
|
+
class<<self
|
26
|
+
attr_reader :facter_path
|
27
|
+
|
28
|
+
def setup(config={})
|
29
|
+
@options = config
|
30
|
+
@facter_path = DEFAULT_FACTER_PATH
|
31
|
+
|
32
|
+
@path = [*@options[:plugin_path]]
|
33
|
+
@path += DEFAULT_PLUGIN_PATH
|
34
|
+
|
35
|
+
# add gem paths to the @path
|
36
|
+
([*@options[:plugin_gems]]+DEFAULT_PLUGIN_GEMNAMES).compact.each do |g|
|
37
|
+
begin
|
38
|
+
p = File.join(Gem::Specification.find_by_name(g).gem_dir, 'lib')
|
39
|
+
@path << File.join(p, 'reporter')
|
40
|
+
@facter_path << File.join(p, 'facter')
|
41
|
+
rescue Gem::LoadError
|
42
|
+
next
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# attempt to load things
|
47
|
+
begin
|
48
|
+
require 'rubygems'
|
49
|
+
rescue
|
50
|
+
nil
|
51
|
+
end
|
52
|
+
|
53
|
+
begin
|
54
|
+
require 'ohai'
|
55
|
+
Ohai::Config[:plugin_path] << '/etc/chef/ohai_plugins'
|
56
|
+
@ohai = Ohai::System.new
|
57
|
+
@ohai.all_plugins
|
58
|
+
rescue LoadError
|
59
|
+
@ohai = nil
|
60
|
+
end
|
61
|
+
|
62
|
+
begin
|
63
|
+
ENV['FACTERLIB'] = @facter_path.join(':')
|
64
|
+
require 'facter'
|
65
|
+
@facter = Facter
|
66
|
+
rescue LoadError
|
67
|
+
@facter = nil
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
|
72
|
+
def fact(name, default=nil)
|
73
|
+
if @facter
|
74
|
+
return (Facter.value(name) or default)
|
75
|
+
end
|
76
|
+
|
77
|
+
return nil
|
78
|
+
end
|
79
|
+
|
80
|
+
def load_plugins
|
81
|
+
# load plugins from @path
|
82
|
+
@path.compact.each do |root|
|
83
|
+
begin
|
84
|
+
Dir["#{root}/*"].each do |directory|
|
85
|
+
|
86
|
+
# only process top-level directories
|
87
|
+
if File.directory?(directory)
|
88
|
+
d = File.basename(directory)
|
89
|
+
|
90
|
+
# allow plugins to be conditionally loaded based on fact values:
|
91
|
+
# default - always load
|
92
|
+
# <fact>-<fact_value> - load if <fact> == <fact_value>
|
93
|
+
#
|
94
|
+
if d == 'default' or Facter.value(d.split('-',2).first).to_s.downcase.nil_empty == d.split('-',2).last.to_s.downcase.nil_empty
|
95
|
+
Dir[File.join(directory, '*.rb')].each do |e|
|
96
|
+
e = File.basename(e, '.rb')
|
97
|
+
require "#{directory}/#{e}"
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
rescue Exception => e
|
103
|
+
STDERR.puts e.message
|
104
|
+
next
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
def add(&block)
|
110
|
+
if block_given?
|
111
|
+
instance_eval(&block)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def property(name, value=nil)
|
116
|
+
@_report[:properties].set(name, value) if value
|
117
|
+
end
|
118
|
+
|
119
|
+
def stat(name, value=nil)
|
120
|
+
@_report[:properties][:metrics] ||= {}
|
121
|
+
@_report[:properties][:metrics].set(name, value) if value
|
122
|
+
end
|
123
|
+
|
124
|
+
def report
|
125
|
+
@id = (@options[:id] || (IO.read('/etc/hardware.id').to_s.nil_empty rescue nil))
|
126
|
+
|
127
|
+
if not @id.nil?
|
128
|
+
hostname = (Facter.value('fqdn') rescue %x{hostname -f}.strip.chomp)
|
129
|
+
|
130
|
+
@_report = {
|
131
|
+
:id => @id,
|
132
|
+
:name => hostname,
|
133
|
+
:aliases => @options[:aliases],
|
134
|
+
:tags => @options[:tags],
|
135
|
+
:status => (@options[:status] || 'online'),
|
136
|
+
:inventory => true,
|
137
|
+
:properties => {}
|
138
|
+
}
|
139
|
+
|
140
|
+
# loads plugins and populates @_report
|
141
|
+
load_plugins
|
142
|
+
|
143
|
+
return @_report.compact
|
144
|
+
else
|
145
|
+
raise "Cannot generate report without a hardware ID"
|
146
|
+
end
|
147
|
+
|
148
|
+
nil
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
data/lib/onering/util.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
module Onering
|
2
2
|
module Util
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
def fact(name, default=nil)
|
8
|
-
Facter.value(name) or default
|
3
|
+
module String
|
4
|
+
def nil_empty
|
5
|
+
return nil if (self.strip.chomp.empty? rescue true)
|
6
|
+
self.strip.chomp
|
9
7
|
end
|
10
8
|
end
|
11
9
|
end
|
12
10
|
end
|
11
|
+
|
12
|
+
class String
|
13
|
+
include Onering::Util::String
|
14
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: onering-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.30
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2013-01-29 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: facter
|
16
|
-
requirement: &
|
16
|
+
requirement: &12458020 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *12458020
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: subcommander
|
27
|
-
requirement: &
|
27
|
+
requirement: &12752860 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *12752860
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: deep_merge
|
38
|
-
requirement: &
|
38
|
+
requirement: &12749280 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *12749280
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: addressable
|
49
|
-
requirement: &
|
49
|
+
requirement: &13196740 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,7 +54,18 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :runtime
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *13196740
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: hashlib
|
60
|
+
requirement: &13192600 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :runtime
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *13192600
|
58
69
|
description: A Ruby wrapper for Onering
|
59
70
|
email: ghetzel@outbrain.com
|
60
71
|
executables:
|
@@ -67,6 +78,7 @@ files:
|
|
67
78
|
- lib/onering/api.rb
|
68
79
|
- lib/onering/plugins/devices.rb
|
69
80
|
- lib/onering/plugins/authentication.rb
|
81
|
+
- lib/onering/plugins/reporter.rb
|
70
82
|
- bin/onering
|
71
83
|
homepage: https://github.com/outbrain/onering-ruby
|
72
84
|
licenses: []
|
@@ -91,5 +103,5 @@ rubyforge_project:
|
|
91
103
|
rubygems_version: 1.8.11
|
92
104
|
signing_key:
|
93
105
|
specification_version: 3
|
94
|
-
summary: Onering client API
|
106
|
+
summary: Onering client API and utilities
|
95
107
|
test_files: []
|