1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Config.TransferTask do
14 @type env() :: :test | :benchmark | :dev | :prod
16 defp reboot_time_keys,
19 {:pleroma, :rate_limit},
24 defp reboot_time_subkeys,
26 {:pleroma, Pleroma.Captcha, [:seconds_valid]},
27 {:pleroma, Pleroma.Upload, [:proxy_remote]},
28 {:pleroma, :instance, [:upload_limit]}
31 def start_link(restart_pleroma? \\ true) do
32 load_and_update_env([], restart_pleroma?)
33 if Config.get(:env) == :test, do: Ecto.Adapters.SQL.Sandbox.checkin(Repo)
37 @spec load_and_update_env([ConfigDB.t()], boolean()) :: :ok
38 def load_and_update_env(deleted_settings \\ [], restart_pleroma? \\ true) do
39 with {_, true} <- {:configurable, Config.get(:configurable_from_database)} do
40 # We need to restart applications for loaded settings take effect
42 (Repo.all(ConfigDB) ++ deleted_settings)
43 |> Enum.map(&merge_with_default/1)
44 |> Enum.split_with(fn {group, _, _, _} -> group in [:logger, :quack] end)
48 |> Enum.each(&configure/1)
50 started_applications = Application.started_applications()
52 reject = [nil, :postgrex]
55 if restart_pleroma? do
62 |> Enum.map(&update/1)
64 |> Enum.reject(&(&1 in reject))
65 |> maybe_set_pleroma_last()
66 |> Enum.each(&restart(started_applications, &1, Config.get(:env)))
70 {:configurable, false} -> Restarter.Pleroma.rebooted()
74 defp maybe_set_pleroma_last(apps) do
75 # to be ensured that pleroma will be restarted last
76 if :pleroma in apps do
78 |> List.delete(:pleroma)
79 |> List.insert_at(-1, :pleroma)
81 Restarter.Pleroma.rebooted()
86 defp merge_with_default(%{group: group, key: key, value: value} = setting) do
88 if group == :pleroma do
89 Config.get([key], Config.Holder.default_config(group, key))
91 Config.Holder.default_config(group, key)
96 Ecto.get_meta(setting, :state) == :deleted -> default
97 can_be_merged?(default, value) -> ConfigDB.merge_group(group, key, default, value)
101 {group, key, value, merged}
104 # change logger configuration in runtime, without restart
105 defp configure({:quack, key, _, merged}) do
106 Logger.configure_backend(Quack.Logger, [{key, merged}])
107 :ok = update_env(:quack, key, merged)
110 defp configure({_, :backends, _, merged}) do
111 # removing current backends
112 Enum.each(Application.get_env(:logger, :backends), &Logger.remove_backend/1)
114 Enum.each(merged, &Logger.add_backend/1)
116 :ok = update_env(:logger, :backends, merged)
119 defp configure({_, key, _, merged}) when key in [:console, :ex_syslogger] do
121 if key == :console do
122 put_in(merged[:format], merged[:format] <> "\n")
128 if key == :ex_syslogger,
129 do: {ExSyslogger, :ex_syslogger},
132 Logger.configure_backend(backend, merged)
133 :ok = update_env(:logger, key, merged)
136 defp configure({_, key, _, merged}) do
137 Logger.configure([{key, merged}])
138 :ok = update_env(:logger, key, merged)
141 defp update({group, key, value, merged}) do
143 :ok = update_env(group, key, merged)
145 if group != :pleroma or pleroma_need_restart?(group, key, value), do: group
149 "updating env causes error, group: #{inspect(group)}, key: #{inspect(key)}, value: #{inspect(value)} error: #{inspect(error)}"
151 Logger.warn(error_msg)
157 defp update_env(group, key, nil), do: Application.delete_env(group, key)
158 defp update_env(group, key, value), do: Application.put_env(group, key, value)
160 @spec pleroma_need_restart?(atom(), atom(), any()) :: boolean()
161 def pleroma_need_restart?(group, key, value) do
162 group_and_key_need_reboot?(group, key) or group_and_subkey_need_reboot?(group, key, value)
165 defp group_and_key_need_reboot?(group, key) do
166 Enum.any?(reboot_time_keys(), fn {g, k} -> g == group and k == key end)
169 defp group_and_subkey_need_reboot?(group, key, value) do
170 Keyword.keyword?(value) and
171 Enum.any?(reboot_time_subkeys(), fn {g, k, subkeys} ->
172 g == group and k == key and
173 Enum.any?(Keyword.keys(value), &(&1 in subkeys))
177 defp restart(_, :pleroma, env), do: Restarter.Pleroma.restart_after_boot(env)
179 defp restart(started_applications, app, _) do
180 with {^app, _, _} <- List.keyfind(started_applications, app, 0),
181 :ok <- Application.stop(app) do
182 :ok = Application.start(app)
185 Logger.warn("#{app} is not started.")
194 defp can_be_merged?(val1, val2) when is_list(val1) and is_list(val2) do
195 Keyword.keyword?(val1) and Keyword.keyword?(val2)
198 defp can_be_merged?(_val1, _val2), do: false