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



135
136
137
138
139
# File 'lib/bmc-daemon-lib/conf.rb', line 135

def self.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



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

def self.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

.feature?(name) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
127
128
129
130
131
132
# File 'lib/bmc-daemon-lib/conf.rb', line 124

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

.feature_newrelic?Boolean

Returns:

  • (Boolean)


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

def self.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)


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

def self.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)


108
109
110
# File 'lib/bmc-daemon-lib/conf.rb', line 108

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

.generate_config_defaultsObject



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

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

.generate_config_etcObject



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

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

.generate_config_messageObject



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

def self.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



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

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

.generate_process_nameObject



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

def self.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



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

def self.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



223
224
225
226
227
228
229
230
# File 'lib/bmc-daemon-lib/conf.rb', line 223

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

.logfile(pipe) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/bmc-daemon-lib/conf.rb', line 81

def self.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



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/bmc-daemon-lib/conf.rb', line 175

def self.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



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/bmc-daemon-lib/conf.rb', line 196

def self.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