static-fe overhaul (#236)
[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, Oban},
19 {:pleroma, :rate_limit},
20 {:pleroma, :markup},
21 {:pleroma, :streamer}
22 ]
23
24 defp reboot_time_subkeys,
25 do: [
26 {:pleroma, Pleroma.Captcha, [:seconds_valid]},
27 {:pleroma, Pleroma.Upload, [:proxy_remote]},
28 {:pleroma, :instance, [:upload_limit]}
29 ]
30
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)
34 :ignore
35 end
36
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
41 {logger, other} =
42 (Repo.all(ConfigDB) ++ deleted_settings)
43 |> Enum.map(&merge_with_default/1)
44 |> Enum.split_with(fn {group, _, _, _} -> group in [:logger, :quack] end)
45
46 logger
47 |> Enum.sort()
48 |> Enum.each(&configure/1)
49
50 started_applications = Application.started_applications()
51
52 reject = [nil, :postgrex]
53
54 reject =
55 if restart_pleroma? do
56 reject
57 else
58 [:pleroma | reject]
59 end
60
61 other
62 |> Enum.map(&update/1)
63 |> Enum.uniq()
64 |> Enum.reject(&(&1 in reject))
65 |> maybe_set_pleroma_last()
66 |> Enum.each(&restart(started_applications, &1, Config.get(:env)))
67
68 :ok
69 else
70 {:configurable, false} -> Restarter.Pleroma.rebooted()
71 end
72 end
73
74 defp maybe_set_pleroma_last(apps) do
75 # to be ensured that pleroma will be restarted last
76 if :pleroma in apps do
77 apps
78 |> List.delete(:pleroma)
79 |> List.insert_at(-1, :pleroma)
80 else
81 Restarter.Pleroma.rebooted()
82 apps
83 end
84 end
85
86 defp merge_with_default(%{group: group, key: key, value: value} = setting) do
87 default =
88 if group == :pleroma do
89 Config.get([key], Config.Holder.default_config(group, key))
90 else
91 Config.Holder.default_config(group, key)
92 end
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: #{inspect(value)} error: #{inspect(error)}"
150
151 Logger.warn(error_msg)
152
153 nil
154 end
155 end
156
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)
159
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)
163 end
164
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)
167 end
168
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))
174 end)
175 end
176
177 defp restart(_, :pleroma, env), do: Restarter.Pleroma.restart_after_boot(env)
178
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)
183 else
184 nil ->
185 Logger.warn("#{app} is not started.")
186
187 error ->
188 error
189 |> inspect()
190 |> Logger.warn()
191 end
192 end
193
194 defp can_be_merged?(val1, val2) when is_list(val1) and is_list(val2) do
195 Keyword.keyword?(val1) and Keyword.keyword?(val2)
196 end
197
198 defp can_be_merged?(_val1, _val2), do: false
199 end