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_env ⇒ Object
Returns the value of attribute app_env.
-
.app_libs ⇒ Object
readonly
Returns the value of attribute app_libs.
-
.app_name ⇒ Object
readonly
Returns the value of attribute app_name.
-
.app_root ⇒ Object
readonly
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.
-
.files ⇒ Object
readonly
Returns the value of attribute files.
-
.host ⇒ Object
readonly
Returns the value of attribute host.
Class Method Summary collapse
-
.at(*path) ⇒ Object
Direct access to any depth.
- .dump ⇒ Object
- .feature?(name) ⇒ Boolean
-
.feature_newrelic? ⇒ Boolean
Feature testers.
- .feature_rollbar? ⇒ Boolean
- .generate_config_defaults ⇒ Object
- .generate_config_etc ⇒ Object
- .generate_config_message ⇒ Object
- .generate_pidfile ⇒ Object
- .generate_process_name ⇒ Object
-
.generate_user_agent ⇒ Object
Generators.
- .init(app_root) ⇒ Object
- .log(origin, message) ⇒ Object
- .logfile(pipe) ⇒ Object
- .prepare(args = {}) ⇒ Object
- .prepare_newrelic ⇒ Object
- .prepare_rollbar ⇒ Object
-
.reload! ⇒ Object
Reload files.
Class Attribute Details
.app_env ⇒ Object
Returns the value of attribute app_env.
18 19 20 |
# File 'lib/bmc-daemon-lib/conf.rb', line 18 def app_env @app_env end |
.app_libs ⇒ Object (readonly)
Returns the value of attribute app_libs.
20 21 22 |
# File 'lib/bmc-daemon-lib/conf.rb', line 20 def app_libs @app_libs end |
.app_name ⇒ Object (readonly)
Returns the value of attribute app_name.
21 22 23 |
# File 'lib/bmc-daemon-lib/conf.rb', line 21 def app_name @app_name end |
.app_root ⇒ Object (readonly)
Returns the value of attribute app_root.
19 20 21 |
# File 'lib/bmc-daemon-lib/conf.rb', line 19 def app_root @app_root end |
.app_spec ⇒ Object (readonly)
Returns the value of attribute app_spec.
24 25 26 |
# File 'lib/bmc-daemon-lib/conf.rb', line 24 def app_spec @app_spec end |
.app_started ⇒ Object (readonly)
Returns the value of attribute app_started.
23 24 25 |
# File 'lib/bmc-daemon-lib/conf.rb', line 23 def app_started @app_started end |
.app_ver ⇒ Object (readonly)
Returns the value of attribute app_ver.
22 23 24 |
# File 'lib/bmc-daemon-lib/conf.rb', line 22 def app_ver @app_ver end |
.files ⇒ Object (readonly)
Returns the value of attribute files.
25 26 27 |
# File 'lib/bmc-daemon-lib/conf.rb', line 25 def files @files end |
.host ⇒ Object (readonly)
Returns the value of attribute host.
26 27 28 |
# File 'lib/bmc-daemon-lib/conf.rb', line 26 def host @host end |
Class Method Details
.at(*path) ⇒ Object
Direct access to any depth
108 109 110 111 |
# File 'lib/bmc-daemon-lib/conf.rb', line 108 def self.at *path ensure_init path.reduce(Conf) { |m, key| m && m[key.to_s] } end |
.dump ⇒ Object
102 103 104 105 |
# File 'lib/bmc-daemon-lib/conf.rb', line 102 def self.dump ensure_init to_hash.to_yaml(indent: 4, useheader: true, useversion: false ) end |
.feature?(name) ⇒ Boolean
155 156 157 158 159 160 161 162 163 |
# File 'lib/bmc-daemon-lib/conf.rb', line 155 def self.feature? name case name when :newrelic return feature_newrelic? when :rollbar return end return false end |
.feature_newrelic? ⇒ Boolean
Feature testers
140 141 142 143 144 145 146 |
# File 'lib/bmc-daemon-lib/conf.rb', line 140 def self.feature_newrelic? ensure_init return false if Gem.datadir('newrelic_rpm').nil? 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
147 148 149 150 151 152 153 |
# File 'lib/bmc-daemon-lib/conf.rb', line 147 def self. ensure_init return false if Gem.datadir('rollbar').nil? return false if self.at(:rollbar, :enabled) == false return false if self.at(:rollbar, :disabled) == true return self.at(:rollbar, :token) || false end |
.generate_config_defaults ⇒ Object
170 171 172 173 |
# File 'lib/bmc-daemon-lib/conf.rb', line 170 def self.generate_config_defaults ensure_init "#{@app_root}/defaults.yml" if @app_root end |
.generate_config_etc ⇒ Object
174 175 176 177 |
# File 'lib/bmc-daemon-lib/conf.rb', line 174 def self.generate_config_etc ensure_init "/etc/#{@app_name}.yml" if @app_name end |
.generate_config_message ⇒ Object
189 190 191 192 193 194 |
# File 'lib/bmc-daemon-lib/conf.rb', line 189 def self. ensure_init config_defaults = self.generate_config_defaults config_etc = self.generate(:config_etc) "A default configuration is available (#{config_defaults}) and can be copied to the default location (#{config_etc}): \n sudo cp #{config_defaults} #{config_etc}" end |
.generate_pidfile ⇒ Object
184 185 186 187 188 |
# File 'lib/bmc-daemon-lib/conf.rb', line 184 def self.generate_pidfile ensure_init process_name = self.generate_process_name File. "#{process_name}.pid", PIDFILE_DIR end |
.generate_process_name ⇒ Object
178 179 180 181 182 183 |
# File 'lib/bmc-daemon-lib/conf.rb', line 178 def self.generate_process_name ensure_init parts = [@app_name, @app_env] parts << self[:port] if self[:port] parts.join('-') end |
.generate_user_agent ⇒ Object
Generators
166 167 168 169 |
# File 'lib/bmc-daemon-lib/conf.rb', line 166 def self.generate_user_agent ensure_init "#{@app_name}/#{@app_ver}" if @app_name && @app_ver end |
.init(app_root) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/bmc-daemon-lib/conf.rb', line 29 def self.init app_root # Permanent flags @initialized = true @app_started = Time.now # Default values @files ||= [] @app_name ||= "app_name" @app_env ||= "production" @host ||= `hostname`.to_s.chomp.split(".").first # Store and clean app_root @app_root = File.(app_root) 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 # Now we know app_name, initalize app_libs @app_libs = File.("lib/#{@app_name}/", @app_root) # By default, Newrelic is disabled ENV["NEWRELIC_AGENT_ENABLED"] = "false" # Add other config files add_config generate_config_defaults add_config generate_config_etc # Return something return @app_name end |
.log(origin, message) ⇒ Object
255 256 257 258 259 260 261 262 |
# File 'lib/bmc-daemon-lib/conf.rb', line 255 def self.log origin, printf( "%s %-14s %s \n", Time.now.strftime("%Y-%m-%d %H:%M:%S"), origin, ) end |
.logfile(pipe) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/bmc-daemon-lib/conf.rb', line 113 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(args = {}) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/bmc-daemon-lib/conf.rb', line 73 def self.prepare args = {} ensure_init # Add extra config file and load them all add_config args[:config] reload! # Set Rack env ENV["RACK_ENV"] = @app_env.to_s # Set up encodings Encoding.default_internal = "utf-8" Encoding.default_external = "utf-8" # 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.} \n #{e.backtrace.to_yaml}" end |
.prepare_newrelic ⇒ Object
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 222 223 224 225 226 |
# File 'lib/bmc-daemon-lib/conf.rb', line 196 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 # logger_newrelic = Logger.new('/tmp/newrelic.log') # logger_newrelic.debug Time.now() # Start the agent # NewRelic::Agent.manual_start({ # agent_enabled: true, # log: logger_newrelic, # env: @app_env, # license_key: conf[:license].to_s, # app_name: conf[:app_name].to_s, # }) end |
.prepare_rollbar ⇒ Object
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 |
# File 'lib/bmc-daemon-lib/conf.rb', line 228 def self. # 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}] #{@host}") end |
.reload! ⇒ Object
Reload files
97 98 99 100 |
# File 'lib/bmc-daemon-lib/conf.rb', line 97 def self.reload! ensure_init load_files end |