Merge branch 'master' into 6156-hostnames-in-nodes
[arvados.git] / services / api / config / initializers / load_config.rb
1 begin
2   # If secret_token.rb exists here, we need to load it first.
3   require_relative 'secret_token.rb'
4 rescue LoadError
5   # Normally secret_token.rb is missing and the secret token is
6   # configured by application.yml (i.e., here!) instead.
7 end
8
9 $application_config = {}
10
11 %w(application.default application).each do |cfgfile|
12   path = "#{::Rails.root.to_s}/config/#{cfgfile}.yml"
13   if File.exists? path
14     yaml = ERB.new(IO.read path).result(binding)
15     confs = YAML.load(yaml)
16     # Ignore empty YAML file:
17     next if confs == false
18     $application_config.merge!(confs['common'] || {})
19     $application_config.merge!(confs[::Rails.env.to_s] || {})
20   end
21 end
22
23 Server::Application.configure do
24   nils = []
25   $application_config.each do |k, v|
26     # "foo.bar: baz" --> { config.foo.bar = baz }
27     cfg = config
28     ks = k.split '.'
29     k = ks.pop
30     ks.each do |kk|
31       cfg = cfg.send(kk)
32     end
33     if cfg.respond_to?(k.to_sym) and !cfg.send(k).nil?
34       # Config must have been set already in environments/*.rb.
35       #
36       # After config files have been migrated, this mechanism should
37       # be deprecated, then removed.
38     elsif v.nil?
39       # Config variables are not allowed to be nil. Make a "naughty"
40       # list, and present it below.
41       nils << k
42     else
43       cfg.send "#{k}=", v
44     end
45   end
46   if !nils.empty?
47     raise <<EOS
48 Refusing to start in #{::Rails.env.to_s} mode with missing configuration.
49
50 The following configuration settings must be specified in
51 config/application.yml:
52 * #{nils.join "\n* "}
53
54 EOS
55   end
56 end