18562: Rename config to AlwaysUsePreemptibleInstances.
[arvados.git] / services / api / 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 and
15 # database.yml to config.yml.  After adding the output of
16 # config:migrate to /etc/arvados/config.yml, you will be able to
17 # delete application.yml and database.yml.
18
19 require "cgi"
20 require 'config_loader'
21 require 'open3'
22
23 begin
24   # If secret_token.rb exists here, we need to load it first.
25   require_relative 'secret_token.rb'
26 rescue LoadError
27   # Normally secret_token.rb is missing and the secret token is
28   # configured by application.yml (i.e., here!) instead.
29 end
30
31 # Load the defaults, used by config:migrate and fallback loading
32 # legacy application.yml
33 defaultYAML, stderr, status = Open3.capture3("arvados-server", "config-dump", "-config=-", "-skip-legacy", stdin_data: "Clusters: {xxxxx: {}}")
34 if !status.success?
35   puts stderr
36   raise "error loading config: #{status}"
37 end
38 confs = YAML.load(defaultYAML, deserialize_symbols: false)
39 clusterID, clusterConfig = confs["Clusters"].first
40 $arvados_config_defaults = clusterConfig
41 $arvados_config_defaults["ClusterID"] = clusterID
42
43 if ENV["ARVADOS_CONFIG"] == "none"
44   # Don't load config. This magic value is set by packaging scripts so
45   # they can run "rake assets:precompile" without a real config.
46   $arvados_config_global = $arvados_config_defaults.deep_dup
47 else
48   # Load the global config file
49   Open3.popen2("arvados-server", "config-dump", "-skip-legacy") do |stdin, stdout, status_thread|
50     confs = YAML.load(stdout, deserialize_symbols: false)
51     if confs && !confs.empty?
52       # config-dump merges defaults with user configuration, so every
53       # key should be set.
54       clusterID, clusterConfig = confs["Clusters"].first
55       $arvados_config_global = clusterConfig
56       $arvados_config_global["ClusterID"] = clusterID
57     else
58       # config-dump failed, assume we will be loading from legacy
59       # application.yml, initialize with defaults.
60       $arvados_config_global = $arvados_config_defaults.deep_dup
61     end
62   end
63 end
64
65 # Now make a copy
66 $arvados_config = $arvados_config_global.deep_dup
67
68 def arrayToHash cfg, k, v
69   val = {}
70   v.each do |entry|
71     val[entry.to_s] = {}
72   end
73   ConfigLoader.set_cfg cfg, k, val
74 end
75
76 # Declare all our configuration items.
77 arvcfg = ConfigLoader.new
78 arvcfg.declare_config "ClusterID", NonemptyString, :uuid_prefix
79 arvcfg.declare_config "ManagementToken", String, :ManagementToken
80 arvcfg.declare_config "SystemRootToken", String
81 arvcfg.declare_config "Git.Repositories", String, :git_repositories_dir
82 arvcfg.declare_config "API.DisabledAPIs", Hash, :disable_api_methods, ->(cfg, k, v) { arrayToHash cfg, "API.DisabledAPIs", v }
83 arvcfg.declare_config "API.MaxRequestSize", Integer, :max_request_size
84 arvcfg.declare_config "API.MaxIndexDatabaseRead", Integer, :max_index_database_read
85 arvcfg.declare_config "API.MaxItemsPerResponse", Integer, :max_items_per_response
86 arvcfg.declare_config "API.MaxTokenLifetime", ActiveSupport::Duration
87 arvcfg.declare_config "API.RequestTimeout", ActiveSupport::Duration
88 arvcfg.declare_config "API.AsyncPermissionsUpdateInterval", ActiveSupport::Duration, :async_permissions_update_interval
89 arvcfg.declare_config "Users.AutoSetupNewUsers", Boolean, :auto_setup_new_users
90 arvcfg.declare_config "Users.AutoSetupNewUsersWithVmUUID", String, :auto_setup_new_users_with_vm_uuid
91 arvcfg.declare_config "Users.AutoSetupNewUsersWithRepository", Boolean, :auto_setup_new_users_with_repository
92 arvcfg.declare_config "Users.AutoSetupUsernameBlacklist", Hash, :auto_setup_name_blacklist, ->(cfg, k, v) { arrayToHash cfg, "Users.AutoSetupUsernameBlacklist", v }
93 arvcfg.declare_config "Users.NewUsersAreActive", Boolean, :new_users_are_active
94 arvcfg.declare_config "Users.AutoAdminUserWithEmail", String, :auto_admin_user
95 arvcfg.declare_config "Users.AutoAdminFirstUser", Boolean, :auto_admin_first_user
96 arvcfg.declare_config "Users.UserProfileNotificationAddress", String, :user_profile_notification_address
97 arvcfg.declare_config "Users.AdminNotifierEmailFrom", String, :admin_notifier_email_from
98 arvcfg.declare_config "Users.EmailSubjectPrefix", String, :email_subject_prefix
99 arvcfg.declare_config "Users.UserNotifierEmailFrom", String, :user_notifier_email_from
100 arvcfg.declare_config "Users.UserNotifierEmailBcc", Hash
101 arvcfg.declare_config "Users.NewUserNotificationRecipients", Hash, :new_user_notification_recipients, ->(cfg, k, v) { arrayToHash cfg, "Users.NewUserNotificationRecipients", v }
102 arvcfg.declare_config "Users.NewInactiveUserNotificationRecipients", Hash, :new_inactive_user_notification_recipients, method(:arrayToHash)
103 arvcfg.declare_config "Users.RoleGroupsVisibleToAll", Boolean
104 arvcfg.declare_config "Login.LoginCluster", String
105 arvcfg.declare_config "Login.TrustedClients", Hash
106 arvcfg.declare_config "Login.RemoteTokenRefresh", ActiveSupport::Duration
107 arvcfg.declare_config "Login.TokenLifetime", ActiveSupport::Duration
108 arvcfg.declare_config "TLS.Insecure", Boolean, :sso_insecure
109 arvcfg.declare_config "AuditLogs.MaxAge", ActiveSupport::Duration, :max_audit_log_age
110 arvcfg.declare_config "AuditLogs.MaxDeleteBatch", Integer, :max_audit_log_delete_batch
111 arvcfg.declare_config "AuditLogs.UnloggedAttributes", Hash, :unlogged_attributes, ->(cfg, k, v) { arrayToHash cfg, "AuditLogs.UnloggedAttributes", v }
112 arvcfg.declare_config "SystemLogs.MaxRequestLogParamsSize", Integer, :max_request_log_params_size
113 arvcfg.declare_config "Collections.DefaultReplication", Integer, :default_collection_replication
114 arvcfg.declare_config "Collections.DefaultTrashLifetime", ActiveSupport::Duration, :default_trash_lifetime
115 arvcfg.declare_config "Collections.CollectionVersioning", Boolean, :collection_versioning
116 arvcfg.declare_config "Collections.PreserveVersionIfIdle", ActiveSupport::Duration, :preserve_version_if_idle
117 arvcfg.declare_config "Collections.TrashSweepInterval", ActiveSupport::Duration, :trash_sweep_interval
118 arvcfg.declare_config "Collections.BlobSigningKey", String, :blob_signing_key
119 arvcfg.declare_config "Collections.BlobSigningTTL", ActiveSupport::Duration, :blob_signature_ttl
120 arvcfg.declare_config "Collections.BlobSigning", Boolean, :permit_create_collection_with_unsigned_manifest, ->(cfg, k, v) { ConfigLoader.set_cfg cfg, "Collections.BlobSigning", !v }
121 arvcfg.declare_config "Collections.ForwardSlashNameSubstitution", String
122 arvcfg.declare_config "Containers.SupportedDockerImageFormats", Hash, :docker_image_formats, ->(cfg, k, v) { arrayToHash cfg, "Containers.SupportedDockerImageFormats", v }
123 arvcfg.declare_config "Containers.LogReuseDecisions", Boolean, :log_reuse_decisions
124 arvcfg.declare_config "Containers.DefaultKeepCacheRAM", Integer, :container_default_keep_cache_ram
125 arvcfg.declare_config "Containers.MaxDispatchAttempts", Integer, :max_container_dispatch_attempts
126 arvcfg.declare_config "Containers.MaxRetryAttempts", Integer, :container_count_max
127 arvcfg.declare_config "Containers.AlwaysUsePreemptibleInstances", Boolean, :preemptible_instances
128 arvcfg.declare_config "Containers.MaxComputeVMs", Integer, :max_compute_nodes
129 arvcfg.declare_config "Containers.Logging.LogBytesPerEvent", Integer, :crunch_log_bytes_per_event
130 arvcfg.declare_config "Containers.Logging.LogSecondsBetweenEvents", ActiveSupport::Duration, :crunch_log_seconds_between_events
131 arvcfg.declare_config "Containers.Logging.LogThrottlePeriod", ActiveSupport::Duration, :crunch_log_throttle_period
132 arvcfg.declare_config "Containers.Logging.LogThrottleBytes", Integer, :crunch_log_throttle_bytes
133 arvcfg.declare_config "Containers.Logging.LogThrottleLines", Integer, :crunch_log_throttle_lines
134 arvcfg.declare_config "Containers.Logging.LimitLogBytesPerJob", Integer, :crunch_limit_log_bytes_per_job
135 arvcfg.declare_config "Containers.Logging.LogPartialLineThrottlePeriod", ActiveSupport::Duration, :crunch_log_partial_line_throttle_period
136 arvcfg.declare_config "Containers.Logging.LogUpdatePeriod", ActiveSupport::Duration, :crunch_log_update_period
137 arvcfg.declare_config "Containers.Logging.LogUpdateSize", Integer, :crunch_log_update_size
138 arvcfg.declare_config "Containers.Logging.MaxAge", ActiveSupport::Duration, :clean_container_log_rows_after
139 arvcfg.declare_config "Containers.SLURM.Managed.DNSServerConfDir", Pathname, :dns_server_conf_dir
140 arvcfg.declare_config "Containers.SLURM.Managed.DNSServerConfTemplate", Pathname, :dns_server_conf_template
141 arvcfg.declare_config "Containers.SLURM.Managed.DNSServerReloadCommand", String, :dns_server_reload_command
142 arvcfg.declare_config "Containers.SLURM.Managed.DNSServerUpdateCommand", String, :dns_server_update_command
143 arvcfg.declare_config "Containers.SLURM.Managed.ComputeNodeDomain", String, :compute_node_domain
144 arvcfg.declare_config "Containers.SLURM.Managed.ComputeNodeNameservers", Hash, :compute_node_nameservers, ->(cfg, k, v) { arrayToHash cfg, "Containers.SLURM.Managed.ComputeNodeNameservers", v }
145 arvcfg.declare_config "Containers.SLURM.Managed.AssignNodeHostname", String, :assign_node_hostname
146 arvcfg.declare_config "Containers.JobsAPI.Enable", String, :enable_legacy_jobs_api, ->(cfg, k, v) { ConfigLoader.set_cfg cfg, "Containers.JobsAPI.Enable", v.to_s }
147 arvcfg.declare_config "Containers.JobsAPI.GitInternalDir", String, :git_internal_dir
148 arvcfg.declare_config "Mail.MailchimpAPIKey", String, :mailchimp_api_key
149 arvcfg.declare_config "Mail.MailchimpListID", String, :mailchimp_list_id
150 arvcfg.declare_config "Services.Controller.ExternalURL", URI
151 arvcfg.declare_config "Services.Workbench1.ExternalURL", URI, :workbench_address
152 arvcfg.declare_config "Services.Websocket.ExternalURL", URI, :websocket_address
153 arvcfg.declare_config "Services.WebDAV.ExternalURL", URI, :keep_web_service_url
154 arvcfg.declare_config "Services.GitHTTP.ExternalURL", URI, :git_repo_https_base
155 arvcfg.declare_config "Services.GitSSH.ExternalURL", URI, :git_repo_ssh_base, ->(cfg, k, v) { ConfigLoader.set_cfg cfg, "Services.GitSSH.ExternalURL", "ssh://#{v}" }
156 arvcfg.declare_config "RemoteClusters", Hash, :remote_hosts, ->(cfg, k, v) {
157   h = if cfg["RemoteClusters"] then
158         cfg["RemoteClusters"].deep_dup
159       else
160         {}
161       end
162   v.each do |clusterid, host|
163     if h[clusterid].nil?
164       h[clusterid] = {
165         "Host" => host,
166         "Proxy" => true,
167         "Scheme" => "https",
168         "Insecure" => false,
169         "ActivateUsers" => false
170       }
171     end
172   end
173   ConfigLoader.set_cfg cfg, "RemoteClusters", h
174 }
175 arvcfg.declare_config "RemoteClusters.*.Proxy", Boolean, :remote_hosts_via_dns
176 arvcfg.declare_config "StorageClasses", Hash
177
178 dbcfg = ConfigLoader.new
179
180 dbcfg.declare_config "PostgreSQL.ConnectionPool", Integer, :pool
181 dbcfg.declare_config "PostgreSQL.Connection.host", String, :host
182 dbcfg.declare_config "PostgreSQL.Connection.port", String, :port
183 dbcfg.declare_config "PostgreSQL.Connection.user", String, :username
184 dbcfg.declare_config "PostgreSQL.Connection.password", String, :password
185 dbcfg.declare_config "PostgreSQL.Connection.dbname", String, :database
186 dbcfg.declare_config "PostgreSQL.Connection.template", String, :template
187 dbcfg.declare_config "PostgreSQL.Connection.encoding", String, :encoding
188 dbcfg.declare_config "PostgreSQL.Connection.collation", String, :collation
189
190 application_config = {}
191 %w(application.default application).each do |cfgfile|
192   path = "#{::Rails.root.to_s}/config/#{cfgfile}.yml"
193   confs = ConfigLoader.load(path, erb: true)
194   # Ignore empty YAML file:
195   next if confs == false
196   application_config.deep_merge!(confs['common'] || {})
197   application_config.deep_merge!(confs[::Rails.env.to_s] || {})
198 end
199
200 db_config = {}
201 path = "#{::Rails.root.to_s}/config/database.yml"
202 if !ENV['ARVADOS_CONFIG_NOLEGACY'] && File.exist?(path)
203   db_config = ConfigLoader.load(path, erb: true)
204 end
205
206 $remaining_config = arvcfg.migrate_config(application_config, $arvados_config)
207 dbcfg.migrate_config(db_config[::Rails.env.to_s] || {}, $arvados_config)
208
209 if application_config[:auto_activate_users_from]
210   application_config[:auto_activate_users_from].each do |cluster|
211     if $arvados_config.RemoteClusters[cluster]
212       $arvados_config.RemoteClusters[cluster]["ActivateUsers"] = true
213     end
214   end
215 end
216
217 if application_config[:host] || application_config[:port] || application_config[:scheme]
218   if !application_config[:host] || application_config[:host].empty?
219     raise "Must set 'host' when setting 'port' or 'scheme'"
220   end
221   $arvados_config.Services["Controller"]["ExternalURL"] = URI((application_config[:scheme] || "https")+"://"+application_config[:host]+
222                                                               (if application_config[:port] then ":#{application_config[:port]}" else "" end))
223 end
224
225 # Checks for wrongly typed configuration items, coerces properties
226 # into correct types (such as Duration), and optionally raise error
227 # for essential configuration that can't be empty.
228 arvcfg.coercion_and_check $arvados_config_defaults, check_nonempty: false
229 arvcfg.coercion_and_check $arvados_config_global, check_nonempty: false
230 arvcfg.coercion_and_check $arvados_config, check_nonempty: true
231 dbcfg.coercion_and_check $arvados_config, check_nonempty: true
232
233 # * $arvados_config_defaults is the defaults
234 # * $arvados_config_global is $arvados_config_defaults merged with the contents of /etc/arvados/config.yml
235 # These are used by the rake config: tasks
236 #
237 # * $arvados_config is $arvados_config_global merged with the migrated contents of application.yml
238 # This is what actually gets copied into the Rails configuration object.
239
240 if $arvados_config["Collections"]["DefaultTrashLifetime"] < 86400.seconds then
241   raise "default_trash_lifetime is %d, must be at least 86400" % Rails.configuration.Collections.DefaultTrashLifetime
242 end
243
244 default_storage_classes = []
245 $arvados_config["StorageClasses"].each do |cls, cfg|
246   if cfg["Default"]
247     default_storage_classes << cls
248   end
249 end
250 if default_storage_classes.length == 0
251   default_storage_classes = ["default"]
252 end
253 $arvados_config["DefaultStorageClasses"] = default_storage_classes.sort
254
255 #
256 # Special case for test database where there's no database.yml,
257 # because the Arvados config.yml doesn't have a concept of multiple
258 # rails environments.
259 #
260 if ::Rails.env.to_s == "test" && db_config["test"].nil?
261   $arvados_config["PostgreSQL"]["Connection"]["dbname"] = "arvados_test"
262 end
263 if ::Rails.env.to_s == "test"
264   # Use template0 when creating a new database. Avoids
265   # character-encoding/collation problems.
266   $arvados_config["PostgreSQL"]["Connection"]["template"] = "template0"
267   # Some test cases depend on en_US.UTF-8 collation.
268   $arvados_config["PostgreSQL"]["Connection"]["collation"] = "en_US.UTF-8"
269 end
270
271 if ENV["ARVADOS_CONFIG"] == "none"
272   # We need the postgresql connection URI to be valid, even if we
273   # don't use it.
274   $arvados_config["PostgreSQL"]["Connection"]["host"] = "localhost"
275   $arvados_config["PostgreSQL"]["Connection"]["user"] = "x"
276   $arvados_config["PostgreSQL"]["Connection"]["password"] = "x"
277   $arvados_config["PostgreSQL"]["Connection"]["dbname"] = "x"
278 end
279
280 if $arvados_config["PostgreSQL"]["Connection"]["password"].empty?
281   raise "Database password is empty, PostgreSQL section is: #{$arvados_config["PostgreSQL"]}"
282 end
283
284 dbhost = $arvados_config["PostgreSQL"]["Connection"]["host"]
285 if $arvados_config["PostgreSQL"]["Connection"]["port"] != 0
286   dbhost += ":#{$arvados_config["PostgreSQL"]["Connection"]["port"]}"
287 end
288
289 #
290 # If DATABASE_URL is set, then ActiveRecord won't error out if database.yml doesn't exist.
291 #
292 # For config migration, we've previously populated the PostgreSQL
293 # section of the config from database.yml
294 #
295 database_url = "postgresql://#{CGI.escape $arvados_config["PostgreSQL"]["Connection"]["user"]}:"+
296                       "#{CGI.escape $arvados_config["PostgreSQL"]["Connection"]["password"]}@"+
297                       "#{dbhost}/#{CGI.escape $arvados_config["PostgreSQL"]["Connection"]["dbname"]}?"+
298                       "template=#{$arvados_config["PostgreSQL"]["Connection"]["template"]}&"+
299                       "encoding=#{$arvados_config["PostgreSQL"]["Connection"]["client_encoding"]}&"+
300                       "collation=#{$arvados_config["PostgreSQL"]["Connection"]["collation"]}&"+
301                       "pool=#{$arvados_config["PostgreSQL"]["ConnectionPool"]}"
302
303 ENV["DATABASE_URL"] = database_url
304
305 Server::Application.configure do
306   # Copy into the Rails config object.  This also turns Hash into
307   # OrderedOptions so that application code can use
308   # Rails.configuration.API.Blah instead of
309   # Rails.configuration.API["Blah"]
310   ConfigLoader.copy_into_config $arvados_config, config
311   ConfigLoader.copy_into_config $remaining_config, config
312
313   # We don't rely on cookies for authentication, so instead of
314   # requiring a signing key in config, we assign a new random one at
315   # startup.
316   secrets.secret_key_base = rand(1<<255).to_s(36)
317 end