sensu-settings 2.0.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ca8f0399f312c1be636acd8b5e2821e506293e27
4
- data.tar.gz: f555802b381e16ee23947c3e384c370b6c865eca
3
+ metadata.gz: 465a54c899448ee7c064f1228578e3fd226daecf
4
+ data.tar.gz: 4e1254a5e35fbdaee4e01be93e14e3e1295a0928
5
5
  SHA512:
6
- metadata.gz: b797e36e3831943b86bbc9ff9fa38cac622b70a987b35041ed5eb8fe5b8a72db9143037d7e34379872490cd9104f7c8e1899a3b5a7c1aefdaed42d58a872b4d4
7
- data.tar.gz: fea30269b1e5d2ee02db99adee8dfcb035fb8bac8ef334d0331137f6e1808a2ded3926dc3f0e91566ecbdc2fc9ea2a9c3d0b1e29ab9bc0c743688d265a4f2a8d
6
+ metadata.gz: 9bd57ce2da4854212f624673af2da4d7c96e0ca1c74c9a825c4f71f4723a041ff1f3ea6f70e878016cfd1e14c92c2cf0c7c33c8c62f1b75b7cbcab69931b391d
7
+ data.tar.gz: bd9d90d96bdf5546b13b796321410897c2d2d80a39688d055759fd4ab10cfff074f1fcd02b90b30b688d004410c288e83df4d3683f4762b82f683ec5c3aac578
@@ -1,6 +1,7 @@
1
1
  require "sensu/settings/validator"
2
2
  require "multi_json"
3
- require "tempfile"
3
+ require "tmpdir"
4
+ require "socket"
4
5
 
5
6
  module Sensu
6
7
  module Settings
@@ -71,24 +72,16 @@ module Sensu
71
72
  end
72
73
 
73
74
  # Load settings from the environment.
74
- # Loads: RABBITMQ_URL, REDIS_URL, REDISTOGO_URL, API_PORT, PORT
75
+ #
76
+ # Loads: SENSU_TRANSPORT_NAME, RABBITMQ_URL, REDIS_URL,
77
+ # SENSU_CLIENT_NAME, SENSU_CLIENT_ADDRESS
78
+ # SENSU_CLIENT_SUBSCRIPTIONS, SENSU_API_PORT
75
79
  def load_env
76
- if ENV["RABBITMQ_URL"]
77
- @settings[:rabbitmq] = ENV["RABBITMQ_URL"]
78
- warning("using rabbitmq url environment variable", :rabbitmq => @settings[:rabbitmq])
79
- end
80
- ENV["REDIS_URL"] ||= ENV["REDISTOGO_URL"]
81
- if ENV["REDIS_URL"]
82
- @settings[:redis] = ENV["REDIS_URL"]
83
- warning("using redis url environment variable", :redis => @settings[:redis])
84
- end
85
- ENV["API_PORT"] ||= ENV["PORT"]
86
- if ENV["API_PORT"]
87
- @settings[:api] ||= {}
88
- @settings[:api][:port] = ENV["API_PORT"].to_i
89
- warning("using api port environment variable", :api => @settings[:api])
90
- end
91
- @indifferent_access = false
80
+ load_transport_env
81
+ load_rabbitmq_env
82
+ load_redis_env
83
+ load_client_env
84
+ load_api_env
92
85
  end
93
86
 
94
87
  # Load settings from a JSON file.
@@ -136,17 +129,6 @@ module Sensu
136
129
  end
137
130
  end
138
131
 
139
- # Create a temporary file containing the colon delimited list of
140
- # loaded configuration files.
141
- #
142
- # @return [String] tempfile path.
143
- def create_loaded_tempfile!
144
- file = Tempfile.new("sensu_loaded_files")
145
- file.write(@loaded_files.join(":"))
146
- file.close
147
- file.path
148
- end
149
-
150
132
  # Set Sensu settings related environment variables. This method
151
133
  # sets `SENSU_LOADED_TEMPFILE` to a new temporary file path,
152
134
  # a file containing the colon delimited list of loaded
@@ -217,6 +199,68 @@ module Sensu
217
199
  @indifferent_access = true
218
200
  end
219
201
 
202
+ # Load Sensu transport settings from the environment. This
203
+ # method sets the Sensu transport name to `SENSU_TRANSPORT_NAME`
204
+ # if set.
205
+ def load_transport_env
206
+ if ENV["SENSU_TRANSPORT_NAME"]
207
+ @settings[:transport][:name] = ENV["SENSU_TRANSPORT_NAME"]
208
+ warning("using sensu transport name environment variable", :transport => @settings[:transport])
209
+ @indifferent_access = false
210
+ end
211
+ end
212
+
213
+ # Load Sensu RabbitMQ settings from the environment. This method
214
+ # sets the RabbitMQ settings to `RABBITMQ_URL` if set. The Sensu
215
+ # RabbitMQ transport accepts a URL string for options.
216
+ def load_rabbitmq_env
217
+ if ENV["RABBITMQ_URL"]
218
+ @settings[:rabbitmq] = ENV["RABBITMQ_URL"]
219
+ warning("using rabbitmq url environment variable", :rabbitmq => @settings[:rabbitmq])
220
+ @indifferent_access = false
221
+ end
222
+ end
223
+
224
+ # Load Sensu Redis settings from the environment. This method
225
+ # sets the Redis settings to `REDIS_URL` if set. The Sensu Redis
226
+ # library accepts a URL string for options, this applies to data
227
+ # storage and the transport.
228
+ def load_redis_env
229
+ if ENV["REDIS_URL"]
230
+ @settings[:redis] = ENV["REDIS_URL"]
231
+ warning("using redis url environment variable", :redis => @settings[:redis])
232
+ @indifferent_access = false
233
+ end
234
+ end
235
+
236
+ # Load Sensu client settings from the environment. This method
237
+ # loads client settings from several variables if
238
+ # `SENSU_CLIENT_NAME` is set: `SENSU_CLIENT_NAME`,
239
+ # `SENSU_CLIENT_ADDRESS`, and `SENSU_CLIENT_SUBSCRIPTIONS`. The
240
+ # Sensu client address defaults to the current system hostname
241
+ # and subscriptions defaults to an empty array.
242
+ def load_client_env
243
+ if ENV["SENSU_CLIENT_NAME"]
244
+ @settings[:client] ||= {}
245
+ @settings[:client][:name] = ENV["SENSU_CLIENT_NAME"]
246
+ @settings[:client][:address] = ENV.fetch("SENSU_CLIENT_ADDRESS", system_hostname)
247
+ @settings[:client][:subscriptions] = ENV.fetch("SENSU_CLIENT_SUBSCRIPTIONS", "").split(",")
248
+ warning("using sensu client environment variables", :client => @settings[:client])
249
+ @indifferent_access = false
250
+ end
251
+ end
252
+
253
+ # Load Sensu API settings from the environment. This method sets
254
+ # the API port to `SENSU_API_PORT` if set.
255
+ def load_api_env
256
+ if ENV["SENSU_API_PORT"]
257
+ @settings[:api] ||= {}
258
+ @settings[:api][:port] = ENV["SENSU_API_PORT"].to_i
259
+ warning("using api port environment variable", :api => @settings[:api])
260
+ @indifferent_access = false
261
+ end
262
+ end
263
+
220
264
  # Read a configuration file and force its encoding to 8-bit
221
265
  # ASCII, ignoring invalid characters. If there is a UTF-8 BOM,
222
266
  # it will be removed. Some JSON parsers force ASCII but do not
@@ -277,6 +321,25 @@ module Sensu
277
321
  end
278
322
  end
279
323
 
324
+ # Create a temporary file containing the colon delimited list of
325
+ # loaded configuration files. Ruby TempFile is not used to
326
+ # create the temporary file as it would be removed if the Sensu
327
+ # service daemonizes (fork/detach). The file is created in the
328
+ # system temporary file directory for the platform (Linux,
329
+ # Windows, etc.) and the file name contains the Sensu service
330
+ # name to reduce the likelihood of one Sensu service affecting
331
+ # another.
332
+ #
333
+ # @return [String] tempfile path.
334
+ def create_loaded_tempfile!
335
+ file_name = "sensu_#{sensu_service_name}_loaded_files"
336
+ path = File.join(Dir.tmpdir, file_name)
337
+ File.open(path, "w") do |file|
338
+ file.write(@loaded_files.join(":"))
339
+ end
340
+ path
341
+ end
342
+
280
343
  # Retrieve Sensu service name.
281
344
  #
282
345
  # @return [String] service name.
@@ -284,6 +347,15 @@ module Sensu
284
347
  File.basename($0).split("-").last
285
348
  end
286
349
 
350
+ # Retrieve the system hostname. If the hostname cannot be
351
+ # determined and an error is thrown, return "unknown", the same
352
+ # value Sensu uses for JIT clients.
353
+ #
354
+ # @return [String] system hostname.
355
+ def system_hostname
356
+ Socket.gethostname rescue "unknown"
357
+ end
358
+
287
359
  # Record a warning.
288
360
  #
289
361
  # @param message [String] warning message.
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "sensu-settings"
5
- spec.version = "2.0.0"
5
+ spec.version = "3.0.0"
6
6
  spec.authors = ["Sean Porter"]
7
7
  spec.email = ["portertech@gmail.com"]
8
8
  spec.summary = "The Sensu settings library, loader and validator"
data/spec/loader_spec.rb CHANGED
@@ -25,34 +25,65 @@ describe "Sensu::Settings::Loader" do
25
25
  expect(failures.size).to eq(0)
26
26
  end
27
27
 
28
+ it "can load Sensu transport settings from the environment" do
29
+ ENV["SENSU_TRANSPORT_NAME"] = "redis"
30
+ @loader.load_env
31
+ expect(@loader.warnings.size).to eq(1)
32
+ warning = @loader.warnings.shift
33
+ transport = warning[:transport]
34
+ expect(transport[:name]).to eq("redis")
35
+ ENV["SENSU_TRANSPORT_NAME"] = nil
36
+ end
37
+
28
38
  it "can load RabbitMQ settings from the environment" do
29
39
  ENV["RABBITMQ_URL"] = "amqp://guest:guest@localhost:5672/"
30
40
  @loader.load_env
31
41
  expect(@loader.warnings.size).to eq(1)
42
+ warning = @loader.warnings.shift
43
+ expect(warning[:rabbitmq]).to eq("amqp://guest:guest@localhost:5672/")
32
44
  ENV["RABBITMQ_URL"] = nil
33
45
  end
34
46
 
35
47
  it "can load Redis settings from the environment" do
36
- ENV["REDIS_URL"] = "redis://username:password@localhost:6789"
48
+ ENV["REDIS_URL"] = "redis://:password@localhost:6789"
37
49
  @loader.load_env
38
50
  expect(@loader.warnings.size).to eq(1)
51
+ warning = @loader.warnings.shift
52
+ expect(warning[:redis]).to eq("redis://:password@localhost:6789")
39
53
  ENV["REDIS_URL"] = nil
40
54
  end
41
55
 
42
- it "can load Sensu API settings from the environment" do
43
- ENV["API_PORT"] = "4567"
56
+ it "can load Sensu client settings with defaults from the environment" do
57
+ ENV["SENSU_CLIENT_NAME"] = "i-424242"
44
58
  @loader.load_env
45
59
  expect(@loader.warnings.size).to eq(1)
46
- ENV["API_PORT"] = nil
60
+ warning = @loader.warnings.shift
61
+ client = warning[:client]
62
+ expect(client[:name]).to eq("i-424242")
63
+ expect(client[:address]).to be_kind_of(String)
64
+ expect(client[:subscriptions]).to eq([])
65
+ ENV["SENSU_CLIENT_NAME"] = nil
66
+ end
67
+
68
+ it "can load Sensu client settings with defaults from the environment" do
69
+ ENV["SENSU_CLIENT_NAME"] = "i-424242"
70
+ ENV["SENSU_CLIENT_ADDRESS"] = "127.0.0.1"
71
+ ENV["SENSU_CLIENT_SUBSCRIPTIONS"] = "foo,bar,baz"
72
+ @loader.load_env
73
+ expect(@loader.warnings.size).to eq(1)
74
+ warning = @loader.warnings.shift
75
+ client = warning[:client]
76
+ expect(client[:name]).to eq("i-424242")
77
+ expect(client[:address]).to eq("127.0.0.1")
78
+ expect(client[:subscriptions]).to eq(["foo", "bar", "baz"])
79
+ ENV["SENSU_CLIENT_NAME"] = nil
47
80
  end
48
81
 
49
- it "can load Redis and Sensu API settings from the environment using alternative variables" do
50
- ENV["REDISTOGO_URL"] = "redis://username:password@localhost:6789"
51
- ENV["PORT"] = "4567"
82
+ it "can load Sensu API settings from the environment" do
83
+ ENV["SENSU_API_PORT"] = "4567"
52
84
  @loader.load_env
53
- expect(@loader.warnings.size).to eq(2)
54
- ENV["REDISTOGO_URL"] = nil
55
- ENV["PORT"] = nil
85
+ expect(@loader.warnings.size).to eq(1)
86
+ ENV["SENSU_API_PORT"] = nil
56
87
  end
57
88
 
58
89
  it "can load settings from a file" do
@@ -130,7 +161,7 @@ describe "Sensu::Settings::Loader" do
130
161
  @loader.load_directory(@config_dir)
131
162
  expect(@loader.loaded_files.size).to eq(3)
132
163
  @loader.set_env!
133
- expect(ENV["SENSU_LOADED_TEMPFILE"]).to match(/sensu_loaded_files/)
164
+ expect(ENV["SENSU_LOADED_TEMPFILE"]).to match(/sensu_rspec_loaded_files/)
134
165
  loaded_files = IO.read(ENV["SENSU_LOADED_TEMPFILE"])
135
166
  expect(loaded_files.split(":")).to eq(@loader.loaded_files)
136
167
  end
@@ -38,7 +38,7 @@ describe "Sensu::Settings" do
38
38
  expect(settings[:checks][:merger][:command]).to eq("echo -n merger")
39
39
  expect(settings[:checks][:merger][:subscribers]).to eq(["foo", "bar"])
40
40
  expect(settings[:checks][:nested][:command]).to eq("true")
41
- expect(ENV["SENSU_LOADED_TEMPFILE"]).to match(/sensu_loaded_files/)
41
+ expect(ENV["SENSU_LOADED_TEMPFILE"]).to match(/sensu_rspec_loaded_files/)
42
42
  loaded_files = IO.read(ENV["SENSU_LOADED_TEMPFILE"])
43
43
  expect(loaded_files.split(":")).to eq(settings.loaded_files)
44
44
  ENV["RABBITMQ_URL"] = nil
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sensu-settings
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Porter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-12 00:00:00.000000000 Z
11
+ date: 2015-07-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json