dropsonde 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/lib/dropsonde.rb CHANGED
@@ -1,26 +1,57 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'json'
2
4
  require 'httpclient'
3
5
  require 'puppetdb'
4
6
  require 'inifile'
5
7
  require 'puppet'
6
8
 
9
+ # This class handles caching module process, generate reports,
10
+ # fetchs all plugins defined in lib/dropsonde/metrics and also
11
+ # handle connection and request to PuppetDB.
7
12
  class Dropsonde
8
13
  require 'dropsonde/cache'
9
14
  require 'dropsonde/metrics'
10
15
  require 'dropsonde/monkeypatches'
11
16
  require 'dropsonde/version'
12
17
 
13
- Puppet.initialize_settings
18
+ def self.puppet_settings_overrides
19
+ overrides = []
20
+ if (confdir = ENV['PUPPET_CONFDIR'])
21
+ overrides << '--confdir'
22
+ overrides << confdir
23
+ end
24
+
25
+ if (codedir = ENV['PUPPET_CODEDIR'])
26
+ overrides << '--codedir'
27
+ overrides << codedir
28
+ end
29
+
30
+ if (vardir = ENV['PUPPET_VARDIR'])
31
+ overrides << '--vardir'
32
+ overrides << vardir
33
+ end
34
+
35
+ if (logdir = ENV['PUPPET_LOGDIR'])
36
+ overrides << '--logdir'
37
+ overrides << logdir
38
+ end
14
39
 
15
- @@pdbclient = nil
16
- @@settings = {}
40
+ overrides
41
+ end
42
+
43
+ Puppet.initialize_settings(puppet_settings_overrides)
44
+
45
+ @pdbclient = nil
46
+ @settings = {}
17
47
  def self.settings=(arg)
18
48
  raise "Requires a Hash to set all settings at once, not a #{arg.class}" unless arg.is_a? Hash
19
- @@settings = arg
49
+
50
+ @settings = arg
20
51
  end
21
52
 
22
- def self.settings
23
- @@settings
53
+ class << self
54
+ attr_reader :settings
24
55
  end
25
56
 
26
57
  def self.generate_schema
@@ -32,24 +63,32 @@ class Dropsonde
32
63
  puts Dropsonde::Metrics.new.list
33
64
  end
34
65
 
35
- def self.generate_report(format)
66
+ def self.generate_report(format, puppetdb_session = nil)
36
67
  case format
37
68
  when 'json'
38
- puts JSON.pretty_generate(Dropsonde::Metrics.new.report)
69
+ puts JSON.pretty_generate(Dropsonde::Metrics.new.report(puppetdb_session))
39
70
  when 'human'
40
71
  puts
41
- puts Dropsonde::Metrics.new.preview
72
+ puts Dropsonde::Metrics.new.preview(puppetdb_session)
42
73
  else
43
- raise "unknown format"
74
+ raise 'unknown format'
44
75
  end
45
76
  end
46
77
 
47
78
  def self.submit_report(endpoint, port)
48
- client = HTTPClient.new()
79
+ client = HTTPClient.new
80
+
81
+ # The httpclient gem ships with some expired CA certificates.
82
+ # This causes us to load the certs shipped with whatever
83
+ # Ruby is used to execute this gem's commands, which are generally
84
+ # more up-to-date, especially if using puppet-agent's Ruby.
85
+ #
86
+ # Note that this is no-op with Windows system Ruby.
87
+ client.ssl_config.set_default_paths
88
+
49
89
  result = client.post("#{endpoint}:#{port}",
50
- :header => {'Content-Type' => 'application/json'},
51
- :body => Dropsonde::Metrics.new.report.to_json
52
- )
90
+ header: { 'Content-Type' => 'application/json' },
91
+ body: Dropsonde::Metrics.new.report.to_json)
53
92
 
54
93
  if result.status == 200
55
94
  data = JSON.parse(result.body)
@@ -69,15 +108,15 @@ class Dropsonde
69
108
  def self.generate_example(size, filename)
70
109
  metrics = Dropsonde::Metrics.new
71
110
  File.open(filename, 'w') do |file|
72
- for i in 0...size
111
+ (0...size).each do |_i|
73
112
  file.write(metrics.example.to_json)
74
113
  file.write("\n")
75
114
  end
76
115
  end
77
116
  end
78
117
 
79
- def self.puppetDB
80
- return @@pdbclient if @@pdbclient
118
+ def puppet_db
119
+ return @pdbclient if @pdbclient
81
120
 
82
121
  config = File.join(Puppet.settings[:confdir], 'puppetdb.conf')
83
122
 
@@ -85,14 +124,13 @@ class Dropsonde
85
124
 
86
125
  server = IniFile.load(config)['main']['server_urls'].split(',').first
87
126
 
88
- @@pdbclient = PuppetDB::Client.new({
89
- :server => server,
90
- :pem => {
91
- 'key' => Puppet.settings[:hostprivkey],
92
- 'cert' => Puppet.settings[:hostcert],
93
- 'ca_file' => Puppet.settings[:localcacert],
94
- }
95
- })
127
+ @pdbclient = PuppetDB::Client.new({
128
+ server: server,
129
+ pem: {
130
+ 'key' => Puppet.settings[:hostprivkey],
131
+ 'cert' => Puppet.settings[:hostcert],
132
+ 'ca_file' => Puppet.settings[:localcacert],
133
+ },
134
+ })
96
135
  end
97
-
98
136
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dropsonde
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Ford
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-11 00:00:00.000000000 Z
11
+ date: 2021-11-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -161,7 +161,7 @@ files:
161
161
  - lib/dropsonde/version.rb
162
162
  homepage: https://github.com/puppetlabs/dropsonde
163
163
  licenses:
164
- - Apache 2
164
+ - Apache-2.0
165
165
  metadata: {}
166
166
  post_install_message:
167
167
  rdoc_options: []
@@ -178,7 +178,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
178
178
  - !ruby/object:Gem::Version
179
179
  version: '0'
180
180
  requirements: []
181
- rubygems_version: 3.0.6
181
+ rubygems_version: 3.1.2
182
182
  signing_key:
183
183
  specification_version: 4
184
184
  summary: A simple telemetry probe for gathering usage information about Puppet infrastructures.