Class: Onering::CLI::Report

Inherits:
Plugin
  • Object
show all
Defined in:
lib/onering/cli/reporter.rb

Constant Summary collapse

DEFAULT_CACHE_MAXAGE =
600

Class Method Summary collapse

Methods inherited from Plugin

default_format, inherited, registered_plugins

Class Method Details

.configure(global = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/onering/cli/reporter.rb', line 6

def self.configure(global={})
  @global_opts = global
  @api = Onering::CLI.connect(@global_opts.merge({
    :autoconnect => false
  }))

  @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] [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
    opt :save,           "Save the report output to the configured Onering server"
    opt :timeout,        "The maximum amount of time to wait for a report to be generated", :type => :integer, :default => 60
    opt :plugin_timeout, "The maximum amount of time to wait for a report plugin to return data", :type => :integer, :default => 10
    opt :local,          "Do not attempt to contact a remote server for retrieving values not found locally", :type => :boolean, :default => false
    opt :cachefile,      "Use the specified file as a local report cache", :type => :string, :short => '-F'
    opt :nocache,        "Do not attempt to use a cache file for report generation", :type => :boolean, :default => false
    opt :maxage,         "Maxmimum age (in seconds) of the cache before it is automatically regenerated", :type => :integer, :default => DEFAULT_CACHE_MAXAGE

    stop_on %w{get save}
  end

# initialize report generator with user options
  @_reporter = Onering::Reporter.new({
    :id             => @opts[:id],
    :timeout        => @opts[:timeout],
    :plugin_timeout => @opts[:plugin_timeout],
    :nocache        => @opts[:nocache],
    :cachefile      => @opts[:cachefile],
    :maxage         => @opts[:maxage]
  }.compact)
end

.run(args) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/onering/cli/reporter.rb', line 55

def self.run(args)
# saving, by default, should not use the cache (but should update it to keep it fresh)
  if @opts[:save] === true or args[0] == 'save'
    report = _report({
      :cacheregen => true
    })
  else
    report = _report()
  end
  
# pull overrides from CLI arguments
  @opts[:fields].each do |field|
    key, value = field.split('=', 2)
    Onering::Logger.debug("Override value #{key} from command line argument", "Onering::CLI::Report")

    value = nil if ['null', '', '-'].include?(value.to_s.strip.chomp)
    report = report.set(key, value)
  end

# save if specified
  if @opts[:save] === true
    _save(report)

  else
    sc = args.shift

    case (sc.downcase.to_sym rescue nil)
  # -----------------------------------------------------------------------------
    when :save
      _save(report)
      return nil

    when :get
      Onering::Logger.fatal!("Expected at least 1 parameter, got #{args.length}", "Onering::CLI::Report") unless args.length >= 1
      return @_reporter.get(args[0], args[1], {
        :data  => report,
        :api   => @global_opts,
        :local => @opts[:local]
      })
    end
  end

  return report
end