bmc-daemon-lib 0.13.2 → 0.13.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bmc-daemon-lib.gemspec +1 -1
- data/lib/bmc-daemon-lib/conf.rb +227 -221
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a692d3958d9367cbf2e7484d7cdbb116f92d8b3e3912a2f9d6d1e897fa349dd3
|
4
|
+
data.tar.gz: 4674d842282a1b12c0e5ce0db796fddbd00149690e4cb0561a448fbc166c2e75
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b2da920d6c05e41c46563391fdf756a01d223b6366e64a0b8d37f55de22c4ef489fb8a4fd21f445b3d7d4c5359dca8df69d84f223ac8a9716c871a74da9aac29
|
7
|
+
data.tar.gz: 0fec9399f9e943f54f48d7464aa420558ac15057f49f4a78481290b105b0497028b7686626679f021d2b364db27f25739c58756c53fdf9c092a5c3d824815380
|
data/bmc-daemon-lib.gemspec
CHANGED
data/lib/bmc-daemon-lib/conf.rb
CHANGED
@@ -71,286 +71,292 @@ module BmcDaemonLib
|
|
71
71
|
to_hash.to_yaml(indent: 4, useheader: true, useversion: false )
|
72
72
|
end
|
73
73
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
path.reduce(Conf) { |m, key| m && m[key.to_s] }
|
79
|
-
end
|
80
|
-
|
81
|
-
def self.logfile pipe
|
82
|
-
# Build logfile from Conf
|
83
|
-
logfile = self.logfile_path(pipe)
|
84
|
-
return nil if logfile.nil?
|
85
|
-
|
86
|
-
# Check that we'll be able to create logfiles
|
87
|
-
if File.exists?(logfile)
|
88
|
-
# File is there, is it writable ?
|
89
|
-
unless File.writable?(logfile)
|
90
|
-
log :conf, "logging [#{pipe}] disabled: file not writable [#{logfile}]"
|
91
|
-
return nil
|
74
|
+
def dump_to_logs
|
75
|
+
self.log :conf, "configuration dump
|
76
|
+
dump.lines.each do |line|
|
77
|
+
self.log :conf, "| #{line.rstrip}"
|
92
78
|
end
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
79
|
+
end
|
80
|
+
|
81
|
+
# Direct access to any depth
|
82
|
+
def at *path
|
83
|
+
path.reduce(Conf) { |m, key| m && m[key.to_s] }
|
84
|
+
end
|
85
|
+
|
86
|
+
def logfile pipe
|
87
|
+
# Build logfile from Conf
|
88
|
+
logfile = self.logfile_path(pipe)
|
89
|
+
return nil if logfile.nil?
|
90
|
+
|
91
|
+
# Check that we'll be able to create logfiles
|
92
|
+
if ::File.exists?(logfile)
|
93
|
+
# File is there, is it writable ?
|
94
|
+
unless ::File.writable?(logfile)
|
95
|
+
log :conf, "logging [#{pipe}] disabled: file not writable [#{logfile}]"
|
96
|
+
return nil
|
97
|
+
end
|
98
|
+
else
|
99
|
+
# No file here, can we create it ?
|
100
|
+
logdir = ::File.dirname(logfile)
|
101
|
+
unless ::File.writable?(logdir)
|
102
|
+
log :conf, "logging [#{pipe}] disabled: directory not writable [#{logdir}]"
|
103
|
+
return nil
|
104
|
+
end
|
99
105
|
end
|
106
|
+
|
107
|
+
# OK, return a clean file path
|
108
|
+
log :conf, "logging [#{pipe}] to [#{logfile}]"
|
109
|
+
return logfile
|
100
110
|
end
|
101
111
|
|
102
|
-
#
|
103
|
-
|
104
|
-
|
105
|
-
|
112
|
+
# Feature testers
|
113
|
+
def gem_installed? gemname
|
114
|
+
Gem::Specification.collect(&:name).include? gemname
|
115
|
+
end
|
116
|
+
def feature_newrelic?
|
117
|
+
return false unless gem_installed?('newrelic_rpm')
|
118
|
+
return false if self.at(:newrelic, :enabled) == false
|
119
|
+
return false if self.at(:newrelic, :disabled) == true
|
120
|
+
return self.at(:newrelic, :license) || false
|
121
|
+
end
|
122
|
+
def feature_rollbar?
|
123
|
+
return false unless gem_installed?('rollbar')
|
124
|
+
return false if self.at(:rollbar, :enabled) == false
|
125
|
+
return false if self.at(:rollbar, :disabled) == true
|
126
|
+
return self.at(:rollbar, :token) || false
|
127
|
+
end
|
106
128
|
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
end
|
117
|
-
def self.feature_rollbar?
|
118
|
-
return false unless gem_installed?('rollbar')
|
119
|
-
return false if self.at(:rollbar, :enabled) == false
|
120
|
-
return false if self.at(:rollbar, :disabled) == true
|
121
|
-
return self.at(:rollbar, :token) || false
|
122
|
-
end
|
129
|
+
def feature? name
|
130
|
+
case name
|
131
|
+
when :newrelic
|
132
|
+
return feature_newrelic?
|
133
|
+
when :rollbar
|
134
|
+
return feature_rollbar?
|
135
|
+
end
|
136
|
+
return false
|
137
|
+
end
|
123
138
|
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
return feature_rollbar?
|
139
|
+
# Generators
|
140
|
+
def app_libs
|
141
|
+
check_presence_of @app_name, @app_root
|
142
|
+
|
143
|
+
::File.expand_path("lib/#{@app_name}/", @app_root)
|
130
144
|
end
|
131
|
-
return false
|
132
|
-
end
|
133
145
|
|
134
|
-
|
135
|
-
|
136
|
-
check_presence_of @app_name, @app_root
|
146
|
+
def generate_user_agent
|
147
|
+
check_presence_of @app_name, @app_ver
|
137
148
|
|
138
|
-
|
139
|
-
|
149
|
+
"#{@app_name}/#{@app_ver}"
|
150
|
+
end
|
140
151
|
|
141
|
-
|
142
|
-
|
152
|
+
def generate_process_name
|
153
|
+
check_presence_of @app_name, @app_env
|
143
154
|
|
144
|
-
|
145
|
-
|
155
|
+
parts = [@app_name, @app_env]
|
156
|
+
parts << self[:port] if self[:port]
|
157
|
+
parts.join('-')
|
158
|
+
end
|
146
159
|
|
147
|
-
|
148
|
-
|
160
|
+
def generate_config_defaults
|
161
|
+
check_presence_of @app_root
|
162
|
+
"#{@app_root}/defaults.yml"
|
163
|
+
end
|
149
164
|
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
165
|
+
def generate_config_etc
|
166
|
+
check_presence_of @app_name
|
167
|
+
"/etc/#{@app_name}.yml"
|
168
|
+
end
|
154
169
|
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
end
|
170
|
+
def generate_pidfile
|
171
|
+
::File.expand_path "#{self.generate_process_name}.pid", PIDFILE_DIR
|
172
|
+
end
|
159
173
|
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
174
|
+
def generate_config_message
|
175
|
+
return unless self.generate_config_defaults && self.generate_config_etc
|
176
|
+
"A default configuration is available (#{self.generate_config_defaults}) and can be copied to the default location (#{self.generate_config_etc}): \n sudo cp #{self.generate_config_defaults} #{self.generate_config_etc}"
|
177
|
+
end
|
164
178
|
|
165
|
-
|
166
|
-
|
167
|
-
|
179
|
+
# Plugins
|
180
|
+
def prepare_newrelic
|
181
|
+
# Disable if no config present
|
182
|
+
return unless self.feature?(:newrelic)
|
168
183
|
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
end
|
184
|
+
# Ok, let's start
|
185
|
+
log :conf, "prepare NewRelic"
|
186
|
+
conf = self[:newrelic]
|
173
187
|
|
174
|
-
|
175
|
-
|
176
|
-
# Disable if no config present
|
177
|
-
return unless self.feature?(:newrelic)
|
188
|
+
# Enable GC profiler
|
189
|
+
GC::Profiler.enable
|
178
190
|
|
179
|
-
|
180
|
-
|
181
|
-
conf = self[:newrelic]
|
191
|
+
# Build NewRelic app_name if not provided as-is
|
192
|
+
self.newrelic_init_app_name(conf)
|
182
193
|
|
183
|
-
|
184
|
-
|
194
|
+
# Set env variables
|
195
|
+
ENV["NEW_RELIC_AGENT_ENABLED"] = "true"
|
196
|
+
ENV["NEW_RELIC_LOG"] = logfile_path(:newrelic)
|
197
|
+
ENV["NEW_RELIC_LICENSE_KEY"] = conf[:license].to_s
|
198
|
+
ENV["NEW_RELIC_APP_NAME"] = conf[:app_name].to_s
|
199
|
+
end
|
185
200
|
|
186
|
-
|
187
|
-
|
201
|
+
def prepare_rollbar
|
202
|
+
# Disable if no config present
|
203
|
+
unless self.feature?(:rollbar)
|
204
|
+
Rollbar.configure do |config|
|
205
|
+
config.enabled = false
|
206
|
+
end
|
207
|
+
return
|
208
|
+
end
|
188
209
|
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
ENV["NEW_RELIC_LICENSE_KEY"] = conf[:license].to_s
|
193
|
-
ENV["NEW_RELIC_APP_NAME"] = conf[:app_name].to_s
|
194
|
-
end
|
210
|
+
# Ok, let's start
|
211
|
+
log :conf, "prepare Rollbar"
|
212
|
+
conf = self[:rollbar]
|
195
213
|
|
196
|
-
|
197
|
-
# Disable if no config present
|
198
|
-
unless self.feature?(:rollbar)
|
214
|
+
# Configure
|
199
215
|
Rollbar.configure do |config|
|
200
|
-
config.enabled =
|
216
|
+
config.enabled = true
|
217
|
+
config.access_token = conf[:token].to_s
|
218
|
+
config.code_version = @app_version
|
219
|
+
config.environment = @app_env
|
220
|
+
config.logger = LoggerPool.instance.get(:rollbar)
|
221
|
+
config.use_async = true
|
201
222
|
end
|
202
|
-
return
|
203
|
-
end
|
204
223
|
|
205
|
-
|
206
|
-
|
207
|
-
conf = self[:rollbar]
|
208
|
-
|
209
|
-
# Configure
|
210
|
-
Rollbar.configure do |config|
|
211
|
-
config.enabled = true
|
212
|
-
config.access_token = conf[:token].to_s
|
213
|
-
config.code_version = @app_version
|
214
|
-
config.environment = @app_env
|
215
|
-
config.logger = LoggerPool.instance.get(:rollbar)
|
216
|
-
config.use_async = true
|
224
|
+
# Notify startup
|
225
|
+
Rollbar.info("[#{@app_ver}] #{@app_host}")
|
217
226
|
end
|
218
227
|
|
219
|
-
|
220
|
-
|
221
|
-
end
|
228
|
+
def reload
|
229
|
+
files=[]
|
222
230
|
|
223
|
-
|
224
|
-
|
225
|
-
"%s %-14s %s \n",
|
226
|
-
Time.now.strftime("%Y-%m-%d %H:%M:%S"),
|
227
|
-
origin,
|
228
|
-
message
|
229
|
-
)
|
230
|
-
end
|
231
|
+
# Load defaults
|
232
|
+
add_config(files, self.generate_config_defaults)
|
231
233
|
|
232
|
-
|
234
|
+
# Load etc config
|
235
|
+
add_config(files, self.generate_config_etc)
|
233
236
|
|
234
|
-
|
235
|
-
|
236
|
-
check_presence_of @app_root
|
237
|
+
# Load app config
|
238
|
+
add_config(files, @app_config)
|
237
239
|
|
238
|
-
|
239
|
-
|
240
|
+
# Reload config
|
241
|
+
# puts "Conf.reload: loading files: #{files.inspect}"
|
242
|
+
log :conf, "reloading from files: #{files.inspect}"
|
243
|
+
load files: files, namespaces: { environment: @app_env }
|
240
244
|
|
241
|
-
|
242
|
-
|
243
|
-
fail ConfigGemspecMissing, "gemspec file not found: #{gemspec_path}" if matches.size < 1
|
244
|
-
fail ConfigGemspecNotUnique, "gemspec file not found: #{gemspec_path}" if matches.size > 1
|
245
|
+
# Try to access any key to force parsing of the files
|
246
|
+
self[:test35547647654856865436346453754746588586799078079876543245678654324567865432]
|
245
247
|
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
fail ConfigMissingParameter, "gemspec: missing name" unless @app_name
|
254
|
-
fail ConfigMissingParameter, "gemspec: missing version" unless @app_ver
|
248
|
+
rescue Psych::SyntaxError => e
|
249
|
+
fail ConfigParseError, e.message
|
250
|
+
rescue StandardError => e
|
251
|
+
fail ConfigOtherError, "#{e.message} \n #{e.backtrace.to_yaml}"
|
252
|
+
else
|
253
|
+
return to_hash
|
254
|
+
end
|
255
255
|
|
256
|
-
|
257
|
-
|
256
|
+
def log origin, message
|
257
|
+
printf(
|
258
|
+
"%s %-14s %s \n",
|
259
|
+
Time.now.strftime("%Y-%m-%d %H:%M:%S"),
|
260
|
+
origin,
|
261
|
+
message
|
262
|
+
)
|
263
|
+
end
|
258
264
|
|
259
|
-
|
260
|
-
# Ignore if already set
|
261
|
-
return if @app_name
|
265
|
+
protected
|
262
266
|
|
263
|
-
|
264
|
-
|
267
|
+
def init_from_gemspec
|
268
|
+
# Check conditions
|
269
|
+
check_presence_of @app_root
|
265
270
|
|
266
|
-
|
267
|
-
|
268
|
-
stack << (conf[:prefix] || @app_name)
|
269
|
-
stack << conf[:platform] if conf[:platform]
|
270
|
-
stack << @app_env
|
271
|
-
text = stack.join('-')
|
271
|
+
# puts "Conf.init_from_gemspec"
|
272
|
+
gemspec_path = "#{@app_root}/*.gemspec"
|
272
273
|
|
273
|
-
|
274
|
-
|
275
|
-
|
274
|
+
# Try to find any gemspec file
|
275
|
+
matches = Dir[gemspec_path]
|
276
|
+
fail ConfigGemspecMissing, "gemspec file not found: #{gemspec_path}" if matches.size < 1
|
277
|
+
fail ConfigGemspecNotUnique, "gemspec file not found: #{gemspec_path}" if matches.size > 1
|
276
278
|
|
277
|
-
|
278
|
-
|
279
|
+
# Load Gemspec (just the only match)
|
280
|
+
@spec = Gem::Specification::load(matches.first)
|
281
|
+
fail ConfigGemspecInvalid, "gemspec not readable: #{gemspec_path}" unless @spec
|
279
282
|
|
280
|
-
|
281
|
-
|
283
|
+
# Extract useful information from gemspec
|
284
|
+
@app_name = @spec.name.to_s
|
285
|
+
@app_ver = @spec.version.to_s
|
286
|
+
fail ConfigMissingParameter, "gemspec: missing name" unless @app_name
|
287
|
+
fail ConfigMissingParameter, "gemspec: missing version" unless @app_ver
|
282
288
|
|
283
|
-
|
284
|
-
|
289
|
+
return @spec
|
290
|
+
end
|
285
291
|
|
286
|
-
|
287
|
-
|
292
|
+
def newrelic_init_app_name conf
|
293
|
+
# Ignore if already set
|
294
|
+
return if @app_name
|
288
295
|
|
289
|
-
|
290
|
-
|
291
|
-
log :conf, "reloading from files: #{files.inspect}"
|
292
|
-
load files: files, namespaces: { environment: @app_env }
|
296
|
+
# Check conditions
|
297
|
+
check_presence_of @app_env
|
293
298
|
|
294
|
-
|
295
|
-
|
299
|
+
# Stack all those parts
|
300
|
+
stack = []
|
301
|
+
stack << (conf[:prefix] || @app_name)
|
302
|
+
stack << conf[:platform] if conf[:platform]
|
303
|
+
stack << @app_env
|
304
|
+
text = stack.join('-')
|
296
305
|
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
fail ConfigOtherError, "#{e.message} \n #{e.backtrace.to_yaml}"
|
306
|
+
# Return a composite appname
|
307
|
+
conf[:app_name] = "#{text}; #{text}-#{@app_host}"
|
308
|
+
end
|
301
309
|
|
302
|
-
|
303
|
-
|
304
|
-
|
310
|
+
def add_config files, path
|
311
|
+
# Should be not empty/nil
|
312
|
+
return unless path
|
305
313
|
|
306
|
-
|
307
|
-
|
308
|
-
return unless path
|
314
|
+
# Should be readable
|
315
|
+
return unless ::File.readable?(path)
|
309
316
|
|
310
|
-
|
311
|
-
|
317
|
+
# Check if Chamber's behaviour may cause problems with hyphens
|
318
|
+
basename = ::File.basename(path)
|
319
|
+
if basename.include?'-'
|
320
|
+
log :conf, "WARNING: files with dashes may cause unexpected behaviour with Chamber (#{basename})"
|
321
|
+
end
|
312
322
|
|
313
|
-
|
314
|
-
|
315
|
-
if basename.include?'-'
|
316
|
-
log :conf, "WARNING: files with dashes may cause unexpected behaviour with Chamber (#{basename})"
|
323
|
+
# Add it
|
324
|
+
files << ::File.expand_path(path)
|
317
325
|
end
|
318
326
|
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
# Access configuration
|
325
|
-
path = self.at :logs, :path
|
326
|
-
specific = self.at :logs, pipe
|
327
|
-
default = self.at :logs, :default
|
327
|
+
def logfile_path pipe
|
328
|
+
# Access configuration
|
329
|
+
path = self.at :logs, :path
|
330
|
+
specific = self.at :logs, pipe
|
331
|
+
default = self.at :logs, :default
|
328
332
|
|
329
|
-
|
330
|
-
|
333
|
+
# Ignore if explicitely disabled
|
334
|
+
return nil if specific == false
|
331
335
|
|
332
|
-
|
333
|
-
|
334
|
-
|
336
|
+
# Fallback on default path if not provided,
|
337
|
+
specific ||= default
|
338
|
+
specific ||= "default.log"
|
335
339
|
|
336
|
-
|
337
|
-
|
338
|
-
|
340
|
+
# Build logfile_path
|
341
|
+
::File.expand_path specific.to_s, path.to_s
|
342
|
+
end
|
339
343
|
|
340
|
-
|
344
|
+
private
|
341
345
|
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
346
|
+
# Check every argument for value presence
|
347
|
+
def check_presence_of *args
|
348
|
+
# puts "check_presence_of #{args.inspect}"
|
349
|
+
args.each do |arg|
|
350
|
+
# OK if it's not empty
|
351
|
+
# puts "- [#{arg}]"
|
352
|
+
next unless arg.to_s.empty?
|
349
353
|
|
350
|
-
|
351
|
-
|
352
|
-
|
354
|
+
# Otherise, we just exit
|
355
|
+
log :conf, "FAILED: object Conf has not been initialized correctly yet"
|
356
|
+
exit 200
|
357
|
+
end
|
353
358
|
end
|
359
|
+
|
354
360
|
end
|
355
361
|
|
356
362
|
end
|