bmc-daemon-lib 0.12.2 → 0.13.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 14fde8ed0a71f9ab7c1e91d25bc1c79c7b2ed0bc
4
- data.tar.gz: 1367640e1bcbbdbf83bfec6c36e5117dfa644994
2
+ SHA256:
3
+ metadata.gz: c82b143ff8a357de485934538617e19a176535ed160cf608ccd652ded70abaee
4
+ data.tar.gz: 46f2b9db709b3197195eadfbd68378407cd1212c8ad61e63ef0c3856d7059814
5
5
  SHA512:
6
- metadata.gz: 62de2bca2d7fc442618e2c21f0fb6cbfc3f00c659729ae90ada05b7df48eb12fec98ba085b50b770339b09789204da2f85ca45eaa231da9a89a06d99f2aaa68a
7
- data.tar.gz: d227a660a0b54e3e0abe0f3c025b3d26c973b31b84e4a2b33a94e3b01b9c4da7554e090a8c3eb107cf121ec92f85f0981bd4633487a7697fc1f7857b42796608
6
+ metadata.gz: 396b40feb073d614a22ffc7b3999981bb2539d28fe44494aa790b8c0428183fc3d73cb1d2c545553ad88bbf6d86cfa1cc13654c974d55b3eb61736c40d793772
7
+ data.tar.gz: b5f46871abf578101a6bf51078811a9c432276b382c02e7fd623c0b5d674b4635622552be6c466172435d79bdd389d94067b6cf3b646f9d8d97a445eb68c34ba
@@ -1,7 +1,7 @@
1
1
  # coding: utf-8
2
2
  Gem::Specification.new do |spec|
3
3
  # Project version
4
- spec.version = "0.12.2"
4
+ spec.version = "0.13.2"
5
5
 
6
6
  # Project description
7
7
  spec.name = "bmc-daemon-lib"
@@ -24,12 +24,12 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency "rake"
25
25
  spec.add_development_dependency "rspec"
26
26
  spec.add_development_dependency "rubocop"
27
- #spec.add_development_dependency "pry"
28
-
27
+
29
28
  # Runtime dependencies
30
29
  # spec.add_runtime_dependency "hashie" , "~> 3.4.6" # upgrading to 3.5.4 breaks things !
31
30
  # spec.add_runtime_dependency "chamber" , "~> 2.9.1"
32
31
 
33
- spec.add_runtime_dependency "hashie" , "~> 3.4.6" # upgrading to 3.5.4 breaks things !
32
+ # spec.add_runtime_dependency "hashie" , "~> 3.4.6" # upgrading to 3.5.4 breaks things !
33
+ spec.add_runtime_dependency "hashie" , "~> 3" # upgrading to 3.5.4 breaks things !
34
34
  spec.add_runtime_dependency "chamber"
35
35
  end
@@ -14,99 +14,67 @@ module BmcDaemonLib
14
14
  extend Chamber
15
15
  PIDFILE_DIR = "/tmp/"
16
16
 
17
+ # Set up encodings
18
+ Encoding.default_internal = "utf-8"
19
+ Encoding.default_external = "utf-8"
20
+
21
+ # Some global init
22
+ @app_started = Time.now
23
+ @app_name = ""
24
+ @app_env = "production"
25
+ @app_host = `hostname`.to_s.chomp.split(".").first
26
+
27
+ # By default, Newrelic is disabled
28
+ ENV["NEWRELIC_AGENT_ENABLED"] = "false"
29
+
17
30
  class << self
18
- attr_accessor :app_env
19
31
  attr_reader :app_root
20
- attr_reader :app_libs
32
+ attr_reader :app_started
21
33
  attr_reader :app_name
34
+ attr_reader :app_env
35
+ attr_reader :app_host
22
36
  attr_reader :app_ver
23
- attr_reader :app_started
24
37
  attr_reader :app_spec
25
- attr_reader :files
26
- attr_reader :host
27
- end
28
-
29
- def self.init app_root
30
- # Permanent flags
31
- @initialized = true
32
- @app_started = Time.now
33
-
34
- # Default values
35
- @files ||= []
36
- @app_name ||= "app_name"
37
- @app_env ||= "production"
38
- @host ||= `hostname`.to_s.chomp.split(".").first
39
-
40
- # Store and clean app_root
41
- @app_root = File.expand_path(app_root)
42
- gemspec_path = "#{@app_root}/*.gemspec"
43
-
44
- # Try to find any gemspec file
45
- matches = Dir[gemspec_path]
46
- fail ConfigGemspecMissing, "gemspec file not found: #{gemspec_path}" if matches.size < 1
47
- fail ConfigGemspecNotUnique, "gemspec file not found: #{gemspec_path}" if matches.size > 1
48
-
49
- # Load Gemspec (just the only match)
50
- @spec = Gem::Specification::load(matches.first)
51
- fail ConfigGemspecInvalid, "gemspec not readable: #{gemspec_path}" unless @spec
52
-
53
- # Extract useful information from gemspec
54
- @app_name = @spec.name.to_s
55
- @app_ver = @spec.version.to_s
56
- fail ConfigMissingParameter, "gemspec: missing name" unless @app_name
57
- fail ConfigMissingParameter, "gemspec: missing version" unless @app_ver
38
+ attr_reader :app_config
58
39
 
59
- # Now we know app_name, initalize app_libs
60
- @app_libs = File.expand_path("lib/#{@app_name}/", @app_root)
61
-
62
- # By default, Newrelic is disabled
63
- ENV["NEWRELIC_AGENT_ENABLED"] = "false"
64
-
65
- # Add other config files
66
- add_config generate_config_defaults
67
- add_config generate_config_etc
68
-
69
- # Return something
70
- return @app_name
71
- end
40
+ def app_env= value
41
+ @app_env = value
42
+ ENV["RACK_ENV"] = value.to_s
43
+ end
72
44
 
73
- def self.prepare args = {}
74
- ensure_init
45
+ def app_config= path
46
+ @app_config= path
47
+ end
75
48
 
76
- # Add extra config file and load them all
77
- add_config args[:config]
78
- reload!
49
+ def cmd_config= path
50
+ @app_config= path
51
+ end
79
52
 
80
- # Set Rack env
81
- ENV["RACK_ENV"] = @app_env.to_s
53
+ # def self.init app_root = nil
54
+ def app_root= path
55
+ self.init_from path
56
+ end
82
57
 
83
- # Set up encodings
84
- Encoding.default_internal = "utf-8"
85
- Encoding.default_external = "utf-8"
58
+ def init_from path
59
+ # Store it
60
+ @app_root = ::File.expand_path(path)
61
+ return unless @app_root
86
62
 
87
- # Try to access any key to force parsing of the files
88
- self[:test35547647654856865436346453754746588586799078079876543245678654324567865432]
63
+ # Read the gemspec
64
+ gemspec = init_from_gemspec
89
65
 
90
- rescue Psych::SyntaxError => e
91
- fail ConfigParseError, e.message
92
- rescue StandardError => e
93
- fail ConfigOtherError, "#{e.message} \n #{e.backtrace.to_yaml}"
94
- end
95
-
96
- # Reload files
97
- def self.reload!
98
- ensure_init
99
- load_files
100
- end
66
+ #return gemspec
67
+ return @app_root
68
+ end
101
69
 
102
- def self.dump
103
- ensure_init
104
- to_hash.to_yaml(indent: 4, useheader: true, useversion: false )
70
+ def dump
71
+ to_hash.to_yaml(indent: 4, useheader: true, useversion: false )
72
+ end
73
+
105
74
  end
106
75
 
107
76
  # Direct access to any depth
108
77
  def self.at *path
109
- ensure_init
110
78
  path.reduce(Conf) { |m, key| m && m[key.to_s] }
111
79
  end
112
80
 
@@ -140,17 +108,13 @@ module BmcDaemonLib
140
108
  def self.gem_installed? gemname
141
109
  Gem::Specification.collect(&:name).include? gemname
142
110
  end
143
-
144
-
145
111
  def self.feature_newrelic?
146
- ensure_init
147
112
  return false unless gem_installed?('newrelic_rpm')
148
113
  return false if self.at(:newrelic, :enabled) == false
149
114
  return false if self.at(:newrelic, :disabled) == true
150
115
  return self.at(:newrelic, :license) || false
151
116
  end
152
117
  def self.feature_rollbar?
153
- ensure_init
154
118
  return false unless gem_installed?('rollbar')
155
119
  return false if self.at(:rollbar, :enabled) == false
156
120
  return false if self.at(:rollbar, :disabled) == true
@@ -168,36 +132,46 @@ module BmcDaemonLib
168
132
  end
169
133
 
170
134
  # Generators
171
- def self.generate_user_agent
172
- ensure_init
173
- "#{@app_name}/#{@app_ver}" if @app_name && @app_ver
174
- end
175
- def self.generate_config_defaults
176
- ensure_init
177
- "#{@app_root}/defaults.yml" if @app_root
135
+ def self.app_libs
136
+ check_presence_of @app_name, @app_root
137
+
138
+ ::File.expand_path("lib/#{@app_name}/", @app_root)
178
139
  end
179
- def self.generate_config_etc
180
- ensure_init
181
- "/etc/#{@app_name}.yml" if @app_name
140
+
141
+ def self.generate_user_agent
142
+ check_presence_of @app_name, @app_ver
143
+
144
+ "#{@app_name}/#{@app_ver}"
182
145
  end
146
+
183
147
  def self.generate_process_name
184
- ensure_init
148
+ check_presence_of @app_name, @app_env
149
+
185
150
  parts = [@app_name, @app_env]
186
151
  parts << self[:port] if self[:port]
187
152
  parts.join('-')
188
153
  end
154
+
155
+ def self.generate_config_defaults
156
+ check_presence_of @app_root
157
+ "#{@app_root}/defaults.yml"
158
+ end
159
+
160
+ def self.generate_config_etc
161
+ check_presence_of @app_name
162
+ "/etc/#{@app_name}.yml"
163
+ end
164
+
189
165
  def self.generate_pidfile
190
- ensure_init
191
- process_name = self.generate_process_name
192
- File.expand_path "#{process_name}.pid", PIDFILE_DIR
166
+ ::File.expand_path "#{self.generate_process_name}.pid", PIDFILE_DIR
193
167
  end
168
+
194
169
  def self.generate_config_message
195
- ensure_init
196
- config_defaults = self.generate_config_defaults
197
- config_etc = self.generate(:config_etc)
198
- "A default configuration is available (#{config_defaults}) and can be copied to the default location (#{config_etc}): \n sudo cp #{config_defaults} #{config_etc}"
170
+ return unless self.generate_config_defaults && self.generate_config_etc
171
+ "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}"
199
172
  end
200
173
 
174
+ # Plugins
201
175
  def self.prepare_newrelic
202
176
  # Disable if no config present
203
177
  return unless self.feature?(:newrelic)
@@ -217,17 +191,6 @@ module BmcDaemonLib
217
191
  ENV["NEW_RELIC_LOG"] = logfile_path(:newrelic)
218
192
  ENV["NEW_RELIC_LICENSE_KEY"] = conf[:license].to_s
219
193
  ENV["NEW_RELIC_APP_NAME"] = conf[:app_name].to_s
220
-
221
- # logger_newrelic = Logger.new('/tmp/newrelic.log')
222
- # logger_newrelic.debug Time.now()
223
- # Start the agent
224
- # NewRelic::Agent.manual_start({
225
- # agent_enabled: true,
226
- # log: logger_newrelic,
227
- # env: @app_env,
228
- # license_key: conf[:license].to_s,
229
- # app_name: conf[:app_name].to_s,
230
- # })
231
194
  end
232
195
 
233
196
  def self.prepare_rollbar
@@ -254,7 +217,7 @@ module BmcDaemonLib
254
217
  end
255
218
 
256
219
  # Notify startup
257
- Rollbar.info("[#{@app_ver}] #{@host}")
220
+ Rollbar.info("[#{@app_ver}] #{@app_host}")
258
221
  end
259
222
 
260
223
  def self.log origin, message
@@ -268,9 +231,37 @@ module BmcDaemonLib
268
231
 
269
232
  protected
270
233
 
234
+ def self.init_from_gemspec
235
+ # Check conditions
236
+ check_presence_of @app_root
237
+
238
+ # puts "Conf.init_from_gemspec"
239
+ gemspec_path = "#{@app_root}/*.gemspec"
240
+
241
+ # Try to find any gemspec file
242
+ matches = Dir[gemspec_path]
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
+
246
+ # Load Gemspec (just the only match)
247
+ @spec = Gem::Specification::load(matches.first)
248
+ fail ConfigGemspecInvalid, "gemspec not readable: #{gemspec_path}" unless @spec
249
+
250
+ # Extract useful information from gemspec
251
+ @app_name = @spec.name.to_s
252
+ @app_ver = @spec.version.to_s
253
+ fail ConfigMissingParameter, "gemspec: missing name" unless @app_name
254
+ fail ConfigMissingParameter, "gemspec: missing version" unless @app_ver
255
+
256
+ return @spec
257
+ end
258
+
271
259
  def self.newrelic_init_app_name conf
272
260
  # Ignore if already set
273
- return if conf[:app_name]
261
+ return if @app_name
262
+
263
+ # Check conditions
264
+ check_presence_of @app_env
274
265
 
275
266
  # Stack all those parts
276
267
  stack = []
@@ -280,25 +271,53 @@ module BmcDaemonLib
280
271
  text = stack.join('-')
281
272
 
282
273
  # Return a composite appname
283
- conf[:app_name] = "#{text}; #{text}-#{@host}"
274
+ conf[:app_name] = "#{text}; #{text}-#{@app_host}"
284
275
  end
285
276
 
286
- def self.load_files
287
- load files: @files, namespaces: { environment: @app_env }
277
+ def self.reload
278
+ files=[]
279
+
280
+ # Load defaults
281
+ add_config(files, self.generate_config_defaults)
282
+
283
+ # Load etc config
284
+ add_config(files, self.generate_config_etc)
285
+
286
+ # Load app config
287
+ add_config(files, @app_config)
288
+
289
+ # Reload config
290
+ # puts "Conf.reload: loading files: #{files.inspect}"
291
+ log :conf, "reloading from files: #{files.inspect}"
292
+ load files: files, namespaces: { environment: @app_env }
293
+
294
+ # Try to access any key to force parsing of the files
295
+ self[:test35547647654856865436346453754746588586799078079876543245678654324567865432]
296
+
297
+ rescue Psych::SyntaxError => e
298
+ fail ConfigParseError, e.message
299
+ rescue StandardError => e
300
+ fail ConfigOtherError, "#{e.message} \n #{e.backtrace.to_yaml}"
301
+
302
+ else
303
+ return to_hash
288
304
  end
289
305
 
290
- def self.add_config path
291
- # Skip if path is not readable
292
- return unless path && File.readable?(path)
306
+ def self.add_config files, path
307
+ # Should be not empty/nil
308
+ return unless path
309
+
310
+ # Should be readable
311
+ return unless File.readable?(path)
293
312
 
294
313
  # Check if Chamber's behaviour may cause problems with hyphens
295
314
  basename = File.basename(path)
296
- if File.basename(path).include?'-'
315
+ if basename.include?'-'
297
316
  log :conf, "WARNING: files with dashes may cause unexpected behaviour with Chamber (#{basename})"
298
317
  end
299
318
 
300
- # Store the files
301
- @files << File.expand_path(path)
319
+ # Add it
320
+ files << File.expand_path(path)
302
321
  end
303
322
 
304
323
  def self.logfile_path pipe
@@ -320,9 +339,17 @@ module BmcDaemonLib
320
339
 
321
340
  private
322
341
 
323
- def self.ensure_init
324
- unless @initialized
325
- fail ConfigInitiRequired, "ensure_init: Conf.init(app_root) should be invoked beforehand"
342
+ # Check every argument for value presence
343
+ def self.check_presence_of *args
344
+ # puts "check_presence_of #{args.inspect}"
345
+ args.each do |arg|
346
+ # OK if it's not empty
347
+ # puts "- [#{arg}]"
348
+ next unless arg.to_s.empty?
349
+
350
+ # Otherise, we just exit
351
+ log :conf, "FAILED: object Conf has not been initialized correctly yet"
352
+ exit 200
326
353
  end
327
354
  end
328
355
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bmc-daemon-lib
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.2
4
+ version: 0.13.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bruno MEDICI
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-12-21 00:00:00.000000000 Z
11
+ date: 2018-12-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -72,14 +72,14 @@ dependencies:
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: 3.4.6
75
+ version: '3'
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: 3.4.6
82
+ version: '3'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: chamber
85
85
  requirement: !ruby/object:Gem::Requirement
@@ -134,7 +134,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
134
134
  version: '0'
135
135
  requirements: []
136
136
  rubyforge_project:
137
- rubygems_version: 2.4.5
137
+ rubygems_version: 2.7.3
138
138
  signing_key:
139
139
  specification_version: 4
140
140
  summary: 'Shared utilities to build a daemon: logger, configuration, helpers'