2019-07-10 07:13:23 +02:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-02 06:08:45 +01:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2019-07-10 07:13:23 +02:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-06-14 17:45:05 +02:00
|
|
|
defmodule Pleroma.Config.TransferTask do
|
|
|
|
use Task
|
2019-09-29 10:17:38 +02:00
|
|
|
|
2020-03-12 16:28:54 +01:00
|
|
|
alias Pleroma.Config
|
2020-01-16 06:50:27 +01:00
|
|
|
alias Pleroma.ConfigDB
|
2019-09-29 10:17:38 +02:00
|
|
|
alias Pleroma.Repo
|
2020-01-16 06:50:27 +01:00
|
|
|
|
|
|
|
require Logger
|
2019-06-14 17:45:05 +02:00
|
|
|
|
2020-01-25 16:42:04 +01:00
|
|
|
@type env() :: :test | :benchmark | :dev | :prod
|
|
|
|
|
|
|
|
@reboot_time_keys [
|
|
|
|
{:pleroma, :hackney_pools},
|
|
|
|
{:pleroma, :chat},
|
|
|
|
{:pleroma, Oban},
|
|
|
|
{:pleroma, :rate_limit},
|
|
|
|
{:pleroma, :markup},
|
2020-02-11 08:12:57 +01:00
|
|
|
{:pleroma, :streamer},
|
|
|
|
{:pleroma, :pools},
|
2020-03-10 13:31:44 +01:00
|
|
|
{:pleroma, :connections_pool}
|
2020-01-25 16:42:04 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
@reboot_time_subkeys [
|
|
|
|
{:pleroma, Pleroma.Captcha, [:seconds_valid]},
|
|
|
|
{:pleroma, Pleroma.Upload, [:proxy_remote]},
|
|
|
|
{:pleroma, :instance, [:upload_limit]},
|
|
|
|
{:pleroma, :email_notifications, [:digest]},
|
|
|
|
{:pleroma, :oauth2, [:clean_expired_tokens]},
|
|
|
|
{:pleroma, Pleroma.ActivityExpiration, [:enabled]},
|
|
|
|
{:pleroma, Pleroma.ScheduledActivity, [:enabled]},
|
|
|
|
{:pleroma, :gopher, [:enabled]}
|
|
|
|
]
|
|
|
|
|
2019-08-14 17:55:17 +02:00
|
|
|
def start_link(_) do
|
2019-06-14 17:45:05 +02:00
|
|
|
load_and_update_env()
|
2020-03-12 16:28:54 +01:00
|
|
|
if Config.get(:env) == :test, do: Ecto.Adapters.SQL.Sandbox.checkin(Repo)
|
2019-06-14 17:45:05 +02:00
|
|
|
:ignore
|
|
|
|
end
|
|
|
|
|
2020-03-12 16:28:54 +01: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-23 07:16:16 +02:00
|
|
|
# We need to restart applications for loaded settings take effect
|
2020-01-22 13:14:11 +01:00
|
|
|
|
2020-03-10 13:31:44 +01:00
|
|
|
# TODO: some problem with prometheus after restart!
|
2020-03-12 16:28:54 +01:00
|
|
|
reject_restart =
|
2020-03-10 13:31:44 +01:00
|
|
|
if restart_pleroma? do
|
2020-03-12 16:28:54 +01:00
|
|
|
[nil, :prometheus]
|
2020-01-25 16:42:04 +01:00
|
|
|
else
|
2020-03-12 16:28:54 +01:00
|
|
|
[:pleroma, nil, :prometheus]
|
2020-01-25 16:42:04 +01:00
|
|
|
end
|
|
|
|
|
2020-03-10 13:31:44 +01:00
|
|
|
started_applications = Application.started_applications()
|
|
|
|
|
2020-03-12 16:28:54 +01:00
|
|
|
(Repo.all(ConfigDB) ++ deleted_settings)
|
|
|
|
|> Enum.map(&merge_and_update/1)
|
2020-03-10 13:31:44 +01:00
|
|
|
|> Enum.uniq()
|
2020-03-12 16:28:54 +01:00
|
|
|
|> Enum.reject(&(&1 in reject_restart))
|
2020-03-10 13:31:44 +01:00
|
|
|
|> maybe_set_pleroma_last()
|
2020-03-12 16:28:54 +01:00
|
|
|
|> Enum.each(&restart(started_applications, &1, Config.get(:env)))
|
2020-01-22 13:14:11 +01:00
|
|
|
|
|
|
|
:ok
|
2020-02-18 16:10:39 +01:00
|
|
|
else
|
|
|
|
{:configurable, false} -> Restarter.Pleroma.rebooted()
|
2019-06-14 17:45:05 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-10 13:31:44 +01: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-02-11 08:12:57 +01:00
|
|
|
defp group_for_restart(:logger, key, _, merged_value) do
|
|
|
|
# change logger configuration in runtime, without restart
|
|
|
|
if Keyword.keyword?(merged_value) and
|
|
|
|
key not in [:compile_time_application, :backends, :compile_time_purge_matching] do
|
|
|
|
Logger.configure_backend(key, merged_value)
|
|
|
|
else
|
|
|
|
Logger.configure([{key, merged_value}])
|
|
|
|
end
|
|
|
|
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
defp group_for_restart(group, _, _, _) when group != :pleroma, do: group
|
|
|
|
|
|
|
|
defp group_for_restart(group, key, value, _) do
|
2020-03-10 13:31:44 +01:00
|
|
|
if pleroma_need_restart?(group, key, value), do: group
|
2020-02-11 08:12:57 +01:00
|
|
|
end
|
|
|
|
|
2020-01-22 13:14:11 +01:00
|
|
|
defp merge_and_update(setting) do
|
2019-06-14 17:45:05 +02:00
|
|
|
try do
|
2020-01-16 06:50:27 +01:00
|
|
|
key = ConfigDB.from_string(setting.key)
|
|
|
|
group = ConfigDB.from_string(setting.group)
|
2019-06-14 17:45:05 +02:00
|
|
|
|
2020-03-12 16:28:54 +01:00
|
|
|
default = Config.Holder.config(group, key)
|
2020-01-25 16:42:04 +01:00
|
|
|
value = ConfigDB.from_binary(setting.value)
|
|
|
|
|
|
|
|
merged_value =
|
2020-03-12 16:28:54 +01:00
|
|
|
cond do
|
|
|
|
Ecto.get_meta(setting, :state) == :deleted -> default
|
|
|
|
can_be_merged?(default, value) -> ConfigDB.merge_group(group, key, default, value)
|
|
|
|
true -> value
|
2020-01-25 16:42:04 +01:00
|
|
|
end
|
2020-01-17 09:45:44 +01:00
|
|
|
|
2020-01-22 13:14:11 +01:00
|
|
|
:ok = update_env(group, key, merged_value)
|
2020-01-17 14:28:44 +01:00
|
|
|
|
2020-02-11 08:12:57 +01:00
|
|
|
group_for_restart(group, key, value, merged_value)
|
2019-06-14 17:45:05 +02:00
|
|
|
rescue
|
2020-01-22 13:14:11 +01:00
|
|
|
error ->
|
|
|
|
error_msg =
|
|
|
|
"updating env causes error, group: " <>
|
|
|
|
inspect(setting.group) <>
|
|
|
|
" key: " <>
|
|
|
|
inspect(setting.key) <>
|
|
|
|
" value: " <>
|
|
|
|
inspect(ConfigDB.from_binary(setting.value)) <> " error: " <> inspect(error)
|
|
|
|
|
|
|
|
Logger.warn(error_msg)
|
2019-09-29 10:17:38 +02:00
|
|
|
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-25 16:42:04 +01: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 13:14:11 +01:00
|
|
|
|
2020-01-25 16:42:04 +01:00
|
|
|
defp group_and_key_need_reboot?(group, key) do
|
|
|
|
Enum.any?(@reboot_time_keys, fn {g, k} -> g == group and k == key end)
|
|
|
|
end
|
2020-01-22 13:14:11 +01:00
|
|
|
|
2020-01-25 16:42:04 +01:00
|
|
|
defp group_and_subkey_need_reboot?(group, key, value) do
|
|
|
|
Keyword.keyword?(value) and
|
|
|
|
Enum.any?(@reboot_time_subkeys, fn {g, k, subkeys} ->
|
|
|
|
g == group and k == key and
|
|
|
|
Enum.any?(Keyword.keys(value), &(&1 in subkeys))
|
|
|
|
end)
|
2020-01-22 13:14:11 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
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-02-08 10:55:37 +01:00
|
|
|
defp restart(_, :pleroma, env), do: Restarter.Pleroma.restart_after_boot(env)
|
2020-01-25 16:42:04 +01:00
|
|
|
|
|
|
|
defp restart(started_applications, app, _) do
|
2019-09-29 10:17:38 +02:00
|
|
|
with {^app, _, _} <- List.keyfind(started_applications, app, 0),
|
|
|
|
:ok <- Application.stop(app) do
|
|
|
|
:ok = Application.start(app)
|
|
|
|
else
|
2020-01-21 15:49:22 +01:00
|
|
|
nil ->
|
|
|
|
Logger.warn("#{app} is not started.")
|
|
|
|
|
|
|
|
error ->
|
|
|
|
error
|
|
|
|
|> inspect()
|
|
|
|
|> Logger.warn()
|
2019-06-14 17:45:05 +02:00
|
|
|
end
|
|
|
|
end
|
2020-01-17 09:45:44 +01: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 17:45:05 +02:00
|
|
|
end
|