2019-07-10 05:13:23 +00: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 15:45:05 +00:00
|
|
|
defmodule Pleroma.Config.TransferTask do
|
|
|
|
use Task
|
2019-09-29 08:17:38 +00:00
|
|
|
|
2020-01-16 05:50:27 +00:00
|
|
|
alias Pleroma.ConfigDB
|
2019-09-29 08:17:38 +00:00
|
|
|
alias Pleroma.Repo
|
2020-01-16 05:50:27 +00:00
|
|
|
|
|
|
|
require Logger
|
2019-06-14 15:45:05 +00:00
|
|
|
|
2019-08-14 15:55:17 +00:00
|
|
|
def start_link(_) do
|
2019-06-14 15:45:05 +00:00
|
|
|
load_and_update_env()
|
2019-09-29 08:17:38 +00:00
|
|
|
if Pleroma.Config.get(:env) == :test, do: Ecto.Adapters.SQL.Sandbox.checkin(Repo)
|
2019-06-14 15:45:05 +00:00
|
|
|
:ignore
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_and_update_env do
|
2020-01-15 14:10:33 +00:00
|
|
|
with true <- Pleroma.Config.get(:configurable_from_database),
|
2019-09-29 08:17:38 +00:00
|
|
|
true <- Ecto.Adapters.SQL.table_exists?(Repo, "config"),
|
|
|
|
started_applications <- Application.started_applications() do
|
2019-06-23 05:16:16 +00:00
|
|
|
# We need to restart applications for loaded settings take effect
|
2020-01-16 05:50:27 +00:00
|
|
|
ConfigDB
|
2019-09-29 08:17:38 +00:00
|
|
|
|> Repo.all()
|
|
|
|
|> Enum.map(&update_env(&1))
|
|
|
|
|> Enum.uniq()
|
|
|
|
# TODO: some problem with prometheus after restart!
|
|
|
|
|> Enum.reject(&(&1 in [:pleroma, nil, :prometheus]))
|
|
|
|
|> Enum.each(&restart(started_applications, &1))
|
2019-06-14 15:45:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp update_env(setting) do
|
|
|
|
try do
|
2020-01-16 05:50:27 +00:00
|
|
|
key = ConfigDB.from_string(setting.key)
|
|
|
|
group = ConfigDB.from_string(setting.group)
|
|
|
|
value = ConfigDB.from_binary(setting.value)
|
2019-06-14 15:45:05 +00:00
|
|
|
|
2019-12-10 09:00:40 +00:00
|
|
|
if group != :phoenix and key != :serve_endpoints do
|
2020-01-17 08:45:44 +00:00
|
|
|
default = Pleroma.Config.Holder.config(group, key)
|
|
|
|
|
|
|
|
merged_value =
|
|
|
|
if can_be_merged?(default, value) do
|
|
|
|
DeepMerge.deep_merge(default, value)
|
|
|
|
else
|
|
|
|
value
|
|
|
|
end
|
2019-06-23 05:16:16 +00:00
|
|
|
|
2020-01-17 08:45:44 +00:00
|
|
|
:ok = Application.put_env(group, key, merged_value)
|
|
|
|
group
|
|
|
|
end
|
2019-06-14 15:45:05 +00:00
|
|
|
rescue
|
|
|
|
e ->
|
|
|
|
Logger.warn(
|
|
|
|
"updating env causes error, key: #{inspect(setting.key)}, error: #{inspect(e)}"
|
|
|
|
)
|
2019-09-29 08:17:38 +00:00
|
|
|
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp restart(started_applications, app) do
|
|
|
|
with {^app, _, _} <- List.keyfind(started_applications, app, 0),
|
|
|
|
:ok <- Application.stop(app) do
|
|
|
|
:ok = Application.start(app)
|
|
|
|
else
|
|
|
|
nil -> Logger.warn("#{app} is not started.")
|
|
|
|
error -> Logger.warn(inspect(error))
|
2019-06-14 15:45:05 +00:00
|
|
|
end
|
|
|
|
end
|
2020-01-17 08:45:44 +00:00
|
|
|
|
|
|
|
defp can_be_merged?(val1, val2) when is_map(val1) and is_map(val2), do: true
|
|
|
|
|
|
|
|
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 15:45:05 +00:00
|
|
|
end
|