class BmcDaemonLib::Conf
Constants
- PIDFILE_DIR
Attributes
app_config[R]
app_env[R]
app_host[R]
app_name[R]
app_root[R]
app_spec[R]
app_started[R]
app_ver[R]
Public Class Methods
app_config=(path)
click to toggle source
# File lib/bmc-daemon-lib/conf.rb, line 45 def app_config= path @app_config= path end
app_env=(value)
click to toggle source
# File lib/bmc-daemon-lib/conf.rb, line 40 def app_env= value @app_env = value ENV["RACK_ENV"] = value.to_s end
app_libs()
click to toggle source
Generators
# File lib/bmc-daemon-lib/conf.rb, line 143 def app_libs check_presence_of @app_name, @app_root ::File.expand_path("lib/#{@app_name}/", @app_root) end
app_root=(path)
click to toggle source
def self.init app_root
= nil
# File lib/bmc-daemon-lib/conf.rb, line 54 def app_root= path self.init_from path end
at(*path)
click to toggle source
Direct access to any depth
# 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)
click to toggle source
# File lib/bmc-daemon-lib/conf.rb, line 49 def cmd_config= path @app_config= path end
dump()
click to toggle source
# 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()
click to toggle source
# 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)
click to toggle source
# File lib/bmc-daemon-lib/conf.rb, line 132 def feature? name case name when :newrelic return feature_newrelic? when :rollbar return feature_rollbar? end return false end
feature_newrelic?()
click to toggle source
# 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?()
click to toggle source
# File lib/bmc-daemon-lib/conf.rb, line 125 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)
click to toggle source
Feature testers
# File lib/bmc-daemon-lib/conf.rb, line 116 def gem_installed? gemname Gem::Specification.collect(&:name).include? gemname end
generate_config_defaults()
click to toggle source
# 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()
click to toggle source
# 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()
click to toggle source
# File lib/bmc-daemon-lib/conf.rb, line 177 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_pidfile()
click to toggle source
# File lib/bmc-daemon-lib/conf.rb, line 173 def generate_pidfile ::File.expand_path "#{self.generate_process_name}.pid", PIDFILE_DIR end
generate_process_name()
click to toggle source
# 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()
click to toggle source
# 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)
click to toggle source
# 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 # Annnounce loaded context log_conf "initialized [#{@app_name}] version [#{@app_ver}]" #return gemspec return @app_root end
log(origin, message)
click to toggle source
# File lib/bmc-daemon-lib/conf.rb, line 263 def log origin, message printf( "%s %-14s %s \n", Time.now.strftime("%Y-%m-%d %H:%M:%S"), origin.to_s, message.to_s ) end
log_conf(msg)
click to toggle source
# File lib/bmc-daemon-lib/conf.rb, line 259 def log_conf msg self.log :conf, msg end
logfile(pipe)
click to toggle source
# 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()
click to toggle source
Plugins
# 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()
click to toggle source
# File lib/bmc-daemon-lib/conf.rb, line 204 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
reload()
click to toggle source
# 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.message rescue StandardError => e fail ConfigOtherError, "#{e.message} \n #{e.backtrace.to_yaml}" else return to_hash end
Protected Class Methods
add_config(files, path)
click to toggle source
# File lib/bmc-daemon-lib/conf.rb, line 317 def add_config files, path # Should be not empty/nil return unless path # Should be readable return unless ::File.readable?(path) # Check if Chamber's behaviour may cause problems with hyphens basename = ::File.basename(path) if basename.include?'-' log_conf "WARNING: files with dashes may cause unexpected behaviour with Chamber (#{basename})" end # Add it files << ::File.expand_path(path) end
init_from_gemspec()
click to toggle source
# File lib/bmc-daemon-lib/conf.rb, line 274 def init_from_gemspec # Check conditions check_presence_of @app_root # puts "Conf.init_from_gemspec" gemspec_path = "#{@app_root}/*.gemspec" # Try to find any gemspec file matches = Dir[gemspec_path] fail ConfigGemspecMissing, "gemspec file not found: #{gemspec_path}" if matches.size < 1 fail ConfigGemspecNotUnique, "gemspec file not found: #{gemspec_path}" if matches.size > 1 # Load Gemspec (just the only match) @spec = Gem::Specification::load(matches.first) fail ConfigGemspecInvalid, "gemspec not readable: #{gemspec_path}" unless @spec # Extract useful information from gemspec @app_name = @spec.name.to_s @app_ver = @spec.version.to_s fail ConfigMissingParameter, "gemspec: missing name" unless @app_name fail ConfigMissingParameter, "gemspec: missing version" unless @app_ver return @spec end
logfile_path(pipe)
click to toggle source
# File lib/bmc-daemon-lib/conf.rb, line 334 def logfile_path pipe # Access configuration path = self.at :logs, :path specific = self.at :logs, pipe default = self.at :logs, :default # Ignore if explicitely disabled return nil if specific == false # Fallback on default path if not provided, specific ||= default specific ||= "log/default.log" # Build logfile_path ::File.expand_path specific.to_s, path.to_s end
newrelic_init_app_name(conf)
click to toggle source
# File lib/bmc-daemon-lib/conf.rb, line 299 def newrelic_init_app_name conf # Ignore if already set return if @app_name # Check conditions check_presence_of @app_env # Stack all those parts stack = [] stack << (conf[:prefix] || @app_name) stack << conf[:platform] if conf[:platform] stack << @app_env text = stack.join('-') # Return a composite appname conf[:app_name] = "#{text}; #{text}-#{@app_host}" end
Private Class Methods
check_presence_of(*args)
click to toggle source
Check every argument for value presence
# File lib/bmc-daemon-lib/conf.rb, line 354 def check_presence_of *args # puts "check_presence_of #{args.inspect}" args.each do |arg| # OK if it's not empty # puts "- [#{arg}]" next unless arg.to_s.empty? # Otherise, we just exit log_conf "FAILED: object Conf has not been initialized correctly yet" exit 200 end end