Class: Shared::Conf
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
- .add_config(path) ⇒ Object
-
.at(*path) ⇒ Object
Direct access to any depth.
- .dump ⇒ Object
- .ensure_init ⇒ Object
-
.generate(what) ⇒ Object
Defaults generators.
- .init(app_root) ⇒ Object
- .load_files ⇒ Object
- .newrelic_enabled? ⇒ Boolean
- .prepare(args = {}) ⇒ Object
- .prepare_newrelic(section, logfile) ⇒ Object
-
.reload! ⇒ Object
Reload files.
Class Attribute Details
.app_env ⇒ Object
Returns the value of attribute app_env.
16 17 18 |
# File 'lib/shared/conf.rb', line 16 def app_env @app_env end |
.app_libs ⇒ Object (readonly)
Returns the value of attribute app_libs.
18 19 20 |
# File 'lib/shared/conf.rb', line 18 def app_libs @app_libs end |
.app_name ⇒ Object (readonly)
Returns the value of attribute app_name.
19 20 21 |
# File 'lib/shared/conf.rb', line 19 def app_name @app_name end |
.app_root ⇒ Object (readonly)
Returns the value of attribute app_root.
17 18 19 |
# File 'lib/shared/conf.rb', line 17 def app_root @app_root end |
.app_spec ⇒ Object (readonly)
Returns the value of attribute app_spec.
22 23 24 |
# File 'lib/shared/conf.rb', line 22 def app_spec @app_spec end |
.app_started ⇒ Object (readonly)
Returns the value of attribute app_started.
21 22 23 |
# File 'lib/shared/conf.rb', line 21 def app_started @app_started end |
.app_ver ⇒ Object (readonly)
Returns the value of attribute app_ver.
20 21 22 |
# File 'lib/shared/conf.rb', line 20 def app_ver @app_ver end |
.files ⇒ Object (readonly)
Returns the value of attribute files.
23 24 25 |
# File 'lib/shared/conf.rb', line 23 def files @files end |
.host ⇒ Object (readonly)
Returns the value of attribute host.
24 25 26 |
# File 'lib/shared/conf.rb', line 24 def host @host end |
Class Method Details
.add_config(path) ⇒ Object
153 154 155 |
# File 'lib/shared/conf.rb', line 153 def self.add_config path @files << File.(path) if path && File.readable?(path) end |
.at(*path) ⇒ Object
Direct access to any depth
104 105 106 107 |
# File 'lib/shared/conf.rb', line 104 def self.at *path ensure_init path.reduce(Conf) { |m, key| m && m[key.to_s] } end |
.dump ⇒ Object
98 99 100 101 |
# File 'lib/shared/conf.rb', line 98 def self.dump ensure_init to_hash.to_yaml(indent: 4, useheader: true, useversion: false ) end |
.ensure_init ⇒ Object
192 193 194 195 196 197 198 |
# File 'lib/shared/conf.rb', line 192 def self.ensure_init # Skip is already done return if @initialized # Go through init if not already done self.init end |
.generate(what) ⇒ Object
Defaults generators
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/shared/conf.rb', line 115 def self.generate what ensure_init return case what when :user_agent "#{@app_name}/#{@app_ver}" if @app_name && @app_ver when :config_defaults "#{@app_root}/defaults.yml" if @app_root when :config_etc "/etc/#{@app_name}.yml" if @app_name when :process_name parts = [@app_name, @app_env] parts << self[:port] if self[:port] parts.join('-') when :pidfile process_name = self.generate(:process_name) File. "#{process_name}.pid", PIDFILE_DIR when :config_message 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 end |
.init(app_root) ⇒ Object
27 28 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 |
# File 'lib/shared/conf.rb', line 27 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) # Try to find any gemspec file matches = Dir["#{@app_root}/*.gemspec"] fail ConfigMissingGemspec, "gemspec file not found: #{gemspec_path}" if matches.size < 1 fail ConfigMultipleGemspec, "gemspec file not found: #{gemspec_path}" if matches.size > 1 # Load Gemspec (just the only match) @spec = Gem::Specification::load(matches.first) @app_name = @spec.name @app_ver = @spec.version 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) # Add other config files #add_default_config add_config generate(:config_defaults) add_config generate(:config_etc) # Return something return @app_name end |
.load_files ⇒ Object
149 150 151 |
# File 'lib/shared/conf.rb', line 149 def self.load_files load files: @files, namespaces: { environment: @app_env } end |
.newrelic_enabled? ⇒ Boolean
109 110 111 112 |
# File 'lib/shared/conf.rb', line 109 def self.newrelic_enabled? ensure_init self[:newrelic] && self[:newrelic][:licence] end |
.prepare(args = {}) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/shared/conf.rb', line 65 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" # Init New Relic newrelic_logfile = File.(Conf[:logs][:newrelic].to_s, Conf[:logs][:path].to_s) prepare_newrelic self[:newrelic], newrelic_logfile # Try to access any key to force parsing of the files self[:dummy] rescue Psych::SyntaxError => e fail ConfigParseError, e. rescue StandardError => e fail ConfigOtherError, "#{e.message} \n #{e.backtrace.to_yaml}" end |
.prepare_newrelic(section, logfile) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/shared/conf.rb', line 157 def self.prepare_newrelic section, logfile # Disable NewRelic if no config present unless self.newrelic_enabled? ENV["NEWRELIC_AGENT_ENABLED"] = "false" return end # Enable GC profiler GC::Profiler.enable # Enable module ENV["NEWRELIC_AGENT_ENABLED"] = "true" ENV["NEW_RELIC_MONITOR_MODE"] = "true" # License ENV["NEW_RELIC_LICENSE_KEY"] = section[:licence].to_s # Build NewRelic app_name if not provided as-is if section[:app_name] ENV["NEW_RELIC_APP_NAME"] = section[:app_name].to_s else stack = [] stack << (section[:prefix] || @app_name) stack << section[:platform] if section[:platform] stack << @app_env text = stack.join('-') ENV["NEW_RELIC_APP_NAME"] = "#{text}-#{host};#{text}" end # Logfile ENV["NEW_RELIC_LOG"] = logfile.to_s if logfile end |
.reload! ⇒ Object
Reload files
93 94 95 96 |
# File 'lib/shared/conf.rb', line 93 def self.reload! ensure_init load_files end |