set smtp opts via settings allowing runtime override

otherwise it's impossible to update
it at runtime/inapp since the merge
of configuration and settings in 12.1
pull/10667/head
Markus Kahl 3 years ago committed by Oliver Günther
parent 45ea07d588
commit 3436b3694d
  1. 6
      app/models/setting.rb
  2. 13
      lib/tasks/setting.rake
  3. 9
      packaging/conf/configuration.yml
  4. 37
      packaging/scripts/postinstall

@ -157,7 +157,11 @@ class Setting < ApplicationRecord
end
def value=(val)
unless definition.writable?
set_value! val
end
def set_value!(val, force: false)
unless force || definition.writable?
raise NoMethodError, "#{name} is not writable but can be set through env vars or configuration.yml file."
end

@ -47,4 +47,17 @@ namespace :setting do
puts(setting.value)
end
end
desc 'Allow to set a Setting read from an ENV var. Example: rake setting:set_to_env[smtp_address=SMTP_HOST]'
task set_to_env: :environment do |_t, args|
args.extras.each do |tuple|
setting_name, env_var_name = tuple.split('=')
next unless Settings::Definition.exists? setting_name
next unless ENV.has_key? env_var_name
setting = Setting.find_by name: setting_name
setting.set_value! ENV[env_var_name].presence, force: true
end
end
end

@ -37,15 +37,6 @@
default:
rails_cache_store: <%= ENV.fetch('RAILS_CACHE_STORE') { :memcache }.to_sym %>
session_store: <%= ENV.fetch('SESSION_STORE') { :active_record_store }.to_sym %>
email_delivery_method: <%= ENV.fetch('EMAIL_DELIVERY_METHOD') { :sendmail } %>
smtp_address: <%= ENV['SMTP_HOST'] %>
smtp_port: <%= ENV.fetch('SMTP_PORT') { 25 }.to_i %>
smtp_domain: <%= ENV.fetch('SMTP_DOMAIN') { ENV['HOSTNAME'] } %>
smtp_authentication: <%= ENV.fetch('SMTP_AUTHENTICATION') { :login }.to_sym %>
smtp_user_name: <%= ENV['SMTP_USERNAME'] %>
smtp_password: <%= ENV['SMTP_PASSWORD'] %>
smtp_enable_starttls_auto: <%= ENV.fetch('SMTP_ENABLE_STARTTLS_AUTO') { "false" } %>
smtp_ssl: <%= ENV.fetch('SMTP_SSL') { "false" } %>
attachments_storage_path: <%= ENV.fetch('ATTACHMENTS_STORAGE_PATH') { "/var/db/_APP_NAME_/files" } %>
<% git_configured = ENV['GIT_REPOSITORIES'].present? %>
<% svn_configured = ENV['SVN_REPOSITORIES'].present? %>

@ -0,0 +1,37 @@
#!/bin/bash -e
# Apply SMTP options given in the packager wizard to the settings within
# the database. This will override existing settings.
# The user can just choose `skip` during `reconfigure` if they do not want that.
if [ "$EMAIL_DELIVERY_METHOD" = "smtp" ]; then
${CLI} run rake "setting:set_to_env[\
email_delivery_method=EMAIL_DELIVERY_METHOD,\
smtp_authentication=SMTP_AUTHENTICATION,\
smtp_address=SMTP_HOST,\
smtp_port=SMTP_PORT,\
smtp_user_name=SMTP_USERNAME,\
smtp_password=SMTP_PASSWORD,\
smtp_domain=SMTP_DOMAIN\
]"
elif [ "$EMAIL_DELIVERY_METHOD" = "sendmail" ]; then
${CLI} run rake "setting:set[email_delivery_method=sendmail]"
fi
# We have transferred all the SMTP options to their settings within the database.
# Next we remove them from the env. This way they don't pin the settings to the
# env values allowing users to still change the SMTP options at runtime.
# This only removes env vars generated by packager.
# If an admin still wants to pin a certain setting to a given value they can still do
# so by using the prefixed setting names. For instance:
#
# openproject config:set OPENPROJECT_SMTP__PORT=42
${CLI} config:unset EMAIL_DELIVERY_METHOD
${CLI} config:unset SMTP_AUTHENTICATION
${CLI} config:unset SMTP_HOST
${CLI} config:unset SMTP_PORT
${CLI} config:unset SMTP_USERNAME
${CLI} config:unset SMTP_PASSWORD
${CLI} config:unset SMTP_DOMAIN
Loading…
Cancel
Save