lib/bmc-daemon-lib/conf.rb in bmc-daemon-lib-0.13.2 vs lib/bmc-daemon-lib/conf.rb in bmc-daemon-lib-0.13.3

- old
+ new

@@ -69,289 +69,295 @@ def dump to_hash.to_yaml(indent: 4, useheader: true, useversion: false ) end - end + def dump_to_logs + self.log :conf, "configuration dump + dump.lines.each do |line| + self.log :conf, "| #{line.rstrip}" + end + end + + # Direct access to any depth + def at *path + path.reduce(Conf) { |m, key| m && m[key.to_s] } + end - # Direct access to any depth - def self.at *path - path.reduce(Conf) { |m, key| m && m[key.to_s] } - end + def logfile pipe + # Build logfile from Conf + logfile = self.logfile_path(pipe) + return nil if logfile.nil? - 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 + # 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 - 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 + + # OK, return a clean file path + log :conf, "logging [#{pipe}] to [#{logfile}]" + return logfile end - # OK, return a clean file path - log :conf, "logging [#{pipe}] to [#{logfile}]" - return logfile - end + # Feature testers + def gem_installed? gemname + Gem::Specification.collect(&:name).include? gemname + end + 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 + 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 - # Feature testers - def self.gem_installed? gemname - Gem::Specification.collect(&:name).include? gemname - end - 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 - 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 + def feature? name + case name + when :newrelic + return feature_newrelic? + when :rollbar + return feature_rollbar? + end + return false + end - def self.feature? name - case name - when :newrelic - return feature_newrelic? - when :rollbar - return feature_rollbar? + # Generators + def app_libs + check_presence_of @app_name, @app_root + + ::File.expand_path("lib/#{@app_name}/", @app_root) end - return false - end - # Generators - def self.app_libs - check_presence_of @app_name, @app_root + def generate_user_agent + check_presence_of @app_name, @app_ver - ::File.expand_path("lib/#{@app_name}/", @app_root) - end + "#{@app_name}/#{@app_ver}" + end - def self.generate_user_agent - check_presence_of @app_name, @app_ver + def generate_process_name + check_presence_of @app_name, @app_env - "#{@app_name}/#{@app_ver}" - end + parts = [@app_name, @app_env] + parts << self[:port] if self[:port] + parts.join('-') + end - def self.generate_process_name - check_presence_of @app_name, @app_env + def generate_config_defaults + check_presence_of @app_root + "#{@app_root}/defaults.yml" + end - parts = [@app_name, @app_env] - parts << self[:port] if self[:port] - parts.join('-') - end + def generate_config_etc + check_presence_of @app_name + "/etc/#{@app_name}.yml" + end - def self.generate_config_defaults - check_presence_of @app_root - "#{@app_root}/defaults.yml" - end + def generate_pidfile + ::File.expand_path "#{self.generate_process_name}.pid", PIDFILE_DIR + end - def self.generate_config_etc - check_presence_of @app_name - "/etc/#{@app_name}.yml" - end + 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 - def self.generate_pidfile - ::File.expand_path "#{self.generate_process_name}.pid", PIDFILE_DIR - end + # Plugins + def prepare_newrelic + # Disable if no config present + return unless self.feature?(:newrelic) - 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 + # Ok, let's start + log :conf, "prepare NewRelic" + conf = self[:newrelic] - # Plugins - def self.prepare_newrelic - # Disable if no config present - return unless self.feature?(:newrelic) + # Enable GC profiler + GC::Profiler.enable - # Ok, let's start - log :conf, "prepare NewRelic" - conf = self[:newrelic] + # Build NewRelic app_name if not provided as-is + self.newrelic_init_app_name(conf) - # Enable GC profiler - GC::Profiler.enable + # 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 - # Build NewRelic app_name if not provided as-is - self.newrelic_init_app_name(conf) + def prepare_rollbar + # Disable if no config present + unless self.feature?(:rollbar) + Rollbar.configure do |config| + config.enabled = false + end + return + end - # 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 + # Ok, let's start + log :conf, "prepare Rollbar" + conf = self[:rollbar] - def self.prepare_rollbar - # Disable if no config present - unless self.feature?(:rollbar) + # Configure Rollbar.configure do |config| - config.enabled = false + 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 - 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 + # Notify startup + Rollbar.info("[#{@app_ver}] #{@app_host}") end - # Notify startup - Rollbar.info("[#{@app_ver}] #{@app_host}") - end + def reload + files=[] - def self.log origin, message - printf( - "%s %-14s %s \n", - Time.now.strftime("%Y-%m-%d %H:%M:%S"), - origin, - message - ) - end + # Load defaults + add_config(files, self.generate_config_defaults) - protected + # Load etc config + add_config(files, self.generate_config_etc) - def self.init_from_gemspec - # Check conditions - check_presence_of @app_root + # Load app config + add_config(files, @app_config) - # puts "Conf.init_from_gemspec" - gemspec_path = "#{@app_root}/*.gemspec" + # 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 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 + # Try to access any key to force parsing of the files + self[:test35547647654856865436346453754746588586799078079876543245678654324567865432] - # Load Gemspec (just the only match) - @spec = Gem::Specification::load(matches.first) - fail ConfigGemspecInvalid, "gemspec not readable: #{gemspec_path}" unless @spec + 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 - # 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 + def log origin, message + printf( + "%s %-14s %s \n", + Time.now.strftime("%Y-%m-%d %H:%M:%S"), + origin, + message + ) + end - return @spec - end + protected - def self.newrelic_init_app_name conf - # Ignore if already set - return if @app_name + def init_from_gemspec + # Check conditions + check_presence_of @app_root - # Check conditions - check_presence_of @app_env + # puts "Conf.init_from_gemspec" + gemspec_path = "#{@app_root}/*.gemspec" - # Stack all those parts - stack = [] - stack << (conf[:prefix] || @app_name) - stack << conf[:platform] if conf[:platform] - stack << @app_env - text = stack.join('-') + # 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 - # Return a composite appname - conf[:app_name] = "#{text}; #{text}-#{@app_host}" - end + # Load Gemspec (just the only match) + @spec = Gem::Specification::load(matches.first) + fail ConfigGemspecInvalid, "gemspec not readable: #{gemspec_path}" unless @spec - def self.reload - files=[] + # 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 - # Load defaults - add_config(files, self.generate_config_defaults) + return @spec + end - # Load etc config - add_config(files, self.generate_config_etc) + def newrelic_init_app_name conf + # Ignore if already set + return if @app_name - # Load app config - add_config(files, @app_config) + # Check conditions + check_presence_of @app_env - # Reload config - # puts "Conf.reload: loading files: #{files.inspect}" - log :conf, "reloading from files: #{files.inspect}" - load files: files, namespaces: { environment: @app_env } + # Stack all those parts + stack = [] + stack << (conf[:prefix] || @app_name) + stack << conf[:platform] if conf[:platform] + stack << @app_env + text = stack.join('-') - # Try to access any key to force parsing of the files - self[:test35547647654856865436346453754746588586799078079876543245678654324567865432] + # Return a composite appname + conf[:app_name] = "#{text}; #{text}-#{@app_host}" + end - rescue Psych::SyntaxError => e - fail ConfigParseError, e.message - rescue StandardError => e - fail ConfigOtherError, "#{e.message} \n #{e.backtrace.to_yaml}" + def add_config files, path + # Should be not empty/nil + return unless path - else - return to_hash - end + # Should be readable + return unless ::File.readable?(path) - def self.add_config files, path - # Should be not empty/nil - return unless 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 - # 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})" + # Add it + files << ::File.expand_path(path) end - # Add it - files << File.expand_path(path) - end + def logfile_path pipe + # Access configuration + path = self.at :logs, :path + specific = self.at :logs, pipe + default = self.at :logs, :default - def self.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 - # Ignore if explicitely disabled - return nil if specific == false + # Fallback on default path if not provided, + specific ||= default + specific ||= "default.log" - # Fallback on default path if not provided, - specific ||= default - specific ||= "default.log" + # Build logfile_path + ::File.expand_path specific.to_s, path.to_s + end - # Build logfile_path - File.expand_path specific.to_s, path.to_s - end + private - private + # Check every argument for value presence + 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? - # Check every argument for value presence - def self.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 + # Otherise, we just exit + log :conf, "FAILED: object Conf has not been initialized correctly yet" + exit 200 + end end + end end end