lib/onering/cli/reporter.rb in onering-client-0.1.7 vs lib/onering/cli/reporter.rb in onering-client-0.2.0
- old
+ new
@@ -3,20 +3,30 @@
module Report
DEFAULT_CACHE_MAXAGE=600
def self.configure(global={})
- @api = (Onering::CLI.connect(global.merge({
+ @api = Onering::CLI.connect(global.merge({
:autoconnect => false
- })) rescue nil)
+ }))
@opts = ::Trollop::options do
banner <<-EOS
Generate a system report that can be saved or submitted to a Onering server
+Subcommands:
+ <none>
+ Generate and output the system inventory report.
+
+ get <property> [fallback]
+ Get a specific <property> from the report, return [fallback] if not found.
+
+ save
+ Generate and save the system inventory report to Onering.
+
Usage:
- onering [global] report [options]
+ onering [global] report [options] [subcommands]
Options:
EOS
opt :id, "Override the autodetected Hardware ID for this node", :short => '-I', :type => :string
opt :fields, "Set the named FIELD to equal VALUE in the format FIELD=VALUE. Can be specified multiple times", :short => '-o', :type => :string, :multi => true
@@ -30,11 +40,11 @@
stop_on %w{get save}
end
# initialize report generator with user options
- Onering::Reporter.setup({
+ @_reporter = Onering::Reporter.new({
:id => @opts[:id],
:timeout => @opts[:timeout],
:plugin_timeout => @opts[:plugin_timeout],
:nocache => @opts[:nocache],
:cachefile => @opts[:cachefile],
@@ -73,13 +83,43 @@
when :save
_save(report)
return nil
when :get
- Onering::Logger.fatal!("Expected at least 1 parameter, got #{args.length}", "Onering::CLI::Devices") unless args.length >= 1
+ Onering::Logger.fatal!("Expected at least 1 parameter, got #{args.length}", "Onering::CLI::Report") unless args.length >= 1
- rv = report.get("properties.#{args[0]}", report.get(args[0], args[1]))
+ # this is kinda ugly
+ # because we don't know which property might have an @-prefix, progressively
+ # search through all of them. first non-null match wins
+ parts = args[0].split('.')
+
+ # create an array with every component of the path prefixed with the @-symbol, then with
+ # the path as is.
+ #
+ # e.g.: onering report get metrics.disk.block
+ # -> value exists in the inventory as properties.metrics.disk.@block,
+ # but the user shouldn't need to know where that @-prefix is, so...
+ #
+ # Search for all of these, first non-nil value wins:
+ # * properties.metrics.disk.block
+ # * properties.@metrics.disk.block
+ # * properties.metrics.@disk.block
+ # * properties.metrics.disk.@block
+ # * metrics.disk.block
+ #
+ candidates = [(['properties']+parts).join('.')] + parts.collect.with_index{|i,ix|
+ (['properties']+(ix == 0 ? [] : parts[0..(ix-1)]) + ["@#{i}"] + parts[ix+1..-1]).join('.')
+ }.flatten()
+
+ # search for the key using science or something
+ candidates.each do |c|
+ rv = report.get(c)
+ break unless rv.nil?
+ end
+
+ # if we're still nil by this point, use the fallback value
+ rv = report.get(args[0], args[1]) if rv.nil?
# attempt to get the value remotely if not found locally
if rv.nil? and not @opts[:local]
hid = Onering::Util.fact(:hardwareid)
@@ -106,10 +146,11 @@
end
def self._report(options={})
begin
Onering::Logger.debug("Gathering local data for report", "Onering::CLI::Report")
- report = Onering::Reporter.report(options).stringify_keys()
+ Onering::Logger.fatal("Reporter not configured. This is a bug", "Onering::CLI::Report") if @_reporter.nil?
+ report = @_reporter.report(options).stringify_keys()
# pull report overrides from the config file
Onering::Config.get('reporter.fields',{}).each do |key, value|
Onering::Logger.debug("Override value #{key} from config file", "Onering::CLI::Report")
\ No newline at end of file