2019-07-09 23:13:23 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2022-02-25 23:11:42 -07:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2019-07-09 23:13:23 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-06-14 09:45:05 -06:00
|
|
|
defmodule Pleroma.Config.TransferTask do
|
|
|
|
use Task
|
2019-09-29 02:17:38 -06:00
|
|
|
|
2020-03-12 09:28:54 -06:00
|
|
|
alias Pleroma.Config
|
2020-01-15 22:50:27 -07:00
|
|
|
alias Pleroma.ConfigDB
|
2019-09-29 02:17:38 -06:00
|
|
|
alias Pleroma.Repo
|
2020-01-15 22:50:27 -07:00
|
|
|
|
|
|
|
require Logger
|
2019-06-14 09:45:05 -06:00
|
|
|
|
2020-01-25 08:42:04 -07:00
|
|
|
@type env() :: :test | :benchmark | :dev | :prod
|
|
|
|
|
2021-05-21 12:04:57 -06:00
|
|
|
defp reboot_time_keys,
|
|
|
|
do: [
|
|
|
|
{:pleroma, :hackney_pools},
|
2020-08-03 16:08:35 -06:00
|
|
|
{:pleroma, :shout},
|
2021-05-21 12:04:57 -06:00
|
|
|
{:pleroma, Oban},
|
|
|
|
{:pleroma, :rate_limit},
|
|
|
|
{:pleroma, :markup},
|
|
|
|
{:pleroma, :streamer},
|
|
|
|
{:pleroma, :pools},
|
|
|
|
{:pleroma, :connections_pool}
|
|
|
|
]
|
|
|
|
|
|
|
|
defp reboot_time_subkeys,
|
|
|
|
do: [
|
|
|
|
{:pleroma, Pleroma.Captcha, [:seconds_valid]},
|
|
|
|
{:pleroma, Pleroma.Upload, [:proxy_remote]},
|
|
|
|
{:pleroma, :instance, [:upload_limit]},
|
|
|
|
{:pleroma, :gopher, [:enabled]}
|
|
|
|
]
|
2020-01-25 08:42:04 -07:00
|
|
|
|
2020-07-03 11:12:00 -06:00
|
|
|
def start_link(restart_pleroma? \\ true) do
|
|
|
|
load_and_update_env([], restart_pleroma?)
|
2020-03-12 09:28:54 -06:00
|
|
|
if Config.get(:env) == :test, do: Ecto.Adapters.SQL.Sandbox.checkin(Repo)
|
2019-06-14 09:45:05 -06:00
|
|
|
:ignore
|
|
|
|
end
|
|
|
|
|
2020-03-12 09:28:54 -06:00
|
|
|
@spec load_and_update_env([ConfigDB.t()], boolean()) :: :ok
|
|
|
|
def load_and_update_env(deleted_settings \\ [], restart_pleroma? \\ true) do
|
|
|
|
with {_, true} <- {:configurable, Config.get(:configurable_from_database)} do
|
2019-06-22 23:16:16 -06:00
|
|
|
# We need to restart applications for loaded settings take effect
|
2020-01-22 05:14:11 -07:00
|
|
|
|
2020-04-10 05:42:52 -06:00
|
|
|
{logger, other} =
|
|
|
|
(Repo.all(ConfigDB) ++ deleted_settings)
|
2020-05-31 01:46:02 -06:00
|
|
|
|> Enum.map(&merge_with_default/1)
|
2020-04-10 05:42:52 -06:00
|
|
|
|> Enum.split_with(fn {group, _, _, _} -> group in [:logger, :quack] end)
|
|
|
|
|
|
|
|
logger
|
|
|
|
|> Enum.sort()
|
|
|
|
|> Enum.each(&configure/1)
|
|
|
|
|
2020-03-10 06:31:44 -06:00
|
|
|
started_applications = Application.started_applications()
|
|
|
|
|
2020-04-16 01:36:37 -06:00
|
|
|
# TODO: some problem with prometheus after restart!
|
|
|
|
reject = [nil, :prometheus, :postgrex]
|
|
|
|
|
|
|
|
reject =
|
|
|
|
if restart_pleroma? do
|
|
|
|
reject
|
|
|
|
else
|
|
|
|
[:pleroma | reject]
|
|
|
|
end
|
|
|
|
|
2020-04-10 05:42:52 -06:00
|
|
|
other
|
|
|
|
|> Enum.map(&update/1)
|
2020-03-10 06:31:44 -06:00
|
|
|
|> Enum.uniq()
|
2020-04-16 01:36:37 -06:00
|
|
|
|> Enum.reject(&(&1 in reject))
|
2020-03-10 06:31:44 -06:00
|
|
|
|> maybe_set_pleroma_last()
|
2020-03-12 09:28:54 -06:00
|
|
|
|> Enum.each(&restart(started_applications, &1, Config.get(:env)))
|
2020-01-22 05:14:11 -07:00
|
|
|
|
|
|
|
:ok
|
2020-02-18 08:10:39 -07:00
|
|
|
else
|
|
|
|
{:configurable, false} -> Restarter.Pleroma.rebooted()
|
2019-06-14 09:45:05 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-10 06:31:44 -06:00
|
|
|
defp maybe_set_pleroma_last(apps) do
|
|
|
|
# to be ensured that pleroma will be restarted last
|
|
|
|
if :pleroma in apps do
|
|
|
|
apps
|
|
|
|
|> List.delete(:pleroma)
|
|
|
|
|> List.insert_at(-1, :pleroma)
|
|
|
|
else
|
|
|
|
Restarter.Pleroma.rebooted()
|
|
|
|
apps
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-31 01:46:02 -06:00
|
|
|
defp merge_with_default(%{group: group, key: key, value: value} = setting) do
|
2020-04-10 05:42:52 -06:00
|
|
|
default = Config.Holder.default_config(group, key)
|
2020-02-11 00:12:57 -07:00
|
|
|
|
2020-04-10 05:42:52 -06:00
|
|
|
merged =
|
|
|
|
cond do
|
|
|
|
Ecto.get_meta(setting, :state) == :deleted -> default
|
|
|
|
can_be_merged?(default, value) -> ConfigDB.merge_group(group, key, default, value)
|
|
|
|
true -> value
|
|
|
|
end
|
2020-02-11 00:12:57 -07:00
|
|
|
|
2020-04-10 05:42:52 -06:00
|
|
|
{group, key, value, merged}
|
2020-02-11 00:12:57 -07:00
|
|
|
end
|
|
|
|
|
2020-04-10 05:42:52 -06:00
|
|
|
# change logger configuration in runtime, without restart
|
|
|
|
defp configure({:quack, key, _, merged}) do
|
|
|
|
Logger.configure_backend(Quack.Logger, [{key, merged}])
|
|
|
|
:ok = update_env(:quack, key, merged)
|
|
|
|
end
|
2019-06-14 09:45:05 -06:00
|
|
|
|
2020-04-10 05:42:52 -06:00
|
|
|
defp configure({_, :backends, _, merged}) do
|
|
|
|
# removing current backends
|
|
|
|
Enum.each(Application.get_env(:logger, :backends), &Logger.remove_backend/1)
|
2020-01-25 08:42:04 -07:00
|
|
|
|
2020-04-10 05:42:52 -06:00
|
|
|
Enum.each(merged, &Logger.add_backend/1)
|
2020-01-17 01:45:44 -07:00
|
|
|
|
2020-04-10 05:42:52 -06:00
|
|
|
:ok = update_env(:logger, :backends, merged)
|
|
|
|
end
|
2020-01-17 06:28:44 -07:00
|
|
|
|
2020-04-16 23:42:48 -06:00
|
|
|
defp configure({_, key, _, merged}) when key in [:console, :ex_syslogger] do
|
2020-04-10 05:42:52 -06:00
|
|
|
merged =
|
|
|
|
if key == :console do
|
|
|
|
put_in(merged[:format], merged[:format] <> "\n")
|
|
|
|
else
|
|
|
|
merged
|
|
|
|
end
|
|
|
|
|
|
|
|
backend =
|
|
|
|
if key == :ex_syslogger,
|
|
|
|
do: {ExSyslogger, :ex_syslogger},
|
|
|
|
else: key
|
|
|
|
|
|
|
|
Logger.configure_backend(backend, merged)
|
2020-04-16 23:42:48 -06:00
|
|
|
:ok = update_env(:logger, key, merged)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp configure({_, key, _, merged}) do
|
|
|
|
Logger.configure([{key, merged}])
|
|
|
|
:ok = update_env(:logger, key, merged)
|
2020-04-10 05:42:52 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
defp update({group, key, value, merged}) do
|
|
|
|
try do
|
|
|
|
:ok = update_env(group, key, merged)
|
|
|
|
|
|
|
|
if group != :pleroma or pleroma_need_restart?(group, key, value), do: group
|
2019-06-14 09:45:05 -06:00
|
|
|
rescue
|
2020-01-22 05:14:11 -07:00
|
|
|
error ->
|
|
|
|
error_msg =
|
2021-10-06 00:08:21 -06:00
|
|
|
"updating env causes error, group: #{inspect(group)}, key: #{inspect(key)}, value: #{inspect(value)} error: #{inspect(error)}"
|
2020-01-22 05:14:11 -07:00
|
|
|
|
|
|
|
Logger.warn(error_msg)
|
2019-09-29 02:17:38 -06:00
|
|
|
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-10 05:42:52 -06:00
|
|
|
defp update_env(group, key, nil), do: Application.delete_env(group, key)
|
|
|
|
defp update_env(group, key, value), do: Application.put_env(group, key, value)
|
|
|
|
|
2020-01-25 08:42:04 -07:00
|
|
|
@spec pleroma_need_restart?(atom(), atom(), any()) :: boolean()
|
|
|
|
def pleroma_need_restart?(group, key, value) do
|
|
|
|
group_and_key_need_reboot?(group, key) or group_and_subkey_need_reboot?(group, key, value)
|
|
|
|
end
|
2020-01-22 05:14:11 -07:00
|
|
|
|
2020-01-25 08:42:04 -07:00
|
|
|
defp group_and_key_need_reboot?(group, key) do
|
2021-05-21 12:04:57 -06:00
|
|
|
Enum.any?(reboot_time_keys(), fn {g, k} -> g == group and k == key end)
|
2020-01-25 08:42:04 -07:00
|
|
|
end
|
2020-01-22 05:14:11 -07:00
|
|
|
|
2020-01-25 08:42:04 -07:00
|
|
|
defp group_and_subkey_need_reboot?(group, key, value) do
|
|
|
|
Keyword.keyword?(value) and
|
2021-05-21 12:04:57 -06:00
|
|
|
Enum.any?(reboot_time_subkeys(), fn {g, k, subkeys} ->
|
2020-01-25 08:42:04 -07:00
|
|
|
g == group and k == key and
|
|
|
|
Enum.any?(Keyword.keys(value), &(&1 in subkeys))
|
|
|
|
end)
|
2020-01-22 05:14:11 -07:00
|
|
|
end
|
|
|
|
|
2020-02-08 02:55:37 -07:00
|
|
|
defp restart(_, :pleroma, env), do: Restarter.Pleroma.restart_after_boot(env)
|
2020-01-25 08:42:04 -07:00
|
|
|
|
|
|
|
defp restart(started_applications, app, _) do
|
2019-09-29 02:17:38 -06:00
|
|
|
with {^app, _, _} <- List.keyfind(started_applications, app, 0),
|
|
|
|
:ok <- Application.stop(app) do
|
|
|
|
:ok = Application.start(app)
|
|
|
|
else
|
2020-01-21 07:49:22 -07:00
|
|
|
nil ->
|
|
|
|
Logger.warn("#{app} is not started.")
|
|
|
|
|
|
|
|
error ->
|
|
|
|
error
|
|
|
|
|> inspect()
|
|
|
|
|> Logger.warn()
|
2019-06-14 09:45:05 -06:00
|
|
|
end
|
|
|
|
end
|
2020-01-17 01:45:44 -07:00
|
|
|
|
|
|
|
defp can_be_merged?(val1, val2) when is_list(val1) and is_list(val2) do
|
|
|
|
Keyword.keyword?(val1) and Keyword.keyword?(val2)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp can_be_merged?(_val1, _val2), do: false
|
2019-06-14 09:45:05 -06:00
|
|
|
end
|