2019-07-09 23:13:23 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# 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-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
|
|
|
|
2019-08-14 09:55:17 -06:00
|
|
|
def start_link(_) do
|
2019-06-14 09:45:05 -06:00
|
|
|
load_and_update_env()
|
2019-09-29 02:17:38 -06:00
|
|
|
if Pleroma.Config.get(:env) == :test, do: Ecto.Adapters.SQL.Sandbox.checkin(Repo)
|
2019-06-14 09:45:05 -06:00
|
|
|
:ignore
|
|
|
|
end
|
|
|
|
|
2020-01-22 05:14:11 -07:00
|
|
|
@spec load_and_update_env([ConfigDB.t()]) :: :ok | false
|
|
|
|
def load_and_update_env(deleted \\ []) do
|
2020-01-15 07:10:33 -07:00
|
|
|
with true <- Pleroma.Config.get(:configurable_from_database),
|
2019-09-29 02:17:38 -06:00
|
|
|
true <- Ecto.Adapters.SQL.table_exists?(Repo, "config"),
|
|
|
|
started_applications <- Application.started_applications() 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
|
|
|
in_db = Repo.all(ConfigDB)
|
|
|
|
|
|
|
|
with_deleted = in_db ++ deleted
|
|
|
|
|
|
|
|
with_deleted
|
|
|
|
|> Enum.map(&merge_and_update(&1))
|
2019-09-29 02:17:38 -06:00
|
|
|
|> Enum.uniq()
|
|
|
|
# TODO: some problem with prometheus after restart!
|
|
|
|
|> Enum.reject(&(&1 in [:pleroma, nil, :prometheus]))
|
|
|
|
|> Enum.each(&restart(started_applications, &1))
|
2020-01-22 05:14:11 -07:00
|
|
|
|
|
|
|
:ok
|
2019-06-14 09:45:05 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-22 05:14:11 -07:00
|
|
|
defp merge_and_update(setting) do
|
2019-06-14 09:45:05 -06:00
|
|
|
try do
|
2020-01-15 22:50:27 -07:00
|
|
|
key = ConfigDB.from_string(setting.key)
|
|
|
|
group = ConfigDB.from_string(setting.group)
|
2019-06-14 09:45:05 -06:00
|
|
|
|
2020-01-21 07:49:22 -07:00
|
|
|
default = Pleroma.Config.Holder.config(group, key)
|
2020-01-22 05:14:11 -07:00
|
|
|
merged_value = merge_value(setting, default, group, key)
|
2020-01-17 01:45:44 -07:00
|
|
|
|
2020-01-22 05:14:11 -07:00
|
|
|
:ok = update_env(group, key, merged_value)
|
2020-01-17 06:28:44 -07:00
|
|
|
|
2020-01-21 07:49:22 -07:00
|
|
|
if group != :logger do
|
|
|
|
group
|
|
|
|
else
|
|
|
|
# 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)
|
2020-01-17 06:28:44 -07:00
|
|
|
else
|
2020-01-21 07:49:22 -07:00
|
|
|
Logger.configure([{key, merged_value}])
|
2020-01-17 06:28:44 -07:00
|
|
|
end
|
2020-01-21 07:49:22 -07:00
|
|
|
|
|
|
|
nil
|
2020-01-17 01:45:44 -07:00
|
|
|
end
|
2019-06-14 09:45:05 -06:00
|
|
|
rescue
|
2020-01-22 05:14:11 -07: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 02:17:38 -06:00
|
|
|
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-22 05:14:11 -07:00
|
|
|
defp merge_value(%{__meta__: %{state: :deleted}}, default, _group, _key), do: default
|
|
|
|
|
|
|
|
defp merge_value(setting, default, group, key) do
|
|
|
|
value = ConfigDB.from_binary(setting.value)
|
|
|
|
|
|
|
|
if can_be_merged?(default, value) do
|
|
|
|
ConfigDB.merge_group(group, key, default, value)
|
|
|
|
else
|
|
|
|
value
|
|
|
|
end
|
|
|
|
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)
|
|
|
|
|
2019-09-29 02:17:38 -06:00
|
|
|
defp restart(started_applications, app) do
|
|
|
|
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
|