5371aae7a420c96bd857e1a7fe60a242e97e08d0
[akkoma] / lib / pleroma / config / transfer_task.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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 defp reboot_time_keys,
17 do: [
18 {:pleroma, :hackney_pools},
19 {:pleroma, :shout},
20 {:pleroma, Oban},
21 {:pleroma, :rate_limit},
22 {:pleroma, :markup},
23 {:pleroma, :streamer},
24 {:pleroma, :pools},
25 {:pleroma, :connections_pool}
26 ]
27
28 defp reboot_time_subkeys,
29 do: [
30 {:pleroma, Pleroma.Captcha, [:seconds_valid]},
31 {:pleroma, Pleroma.Upload, [:proxy_remote]},
32 {:pleroma, :instance, [:upload_limit]},
33 {:pleroma, :gopher, [:enabled]}
34 ]
35
36 def start_link(restart_pleroma? \\ true) do
37 load_and_update_env([], restart_pleroma?)
38 if Config.get(:env) == :test, do: Ecto.Adapters.SQL.Sandbox.checkin(Repo)
39 :ignore
40 end
41
42 @spec load_and_update_env([ConfigDB.t()], boolean()) :: :ok
43 def load_and_update_env(deleted_settings \\ [], restart_pleroma? \\ true) do
44 with {_, true} <- {:configurable, Config.get(:configurable_from_database)} do
45 # We need to restart applications for loaded settings take effect
46
47 {logger, other} =
48 (Repo.all(ConfigDB) ++ deleted_settings)
49 |> Enum.map(&merge_with_default/1)
50 |> Enum.split_with(fn {group, _, _, _} -> group in [:logger, :quack] end)
51
52 logger
53 |> Enum.sort()
54 |> Enum.each(&configure/1)
55
56 started_applications = Application.started_applications()
57
58 # TODO: some problem with prometheus after restart!
59 reject = [nil, :prometheus, :postgrex]
60
61 reject =
62 if restart_pleroma? do
63 reject
64 else
65 [:pleroma | reject]
66 end
67
68 other
69 |> Enum.map(&update/1)
70 |> Enum.uniq()
71 |> Enum.reject(&(&1 in reject))
72 |> maybe_set_pleroma_last()
73 |> Enum.each(&restart(started_applications, &1, Config.get(:env)))
74
75 :ok
76 else
77 {:configurable, false} -> Restarter.Pleroma.rebooted()
78 end
79 end
80
81 defp maybe_set_pleroma_last(apps) do
82 # to be ensured that pleroma will be restarted last
83 if :pleroma in apps do
84 apps
85 |> List.delete(:pleroma)
86 |> List.insert_at(-1, :pleroma)
87 else
88 Restarter.Pleroma.rebooted()
89 apps
90 end
91 end
92
93 defp merge_with_default(%{group: group, key: key, value: value} = setting) do
94 default = Config.Holder.default_config(group, key)
95
96 merged =
97 cond do
98 Ecto.get_meta(setting, :state) == :deleted -> default
99 can_be_merged?(default, value) -> ConfigDB.merge_group(group, key, default, value)
100 true -> value
101 end
102
103 {group, key, value, merged}
104 end
105
106 # change logger configuration in runtime, without restart
107 defp configure({:quack, key, _, merged}) do
108 Logger.configure_backend(Quack.Logger, [{key, merged}])
109 :ok = update_env(:quack, key, merged)
110 end
111
112 defp configure({_, :backends, _, merged}) do
113 # removing current backends
114 Enum.each(Application.get_env(:logger, :backends), &Logger.remove_backend/1)
115
116 Enum.each(merged, &Logger.add_backend/1)
117
118 :ok = update_env(:logger, :backends, merged)
119 end
120
121 defp configure({_, key, _, merged}) when key in [:console, :ex_syslogger] do
122 merged =
123 if key == :console do
124 put_in(merged[:format], merged[:format] <> "\n")
125 else
126 merged
127 end
128
129 backend =
130 if key == :ex_syslogger,
131 do: {ExSyslogger, :ex_syslogger},
132 else: key
133
134 Logger.configure_backend(backend, merged)
135 :ok = update_env(:logger, key, merged)
136 end
137
138 defp configure({_, key, _, merged}) do
139 Logger.configure([{key, merged}])
140 :ok = update_env(:logger, key, merged)
141 end
142
143 defp update({group, key, value, merged}) do
144 try do
145 :ok = update_env(group, key, merged)
146
147 if group != :pleroma or pleroma_need_restart?(group, key, value), do: group
148 rescue
149 error ->
150 error_msg =
151 "updating env causes error, group: #{inspect(group)}, key: #{inspect(key)}, value: #{inspect(value)} 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