Class: Shared::Conf

Inherits:
Object
  • Object
show all
Extended by:
Chamber
Defined in:
lib/shared/conf.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.app_envObject

Returns the value of attribute app_env.



15
16
17
# File 'lib/shared/conf.rb', line 15

def app_env
  @app_env
end

.app_libsObject (readonly)

Returns the value of attribute app_libs.



17
18
19
# File 'lib/shared/conf.rb', line 17

def app_libs
  @app_libs
end

.app_nameObject (readonly)

Returns the value of attribute app_name.



18
19
20
# File 'lib/shared/conf.rb', line 18

def app_name
  @app_name
end

.app_rootObject (readonly)

Returns the value of attribute app_root.



16
17
18
# File 'lib/shared/conf.rb', line 16

def app_root
  @app_root
end

.app_specObject (readonly)

Returns the value of attribute app_spec.



21
22
23
# File 'lib/shared/conf.rb', line 21

def app_spec
  @app_spec
end

.app_startedObject (readonly)

Returns the value of attribute app_started.



20
21
22
# File 'lib/shared/conf.rb', line 20

def app_started
  @app_started
end

.app_verObject (readonly)

Returns the value of attribute app_ver.



19
20
21
# File 'lib/shared/conf.rb', line 19

def app_ver
  @app_ver
end

.filesObject (readonly)

Returns the value of attribute files.



22
23
24
# File 'lib/shared/conf.rb', line 22

def files
  @files
end

.hostObject (readonly)

Returns the value of attribute host.



23
24
25
# File 'lib/shared/conf.rb', line 23

def host
  @host
end

Class Method Details

.at(*path) ⇒ Object

Direct access to any depth



100
101
102
103
# File 'lib/shared/conf.rb', line 100

def self.at *path
  ensure_init
  path.reduce(Conf) { |m, key| m && m[key.to_s] }
end

.dumpObject



94
95
96
97
# File 'lib/shared/conf.rb', line 94

def self.dump
  ensure_init
  to_hash.to_yaml(indent: 4, useheader: true, useversion: false )
end

.gen_config_etcObject



119
120
121
122
# File 'lib/shared/conf.rb', line 119

def self.gen_config_etc
  ensure_init
  "/etc/#{@app_name}.yml"
end

.gen_config_messageObject



127
128
129
130
131
132
133
134
135
# File 'lib/shared/conf.rb', line 127

def self.gen_config_message
  config_etc = gen_config_etc
  config_sample = gen_config_sample
  return "
A default configuration is available here: #{config_sample}.
You should copy it to the default location: #{config_etc}.
sudo cp #{config_sample} #{config_etc}
"
end

.gen_config_sampleObject



123
124
125
126
# File 'lib/shared/conf.rb', line 123

def self.gen_config_sample
  ensure_init
  "#{@app_root}/#{@app_name}.sample.yml"
end

.gen_pidfileObject

Defaults generators



111
112
113
114
# File 'lib/shared/conf.rb', line 111

def self.gen_pidfile
  ensure_init
  "/tmp/#{@app_name}-#{@host}-#{Process.pid}.pid"
end

.gen_process_nameObject



115
116
117
118
# File 'lib/shared/conf.rb', line 115

def self.gen_process_name
  ensure_init
  "#{@app_name}/#{@app_env}/#{Process.pid}"
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
# 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.expand_path(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.expand_path( @app_root + "/lib/#{@app_name}/" )

  # Add other config files
  add_default_config
  add_etc_config

  # Return something
  return @app_name
end

.newrelic_enabled?Boolean

Returns:

  • (Boolean)


105
106
107
108
# File 'lib/shared/conf.rb', line 105

def self.newrelic_enabled?
  ensure_init
  !!self[:newrelic]
end

.prepare(args = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/shared/conf.rb', line 64

def self.prepare args = {}
  ensure_init

  # Add extra config file
  add_extra_config args[:config]

  # Load configuration files
  load_files

  # Set Rack env
  ENV["RACK_ENV"] = @app_env.to_s

  # Init New Relic
  prepare_newrelic self[:newrelic], self.at(:logs, :newrelic)

  # Try to access any key to force parsing of the files
  self[:dummy]

rescue Psych::SyntaxError => e
  fail ConfigParseError, e.message
rescue StandardError => e
  fail ConfigOtherError, "#{e.message} \n #{e.backtrace.to_yaml}"
end

.reload!Object

Reload files



89
90
91
92
# File 'lib/shared/conf.rb', line 89

def self.reload!
  ensure_init
  load_files
end