2019-07-10 01:13:23 -04: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 11:45:05 -04:00
|
|
|
defmodule Pleroma.Config.TransferTask do
|
|
|
|
use Task
|
|
|
|
alias Pleroma.Web.AdminAPI.Config
|
|
|
|
|
2019-08-14 11:55:17 -04:00
|
|
|
def start_link(_) do
|
2019-06-14 11:45:05 -04:00
|
|
|
load_and_update_env()
|
|
|
|
if Pleroma.Config.get(:env) == :test, do: Ecto.Adapters.SQL.Sandbox.checkin(Pleroma.Repo)
|
|
|
|
:ignore
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_and_update_env do
|
2019-06-16 06:01:15 -04:00
|
|
|
if Pleroma.Config.get([:instance, :dynamic_configuration]) and
|
|
|
|
Ecto.Adapters.SQL.table_exists?(Pleroma.Repo, "config") do
|
2019-06-23 01:16:16 -04:00
|
|
|
for_restart =
|
|
|
|
Pleroma.Repo.all(Config)
|
|
|
|
|> Enum.map(&update_env(&1))
|
|
|
|
|
|
|
|
# We need to restart applications for loaded settings take effect
|
|
|
|
for_restart
|
|
|
|
|> Enum.reject(&(&1 in [:pleroma, :ok]))
|
|
|
|
|> Enum.each(fn app ->
|
|
|
|
Application.stop(app)
|
|
|
|
:ok = Application.start(app)
|
|
|
|
end)
|
2019-06-14 11:45:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp update_env(setting) do
|
|
|
|
try do
|
|
|
|
key =
|
|
|
|
if String.starts_with?(setting.key, "Pleroma.") do
|
|
|
|
"Elixir." <> setting.key
|
|
|
|
else
|
2019-07-15 04:00:55 -04:00
|
|
|
String.trim_leading(setting.key, ":")
|
2019-06-14 11:45:05 -04:00
|
|
|
end
|
|
|
|
|
2019-06-23 01:16:16 -04:00
|
|
|
group = String.to_existing_atom(setting.group)
|
|
|
|
|
2019-06-14 11:45:05 -04:00
|
|
|
Application.put_env(
|
2019-06-23 01:16:16 -04:00
|
|
|
group,
|
2019-06-14 11:45:05 -04:00
|
|
|
String.to_existing_atom(key),
|
|
|
|
Config.from_binary(setting.value)
|
|
|
|
)
|
2019-06-23 01:16:16 -04:00
|
|
|
|
|
|
|
group
|
2019-06-14 11:45:05 -04:00
|
|
|
rescue
|
|
|
|
e ->
|
|
|
|
require Logger
|
|
|
|
|
|
|
|
Logger.warn(
|
|
|
|
"updating env causes error, key: #{inspect(setting.key)}, error: #{inspect(e)}"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|