14812: Config migration WIP
[arvados.git] / apps / workbench / config / arvados_config.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 #
6 # Load Arvados configuration from /etc/arvados/config.yml, using defaults
7 # from config.default.yml
8 #
9 # Existing application.yml is migrated into the new config structure.
10 # Keys in the legacy application.yml take precedence.
11 #
12 # Use "bundle exec config:dump" to get the complete active configuration
13 #
14 # Use "bundle exec config:migrate" to migrate application.yml to
15 # config.yml.  After adding the output of config:migrate to
16 # /etc/arvados/config.yml, you will be able to delete application.yml.
17
18 require 'config_loader'
19
20 begin
21   # If secret_token.rb exists here, we need to load it first.
22   require_relative 'secret_token.rb'
23 rescue LoadError
24   # Normally secret_token.rb is missing and the secret token is
25   # configured by application.yml (i.e., here!) instead.
26 end
27
28 # Load the defaults
29 $arvados_config_defaults = ConfigLoader.load "#{::Rails.root.to_s}/config/config.default.yml"
30 if $arvados_config_defaults.empty?
31   raise "Missing #{::Rails.root.to_s}/config/config.default.yml"
32 end
33
34 def remove_sample_entries(h)
35   return unless h.is_a? Hash
36   h.delete("SAMPLE")
37   h.each { |k, v| remove_sample_entries(v) }
38 end
39 remove_sample_entries($arvados_config_defaults)
40
41 clusterID, clusterConfig = $arvados_config_defaults["Clusters"].first
42 $arvados_config_defaults = clusterConfig
43 $arvados_config_defaults["ClusterID"] = clusterID
44
45 # Initialize the global config with the defaults
46 $arvados_config_global = $arvados_config_defaults.deep_dup
47
48 # Load the global config file
49 confs = ConfigLoader.load "/etc/arvados/config.yml"
50 if !confs.empty?
51   clusterID, clusterConfig = confs["Clusters"].first
52   $arvados_config_global["ClusterID"] = clusterID
53
54   # Copy the cluster config over the defaults
55   $arvados_config_global.deep_merge!(clusterConfig)
56 end
57
58 # Now make a copy
59 $arvados_config = $arvados_config_global.deep_dup
60
61 # Declare all our configuration items.
62 arvcfg = ConfigLoader.new
63
64 arvcfg.declare_config "ManagementToken", String, :ManagementToken
65 arvcfg.declare_config "TLS.Insecure", Boolean, :arvados_insecure_https
66
67 arvcfg.declare_config "Services.Controller.ExternalURL", URI, :arvados_v1_base, ->(cfg, k, v) {
68   u = URI(v)
69   u.path = ""
70   ConfigLoader.set_cfg cfg, "Services.Controller.ExternalURL", u
71 }
72
73 arvcfg.declare_config "Services.WebShell.ExternalURL", URI, :shell_in_a_box_url, ->(cfg, k, v) {
74   v ||= ""
75   u = URI(v.sub("%{hostname}", "*"))
76   u.path = ""
77   ConfigLoader.set_cfg cfg, "Services.WebShell.ExternalURL", u
78 }
79
80 arvcfg.declare_config "Services.WebDAV.ExternalURL", URI, :keep_web_service_url, ->(cfg, k, v) {
81   v ||= ""
82   u = URI(v.sub("%{uuid_or_pdh}", "*"))
83   u.path = ""
84   ConfigLoader.set_cfg cfg, "Services.WebDAV.ExternalURL", u
85 }
86
87 arvcfg.declare_config "Services.WebDAVDownload.ExternalURL", URI, :keep_web_download_url, ->(cfg, k, v) {
88   v ||= ""
89   u = URI(v.sub("%{uuid_or_pdh}", "*"))
90   u.path = ""
91   ConfigLoader.set_cfg cfg, "Services.WebDAVDownload.ExternalURL", u
92 }
93
94 arvcfg.declare_config "Services.Composer.ExternalURL", URI, :composer_url
95 arvcfg.declare_config "Services.Workbench2.ExternalURL", URI, :workbench2_url
96
97 arvcfg.declare_config "Workbench.ApplicationMimetypesWithViewIcon", Hash, :application_mimetypes_with_view_icon, ->(cfg, k, v) {
98   mimetypes = {}
99   v.each do |m|
100     mimetypes[m] = {}
101   end
102   ConfigLoader.set_cfg cfg, "Workbench.ApplicationMimetypesWithViewIcon", mimetypes
103 }
104
105 arvcfg.declare_config "Users.AnonymousUserToken", String, :anonymous_user_token
106
107
108 application_config = {}
109 %w(application.default application).each do |cfgfile|
110   path = "#{::Rails.root.to_s}/config/#{cfgfile}.yml"
111   confs = ConfigLoader.load(path, erb: true)
112   # Ignore empty YAML file:
113   next if confs == false
114   application_config.deep_merge!(confs['common'] || {})
115   application_config.deep_merge!(confs[::Rails.env.to_s] || {})
116 end
117
118 $remaining_config = arvcfg.migrate_config(application_config, $arvados_config)
119
120 # Checks for wrongly typed configuration items, coerces properties
121 # into correct types (such as Duration), and optionally raise error
122 # for essential configuration that can't be empty.
123 arvcfg.coercion_and_check $arvados_config_defaults, check_nonempty: false
124 arvcfg.coercion_and_check $arvados_config_global, check_nonempty: false
125 arvcfg.coercion_and_check $arvados_config, check_nonempty: true
126
127 # * $arvados_config_defaults is the defaults
128 # * $arvados_config_global is $arvados_config_defaults merged with the contents of /etc/arvados/config.yml
129 # These are used by the rake config: tasks
130 #
131 # * $arvados_config is $arvados_config_global merged with the migrated contents of application.yml
132 # This is what actually gets copied into the Rails configuration object.
133
134 ArvadosWorkbench::Application.configure do
135   # Copy into the Rails config object.  This also turns Hash into
136   # OrderedOptions so that application code can use
137   # Rails.configuration.API.Blah instead of
138   # Rails.configuration.API["Blah"]
139   ConfigLoader.copy_into_config $arvados_config, config
140   ConfigLoader.copy_into_config $remaining_config, config
141   secrets.secret_key_base = $arvados_config["Workbench"]["SecretToken"]
142 end