Merge remote-tracking branch 'upstream/develop' into restrict-domain
[akkoma] / lib / pleroma / config / transfer_task.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Config.TransferTask do
6 use Task
7
8 alias Pleroma.Config
9 alias Pleroma.ConfigDB
10 alias Pleroma.Repo
11
12 require Logger
13
14 @type env() :: :test | :benchmark | :dev | :prod
15
16 @reboot_time_keys [
17 {:pleroma, :hackney_pools},
18 {:pleroma, :chat},
19 {:pleroma, Oban},
20 {:pleroma, :rate_limit},
21 {:pleroma, :markup},
22 {:pleroma, :streamer},
23 {:pleroma, :pools},
24 {:pleroma, :connections_pool}
25 ]
26
27 @reboot_time_subkeys [
28 {:pleroma, Pleroma.Captcha, [:seconds_valid]},
29 {:pleroma, Pleroma.Upload, [:proxy_remote]},
30 {:pleroma, :instance, [:upload_limit]},
31 {:pleroma, :gopher, [:enabled]}
32 ]
33
34 def start_link(restart_pleroma? \\ true) do
35 load_and_update_env([], restart_pleroma?)
36 if Config.get(:env) == :test, do: Ecto.Adapters.SQL.Sandbox.checkin(Repo)
37 :ignore
38 end
39
40 @spec load_and_update_env([ConfigDB.t()], boolean()) :: :ok
41 def load_and_update_env(deleted_settings \\ [], restart_pleroma? \\ true) do
42 with {_, true} <- {:configurable, Config.get(:configurable_from_database)} do
43 # We need to restart applications for loaded settings take effect
44
45 {logger, other} =
46 (Repo.all(ConfigDB) ++ deleted_settings)
47 |> Enum.map(&merge_with_default/1)
48 |> Enum.split_with(fn {group, _, _, _} -> group in [:logger, :quack] end)
49
50 logger
51 |> Enum.sort()
52 |> Enum.each(&configure/1)
53
54 started_applications = Application.started_applications()
55
56 # TODO: some problem with prometheus after restart!
57 reject = [nil, :prometheus, :postgrex]
58
59 reject =
60 if restart_pleroma? do
61 reject
62 else
63 [:pleroma | reject]
64 end
65
66 other
67 |> Enum.map(&update/1)
68 |> Enum.uniq()
69 |> Enum.reject(&(&1 in reject))
70 |> maybe_set_pleroma_last()
71 |> Enum.each(&restart(started_applications, &1, Config.get(:env)))
72
73 :ok
74 else
75 {:configurable, false} -> Restarter.Pleroma.rebooted()
76 end
77 end
78
79 defp maybe_set_pleroma_last(apps) do
80 # to be ensured that pleroma will be restarted last
81 if :pleroma in apps do
82 apps
83 |> List.delete(:pleroma)
84 |> List.insert_at(-1, :pleroma)
85 else
86 Restarter.Pleroma.rebooted()
87 apps
88 end
89 end
90
91 defp merge_with_default(%{group: group, key: key, value: value} = setting) do
92 default = Config.Holder.default_config(group, key)
93
94 merged =
95 cond do
96 Ecto.get_meta(setting, :state) == :deleted -> default
97 can_be_merged?(default, value) -> ConfigDB.merge_group(group, key, default, value)
98 true -> value
99 end
100
101 {group, key, value, merged}
102 end
103
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)
108 end
109
110 defp configure({_, :backends, _, merged}) do
111 # removing current backends
112 Enum.each(Application.get_env(:logger, :backends), &Logger.remove_backend/1)
113
114 Enum.each(merged, &Logger.add_backend/1)
115
116 :ok = update_env(:logger, :backends, merged)
117 end
118
119 defp configure({_, key, _, merged}) when key in [:console, :ex_syslogger] do
120 merged =
121 if key == :console do
122 put_in(merged[:format], merged[:format] <> "\n")
123 else
124 merged
125 end
126
127 backend =
128 if key == :ex_syslogger,
129 do: {ExSyslogger, :ex_syslogger},
130 else: key
131
132 Logger.configure_backend(backend, merged)
133 :ok = update_env(:logger, key, merged)
134 end
135
136 defp configure({_, key, _, merged}) do
137 Logger.configure([{key, merged}])
138 :ok = update_env(:logger, key, merged)
139 end
140
141 defp update({group, key, value, merged}) do
142 try do
143 :ok = update_env(group, key, merged)
144
145 if group != :pleroma or pleroma_need_restart?(group, key, value), do: group
146 rescue
147 error ->
148 error_msg =
149 "updating env causes error, group: #{inspect(group)}, key: #{inspect(key)}, value: #{
150 inspect(value)
151 } error: #{inspect(error)}"
152
153 Logger.warn(error_msg)
154
155 nil
156 end
157 end
158
159 defp update_env(group, key, nil), do: Application.delete_env(group, key)
160 defp update_env(group, key, value), do: Application.put_env(group, key, value)
161
162 @spec pleroma_need_restart?(atom(), atom(), any()) :: boolean()
163 def pleroma_need_restart?(group, key, value) do
164 group_and_key_need_reboot?(group, key) or group_and_subkey_need_reboot?(group, key, value)
165 end
166
167 defp group_and_key_need_reboot?(group, key) do
168 Enum.any?(@reboot_time_keys, fn {g, k} -> g == group and k == key end)
169 end
170
171 defp group_and_subkey_need_reboot?(group, key, value) do
172 Keyword.keyword?(value) and
173 Enum.any?(@reboot_time_subkeys, fn {g, k, subkeys} ->
174 g == group and k == key and
175 Enum.any?(Keyword.keys(value), &(&1 in subkeys))
176 end)
177 end
178
179 defp restart(_, :pleroma, env), do: Restarter.Pleroma.restart_after_boot(env)
180
181 defp restart(started_applications, app, _) do
182 with {^app, _, _} <- List.keyfind(started_applications, app, 0),
183 :ok <- Application.stop(app) do
184 :ok = Application.start(app)
185 else
186 nil ->
187 Logger.warn("#{app} is not started.")
188
189 error ->
190 error
191 |> inspect()
192 |> Logger.warn()
193 end
194 end
195
196 defp can_be_merged?(val1, val2) when is_list(val1) and is_list(val2) do
197 Keyword.keyword?(val1) and Keyword.keyword?(val2)
198 end
199
200 defp can_be_merged?(_val1, _val2), do: false
201 end