Class: BmcDaemonLib::Conf
- Inherits:
-
Object
- Object
- BmcDaemonLib::Conf
- Extended by:
- Chamber
- Defined in:
- lib/bmc-daemon-lib/conf.rb
Constant Summary collapse
- PIDFILE_DIR =
"/tmp/"
Class Attribute Summary collapse
-
.app_config ⇒ Object
Returns the value of attribute app_config.
-
.app_env ⇒ Object
Returns the value of attribute app_env.
-
.app_host ⇒ Object
readonly
Returns the value of attribute app_host.
-
.app_name ⇒ Object
readonly
Returns the value of attribute app_name.
-
.app_root ⇒ Object
Returns the value of attribute app_root.
-
.app_spec ⇒ Object
readonly
Returns the value of attribute app_spec.
-
.app_started ⇒ Object
readonly
Returns the value of attribute app_started.
-
.app_ver ⇒ Object
readonly
Returns the value of attribute app_ver.
Class Method Summary collapse
-
.app_libs ⇒ Object
Generators.
-
.at(*path) ⇒ Object
Direct access to any depth.
- .cmd_config=(path) ⇒ Object
- .dump ⇒ Object
- .dump_to_logs ⇒ Object
- .feature?(name) ⇒ Boolean
- .feature_newrelic? ⇒ Boolean
- .feature_rollbar? ⇒ Boolean
-
.gem_installed?(gemname) ⇒ Boolean
Feature testers.
- .generate_config_defaults ⇒ Object
- .generate_config_etc ⇒ Object
- .generate_config_message ⇒ Object
- .generate_pidfile ⇒ Object
- .generate_process_name ⇒ Object
- .generate_user_agent ⇒ Object
- .init_from(path) ⇒ Object
- .log(origin, message) ⇒ Object
- .log_conf(msg) ⇒ Object
- .logfile(pipe) ⇒ Object
-
.prepare_newrelic ⇒ Object
Plugins.
- .prepare_rollbar ⇒ Object
- .reload ⇒ Object
Class Attribute Details
.app_config ⇒ Object
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_env ⇒ Object
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_host ⇒ Object (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_name ⇒ Object (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_root ⇒ Object
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_spec ⇒ Object (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_started ⇒ Object (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_ver ⇒ Object (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_libs ⇒ Object
Generators
143 144 145 146 147 |
# File 'lib/bmc-daemon-lib/conf.rb', line 143 def app_libs check_presence_of @app_name, @app_root ::File.("lib/#{@app_name}/", @app_root) end |
.at(*path) ⇒ Object
Direct access to any depth
85 86 87 |
# File 'lib/bmc-daemon-lib/conf.rb', line 85 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 |
.dump ⇒ Object
73 74 75 |
# File 'lib/bmc-daemon-lib/conf.rb', line 73 def dump to_hash.to_yaml(indent: 4, useheader: true, useversion: false ) end |
.dump_to_logs ⇒ Object
77 78 79 80 81 82 |
# File 'lib/bmc-daemon-lib/conf.rb', line 77 def dump_to_logs log_conf "configuration dump" dump.lines.each do |line| log_conf "| #{line.rstrip}" end end |
.feature?(name) ⇒ Boolean
132 133 134 135 136 137 138 139 140 |
# File 'lib/bmc-daemon-lib/conf.rb', line 132 def feature? name case name when :newrelic return feature_newrelic? when :rollbar return end return false end |
.feature_newrelic? ⇒ Boolean
119 120 121 122 123 124 |
# File 'lib/bmc-daemon-lib/conf.rb', line 119 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
125 126 127 128 129 130 |
# File 'lib/bmc-daemon-lib/conf.rb', line 125 def 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
116 117 118 |
# File 'lib/bmc-daemon-lib/conf.rb', line 116 def gem_installed? gemname Gem::Specification.collect(&:name).include? gemname end |
.generate_config_defaults ⇒ Object
163 164 165 166 |
# File 'lib/bmc-daemon-lib/conf.rb', line 163 def generate_config_defaults check_presence_of @app_root "#{@app_root}/defaults.yml" end |
.generate_config_etc ⇒ Object
168 169 170 171 |
# File 'lib/bmc-daemon-lib/conf.rb', line 168 def generate_config_etc check_presence_of @app_name "/etc/#{@app_name}.yml" end |
.generate_config_message ⇒ Object
177 178 179 180 |
# File 'lib/bmc-daemon-lib/conf.rb', line 177 def 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_pidfile ⇒ Object
173 174 175 |
# File 'lib/bmc-daemon-lib/conf.rb', line 173 def generate_pidfile ::File. "#{self.generate_process_name}.pid", PIDFILE_DIR end |
.generate_process_name ⇒ Object
155 156 157 158 159 160 161 |
# File 'lib/bmc-daemon-lib/conf.rb', line 155 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_agent ⇒ Object
149 150 151 152 153 |
# File 'lib/bmc-daemon-lib/conf.rb', line 149 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 69 70 71 |
# File 'lib/bmc-daemon-lib/conf.rb', line 58 def init_from path # Store it @app_root = ::File.(path) return unless @app_root # Read the gemspec gemspec = init_from_gemspec # Annnounce loaded context log_conf "initialized [#{@app_name}] version [#{@app_ver}]" #return gemspec return @app_root end |
.log(origin, message) ⇒ Object
263 264 265 266 267 268 269 270 |
# File 'lib/bmc-daemon-lib/conf.rb', line 263 def log origin, printf( "%s %-14s %s \n", Time.now.strftime("%Y-%m-%d %H:%M:%S"), origin.to_s, .to_s ) end |
.log_conf(msg) ⇒ Object
259 260 261 |
# File 'lib/bmc-daemon-lib/conf.rb', line 259 def log_conf msg self.log :conf, msg end |
.logfile(pipe) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/bmc-daemon-lib/conf.rb', line 89 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_newrelic ⇒ Object
Plugins
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/bmc-daemon-lib/conf.rb', line 183 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_rollbar ⇒ Object
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/bmc-daemon-lib/conf.rb', line 204 def # Disable if no config present unless self.feature?(:rollbar) .configure do |config| config.enabled = false end return end # Ok, let's start log_conf "prepare Rollbar" conf = self[:rollbar] # Configure .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 .info("[#{@app_ver}] #{@app_host}") end |
.reload ⇒ Object
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/bmc-daemon-lib/conf.rb', line 231 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 "loading configuration from: #{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. rescue StandardError => e fail ConfigOtherError, "#{e.message} \n #{e.backtrace.to_yaml}" else return to_hash end |