Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

set smtp opts via settings allowing runtime override

Markus Kahl3436b3694d1b
diff --git a/app/models/setting.rb b/app/models/setting.rb
index 7bc8a447721..14dd8b97184 100644
--- a/app/models/setting.rb
+++ b/app/models/setting.rb
@@ -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
diff --git a/lib/tasks/setting.rake b/lib/tasks/setting.rake
index 745ddd2727d..15527f8e90b 100644
--- a/lib/tasks/setting.rake
+++ b/lib/tasks/setting.rake
@@ -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
diff --git a/packaging/conf/configuration.yml b/packaging/conf/configuration.yml
index 0f114f0220f..b5e0dd2a155 100644
--- a/packaging/conf/configuration.yml
+++ b/packaging/conf/configuration.yml
@@ -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? %>
diff --git a/packaging/scripts/postinstall b/packaging/scripts/postinstall
new file mode 100644
index 00000000000..5ee9a21e40d
--- /dev/null
+++ b/packaging/scripts/postinstall
@@ -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