Class: Dropsonde

Inherits:
Object
  • Object
show all
Defined in:
lib/dropsonde.rb,
lib/dropsonde/version.rb

Overview

This class handles caching module process, generate reports, fetchs all plugins defined in lib/dropsonde/metrics and also handle connection and request to PuppetDB.

Defined Under Namespace

Classes: Cache, Metrics

Constant Summary collapse

VERSION =
'0.0.8'

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.settingsObject

Returns the value of attribute settings.



54
55
56
# File 'lib/dropsonde.rb', line 54

def settings
  @settings
end

Class Method Details

.generate_example(size, filename) ⇒ Object



108
109
110
111
112
113
114
115
116
# File 'lib/dropsonde.rb', line 108

def self.generate_example(size, filename)
  metrics = Dropsonde::Metrics.new
  File.open(filename, 'w') do |file|
    (0...size).each do |_i|
      file.write(metrics.example.to_json)
      file.write("\n")
    end
  end
end

.generate_report(format, puppetdb_session = nil) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/dropsonde.rb', line 66

def self.generate_report(format, puppetdb_session = nil)
  case format
  when 'json'
    puts JSON.pretty_generate(Dropsonde::Metrics.new.report(puppetdb_session))
  when 'human'
    puts
    puts Dropsonde::Metrics.new.preview(puppetdb_session)
  else
    raise 'unknown format'
  end
end

.generate_schemaObject



57
58
59
# File 'lib/dropsonde.rb', line 57

def self.generate_schema
  puts JSON.pretty_generate(Dropsonde::Metrics.new.schema)
end

.list_metricsObject



61
62
63
64
# File 'lib/dropsonde.rb', line 61

def self.list_metrics
  puts
  puts Dropsonde::Metrics.new.list
end

.puppet_settings_overridesObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/dropsonde.rb', line 18

def self.puppet_settings_overrides
  overrides = []
  if (confdir = ENV['PUPPET_CONFDIR'])
    overrides << '--confdir'
    overrides << confdir
  end

  if (codedir = ENV['PUPPET_CODEDIR'])
    overrides << '--codedir'
    overrides << codedir
  end

  if (vardir = ENV['PUPPET_VARDIR'])
    overrides << '--vardir'
    overrides << vardir
  end

  if (logdir = ENV['PUPPET_LOGDIR'])
    overrides << '--logdir'
    overrides << logdir
  end

  overrides
end

.submit_report(endpoint, port) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/dropsonde.rb', line 78

def self.submit_report(endpoint, port)
  client = HTTPClient.new

  # The httpclient gem ships with some expired CA certificates.
  # This causes us to load the certs shipped with whatever
  # Ruby is used to execute this gem's commands, which are generally
  # more up-to-date, especially if using puppet-agent's Ruby.
  #
  # Note that this is no-op with Windows system Ruby.
  client.ssl_config.set_default_paths

  result = client.post("#{endpoint}:#{port}",
                       header: { 'Content-Type' => 'application/json' },
                       body: Dropsonde::Metrics.new.report.to_json)

  if result.status == 200
    data = JSON.parse(result.body)
    if data['newer']
      puts 'A newer version of the telemetry client is available:'
      puts "  -- #{data['link']}"
    else
      puts data['message']
    end
  else
    puts 'Failed to submit report'
    puts JSON.pretty_generate(result.body) if Dropsonde.settings[:verbose]
    exit 1
  end
end

Instance Method Details

#puppet_dbObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/dropsonde.rb', line 118

def puppet_db
  return @pdbclient if @pdbclient

  config = File.join(Puppet.settings[:confdir], 'puppetdb.conf')

  return unless File.file? config

  server = IniFile.load(config)['main']['server_urls'].split(',').first

  @pdbclient = PuppetDB::Client.new({
                                      server: server,
                                      pem: {
                                        'key' => Puppet.settings[:hostprivkey],
                                        'cert' => Puppet.settings[:hostcert],
                                        'ca_file' => Puppet.settings[:localcacert],
                                      },
                                    })
end