Merge branch '8784-dir-listings'
[arvados.git] / services / api / config / initializers / load_config.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 begin
6   # If secret_token.rb exists here, we need to load it first.
7   require_relative 'secret_token.rb'
8 rescue LoadError
9   # Normally secret_token.rb is missing and the secret token is
10   # configured by application.yml (i.e., here!) instead.
11 end
12
13 if (File.exist?(File.expand_path '../omniauth.rb', __FILE__) and
14     not defined? WARNED_OMNIAUTH_CONFIG)
15   Rails.logger.warn <<-EOS
16 DEPRECATED CONFIGURATION:
17  Please move your SSO provider config into config/application.yml
18  and delete config/initializers/omniauth.rb.
19 EOS
20   # Real values will be copied from globals by omniauth_init.rb. For
21   # now, assign some strings so the generic *.yml config loader
22   # doesn't overwrite them or complain that they're missing.
23   Rails.configuration.sso_app_id = 'xxx'
24   Rails.configuration.sso_app_secret = 'xxx'
25   Rails.configuration.sso_provider_url = '//xxx'
26   WARNED_OMNIAUTH_CONFIG = true
27 end
28
29 $application_config = {}
30
31 %w(application.default application).each do |cfgfile|
32   path = "#{::Rails.root.to_s}/config/#{cfgfile}.yml"
33   if File.exist? path
34     yaml = ERB.new(IO.read path).result(binding)
35     confs = YAML.load(yaml, deserialize_symbols: true)
36     # Ignore empty YAML file:
37     next if confs == false
38     $application_config.merge!(confs['common'] || {})
39     $application_config.merge!(confs[::Rails.env.to_s] || {})
40   end
41 end
42
43 Server::Application.configure do
44   nils = []
45   $application_config.each do |k, v|
46     # "foo.bar: baz" --> { config.foo.bar = baz }
47     cfg = config
48     ks = k.split '.'
49     k = ks.pop
50     ks.each do |kk|
51       cfg = cfg.send(kk)
52     end
53     if cfg.respond_to?(k.to_sym) and !cfg.send(k).nil?
54       # Config must have been set already in environments/*.rb.
55       #
56       # After config files have been migrated, this mechanism should
57       # be deprecated, then removed.
58     elsif v.nil?
59       # Config variables are not allowed to be nil. Make a "naughty"
60       # list, and present it below.
61       nils << k
62     else
63       cfg.send "#{k}=", v
64     end
65   end
66   if !nils.empty?
67     raise <<EOS
68 Refusing to start in #{::Rails.env.to_s} mode with missing configuration.
69
70 The following configuration settings must be specified in
71 config/application.yml:
72 * #{nils.join "\n* "}
73
74 EOS
75   end
76   config.secret_key_base = config.secret_token
77 end