sensu-settings 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/sensu/settings/loader.rb +27 -21
- data/lib/sensu/settings/validators/client.rb +11 -1
- data/sensu-settings.gemspec +2 -2
- data/spec/loader_spec.rb +4 -4
- data/spec/settings_spec.rb +1 -1
- data/spec/validator_spec.rb +6 -0
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 48ba35c4667643728d564b4a6f1b896c7309cca4
|
4
|
+
data.tar.gz: b3df632e4ebfde1123b2b9c606304cabff06ea8f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5089acb63a77f61dede53af687a9ae5f9c2d01def001617c0dc360626192183a102c5d3881e28681d21022ae0c7879d41815233c3a1edf9bb18f0a75cbd99579
|
7
|
+
data.tar.gz: 3ef86a3100eddda1ff08aa3dd1e8a8103501ba93677a7bc162f7a1f70db2912867d2d01ff5edda9b59f99a76092ee7ccddcc35ecdab47ad0bfc10a6a75ebd4a6
|
@@ -1,5 +1,3 @@
|
|
1
|
-
gem "multi_json", "1.10.0"
|
2
|
-
|
3
1
|
require "sensu/settings/validator"
|
4
2
|
require "multi_json"
|
5
3
|
|
@@ -16,7 +14,10 @@ module Sensu
|
|
16
14
|
|
17
15
|
def initialize
|
18
16
|
@warnings = []
|
19
|
-
@settings =
|
17
|
+
@settings = {}
|
18
|
+
CATEGORIES.each do |category|
|
19
|
+
@settings[category] = {}
|
20
|
+
end
|
20
21
|
@indifferent_access = false
|
21
22
|
@loaded_files = []
|
22
23
|
self.class.create_category_methods
|
@@ -46,8 +47,8 @@ module Sensu
|
|
46
47
|
@settings
|
47
48
|
end
|
48
49
|
|
49
|
-
# Retrieve the
|
50
|
-
# a Hash object.
|
50
|
+
# Retrieve the setting object corresponding to a key, acting
|
51
|
+
# like a Hash object.
|
51
52
|
#
|
52
53
|
# @param key [String, Symbol]
|
53
54
|
# @return [Object] value for key.
|
@@ -60,18 +61,18 @@ module Sensu
|
|
60
61
|
def load_env
|
61
62
|
if ENV["RABBITMQ_URL"]
|
62
63
|
@settings[:rabbitmq] = ENV["RABBITMQ_URL"]
|
63
|
-
warning(
|
64
|
+
warning("using rabbitmq url environment variable", :rabbitmq => @settings[:rabbitmq])
|
64
65
|
end
|
65
66
|
ENV["REDIS_URL"] ||= ENV["REDISTOGO_URL"]
|
66
67
|
if ENV["REDIS_URL"]
|
67
68
|
@settings[:redis] = ENV["REDIS_URL"]
|
68
|
-
warning(
|
69
|
+
warning("using redis url environment variable", :redis => @settings[:redis])
|
69
70
|
end
|
70
71
|
ENV["API_PORT"] ||= ENV["PORT"]
|
71
72
|
if ENV["API_PORT"]
|
72
73
|
@settings[:api] ||= {}
|
73
74
|
@settings[:api][:port] = ENV["API_PORT"].to_i
|
74
|
-
warning(
|
75
|
+
warning("using api port environment variable", :api => @settings[:api])
|
75
76
|
end
|
76
77
|
@indifferent_access = false
|
77
78
|
end
|
@@ -82,24 +83,30 @@ module Sensu
|
|
82
83
|
def load_file(file)
|
83
84
|
if File.file?(file) && File.readable?(file)
|
84
85
|
begin
|
85
|
-
warning(
|
86
|
+
warning("loading config file", :file => file)
|
86
87
|
contents = IO.read(file)
|
87
88
|
config = MultiJson.load(contents, :symbolize_keys => true)
|
88
89
|
merged = deep_merge(@settings, config)
|
89
90
|
unless @loaded_files.empty?
|
90
91
|
changes = deep_diff(@settings, merged)
|
91
|
-
warning(
|
92
|
+
warning("config file applied changes", {
|
93
|
+
:file => file,
|
94
|
+
:changes => changes
|
95
|
+
})
|
92
96
|
end
|
93
97
|
@settings = merged
|
94
98
|
@indifferent_access = false
|
95
99
|
@loaded_files << file
|
96
100
|
rescue MultiJson::ParseError => error
|
97
|
-
warning(
|
98
|
-
|
101
|
+
warning("config file must be valid json", {
|
102
|
+
:file => file,
|
103
|
+
:error => error.to_s
|
104
|
+
})
|
105
|
+
warning("ignoring config file", :file => file)
|
99
106
|
end
|
100
107
|
else
|
101
|
-
warning(
|
102
|
-
warning(
|
108
|
+
warning("config file does not exist or is not readable", :file => file)
|
109
|
+
warning("ignoring config file", :file => file)
|
103
110
|
end
|
104
111
|
end
|
105
112
|
|
@@ -108,7 +115,7 @@ module Sensu
|
|
108
115
|
#
|
109
116
|
# @param [String] directory path.
|
110
117
|
def load_directory(directory)
|
111
|
-
warning(
|
118
|
+
warning("loading config files from directory", :directory => directory)
|
112
119
|
path = directory.gsub(/\\(?=\S)/, "/")
|
113
120
|
Dir.glob(File.join(path, "**/*.json")).each do |file|
|
114
121
|
load_file(file)
|
@@ -125,7 +132,7 @@ module Sensu
|
|
125
132
|
# Validate the loaded settings.
|
126
133
|
#
|
127
134
|
# @return [Array] validation failures.
|
128
|
-
def validate
|
135
|
+
def validate
|
129
136
|
service = ::File.basename($0).split("-").last
|
130
137
|
validator = Validator.new
|
131
138
|
validator.run(@settings, service)
|
@@ -220,16 +227,15 @@ module Sensu
|
|
220
227
|
end
|
221
228
|
end
|
222
229
|
|
223
|
-
# Record a warning
|
230
|
+
# Record a warning.
|
224
231
|
#
|
225
|
-
# @param object [Object] under suspicion.
|
226
232
|
# @param message [String] warning message.
|
233
|
+
# @param data [Hash] warning context.
|
227
234
|
# @return [Array] current warnings.
|
228
|
-
def warning(
|
235
|
+
def warning(message, data={})
|
229
236
|
@warnings << {
|
230
|
-
:object => object,
|
231
237
|
:message => message
|
232
|
-
}
|
238
|
+
}.merge(data)
|
233
239
|
end
|
234
240
|
end
|
235
241
|
end
|
@@ -15,6 +15,15 @@ module Sensu
|
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
+
# Validate client safe mode.
|
19
|
+
# Validates: safe_mode
|
20
|
+
#
|
21
|
+
# @param client [Hash] sensu client definition.
|
22
|
+
def validate_client_safe_mode(client)
|
23
|
+
must_be_boolean_if_set(client[:safe_mode]) ||
|
24
|
+
invalid(client, "client safe_mode must be boolean")
|
25
|
+
end
|
26
|
+
|
18
27
|
# Validate client socket.
|
19
28
|
# Validates: socket (bind, port)
|
20
29
|
#
|
@@ -88,7 +97,7 @@ module Sensu
|
|
88
97
|
end
|
89
98
|
|
90
99
|
# Validate a Sensu client definition.
|
91
|
-
# Validates: name, address
|
100
|
+
# Validates: name, address, safe_mode
|
92
101
|
#
|
93
102
|
# @param client [Hash] sensu client definition.
|
94
103
|
def validate_client(client)
|
@@ -101,6 +110,7 @@ module Sensu
|
|
101
110
|
invalid(client, "client name cannot contain spaces or special characters")
|
102
111
|
must_be_a_string(client[:address]) ||
|
103
112
|
invalid(client, "client address must be a string")
|
113
|
+
validate_client_safe_mode(client)
|
104
114
|
validate_client_subscriptions(client)
|
105
115
|
validate_client_socket(client)
|
106
116
|
validate_client_keepalive(client)
|
data/sensu-settings.gemspec
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |spec|
|
4
4
|
spec.name = "sensu-settings"
|
5
|
-
spec.version = "0.0.
|
5
|
+
spec.version = "0.0.3"
|
6
6
|
spec.authors = ["Sean Porter"]
|
7
7
|
spec.email = ["portertech@gmail.com"]
|
8
8
|
spec.summary = "The Sensu settings library, loader and validator"
|
@@ -15,7 +15,7 @@ Gem::Specification.new do |spec|
|
|
15
15
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
16
16
|
spec.require_paths = ["lib"]
|
17
17
|
|
18
|
-
spec.add_dependency "multi_json"
|
18
|
+
spec.add_dependency "multi_json"
|
19
19
|
|
20
20
|
spec.add_development_dependency "bundler", "~> 1.6"
|
21
21
|
spec.add_development_dependency "rake"
|
data/spec/loader_spec.rb
CHANGED
@@ -12,7 +12,7 @@ describe "Sensu::Settings::Loader" do
|
|
12
12
|
end
|
13
13
|
|
14
14
|
it "can provide a loader API" do
|
15
|
-
@loader.should respond_to(:load_env, :load_file, :load_directory, :set_env!, :validate
|
15
|
+
@loader.should respond_to(:load_env, :load_file, :load_directory, :set_env!, :validate)
|
16
16
|
end
|
17
17
|
|
18
18
|
it "can provide indifferent access to settings" do
|
@@ -21,7 +21,7 @@ describe "Sensu::Settings::Loader" do
|
|
21
21
|
end
|
22
22
|
|
23
23
|
it "can validate loaded settings" do
|
24
|
-
failures = @loader.validate
|
24
|
+
failures = @loader.validate
|
25
25
|
failures.size.should eq(0)
|
26
26
|
end
|
27
27
|
|
@@ -59,7 +59,7 @@ describe "Sensu::Settings::Loader" do
|
|
59
59
|
@loader.load_file(@config_file)
|
60
60
|
@loader.warnings.size.should eq(1)
|
61
61
|
warning = @loader.warnings.first
|
62
|
-
warning[:
|
62
|
+
warning[:file].should eq(File.expand_path(@config_file))
|
63
63
|
warning[:message].should eq("loading config file")
|
64
64
|
@loader[:api][:port].should eq(4567)
|
65
65
|
@loader["api"]["port"].should eq(4567)
|
@@ -67,7 +67,7 @@ describe "Sensu::Settings::Loader" do
|
|
67
67
|
|
68
68
|
it "can load settings from a file and validate them" do
|
69
69
|
@loader.load_file(@config_file)
|
70
|
-
failures = @loader.validate
|
70
|
+
failures = @loader.validate
|
71
71
|
reasons = failures.map do |failure|
|
72
72
|
failure[:message]
|
73
73
|
end
|
data/spec/settings_spec.rb
CHANGED
@@ -15,7 +15,7 @@ describe "Sensu::Settings" do
|
|
15
15
|
Sensu::Settings.should respond_to(:load)
|
16
16
|
Sensu::Settings.load.should be_an_instance_of(Sensu::Settings::Loader)
|
17
17
|
settings = Sensu::Settings.load
|
18
|
-
settings.should respond_to(:validate
|
18
|
+
settings.should respond_to(:validate)
|
19
19
|
end
|
20
20
|
|
21
21
|
it "can load settings from the environment, a file, and a directory" do
|
data/spec/validator_spec.rb
CHANGED
@@ -478,6 +478,12 @@ describe "Sensu::Settings::Validator" do
|
|
478
478
|
client[:redact] = ["secret"]
|
479
479
|
@validator.validate_client(client)
|
480
480
|
@validator.reset.should eq(0)
|
481
|
+
client[:safe_mode] = 1
|
482
|
+
@validator.validate_client(client)
|
483
|
+
@validator.reset.should eq(1)
|
484
|
+
client[:safe_mode] = false
|
485
|
+
@validator.validate_client(client)
|
486
|
+
@validator.reset.should eq(0)
|
481
487
|
end
|
482
488
|
|
483
489
|
it "can validate client socket" do
|
metadata
CHANGED
@@ -1,29 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sensu-settings
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sean Porter
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-05-
|
11
|
+
date: 2014-05-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: multi_json
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: '0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|