we can't change module attributes and endpoint settings in runtimestable
parent
b7fc722a2e
commit
c2ca1f22a2
@ -0,0 +1,68 @@
|
||||
defmodule Mix.Tasks.Pleroma.Config do
|
||||
use Mix.Task
|
||||
alias Mix.Tasks.Pleroma.Common
|
||||
alias Pleroma.Repo
|
||||
alias Pleroma.Web.AdminAPI.Config
|
||||
@shortdoc "Manages the location of the config"
|
||||
@moduledoc """
|
||||
Manages the location of the config.
|
||||
|
||||
## Transfers config from file to DB.
|
||||
|
||||
mix pleroma.config migrate_to_db
|
||||
|
||||
## Transfers config from DB to file.
|
||||
|
||||
mix pleroma.config migrate_from_db ENV
|
||||
"""
|
||||
|
||||
def run(["migrate_to_db"]) do
|
||||
Common.start_pleroma()
|
||||
|
||||
if Pleroma.Config.get([:instance, :dynamic_configuration]) do
|
||||
Application.get_all_env(:pleroma)
|
||||
|> Enum.reject(fn {k, _v} -> k in [Pleroma.Repo, :env] end)
|
||||
|> Enum.each(fn {k, v} ->
|
||||
key = to_string(k) |> String.replace("Elixir.", "")
|
||||
{:ok, _} = Config.update_or_create(%{key: key, value: v})
|
||||
Mix.shell().info("#{key} is migrated.")
|
||||
end)
|
||||
|
||||
Mix.shell().info("Settings migrated.")
|
||||
else
|
||||
Mix.shell().info(
|
||||
"Migration is not allowed by config. You can change this behavior in instance settings."
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
def run(["migrate_from_db", env]) do
|
||||
Common.start_pleroma()
|
||||
|
||||
if Pleroma.Config.get([:instance, :dynamic_configuration]) do
|
||||
config_path = "config/#{env}.migrated.secret.exs"
|
||||
|
||||
{:ok, file} = File.open(config_path, [:write])
|
||||
|
||||
Repo.all(Config)
|
||||
|> Enum.each(fn config ->
|
||||
mark = if String.starts_with?(config.key, "Pleroma."), do: ",", else: ":"
|
||||
|
||||
IO.write(
|
||||
file,
|
||||
"config :pleroma, #{config.key}#{mark} #{inspect(Config.from_binary(config.value))}\r\n"
|
||||
)
|
||||
|
||||
{:ok, _} = Repo.delete(config)
|
||||
Mix.shell().info("#{config.key} deleted from DB.")
|
||||
end)
|
||||
|
||||
File.close(file)
|
||||
System.cmd("mix", ["format", config_path])
|
||||
else
|
||||
Mix.shell().info(
|
||||
"Migration is not allowed by config. You can change this behavior in instance settings."
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,41 @@
|
||||
defmodule Pleroma.Config.TransferTask do
|
||||
use Task
|
||||
alias Pleroma.Web.AdminAPI.Config
|
||||
|
||||
def start_link do
|
||||
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
|
||||
if Pleroma.Config.get([:instance, :dynamic_configuration]) do
|
||||
Pleroma.Repo.all(Config)
|
||||
|> Enum.each(&update_env(&1))
|
||||
end
|
||||
end
|
||||
|
||||
defp update_env(setting) do
|
||||
try do
|
||||
key =
|
||||
if String.starts_with?(setting.key, "Pleroma.") do
|
||||
"Elixir." <> setting.key
|
||||
else
|
||||
setting.key
|
||||
end
|
||||
|
||||
Application.put_env(
|
||||
:pleroma,
|
||||
String.to_existing_atom(key),
|
||||
Config.from_binary(setting.value)
|
||||
)
|
||||
rescue
|
||||
e ->
|
||||
require Logger
|
||||
|
||||
Logger.warn(
|
||||
"updating env causes error, key: #{inspect(setting.key)}, error: #{inspect(e)}"
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,144 @@
|
||||
# Pleroma: A lightweight social networking server
|
||||
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
defmodule Pleroma.Web.AdminAPI.Config do
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
alias __MODULE__
|
||||
alias Pleroma.Repo
|
||||
|
||||
@type t :: %__MODULE__{}
|
||||
|
||||
schema "config" do
|
||||
field(:key, :string)
|
||||
field(:value, :binary)
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
@spec get_by_key(String.t()) :: Config.t() | nil
|
||||
def get_by_key(key), do: Repo.get_by(Config, key: key)
|
||||
|
||||
@spec changeset(Config.t(), map()) :: Changeset.t()
|
||||
def changeset(config, params \\ %{}) do
|
||||
config
|
||||
|> cast(params, [:key, :value])
|
||||
|> validate_required([:key, :value])
|
||||
|> unique_constraint(:key)
|
||||
end
|
||||
|
||||
@spec create(map()) :: {:ok, Config.t()} | {:error, Changeset.t()}
|
||||
def create(%{key: key, value: value}) do
|
||||
%Config{}
|
||||
|> changeset(%{key: key, value: transform(value)})
|
||||
|> Repo.insert()
|
||||
end
|
||||
|
||||
@spec update(Config.t(), map()) :: {:ok, Config} | {:error, Changeset.t()}
|
||||
def update(%Config{} = config, %{value: value}) do
|
||||
config
|
||||
|> change(value: transform(value))
|
||||
|> Repo.update()
|
||||
end
|
||||
|
||||
@spec update_or_create(map()) :: {:ok, Config.t()} | {:error, Changeset.t()}
|
||||
def update_or_create(%{key: key} = params) do
|
||||
with %Config{} = config <- Config.get_by_key(key) do
|
||||
Config.update(config, params)
|
||||
else
|
||||
nil -> Config.create(params)
|
||||
end
|
||||
end
|
||||
|
||||
@spec delete(String.t()) :: {:ok, Config.t()} | {:error, Changeset.t()}
|
||||
def delete(key) do
|
||||
with %Config{} = config <- Config.get_by_key(key) do
|
||||
Repo.delete(config)
|
||||
else
|
||||
nil -> {:error, "Config with key #{key} not found"}
|
||||
end
|
||||
end
|
||||
|
||||
@spec from_binary(binary()) :: term()
|
||||
def from_binary(value), do: :erlang.binary_to_term(value)
|
||||
|
||||
@spec from_binary_to_map(binary()) :: any()
|
||||
def from_binary_to_map(binary) do
|
||||
from_binary(binary)
|
||||
|> do_convert()
|
||||
end
|
||||
|
||||
defp do_convert([{k, v}] = value) when is_list(value) and length(value) == 1,
|
||||
do: %{k => do_convert(v)}
|
||||
|
||||
defp do_convert(values) when is_list(values), do: for(val <- values, do: do_convert(val))
|
||||
|
||||
defp do_convert({k, v} = value) when is_tuple(value),
|
||||
do: %{k => do_convert(v)}
|
||||
|
||||
defp do_convert(value) when is_binary(value) or is_atom(value) or is_map(value),
|
||||
do: value
|
||||
|
||||
@spec transform(any()) :: binary()
|
||||
def transform(entity) when is_map(entity) do
|
||||
tuples =
|
||||
for {k, v} <- entity,
|
||||
into: [],
|
||||
do: {if(is_atom(k), do: k, else: String.to_atom(k)), do_transform(v)}
|
||||
|
||||
Enum.reject(tuples, fn {_k, v} -> is_nil(v) end)
|
||||
|> Enum.sort()
|
||||
|> :erlang.term_to_binary()
|
||||
end
|
||||
|
||||
def transform(entity) when is_list(entity) do
|
||||
list = Enum.map(entity, &do_transform(&1))
|
||||
:erlang.term_to_binary(list)
|
||||
end
|
||||
|
||||
def transform(entity), do: :erlang.term_to_binary(entity)
|
||||
|
||||
defp do_transform(%Regex{} = value) when is_map(value), do: value
|
||||
|
||||
defp do_transform(value) when is_map(value) do
|
||||
values =
|
||||
for {key, val} <- value,
|
||||
into: [],
|
||||
do: {String.to_atom(key), do_transform(val)}
|
||||
|
||||
Enum.sort(values)
|
||||
end
|
||||
|
||||
defp do_transform(value) when is_list(value) do
|
||||
Enum.map(value, &do_transform(&1))
|
||||
end
|
||||
|
||||
defp do_transform(entity) when is_list(entity) and length(entity) == 1, do: hd(entity)
|
||||
|
||||
defp do_transform(value) when is_binary(value) do
|
||||
value = String.trim(value)
|
||||
|
||||
case String.length(value) do
|
||||
0 ->
|
||||
nil
|
||||
|
||||
_ ->
|
||||
cond do
|
||||
String.starts_with?(value, "Pleroma") ->
|
||||
String.to_existing_atom("Elixir." <> value)
|
||||
|
||||
String.starts_with?(value, ":") ->
|
||||
String.replace(value, ":", "") |> String.to_existing_atom()
|
||||
|
||||
String.starts_with?(value, "i:") ->
|
||||
String.replace(value, "i:", "") |> String.to_integer()
|
||||
|
||||
true ->
|
||||
value
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp do_transform(value), do: value
|
||||
end
|
@ -0,0 +1,16 @@
|
||||
defmodule Pleroma.Web.AdminAPI.ConfigView do
|
||||
use Pleroma.Web, :view
|
||||
|
||||
def render("index.json", %{configs: configs}) do
|
||||
%{
|
||||
configs: render_many(configs, __MODULE__, "show.json", as: :config)
|
||||
}
|
||||
end
|
||||
|
||||
def render("show.json", %{config: config}) do
|
||||
%{
|
||||
key: config.key,
|
||||
value: Pleroma.Web.AdminAPI.Config.from_binary_to_map(config.value)
|
||||
}
|
||||
end
|
||||
end
|
@ -0,0 +1,13 @@
|
||||
defmodule Pleroma.Repo.Migrations.CreateConfig do
|
||||
use Ecto.Migration
|
||||
|
||||
def change do
|
||||
create table(:config) do
|
||||
add(:key, :string)
|
||||
add(:value, :binary)
|
||||
timestamps()
|
||||
end
|
||||
|
||||
create(unique_index(:config, :key))
|
||||
end
|
||||
end
|
@ -0,0 +1,35 @@
|
||||
defmodule Pleroma.Config.TransferTaskTest do
|
||||
use Pleroma.DataCase
|
||||
|
||||
setup do
|
||||
dynamic = Pleroma.Config.get([:instance, :dynamic_configuration])
|
||||
|
||||
Pleroma.Config.put([:instance, :dynamic_configuration], true)
|
||||
|
||||
on_exit(fn ->
|
||||
Pleroma.Config.put([:instance, :dynamic_configuration], dynamic)
|
||||
end)
|
||||
end
|
||||
|
||||
test "transfer config values from db to env" do
|
||||
refute Application.get_env(:pleroma, :test_key)
|
||||
Pleroma.Web.AdminAPI.Config.create(%{key: "test_key", value: [live: 2, com: 3]})
|
||||
|
||||
Pleroma.Config.TransferTask.start_link()
|
||||
|
||||
assert Application.get_env(:pleroma, :test_key) == [live: 2, com: 3]
|
||||
|
||||
on_exit(fn ->
|
||||
Application.delete_env(:pleroma, :test_key)
|
||||
end)
|
||||
end
|
||||
|
||||
test "non existing atom" do
|
||||
Pleroma.Web.AdminAPI.Config.create(%{key: "undefined_atom_key", value: [live: 2, com: 3]})
|
||||
|
||||
assert ExUnit.CaptureLog.capture_log(fn ->
|
||||
Pleroma.Config.TransferTask.start_link()
|
||||
end) =~
|
||||
"updating env causes error, key: \"undefined_atom_key\", error: %ArgumentError{message: \"argument error\"}"
|
||||
end
|
||||
end
|
@ -0,0 +1,54 @@
|
||||
defmodule Mix.Tasks.Pleroma.ConfigTest do
|
||||
use Pleroma.DataCase
|
||||
alias Pleroma.Repo
|
||||
alias Pleroma.Web.AdminAPI.Config
|
||||
|
||||
setup_all do
|
||||
Mix.shell(Mix.Shell.Process)
|
||||
temp_file = "config/temp.migrated.secret.exs"
|
||||
|
||||
dynamic = Pleroma.Config.get([:instance, :dynamic_configuration])
|
||||
|
||||
Pleroma.Config.put([:instance, :dynamic_configuration], true)
|
||||
|
||||
on_exit(fn ->
|
||||
Mix.shell(Mix.Shell.IO)
|
||||
Application.delete_env(:pleroma, :first_setting)
|
||||
Application.delete_env(:pleroma, :second_setting)
|
||||
Pleroma.Config.put([:instance, :dynamic_configuration], dynamic)
|
||||
:ok = File.rm(temp_file)
|
||||
end)
|
||||
|
||||
{:ok, temp_file: temp_file}
|
||||
end
|
||||
|
||||
test "settings are migrated to db" do
|
||||
assert Repo.all(Config) == []
|
||||
|
||||
Application.put_env(:pleroma, :first_setting, key: "value", key2: [Pleroma.Repo])
|
||||
Application.put_env(:pleroma, :second_setting, key: "value2", key2: [Pleroma.Activity])
|
||||
|
||||
Mix.Tasks.Pleroma.Config.run(["migrate_to_db"])
|
||||
|
||||
first_db = Config.get_by_key("first_setting")
|
||||
second_db = Config.get_by_key("second_setting")
|
||||
refute Config.get_by_key("Pleroma.Repo")
|
||||
|
||||
assert Config.from_binary(first_db.value) == [key: "value", key2: [Pleroma.Repo]]
|
||||
assert Config.from_binary(second_db.value) == [key: "value2", key2: [Pleroma.Activity]]
|
||||
end
|
||||
|
||||
test "settings are migrated to file and deleted from db", %{temp_file: temp_file} do
|
||||
Config.create(%{key: "setting_first", value: [key: "value", key2: [Pleroma.Activity]]})
|
||||
Config.create(%{key: "setting_second", value: [key: "valu2", key2: [Pleroma.Repo]]})
|
||||
|
||||
Mix.Tasks.Pleroma.Config.run(["migrate_from_db", "temp"])
|
||||
|
||||
assert Repo.all(Config) == []
|
||||
assert File.exists?(temp_file)
|
||||
{:ok, file} = File.read(temp_file)
|
||||
|
||||
assert file =~ "config :pleroma, setting_first:"
|
||||
assert file =~ "config :pleroma, setting_second:"
|
||||
end
|
||||
end
|
@ -0,0 +1,183 @@
|
||||
defmodule Pleroma.Web.AdminAPI.ConfigTest do
|
||||
use Pleroma.DataCase, async: true
|
||||
import Pleroma.Factory
|
||||
alias Pleroma.Web.AdminAPI.Config
|
||||
|
||||
test "get_by_key/1" do
|
||||
config = insert(:config)
|
||||
insert(:config)
|
||||
|
||||
assert config == Config.get_by_key(config.key)
|
||||
end
|
||||
|
||||
test "create/1" do
|
||||
{:ok, config} = Config.create(%{key: "some_key", value: "some_value"})
|
||||
assert config == Config.get_by_key("some_key")
|
||||
end
|
||||
|
||||
test "update/1" do
|
||||
config = insert(:config)
|
||||
{:ok, updated} = Config.update(config, %{value: "some_value"})
|
||||
loaded = Config.get_by_key(config.key)
|
||||
assert loaded == updated
|
||||
end
|
||||
|
||||
test "update_or_create/1" do
|
||||
config = insert(:config)
|
||||
key2 = "another_key"
|
||||
|
||||
params = [
|
||||
%{key: key2, value: "another_value"},
|
||||
%{key: config.key, value: "new_value"}
|
||||
]
|
||||
|
||||
assert Repo.all(Config) |> length() == 1
|
||||
|
||||
Enum.each(params, &Config.update_or_create(&1))
|
||||
|
||||
assert Repo.all(Config) |> length() == 2
|
||||
|
||||
config1 = Config.get_by_key(config.key)
|
||||
config2 = Config.get_by_key(key2)
|
||||
|
||||
assert config1.value == Config.transform("new_value")
|
||||
assert config2.value == Config.transform("another_value")
|
||||
end
|
||||
|
||||
test "delete/1" do
|
||||
config = insert(:config)
|
||||
{:ok, _} = Config.delete(config.key)
|
||||
refute Config.get_by_key(config.key)
|
||||
end
|
||||
|
||||
describe "transform/1" do
|
||||
test "string" do
|
||||
binary = Config.transform("value as string")
|
||||
assert binary == :erlang.term_to_binary("value as string")
|
||||
assert Config.from_binary(binary) == "value as string"
|
||||
end
|
||||
|
||||
test "list of modules" do
|
||||
binary = Config.transform(["Pleroma.Repo", "Pleroma.Activity"])
|
||||
assert binary == :erlang.term_to_binary([Pleroma.Repo, Pleroma.Activity])
|
||||
assert Config.from_binary(binary) == [Pleroma.Repo, Pleroma.Activity]
|
||||
end
|
||||
|
||||
test "list of strings" do
|
||||
binary = Config.transform(["string1", "string2"])
|
||||
assert binary == :erlang.term_to_binary(["string1", "string2"])
|
||||
assert Config.from_binary(binary) == ["string1", "string2"]
|
||||
end
|
||||
|
||||
test "map" do
|
||||
binary =
|
||||
Config.transform(%{
|
||||
"types" => "Pleroma.PostgresTypes",
|
||||
"telemetry_event" => ["Pleroma.Repo.Instrumenter"],
|
||||
"migration_lock" => ""
|
||||
})
|
||||
|
||||
assert binary ==
|
||||
:erlang.term_to_binary(
|
||||
telemetry_event: [Pleroma.Repo.Instrumenter],
|
||||
types: Pleroma.PostgresTypes
|
||||
)
|
||||
|
||||
assert Config.from_binary(binary) == [
|
||||
telemetry_event: [Pleroma.Repo.Instrumenter],
|
||||
types: Pleroma.PostgresTypes
|
||||
]
|
||||
end
|
||||
|
||||
test "complex map with nested integers, lists and atoms" do
|
||||
binary =
|
||||
Config.transform(%{
|
||||
"uploader" => "Pleroma.Uploaders.Local",
|
||||
"filters" => ["Pleroma.Upload.Filter.Dedupe"],
|
||||
"link_name" => ":true",
|
||||
"proxy_remote" => ":false",
|
||||
"proxy_opts" => %{
|
||||
"redirect_on_failure" => ":false",
|
||||
"max_body_length" => "i:1048576",
|
||||
"http" => %{
|
||||
"follow_redirect" => ":true",
|
||||
"pool" => ":upload"
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
assert binary ==
|
||||
:erlang.term_to_binary(
|
||||
filters: [Pleroma.Upload.Filter.Dedupe],
|
||||
link_name: true,
|
||||
proxy_opts: [
|
||||
http: [
|
||||
follow_redirect: true,
|
||||
pool: :upload
|
||||
],
|
||||
max_body_length: 1_048_576,
|
||||
redirect_on_failure: false
|
||||
],
|
||||
proxy_remote: false,
|
||||
uploader: Pleroma.Uploaders.Local
|
||||
)
|
||||
|
||||
assert Config.from_binary(binary) ==
|
||||
[
|
||||
filters: [Pleroma.Upload.Filter.Dedupe],
|
||||
link_name: true,
|
||||
proxy_opts: [
|
||||
http: [
|
||||
follow_redirect: true,
|
||||
pool: :upload
|
||||
],
|
||||
max_body_length: 1_048_576,
|
||||
redirect_on_failure: false
|
||||
],
|
||||
proxy_remote: false,
|
||||
uploader: Pleroma.Uploaders.Local
|
||||
]
|
||||
end
|
||||
|
||||
test "keyword" do
|
||||
binary =
|
||||
Config.transform(%{
|
||||
"level" => ":warn",
|
||||
"meta" => [":all"],
|
||||
"webhook_url" => "https://hooks.slack.com/services/YOUR-KEY-HERE"
|
||||
})
|
||||
|
||||
assert binary ==
|
||||
:erlang.term_to_binary(
|
||||
level: :warn,
|
||||
meta: [:all],
|
||||
webhook_url: "https://hooks.slack.com/services/YOUR-KEY-HERE"
|
||||
)
|
||||
|
||||
assert Config.from_binary(binary) == [
|
||||
level: :warn,
|
||||
meta: [:all],
|
||||
webhook_url: "https://hooks.slack.com/services/YOUR-KEY-HERE"
|
||||
]
|
||||
end
|
||||
|
||||
test "complex map with sigil" do
|
||||
binary =
|
||||
Config.transform(%{
|
||||
federated_timeline_removal: [],
|
||||
reject: [~r/comp[lL][aA][iI][nN]er/],
|
||||
replace: []
|
||||
})
|
||||
|
||||
assert binary ==
|
||||
:erlang.term_to_binary(
|
||||
federated_timeline_removal: [],
|
||||
reject: [~r/comp[lL][aA][iI][nN]er/],
|
||||
replace: []
|
||||
)
|
||||
|
||||
assert Config.from_binary(binary) ==
|
||||
[federated_timeline_removal: [], reject: [~r/comp[lL][aA][iI][nN]er/], replace: []]
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in new issue