Class: BmcDaemonLib::Conf

Inherits:
Object
  • Object
show all
Extended by:
Chamber
Defined in:
lib/bmc-daemon-lib/conf.rb

Constant Summary collapse

PIDFILE_DIR =
"/tmp/"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.app_configObject

Returns the value of attribute app_config.



38
39
40
# File 'lib/bmc-daemon-lib/conf.rb', line 38

def app_config
  @app_config
end

.app_envObject

Returns the value of attribute app_env.



34
35
36
# File 'lib/bmc-daemon-lib/conf.rb', line 34

def app_env
  @app_env
end

.app_hostObject (readonly)

Returns the value of attribute app_host.



35
36
37
# File 'lib/bmc-daemon-lib/conf.rb', line 35

def app_host
  @app_host
end

.app_nameObject (readonly)

Returns the value of attribute app_name.



33
34
35
# File 'lib/bmc-daemon-lib/conf.rb', line 33

def app_name
  @app_name
end

.app_rootObject

Returns the value of attribute app_root.



31
32
33
# File 'lib/bmc-daemon-lib/conf.rb', line 31

def app_root
  @app_root
end

.app_specObject (readonly)

Returns the value of attribute app_spec.



37
38
39
# File 'lib/bmc-daemon-lib/conf.rb', line 37

def app_spec
  @app_spec
end

.app_startedObject (readonly)

Returns the value of attribute app_started.



32
33
34
# File 'lib/bmc-daemon-lib/conf.rb', line 32

def app_started
  @app_started
end

.app_verObject (readonly)

Returns the value of attribute app_ver.



36
37
38
# File 'lib/bmc-daemon-lib/conf.rb', line 36

def app_ver
  @app_ver
end

Class Method Details

.app_libsObject

Generators



140
141
142
143
144
# File 'lib/bmc-daemon-lib/conf.rb', line 140

def app_libs
  check_presence_of @app_name, @app_root

  ::File.expand_path("lib/#{@app_name}/", @app_root)
end

.at(*path) ⇒ Object

Direct access to any depth



82
83
84
# File 'lib/bmc-daemon-lib/conf.rb', line 82

def at *path
  path.reduce(Conf) { |m, key| m && m[key.to_s] }
end

.cmd_config=(path) ⇒ Object



49
50
51
# File 'lib/bmc-daemon-lib/conf.rb', line 49

def cmd_config= path
  @app_config= path
end

.dumpObject



70
71
72
# File 'lib/bmc-daemon-lib/conf.rb', line 70

def dump
  to_hash.to_yaml(indent: 4, useheader: true, useversion: false )
end

.dump_to_logsObject



74
75
76
77
78
79
# File 'lib/bmc-daemon-lib/conf.rb', line 74

def dump_to_logs
  self.log :conf, "configuration dump"
  dump.lines.each do |line|
    self.log :conf, "|  #{line.rstrip}"
  end
end

.feature?(name) ⇒ Boolean

Returns:

  • (Boolean)


129
130
131
132
133
134
135
136
137
# File 'lib/bmc-daemon-lib/conf.rb', line 129

def feature? name
  case name
  when :newrelic
    return feature_newrelic?
  when :rollbar
    return feature_rollbar?
  end
  return false
end

.feature_newrelic?Boolean

Returns:

  • (Boolean)


116
117
118
119
120
121
# File 'lib/bmc-daemon-lib/conf.rb', line 116

def feature_newrelic?
  return false unless gem_installed?('newrelic_rpm')
  return false if self.at(:newrelic, :enabled) == false
  return false if self.at(:newrelic, :disabled) == true
  return self.at(:newrelic, :license) || false
end

.feature_rollbar?Boolean

Returns:

  • (Boolean)


122
123
124
125
126
127
# File 'lib/bmc-daemon-lib/conf.rb', line 122

def feature_rollbar?
  return false unless gem_installed?('rollbar')
  return false if self.at(:rollbar, :enabled) == false
  return false if self.at(:rollbar, :disabled) == true
  return self.at(:rollbar, :token) || false
end

.gem_installed?(gemname) ⇒ Boolean

Feature testers

Returns:

  • (Boolean)


113
114
115
# File 'lib/bmc-daemon-lib/conf.rb', line 113

def gem_installed? gemname
  Gem::Specification.collect(&:name).include? gemname
end

.generate_config_defaultsObject



160
161
162
163
# File 'lib/bmc-daemon-lib/conf.rb', line 160

def generate_config_defaults
  check_presence_of @app_root
  "#{@app_root}/defaults.yml"
end

.generate_config_etcObject



165
166
167
168
# File 'lib/bmc-daemon-lib/conf.rb', line 165

def generate_config_etc
  check_presence_of @app_name
  "/etc/#{@app_name}.yml"
end

.generate_config_messageObject



174
175
176
177
# File 'lib/bmc-daemon-lib/conf.rb', line 174

def generate_config_message
  return unless self.generate_config_defaults && self.generate_config_etc
  "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}"
end

.generate_pidfileObject



170
171
172
# File 'lib/bmc-daemon-lib/conf.rb', line 170

def generate_pidfile
  ::File.expand_path "#{self.generate_process_name}.pid", PIDFILE_DIR
end

.generate_process_nameObject



152
153
154
155
156
157
158
# File 'lib/bmc-daemon-lib/conf.rb', line 152

def generate_process_name
  check_presence_of @app_name, @app_env

  parts = [@app_name, @app_env]
  parts << self[:port] if self[:port]
  parts.join('-')
end

.generate_user_agentObject



146
147
148
149
150
# File 'lib/bmc-daemon-lib/conf.rb', line 146

def generate_user_agent
  check_presence_of @app_name, @app_ver

  "#{@app_name}/#{@app_ver}"
end

.init_from(path) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/bmc-daemon-lib/conf.rb', line 58

def init_from path
  # Store it
  @app_root = ::File.expand_path(path)
  return unless @app_root

  # Read the gemspec
  gemspec = init_from_gemspec

  #return gemspec
  return @app_root
end

.log(origin, message) ⇒ Object



256
257
258
259
260
261
262
263
# File 'lib/bmc-daemon-lib/conf.rb', line 256

def log origin, message
  printf(
    "%s %-14s %s \n",
    Time.now.strftime("%Y-%m-%d %H:%M:%S"),
    origin,
    message
    )
end

.logfile(pipe) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/bmc-daemon-lib/conf.rb', line 86

def logfile pipe
  # Build logfile from Conf
  logfile = self.logfile_path(pipe)
  return nil if logfile.nil?

  # Check that we'll be able to create logfiles
  if ::File.exists?(logfile)
    # File is there, is it writable ?
    unless ::File.writable?(logfile)
      log :conf, "logging [#{pipe}] disabled: file not writable [#{logfile}]"
      return nil
    end
  else
    # No file here, can we create it ?
    logdir = ::File.dirname(logfile)
    unless ::File.writable?(logdir)
      log :conf, "logging [#{pipe}] disabled: directory not writable [#{logdir}]"
      return nil
    end
  end

  # OK, return a clean file path
  log :conf, "logging [#{pipe}] to [#{logfile}]"
  return logfile
end

.prepare_newrelicObject

Plugins



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/bmc-daemon-lib/conf.rb', line 180

def prepare_newrelic
  # Disable if no config present
  return unless self.feature?(:newrelic)

  # Ok, let's start
  log :conf, "prepare NewRelic"
  conf = self[:newrelic]

  # Enable GC profiler
  GC::Profiler.enable

  # Build NewRelic app_name if not provided as-is
  self.newrelic_init_app_name(conf)

  # Set env variables
  ENV["NEW_RELIC_AGENT_ENABLED"] = "true"
  ENV["NEW_RELIC_LOG"] = logfile_path(:newrelic)
  ENV["NEW_RELIC_LICENSE_KEY"] = conf[:license].to_s
  ENV["NEW_RELIC_APP_NAME"] = conf[:app_name].to_s
end

.prepare_rollbarObject



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/bmc-daemon-lib/conf.rb', line 201

def prepare_rollbar
  # Disable if no config present
  unless self.feature?(:rollbar)
    Rollbar.configure do |config|
      config.enabled = false
    end
    return
  end

  # Ok, let's start
  log :conf, "prepare Rollbar"
  conf = self[:rollbar]

  # Configure
  Rollbar.configure do |config|
    config.enabled = true
    config.access_token = conf[:token].to_s
    config.code_version = @app_version
    config.environment  = @app_env
    config.logger       = LoggerPool.instance.get(:rollbar)
    config.use_async = true
  end

  # Notify startup
  Rollbar.info("[#{@app_ver}] #{@app_host}")
end

.reloadObject



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/bmc-daemon-lib/conf.rb', line 228

def reload
  files=[]

  # Load defaults
  add_config(files, self.generate_config_defaults)

  # Load etc config
  add_config(files, self.generate_config_etc)

  # Load app config
  add_config(files, @app_config)

  # Reload config
  # puts "Conf.reload: loading files: #{files.inspect}"
  log :conf, "reloading from files: #{files.inspect}"
  load files: files, namespaces: { environment: @app_env }

  # Try to access any key to force parsing of the files
  self[:test35547647654856865436346453754746588586799078079876543245678654324567865432]

rescue Psych::SyntaxError => e
  fail ConfigParseError, e.message
rescue StandardError => e
  fail ConfigOtherError, "#{e.message} \n #{e.backtrace.to_yaml}"
else
  return to_hash
end