Class: MasterConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/master_config.rb,
lib/master_config/version.rb

Constant Summary collapse

VERSION =
"0.3.3"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, paths, app_name) {|@app_config, @default_config| ... } ⇒ MasterConfig

Returns a new instance of MasterConfig.

Yields:

  • (@app_config, @default_config)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
64
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/master_config.rb', line 7

def initialize(config,paths,app_name,&block)
  @config_path = nil

  # -- Load in the config values from the block -- #

  @app_config = Hashie::Mash.new
  @default_config = Hashie::Mash.new
  yield @app_config, @default_config

  @app_config.root ||= Rails::VERSION::MAJOR == 2 ? RAILS_ROOT : Rails.root

  @jconfig = MasterConfig.read_json_config(:root => @app_config.root)

  @jconfig = @default_config.merge!(@jconfig)

  @config_path = @jconfig._config_path

  # -- Update environment_path to use base_env -- #

  if Rails::VERSION::MAJOR == 2
    config.instance_variable_set( :@_base_env, @jconfig[:base_env] )
    class << config
      def environment_path
        "#{root_path}/config/environments/#{@_base_env}.rb"
      end
    end
  else
    paths["config/environments"] = ["config/environments/#{@jconfig[:base_env]}.rb"]
  end

  # -- load the test gems if we're testing -- #

  if @jconfig[:env] == 'test'
    Bundler.require(:test)
  end

  # -- Load in session secret -- #

  self._set_and_define config, :secret_token


  # -- Load in application settings -- #
  (@jconfig[app_name.to_sym]||{}).each do |k,v|
    @app_config.send("#{k}=",v)
  end

  # Load in ActionMailer settings
  config.action_mailer.default_url_options = {
    :host     => @jconfig[:host],
    :protocol => @jconfig[:ssl] ? "https" : "http"
  }

  # Load in database settings
  if @jconfig.database?
    self._define_method config, "database_configuration", { @jconfig.env => @jconfig.database }
  else
    self._define_method config, "database_configuration", { @jconfig.env => nil }
  end

  # -- Configure New Relic -- #

  self._set_and_define config, :new_relic do |val|
    unless val.present?
      val = Hashie::Mash.new
      val.agent_enabled = false
      val.monitor_mode  = false
    end
    val
  end

  # -- Configure Resque Pool -- #

  self._set_and_define config, :resque_pool

  # -- Configure Data Path -- #

  self._set_and_define config, :data_path

  # -- Configure Redis Host -- #

  self._set_and_define config, :redis_host

  # -- Configure S3 Bucket -- #

  self._set_and_define config, :s3_bucket

  # Change paths for caching
  config.cache_store = [ :file_store, File.join(@app_config.root,"tmp","cache") ]

  if Rails::VERSION::MAJOR == 2
    # -- Set our logging path -- #

    config.log_path = File.join(@app_config.root,"log","#{@jconfig[:env]}.log")

  else
    config.assets.cache_store = [ :file_store, File.join(@app_config.root,"tmp","cache","assets") ]

    if paths
      # set our log path
      paths["log"] = File.join(@app_config.root,"log","#{Rails.env}.log")
      paths["tmp"] = File.join(@app_config.root,"tmp")
    end

  end

  # -- Configure ActionMailer -- #

  config.action_mailer.default_url_options = {
    :host     => @jconfig[:host],
    :protocol => @jconfig[:ssl] ? "https" : "http"
  }

  # -- Return! -- #

  self._define_method config, app_name, @app_config
  self._define_method config, "master_config", self

end

Instance Attribute Details

#config_pathObject

Returns the value of attribute config_path.



5
6
7
# File 'lib/master_config.rb', line 5

def config_path
  @config_path
end

Class Method Details

.read_json_config(opts = nil) ⇒ Object




170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/master_config.rb', line 170

def self.read_json_config(opts=nil)
  # -- Try to find a config file -- #

  config_json_path = nil

  env = nil
  root = nil
  has_rails = false
  has_rack = false

  if defined?(Rails)
    has_rails = true

    if Rails::VERSION::MAJOR == 2
      env = RAILS_ENV
    else
      env = Rails.env
    end
    puts "Set env from RAILS #{  env }"

    root = Rails.root

  elsif defined?(Rack)
    has_rack = true
    env = ENV["RACK_ENV"] || "development"
    puts "Set env from RACK #{  env }"
  else
    puts "No Rails or Rack"
  end

  if opts && opts[:root]
    root = opts[:root]
  end

  if ENV["MC_ENV_JSON"]
    config_json_path = ENV["MC_ENV_JSON"]
  elsif env =~ /\//
    config_json_path = File.expand_path(env)
  elsif env && root
    config_json_path = File.expand_path("#{root}/config/#{env}.json")
  else
    config_json_path = File.expand_path("./config/#{env}.json")
  end

  if File.exist? config_json_path
    jconfig_raw = nil
    File.open(config_json_path) { |f| jconfig_raw = f.read() }
    config_json = Hashie::Mash.new(JSON.parse jconfig_raw)

    if config_json
      ENV["MC_ENV_JSON"] = config_json_path

      if has_rails
        ENV["RAILS_ENV"] = config_json.env

        if Rails::VERSION::MAJOR == 2

          # -- set our environment key -- #

          if defined? RAILS_ENV
            RAILS_ENV.replace ENV["RAILS_ENV"]
          end
        else
          Rails.env = ENV["RAILS_ENV"]
        end
      elsif has_rack
        ENV["RACK_ENV"] = config_json.env
      end

      # Stash the config file path in our results
      config_json._config_path = config_json_path

      # -- Return config -- #

      return config_json
    else
      raise "Unable to find JSON config file."
    end
  end
end

Instance Method Details

#env_defaults {|env_config| ... } ⇒ Object


Yields:

  • (env_config)


154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/master_config.rb', line 154

def env_defaults(&block)
  env_config = ActiveSupport::OrderedOptions.new

  yield env_config

  env_config.each do |k,v|
    if !@app_config.send(k)
      @app_config.send("#{k}=",v)
    end
  end

  true
end

#init_new_relicObject




128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/master_config.rb', line 128

def init_new_relic
  if @new_relic && @new_relic.agent_enabled
    Rails.logger.debug "Loading in JSON New Relic config"

    # load in the defaults
    defaults = Hashie::Mash.new(
      YAML.load( File.read(File.expand_path("config/newrelic.yml",@app_config.root)) )
    ).common

    @new_relic_config = defaults.merge(@new_relic)
    Rails.logger.info "New Relic config is #{@new_relic_config}"

    # We create a new source class here so that it won't get blown away if
    # NewRelic tries to create a different ManualSource (in its resque code,
    # for instance)
    mcpsource = Class.new(NewRelic::Agent::Configuration::ManualSource)
    NewRelic::Agent.config.replace_or_add_config(mcpsource.new(@new_relic_config), 1)
    NewRelic::Agent.manual_start()
  else
    # just let it do its thing and not be enabled
    NewRelic::Control.instance.init_plugin :config => Rails.configuration
  end
end