dynamic_configuration renaming

and moving it from instance settings
This commit is contained in:
Alexander Strizhakov 2020-01-10 19:34:19 +03:00
parent 958d0452e4
commit 7d128ca208
No known key found for this signature in database
GPG Key ID: 022896A53AEF1381
13 changed files with 59 additions and 53 deletions

View File

@ -269,7 +269,6 @@
remote_post_retention_days: 90, remote_post_retention_days: 90,
skip_thread_containment: true, skip_thread_containment: true,
limit_to_local_content: :unauthenticated, limit_to_local_content: :unauthenticated,
dynamic_configuration: false,
user_bio_length: 5000, user_bio_length: 5000,
user_name_length: 100, user_name_length: 100,
max_account_fields: 10, max_account_fields: 10,
@ -623,6 +622,8 @@
config :pleroma, :modules, runtime_dir: "instance/modules" config :pleroma, :modules, runtime_dir: "instance/modules"
config :pleroma, configurable_from_database: false
config :swarm, node_blacklist: [~r/myhtml_.*$/] config :swarm, node_blacklist: [~r/myhtml_.*$/]
# Import environment specific config. This must remain at the bottom # Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above. # of this file so it overrides the configuration defined above.

View File

@ -867,12 +867,6 @@
false false
] ]
}, },
%{
key: :dynamic_configuration,
type: :boolean,
description:
"Allow transferring configuration to DB with the subsequent customization from Admin api. Defaults to `false`"
},
%{ %{
key: :max_account_fields, key: :max_account_fields,
type: :integer, type: :integer,
@ -3111,5 +3105,18 @@
description: "A path to custom Elixir modules (such as MRF policies)." description: "A path to custom Elixir modules (such as MRF policies)."
} }
] ]
},
%{
group: :pleroma,
type: :group,
description: "Allow instance configuration from database.",
children: [
%{
key: :configurable_from_database,
type: :boolean,
description:
"Allow transferring configuration to DB with the subsequent customization from Admin api. Defaults to `false`"
}
]
} }
] ]

View File

@ -684,7 +684,7 @@ Copies all settings from database to `config/{env}.exported_from_db.secret.exs`
### Get saved config settings ### Get saved config settings
**Only works when `:dynamic_configuration` is `true`.** **Only works when configuration from database is enabled.**
- Params: none - Params: none
- Response: - Response:
@ -708,7 +708,7 @@ Copies all settings from database to `config/{env}.exported_from_db.secret.exs`
### Update config settings ### Update config settings
**Only works when `:dynamic_configuration` is `true`.** **Only works when configuration from database is enabled.**
Some modifications are necessary to save the config settings correctly: Some modifications are necessary to save the config settings correctly:

View File

@ -1,9 +1,8 @@
# Configuring instance # Configuring instance
You can configure your instance from admin interface. You need account with admin rights and little change in config file, which will allow settings dynamic configuration from database. You can configure your instance from admin interface. You need account with admin rights and little change in config file, which will allow settings configuration from database.
```elixir ```elixir
config :pleroma, :instance, config :pleroma, configurable_from_database: true
dynamic_configuration: true
``` ```
## How it works ## How it works
@ -48,10 +47,9 @@ rm -rf config/prod.exported_from_db.exs
``` ```
*If you don't want to backup settings, you can skip step with `cp` command.* *If you don't want to backup settings, you can skip step with `cp` command.*
3. Set dynamic configuration to `false`. 3. Set configurable_from_database to `false`.
```elixir ```elixir
config :pleroma, :instance, config :pleroma, configurable_from_database: false
dynamic_configuration: false
``` ```
4. Restart pleroma instance 4. Restart pleroma instance
```bash ```bash

View File

@ -70,11 +70,6 @@ You shouldn't edit the base config directly to avoid breakages and merge conflic
* `account_field_value_length`: An account field value maximum length (default: `2048`). * `account_field_value_length`: An account field value maximum length (default: `2048`).
* `external_user_synchronization`: Enabling following/followers counters synchronization for external users. * `external_user_synchronization`: Enabling following/followers counters synchronization for external users.
!!! danger
This is a Work In Progress, not usable just yet
* `dynamic_configuration`: Allow transferring configuration to DB with the subsequent customization from Admin api.
## Federation ## Federation
### MRF policies ### MRF policies
@ -841,3 +836,7 @@ config :auto_linker,
## Custom Runtime Modules (`:modules`) ## Custom Runtime Modules (`:modules`)
* `runtime_dir`: A path to custom Elixir modules (such as MRF policies). * `runtime_dir`: A path to custom Elixir modules (such as MRF policies).
## :configurable_from_database
Enable/disable configuration from database.

View File

@ -30,7 +30,7 @@ defmodule Mix.Tasks.Pleroma.Config do
def run(["migrate_to_db"]) do def run(["migrate_to_db"]) do
start_pleroma() start_pleroma()
if Pleroma.Config.get([:instance, :dynamic_configuration]) do if Pleroma.Config.get([:configurable_from_database]) do
Enum.each(@groups, &load_and_create(&1)) Enum.each(@groups, &load_and_create(&1))
else else
Mix.shell().info( Mix.shell().info(
@ -48,7 +48,8 @@ def run(["migrate_from_db" | options]) do
aliases: [d: :delete_from_db] aliases: [d: :delete_from_db]
) )
with {:active?, true} <- {:active?, Pleroma.Config.get([:instance, :dynamic_configuration])}, with {:active?, true} <-
{:active?, Pleroma.Config.get([:configurable_from_database])},
env_path when is_binary(env_path) <- opts[:env], env_path when is_binary(env_path) <- opts[:env],
config_path <- "config/#{env_path}.exported_from_db.secret.exs", config_path <- "config/#{env_path}.exported_from_db.secret.exs",
{:ok, file} <- File.open(config_path, [:write, :utf8]) do {:ok, file} <- File.open(config_path, [:write, :utf8]) do

View File

@ -17,7 +17,7 @@ def start_link(_) do
end end
def load_and_update_env do def load_and_update_env do
with true <- Pleroma.Config.get([:instance, :dynamic_configuration]), with true <- Pleroma.Config.get([:configurable_from_database]),
true <- Ecto.Adapters.SQL.table_exists?(Repo, "config"), true <- Ecto.Adapters.SQL.table_exists?(Repo, "config"),
started_applications <- Application.started_applications() do started_applications <- Application.started_applications() do
# We need to restart applications for loaded settings take effect # We need to restart applications for loaded settings take effect

View File

@ -785,7 +785,7 @@ def config_descriptions(conn, _params) do
end end
def migrate_from_db(conn, _params) do def migrate_from_db(conn, _params) do
with :ok <- check_dynamic_configuration(conn) do with :ok <- configurable_from_database(conn) do
Mix.Tasks.Pleroma.Config.run([ Mix.Tasks.Pleroma.Config.run([
"migrate_from_db", "migrate_from_db",
"--env", "--env",
@ -798,7 +798,7 @@ def migrate_from_db(conn, _params) do
end end
def config_show(conn, _params) do def config_show(conn, _params) do
with :ok <- check_dynamic_configuration(conn) do with :ok <- configurable_from_database(conn) do
configs = Pleroma.Repo.all(Config) configs = Pleroma.Repo.all(Config)
if configs == [] do if configs == [] do
@ -812,7 +812,7 @@ def config_show(conn, _params) do
end end
def config_update(conn, %{"configs" => configs}) do def config_update(conn, %{"configs" => configs}) do
with :ok <- check_dynamic_configuration(conn) do with :ok <- configurable_from_database(conn) do
updated = updated =
Enum.map(configs, fn Enum.map(configs, fn
%{"group" => group, "key" => key, "delete" => true} = params -> %{"group" => group, "key" => key, "delete" => true} = params ->
@ -843,8 +843,8 @@ def config_update(conn, %{"configs" => configs}) do
end end
end end
defp check_dynamic_configuration(conn) do defp configurable_from_database(conn) do
if Pleroma.Config.get([:instance, :dynamic_configuration]) do if Pleroma.Config.get([:configurable_from_database]) do
:ok :ok
else else
errors(conn, {:error, "To use this endpoint you need to enable dynamic configuration."}) errors(conn, {:error, "To use this endpoint you need to enable dynamic configuration."})

View File

@ -20,8 +20,7 @@ config :pleroma, :instance,
email: "<%= email %>", email: "<%= email %>",
notify_email: "<%= notify_email %>", notify_email: "<%= notify_email %>",
limit: 5000, limit: 5000,
registrations_open: true, registrations_open: true
dynamic_configuration: <%= db_configurable? %>
config :pleroma, :media_proxy, config :pleroma, :media_proxy,
enabled: false, enabled: false,
@ -70,3 +69,5 @@ config :pleroma, Pleroma.Uploaders.Local, uploads: "<%= uploads_dir %>"
# host: "s3.wasabisys.com" # host: "s3.wasabisys.com"
config :joken, default_signer: "<%= jwt_secret %>" config :joken, default_signer: "<%= jwt_secret %>"
config :pleroma, configurable_from_database: <%= db_configurable? %>

View File

@ -7,8 +7,8 @@ defmodule Pleroma.Config.TransferTaskTest do
alias Pleroma.Web.AdminAPI.Config alias Pleroma.Web.AdminAPI.Config
clear_config([:instance, :dynamic_configuration]) do clear_config([:configurable_from_database]) do
Pleroma.Config.put([:instance, :dynamic_configuration], true) Pleroma.Config.put([:configurable_from_database], true)
end end
test "transfer config values from db to env" do test "transfer config values from db to env" do

View File

@ -19,8 +19,8 @@ defmodule Mix.Tasks.Pleroma.ConfigTest do
:ok :ok
end end
clear_config_all([:instance, :dynamic_configuration]) do clear_config_all([:configurable_from_database]) do
Pleroma.Config.put([:instance, :dynamic_configuration], true) Pleroma.Config.put([:configurable_from_database], true)
end end
test "settings are migrated to db" do test "settings are migrated to db" do
@ -127,7 +127,6 @@ test "load a settings with large values and pass to file", %{temp_file: temp_fil
remote_post_retention_days: 90, remote_post_retention_days: 90,
skip_thread_containment: true, skip_thread_containment: true,
limit_to_local_content: :unauthenticated, limit_to_local_content: :unauthenticated,
dynamic_configuration: false,
user_bio_length: 5000, user_bio_length: 5000,
user_name_length: 100, user_name_length: 100,
max_account_fields: 10, max_account_fields: 10,
@ -157,7 +156,7 @@ test "load a settings with large values and pass to file", %{temp_file: temp_fil
{:ok, file} = File.read(temp_file) {:ok, file} = File.read(temp_file)
assert file == assert file ==
"use Mix.Config\n\nconfig :pleroma, :instance,\n name: \"Pleroma\",\n email: \"example@example.com\",\n notify_email: \"noreply@example.com\",\n description: \"A Pleroma instance, an alternative fediverse server\",\n limit: 5000,\n chat_limit: 5000,\n remote_limit: 100_000,\n upload_limit: 16_000_000,\n avatar_upload_limit: 2_000_000,\n background_upload_limit: 4_000_000,\n banner_upload_limit: 4_000_000,\n poll_limits: %{\n max_expiration: 31_536_000,\n max_option_chars: 200,\n max_options: 20,\n min_expiration: 0\n },\n registrations_open: true,\n federating: true,\n federation_incoming_replies_max_depth: 100,\n federation_reachability_timeout_days: 7,\n federation_publisher_modules: [Pleroma.Web.ActivityPub.Publisher],\n allow_relay: true,\n rewrite_policy: Pleroma.Web.ActivityPub.MRF.NoOpPolicy,\n public: true,\n quarantined_instances: [],\n managed_config: true,\n static_dir: \"instance/static/\",\n allowed_post_formats: [\"text/plain\", \"text/html\", \"text/markdown\", \"text/bbcode\"],\n mrf_transparency: true,\n mrf_transparency_exclusions: [],\n autofollowed_nicknames: [],\n max_pinned_statuses: 1,\n no_attachment_links: true,\n welcome_user_nickname: nil,\n welcome_message: nil,\n max_report_comment_size: 1000,\n safe_dm_mentions: false,\n healthcheck: false,\n remote_post_retention_days: 90,\n skip_thread_containment: true,\n limit_to_local_content: :unauthenticated,\n dynamic_configuration: false,\n user_bio_length: 5000,\n user_name_length: 100,\n max_account_fields: 10,\n max_remote_account_fields: 20,\n account_field_name_length: 512,\n account_field_value_length: 2048,\n external_user_synchronization: true,\n extended_nickname_format: true,\n multi_factor_authentication: [\n totp: [digits: 6, period: 30],\n backup_codes: [number: 2, length: 6]\n ]\n" "use Mix.Config\n\nconfig :pleroma, :instance,\n name: \"Pleroma\",\n email: \"example@example.com\",\n notify_email: \"noreply@example.com\",\n description: \"A Pleroma instance, an alternative fediverse server\",\n limit: 5000,\n chat_limit: 5000,\n remote_limit: 100_000,\n upload_limit: 16_000_000,\n avatar_upload_limit: 2_000_000,\n background_upload_limit: 4_000_000,\n banner_upload_limit: 4_000_000,\n poll_limits: %{\n max_expiration: 31_536_000,\n max_option_chars: 200,\n max_options: 20,\n min_expiration: 0\n },\n registrations_open: true,\n federating: true,\n federation_incoming_replies_max_depth: 100,\n federation_reachability_timeout_days: 7,\n federation_publisher_modules: [Pleroma.Web.ActivityPub.Publisher],\n allow_relay: true,\n rewrite_policy: Pleroma.Web.ActivityPub.MRF.NoOpPolicy,\n public: true,\n quarantined_instances: [],\n managed_config: true,\n static_dir: \"instance/static/\",\n allowed_post_formats: [\"text/plain\", \"text/html\", \"text/markdown\", \"text/bbcode\"],\n mrf_transparency: true,\n mrf_transparency_exclusions: [],\n autofollowed_nicknames: [],\n max_pinned_statuses: 1,\n no_attachment_links: true,\n welcome_user_nickname: nil,\n welcome_message: nil,\n max_report_comment_size: 1000,\n safe_dm_mentions: false,\n healthcheck: false,\n remote_post_retention_days: 90,\n skip_thread_containment: true,\n limit_to_local_content: :unauthenticated,\n user_bio_length: 5000,\n user_name_length: 100,\n max_account_fields: 10,\n max_remote_account_fields: 20,\n account_field_name_length: 512,\n account_field_value_length: 2048,\n external_user_synchronization: true,\n extended_nickname_format: true,\n multi_factor_authentication: [\n totp: [digits: 6, period: 30],\n backup_codes: [number: 2, length: 6]\n ]\n"
end end
end end
end end

View File

@ -78,7 +78,7 @@ test "running gen" do
assert generated_config =~ "database: \"dbname\"" assert generated_config =~ "database: \"dbname\""
assert generated_config =~ "username: \"dbuser\"" assert generated_config =~ "username: \"dbuser\""
assert generated_config =~ "password: \"dbpass\"" assert generated_config =~ "password: \"dbpass\""
assert generated_config =~ "dynamic_configuration: true" assert generated_config =~ "configurable_from_database: true"
assert generated_config =~ "http: [ip: {127, 0, 0, 1}, port: 4000]" assert generated_config =~ "http: [ip: {127, 0, 0, 1}, port: 4000]"
assert File.read!(tmp_path() <> "setup.psql") == generated_setup_psql() assert File.read!(tmp_path() <> "setup.psql") == generated_setup_psql()
end end

View File

@ -584,7 +584,7 @@ test "sends invitation and returns 204", %{conn: conn, user: user} do
assert json_response(conn, :no_content) assert json_response(conn, :no_content)
token_record = List.last(Pleroma.Repo.all(Pleroma.UserInviteToken)) token_record = List.last(Repo.all(Pleroma.UserInviteToken))
assert token_record assert token_record
refute token_record.used refute token_record.used
@ -1929,8 +1929,8 @@ test "returns error when status is not exist", %{conn: conn} do
end end
describe "GET /api/pleroma/admin/config" do describe "GET /api/pleroma/admin/config" do
clear_config([:instance, :dynamic_configuration]) do clear_config([:configurable_from_database]) do
Pleroma.Config.put([:instance, :dynamic_configuration], true) Pleroma.Config.put([:configurable_from_database], true)
end end
setup %{conn: conn} do setup %{conn: conn} do
@ -1940,9 +1940,9 @@ test "returns error when status is not exist", %{conn: conn} do
end end
test "when dynamic configuration is off", %{conn: conn} do test "when dynamic configuration is off", %{conn: conn} do
initial = Pleroma.Config.get([:instance, :dynamic_configuration]) initial = Pleroma.Config.get([:configurable_from_database])
Pleroma.Config.put([:instance, :dynamic_configuration], false) Pleroma.Config.put([:configurable_from_database], false)
on_exit(fn -> Pleroma.Config.put([:instance, :dynamic_configuration], initial) end) on_exit(fn -> Pleroma.Config.put([:configurable_from_database], initial) end)
conn = get(conn, "/api/pleroma/admin/config") conn = get(conn, "/api/pleroma/admin/config")
assert json_response(conn, 400) == assert json_response(conn, 400) ==
@ -2016,8 +2016,8 @@ test "POST /api/pleroma/admin/config error" do
%{conn: assign(conn, :user, admin)} %{conn: assign(conn, :user, admin)}
end end
clear_config([:instance, :dynamic_configuration]) do clear_config([:configurable_from_database]) do
Pleroma.Config.put([:instance, :dynamic_configuration], true) Pleroma.Config.put([:configurable_from_database], true)
end end
@tag capture_log: true @tag capture_log: true
@ -2908,8 +2908,8 @@ test "proxy tuple ip", %{conn: conn} do
%{conn: assign(conn, :user, admin)} %{conn: assign(conn, :user, admin)}
end end
clear_config([:instance, :dynamic_configuration]) do clear_config([:configurable_from_database]) do
Pleroma.Config.put([:instance, :dynamic_configuration], true) Pleroma.Config.put([:configurable_from_database], true)
end end
clear_config([:feed, :post_title]) do clear_config([:feed, :post_title]) do
@ -2918,20 +2918,20 @@ test "proxy tuple ip", %{conn: conn} do
test "transfer settings to DB and to file", %{conn: conn} do test "transfer settings to DB and to file", %{conn: conn} do
on_exit(fn -> :ok = File.rm("config/test.exported_from_db.secret.exs") end) on_exit(fn -> :ok = File.rm("config/test.exported_from_db.secret.exs") end)
assert Pleroma.Repo.all(Pleroma.Web.AdminAPI.Config) == [] assert Repo.all(Pleroma.Web.AdminAPI.Config) == []
Mix.Tasks.Pleroma.Config.run(["migrate_to_db"]) Mix.Tasks.Pleroma.Config.run(["migrate_to_db"])
assert Pleroma.Repo.all(Pleroma.Web.AdminAPI.Config) > 0 assert Repo.aggregate(Pleroma.Web.AdminAPI.Config, :count, :id) > 0
conn = get(conn, "/api/pleroma/admin/config/migrate_from_db") conn = get(conn, "/api/pleroma/admin/config/migrate_from_db")
assert json_response(conn, 200) == %{} assert json_response(conn, 200) == %{}
assert Pleroma.Repo.all(Pleroma.Web.AdminAPI.Config) == [] assert Repo.all(Pleroma.Web.AdminAPI.Config) == []
end end
test "returns error if dynamic configuration is off", %{conn: conn} do test "returns error if dynamic configuration is off", %{conn: conn} do
initial = Pleroma.Config.get([:instance, :dynamic_configuration]) initial = Pleroma.Config.get([:configurable_from_database])
on_exit(fn -> Pleroma.Config.put([:instance, :dynamic_configuration], initial) end) on_exit(fn -> Pleroma.Config.put([:configurable_from_database], initial) end)
Pleroma.Config.put([:instance, :dynamic_configuration], false) Pleroma.Config.put([:configurable_from_database], false)
conn = get(conn, "/api/pleroma/admin/config/migrate_from_db") conn = get(conn, "/api/pleroma/admin/config/migrate_from_db")