1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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
17 {:pleroma, :hackney_pools},
20 {:pleroma, :rate_limit},
22 {:pleroma, :streamer},
24 {:pleroma, :connections_pool}
27 @reboot_time_subkeys [
28 {:pleroma, Pleroma.Captcha, [:seconds_valid]},
29 {:pleroma, Pleroma.Upload, [:proxy_remote]},
30 {:pleroma, :instance, [:upload_limit]},
31 {:pleroma, :email_notifications, [:digest]},
32 {:pleroma, :oauth2, [:clean_expired_tokens]},
33 {:pleroma, Pleroma.ActivityExpiration, [:enabled]},
34 {:pleroma, Pleroma.ScheduledActivity, [:enabled]},
35 {:pleroma, :gopher, [:enabled]}
40 if Config.get(:env) == :test, do: Ecto.Adapters.SQL.Sandbox.checkin(Repo)
44 @spec load_and_update_env([ConfigDB.t()], boolean()) :: :ok
45 def load_and_update_env(deleted_settings \\ [], restart_pleroma? \\ true) do
46 with {_, true} <- {:configurable, Config.get(:configurable_from_database)} do
47 # We need to restart applications for loaded settings take effect
49 # TODO: some problem with prometheus after restart!
51 if restart_pleroma? do
54 [:pleroma, nil, :prometheus]
58 (Repo.all(ConfigDB) ++ deleted_settings)
59 |> Enum.map(&transform_and_merge/1)
60 |> Enum.split_with(fn {group, _, _, _} -> group in [:logger, :quack] end)
64 |> Enum.each(&configure/1)
66 started_applications = Application.started_applications()
69 |> Enum.map(&update/1)
71 |> Enum.reject(&(&1 in reject_restart))
72 |> maybe_set_pleroma_last()
73 |> Enum.each(&restart(started_applications, &1, Config.get(:env)))
77 {:configurable, false} -> Restarter.Pleroma.rebooted()
81 defp maybe_set_pleroma_last(apps) do
82 # to be ensured that pleroma will be restarted last
83 if :pleroma in apps do
85 |> List.delete(:pleroma)
86 |> List.insert_at(-1, :pleroma)
88 Restarter.Pleroma.rebooted()
93 defp transform_and_merge(%{group: group, key: key, value: value} = setting) do
94 group = ConfigDB.from_string(group)
95 key = ConfigDB.from_string(key)
96 value = ConfigDB.from_binary(value)
98 default = Config.Holder.default_config(group, key)
102 Ecto.get_meta(setting, :state) == :deleted -> default
103 can_be_merged?(default, value) -> ConfigDB.merge_group(group, key, default, value)
107 {group, key, value, merged}
110 # change logger configuration in runtime, without restart
111 defp configure({:quack, key, _, merged}) do
112 Logger.configure_backend(Quack.Logger, [{key, merged}])
113 :ok = update_env(:quack, key, merged)
116 defp configure({_, :backends, _, merged}) do
117 # removing current backends
118 Enum.each(Application.get_env(:logger, :backends), &Logger.remove_backend/1)
120 Enum.each(merged, &Logger.add_backend/1)
122 :ok = update_env(:logger, :backends, merged)
125 defp configure({group, key, _, merged}) do
127 if key == :console do
128 put_in(merged[:format], merged[:format] <> "\n")
134 if key == :ex_syslogger,
135 do: {ExSyslogger, :ex_syslogger},
138 Logger.configure_backend(backend, merged)
139 :ok = update_env(:logger, group, merged)
142 defp update({group, key, value, merged}) do
144 :ok = update_env(group, key, merged)
146 if group != :pleroma or pleroma_need_restart?(group, key, value), do: group
150 "updating env causes error, group: #{inspect(group)}, key: #{inspect(key)}, value: #{
152 } error: #{inspect(error)}"
154 Logger.warn(error_msg)
160 defp update_env(group, key, nil), do: Application.delete_env(group, key)
161 defp update_env(group, key, value), do: Application.put_env(group, key, value)
163 @spec pleroma_need_restart?(atom(), atom(), any()) :: boolean()
164 def pleroma_need_restart?(group, key, value) do
165 group_and_key_need_reboot?(group, key) or group_and_subkey_need_reboot?(group, key, value)
168 defp group_and_key_need_reboot?(group, key) do
169 Enum.any?(@reboot_time_keys, fn {g, k} -> g == group and k == key end)
172 defp group_and_subkey_need_reboot?(group, key, value) do
173 Keyword.keyword?(value) and
174 Enum.any?(@reboot_time_subkeys, fn {g, k, subkeys} ->
175 g == group and k == key and
176 Enum.any?(Keyword.keys(value), &(&1 in subkeys))
180 defp restart(_, :pleroma, env), do: Restarter.Pleroma.restart_after_boot(env)
182 defp restart(started_applications, app, _) do
183 with {^app, _, _} <- List.keyfind(started_applications, app, 0),
184 :ok <- Application.stop(app) do
185 :ok = Application.start(app)
188 Logger.warn("#{app} is not started.")
197 defp can_be_merged?(val1, val2) when is_list(val1) and is_list(val2) do
198 Keyword.keyword?(val1) and Keyword.keyword?(val2)
201 defp can_be_merged?(_val1, _val2), do: false