From 6dcc36baa9b19d18785d6f7ab8ceb7dd941c6180 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Wed, 25 Nov 2020 12:44:06 -0600 Subject: Add mix tasks to give additional recovery and debugging options - pleroma.config dump: prints the entire config as it would be exported to the filesystem - pleroma.config dump KEY: prints the configuration under a specific ConfigDB key in the database - pleroma.config keylist: lists the available keys in ConfigDB - pleroma.config keydel KEY: deletes ConfigDB entry stored under the key This should prevent the need for users to manually execute SQL queries. --- lib/mix/tasks/pleroma/config.ex | 89 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index 18f99318d..b49854528 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -30,6 +30,83 @@ def run(["migrate_from_db" | options]) do migrate_from_db(opts) end + def run(["dump"]) do + with true <- Pleroma.Config.get([:configurable_from_database]) do + start_pleroma() + + header = config_header() + + shell_info("#{header}") + + ConfigDB + |> Repo.all() + |> Enum.each(&dump(&1)) + else + _ -> configdb_not_enabled() + end + end + + def run(["dump" | dbkey]) do + with true <- Pleroma.Config.get([:configurable_from_database]) do + start_pleroma() + + dbkey = dbkey |> List.first() |> String.to_atom() + + ConfigDB + |> Repo.all() + |> Enum.filter(fn x -> + if x.key == dbkey do + x |> dump + end + end) + else + _ -> configdb_not_enabled() + end + end + + def run(["keylist"]) do + with true <- Pleroma.Config.get([:configurable_from_database]) do + start_pleroma() + + keys = + ConfigDB + |> Repo.all() + |> Enum.map(fn x -> x.key end) + + if length(keys) > 0 do + shell_info("The following configuration keys are set in ConfigDB:\r\n") + keys |> Enum.each(fn x -> shell_info("- #{x}") end) + shell_info("\r\n") + end + else + _ -> configdb_not_enabled() + end + end + + def run(["keydel" | dbkey]) do + unless [] == dbkey do + with true <- Pleroma.Config.get([:configurable_from_database]) do + start_pleroma() + + dbkey = dbkey |> List.first() |> String.to_atom() + + ConfigDB + |> Repo.all() + |> Enum.filter(fn x -> + if x.key == dbkey do + x |> delete(true) + end + end) + else + _ -> configdb_not_enabled() + end + else + shell_error( + "You must provide a key to delete. Use the keylist command to get a list of valid keys." + ) + end + end + @spec migrate_to_db(Path.t() | nil) :: any() def migrate_to_db(file_path \\ nil) do with true <- Pleroma.Config.get([:configurable_from_database]), @@ -154,4 +231,16 @@ defp delete(config, true) do end defp delete(_config, _), do: :ok + + defp dump(%Pleroma.ConfigDB{} = config) do + value = inspect(config.value, limit: :infinity) + + shell_info("config #{inspect(config.group)}, #{inspect(config.key)}, #{value}\r\n\r\n") + end + + defp configdb_not_enabled do + shell_error( + "ConfigDB not enabled. Please check the value of :configurable_from_database in your configuration." + ) + end end -- cgit v1.2.3 From a82ba66662fdcdccf0de384b0f57dd20bef0fd9d Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Wed, 25 Nov 2020 17:16:23 -0600 Subject: Better deletion message --- lib/mix/tasks/pleroma/config.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index b49854528..675dda0d0 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -227,7 +227,7 @@ defp write(config, file) do defp delete(config, true) do {:ok, _} = Repo.delete(config) - shell_info("#{config.key} deleted from DB.") + shell_info("#{config.key} deleted from the ConfigDB.") end defp delete(_config, _), do: :ok -- cgit v1.2.3 From e8a4062d9dc042253adc05f2ab964bbd468ace12 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Wed, 25 Nov 2020 17:31:44 -0600 Subject: Document how to delete individual configuration groups and completely reset the config without SQL --- docs/configuration/howto_database_config.md | 26 ++++++++++++++++++-------- lib/mix/tasks/pleroma/config.ex | 13 +++++++++++++ 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/docs/configuration/howto_database_config.md b/docs/configuration/howto_database_config.md index 9ed4d6cdd..d85b46bd1 100644 --- a/docs/configuration/howto_database_config.md +++ b/docs/configuration/howto_database_config.md @@ -140,14 +140,24 @@ If you encounter a situation where the server cannot run properly because of an e.g., here is an example showing a minimal configuration in the database. Only the `config :pleroma, :instance` settings are in the table: ``` -psql -d pleroma_dev -pleroma_dev=# select * from config; - id | key | value | inserted_at | updated_at | group -----+-----------+------------------------------------------------------------+---------------------+---------------------+---------- - 1 | :instance | \x836c0000000168026400046e616d656d00000007426c65726f6d616a | 2020-07-12 15:33:29 | 2020-07-12 15:33:29 | :pleroma -(1 row) -pleroma_dev=# delete from config where key = ':instance' and group = ':pleroma'; -DELETE 1 +$ mix pleroma.config keylist +The following configuration keys are set in ConfigDB: + +- instance + +``` + +``` +$ mix pleroma.config show instance +config :pleroma, :instance, [name: "MyPleroma", description: "A fun place to hang out!", notify_email: "no-reply@mypleroma.com", email: "admin@mypleroma.com", account_activation_required: true] + +``` + +To delete the saved settings for `:instance`: + +``` +$ mix pleroma.config keydel instance +instance deleted from the ConfigDB. ``` Now the `config :pleroma, :instance` settings have been removed from the database. diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index 675dda0d0..574f8f4be 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -83,6 +83,19 @@ def run(["keylist"]) do end end + def run(["reset"]) do + with true <- Pleroma.Config.get([:configurable_from_database]) do + start_pleroma() + + Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;") + Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;") + + shell_info("The ConfigDB settings have been removed from the database.") + else + _ -> configdb_not_enabled() + end + end + def run(["keydel" | dbkey]) do unless [] == dbkey do with true <- Pleroma.Config.get([:configurable_from_database]) do -- cgit v1.2.3 From 92c23bfdecd13c779cf1b0851ada5d846e5264f8 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Wed, 25 Nov 2020 17:46:57 -0600 Subject: Spelling --- docs/administration/CLI_tasks/config.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/administration/CLI_tasks/config.md b/docs/administration/CLI_tasks/config.md index 0923004b5..482f02b64 100644 --- a/docs/administration/CLI_tasks/config.md +++ b/docs/administration/CLI_tasks/config.md @@ -32,7 +32,7 @@ config :pleroma, configurable_from_database: false ``` -To delete transfered settings from database optional flag `-d` can be used. `` is `prod` by default. +To delete transferred settings from database optional flag `-d` can be used. `` is `prod` by default. === "OTP" ```sh -- cgit v1.2.3 From ada073f2511ae57eb22dc9e8a4220b2382b9f97c Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Wed, 25 Nov 2020 17:49:36 -0600 Subject: Rename keys to groups --- docs/administration/CLI_tasks/config.md | 44 +++++++++++++++++++++++++++++++++ lib/mix/tasks/pleroma/config.ex | 6 ++--- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/docs/administration/CLI_tasks/config.md b/docs/administration/CLI_tasks/config.md index 482f02b64..1eb3c7437 100644 --- a/docs/administration/CLI_tasks/config.md +++ b/docs/administration/CLI_tasks/config.md @@ -43,3 +43,47 @@ To delete transferred settings from database optional flag `-d` can be used. `] [-d] ``` + +## Dump all of the config settings defined in the database + +=== "OTP" + + ```sh + ./bin/pleroma_ctl config dump + ``` + +=== "From Source" + + ```sh + mix pleroma.config dump + ``` + +## List individual configuration groups in the database + +=== "OTP" + + ```sh + ./bin/pleroma_ctl config groups + ``` + +=== "From Source" + + ```sh + mix pleroma.config groups + ``` + +## Dump the saved configuration values for a specific group + +e.g., this shows all the settings under `:instance` + +=== "OTP" + + ```sh + ./bin/pleroma_ctl config dump instance + ``` + +=== "From Source" + + ```sh + mix pleroma.config dump instance + ``` diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index 574f8f4be..3c94f1f5f 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -64,7 +64,7 @@ def run(["dump" | dbkey]) do end end - def run(["keylist"]) do + def run(["groups"]) do with true <- Pleroma.Config.get([:configurable_from_database]) do start_pleroma() @@ -96,7 +96,7 @@ def run(["reset"]) do end end - def run(["keydel" | dbkey]) do + def run(["groupdel" | dbkey]) do unless [] == dbkey do with true <- Pleroma.Config.get([:configurable_from_database]) do start_pleroma() @@ -115,7 +115,7 @@ def run(["keydel" | dbkey]) do end else shell_error( - "You must provide a key to delete. Use the keylist command to get a list of valid keys." + "You must provide a group to delete. Use the groups command to get a list of valid configDB groups." ) end end -- cgit v1.2.3 From 2e87378051e311c85926adfae4290189747d0bc2 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Wed, 25 Nov 2020 17:51:31 -0600 Subject: Add the delete and reset instructions --- docs/administration/CLI_tasks/config.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/docs/administration/CLI_tasks/config.md b/docs/administration/CLI_tasks/config.md index 1eb3c7437..3572b5915 100644 --- a/docs/administration/CLI_tasks/config.md +++ b/docs/administration/CLI_tasks/config.md @@ -87,3 +87,35 @@ e.g., this shows all the settings under `:instance` ```sh mix pleroma.config dump instance ``` + +## Delete the saved configuration values for a specific group + +e.g., this deletes all the settings under `:instance` + +=== "OTP" + + ```sh + ./bin/pleroma_ctl config groupdel instance + ``` + +=== "From Source" + + ```sh + mix pleroma.config groupdel instance + ``` + +## Remove all settings from the database + +This forcibly removes all saved values in the database. + +=== "OTP" + + ```sh + ./bin/pleroma_ctl config reset + ``` + +=== "From Source" + + ```sh + mix pleroma.config reset + ``` -- cgit v1.2.3 From a51da3c1d8355de0747605608fc929f5fa345b3f Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Fri, 27 Nov 2020 12:32:53 -0600 Subject: Sort output by group Not the best sorting, but better than nothing. --- lib/mix/tasks/pleroma/config.ex | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index 3c94f1f5f..76753e13c 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -40,6 +40,7 @@ def run(["dump"]) do ConfigDB |> Repo.all() + |> Enum.sort() |> Enum.each(&dump(&1)) else _ -> configdb_not_enabled() -- cgit v1.2.3 From 67437feafc048e56d023370266fe3762405f3199 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Fri, 27 Nov 2020 12:33:55 -0600 Subject: Support listing groups, listing keys in a group, and dumping the config based on group or specific key in that group --- lib/mix/tasks/pleroma/config.ex | 75 ++++++++++++++++++++++++++++++++++------- 1 file changed, 63 insertions(+), 12 deletions(-) diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index 76753e13c..5c01b21f8 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -47,35 +47,61 @@ def run(["dump"]) do end end - def run(["dump" | dbkey]) do + def run(["dump" | args]) when is_list(args) and length(args) < 3 do with true <- Pleroma.Config.get([:configurable_from_database]) do start_pleroma() - dbkey = dbkey |> List.first() |> String.to_atom() - - ConfigDB - |> Repo.all() - |> Enum.filter(fn x -> - if x.key == dbkey do - x |> dump - end - end) + if length(args) > 1 do + [group, key] = args + dump_key(group, key) + else + [group] = args + dump_group(group) + end else _ -> configdb_not_enabled() end end def run(["groups"]) do + with true <- Pleroma.Config.get([:configurable_from_database]) do + start_pleroma() + + groups = + ConfigDB + |> Repo.all() + |> Enum.map(fn x -> x.group end) + |> Enum.sort() + |> Enum.uniq() + + if length(groups) > 0 do + shell_info("The following configuration groups are set in ConfigDB:\r\n") + groups |> Enum.each(fn x -> shell_info("- #{x}") end) + shell_info("\r\n") + end + else + _ -> configdb_not_enabled() + end + end + + def run(["keys" | group]) do with true <- Pleroma.Config.get([:configurable_from_database]) do start_pleroma() keys = ConfigDB |> Repo.all() - |> Enum.map(fn x -> x.key end) + |> Enum.map(fn x -> + if x.group == group do + x.key + end + end) + |> Enum.sort() + |> Enum.uniq() + |> Enum.reject(fn x -> x == nil end) if length(keys) > 0 do - shell_info("The following configuration keys are set in ConfigDB:\r\n") + shell_info("The following configuration keys under :#{group} are set in ConfigDB:\r\n") keys |> Enum.each(fn x -> shell_info("- #{x}") end) shell_info("\r\n") end @@ -257,4 +283,29 @@ defp configdb_not_enabled do "ConfigDB not enabled. Please check the value of :configurable_from_database in your configuration." ) end + + defp dump_key(group, key) do + group = group |> String.to_atom() + key = key |> String.to_atom() + + ConfigDB + |> Repo.all() + |> Enum.filter(fn x -> + if x.group == group && x.key == key do + x |> dump + end + end) + end + + defp dump_group(group) do + group = group |> String.to_atom() + + ConfigDB + |> Repo.all() + |> Enum.filter(fn x -> + if x.group == group do + x |> dump + end + end) + end end -- cgit v1.2.3 From c6a0ca2213be0eac1233ae28c11e563109771c85 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Fri, 27 Nov 2020 13:55:43 -0600 Subject: Improve dumping groups and specific keys; add prompts for delete and reset --- lib/mix/tasks/pleroma/config.ex | 48 +++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index 5c01b21f8..a794344cb 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -47,17 +47,21 @@ def run(["dump"]) do end end - def run(["dump" | args]) when is_list(args) and length(args) < 3 do + def run(["dump", group, key]) do with true <- Pleroma.Config.get([:configurable_from_database]) do start_pleroma() - if length(args) > 1 do - [group, key] = args - dump_key(group, key) - else - [group] = args - dump_group(group) - end + dump_key(group, key) + else + _ -> configdb_not_enabled() + end + end + + def run(["dump", group]) do + with true <- Pleroma.Config.get([:configurable_from_database]) do + start_pleroma() + + dump_group(group) else _ -> configdb_not_enabled() end @@ -114,36 +118,38 @@ def run(["reset"]) do with true <- Pleroma.Config.get([:configurable_from_database]) do start_pleroma() - Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;") - Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;") + if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do + Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;") + Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;") - shell_info("The ConfigDB settings have been removed from the database.") + shell_info("The ConfigDB settings have been removed from the database.") + else + shell_info("No changes made.") + end else _ -> configdb_not_enabled() end end - def run(["groupdel" | dbkey]) do - unless [] == dbkey do - with true <- Pleroma.Config.get([:configurable_from_database]) do - start_pleroma() + def run(["delete" | args]) when is_list(args) and length(args) == 2 do + with true <- Pleroma.Config.get([:configurable_from_database]) do + start_pleroma() - dbkey = dbkey |> List.first() |> String.to_atom() + [group, key] = args + if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do ConfigDB |> Repo.all() |> Enum.filter(fn x -> - if x.key == dbkey do + if x.group == group and x.key == key do x |> delete(true) end end) else - _ -> configdb_not_enabled() + shell_info("No changes made.") end else - shell_error( - "You must provide a group to delete. Use the groups command to get a list of valid configDB groups." - ) + _ -> configdb_not_enabled() end end -- cgit v1.2.3 From ae7d37de0665021373d9bc4d01d648c7d812eaed Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Fri, 27 Nov 2020 14:02:45 -0600 Subject: Fix deletion regression due to strings instead of atoms Improve message after successful deletion --- lib/mix/tasks/pleroma/config.ex | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index a794344cb..e5536d16a 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -131,11 +131,34 @@ def run(["reset"]) do end end - def run(["delete" | args]) when is_list(args) and length(args) == 2 do + def run(["delete", group]) do with true <- Pleroma.Config.get([:configurable_from_database]) do start_pleroma() - [group, key] = args + group = group |> String.to_atom() + + if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do + ConfigDB + |> Repo.all() + |> Enum.filter(fn x -> + if x.group == group do + x |> delete(true) + end + end) + else + shell_info("No changes made.") + end + else + _ -> configdb_not_enabled() + end + end + + def run(["delete", group, key]) do + with true <- Pleroma.Config.get([:configurable_from_database]) do + start_pleroma() + + group = group |> String.to_atom() + key = key |> String.to_atom() if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do ConfigDB @@ -273,7 +296,7 @@ defp write(config, file) do defp delete(config, true) do {:ok, _} = Repo.delete(config) - shell_info("#{config.key} deleted from the ConfigDB.") + shell_info(":#{config.group}, :#{config.key} deleted from the ConfigDB.") end defp delete(_config, _), do: :ok -- cgit v1.2.3 From 3df115b2b0ee9f5ca6f2507550d18002379eeaa8 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Fri, 27 Nov 2020 14:44:05 -0600 Subject: Support atoms and strings as args to the mix task Improve output. Show the user what will be deleted before the prompt. --- lib/mix/tasks/pleroma/config.ex | 95 +++++++++++++++++++++++++++++------------ 1 file changed, 67 insertions(+), 28 deletions(-) diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index e5536d16a..078a4110b 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -36,12 +36,18 @@ def run(["dump"]) do header = config_header() - shell_info("#{header}") + settings = + ConfigDB + |> Repo.all() + |> Enum.sort() - ConfigDB - |> Repo.all() - |> Enum.sort() - |> Enum.each(&dump(&1)) + unless settings == [] do + shell_info("#{header}") + + settings |> Enum.each(&dump(&1)) + else + shell_error("No settings in ConfigDB.") + end else _ -> configdb_not_enabled() end @@ -51,6 +57,9 @@ def run(["dump", group, key]) do with true <- Pleroma.Config.get([:configurable_from_database]) do start_pleroma() + group = atomize(group) + key = atomize(key) + dump_key(group, key) else _ -> configdb_not_enabled() @@ -61,6 +70,8 @@ def run(["dump", group]) do with true <- Pleroma.Config.get([:configurable_from_database]) do start_pleroma() + group = atomize(group) + dump_group(group) else _ -> configdb_not_enabled() @@ -88,10 +99,12 @@ def run(["groups"]) do end end - def run(["keys" | group]) do + def run(["keys", group]) do with true <- Pleroma.Config.get([:configurable_from_database]) do start_pleroma() + group = atomize(group) + keys = ConfigDB |> Repo.all() @@ -124,7 +137,7 @@ def run(["reset"]) do shell_info("The ConfigDB settings have been removed from the database.") else - shell_info("No changes made.") + shell_error("No changes made.") end else _ -> configdb_not_enabled() @@ -135,18 +148,26 @@ def run(["delete", group]) do with true <- Pleroma.Config.get([:configurable_from_database]) do start_pleroma() - group = group |> String.to_atom() + group = atomize(group) - if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do - ConfigDB - |> Repo.all() - |> Enum.filter(fn x -> - if x.group == group do - x |> delete(true) - end - end) + if group_exists?(group) do + shell_info("The following settings will be removed from ConfigDB:\n") + + dump_group(group) + + if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do + ConfigDB + |> Repo.all() + |> Enum.filter(fn x -> + if x.group == group do + x |> delete(true) + end + end) + else + shell_error("No changes made.") + end else - shell_info("No changes made.") + shell_error("No settings in ConfigDB for :#{group}. Aborting.") end else _ -> configdb_not_enabled() @@ -157,8 +178,8 @@ def run(["delete", group, key]) do with true <- Pleroma.Config.get([:configurable_from_database]) do start_pleroma() - group = group |> String.to_atom() - key = key |> String.to_atom() + group = atomize(group) + key = atomize(key) if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do ConfigDB @@ -169,7 +190,7 @@ def run(["delete", group, key]) do end end) else - shell_info("No changes made.") + shell_error("No changes made.") end else _ -> configdb_not_enabled() @@ -296,7 +317,10 @@ defp write(config, file) do defp delete(config, true) do {:ok, _} = Repo.delete(config) - shell_info(":#{config.group}, :#{config.key} deleted from the ConfigDB.") + + shell_info( + "config #{inspect(config.group)}, #{inspect(config.key)} deleted from the ConfigDB." + ) end defp delete(_config, _), do: :ok @@ -313,10 +337,7 @@ defp configdb_not_enabled do ) end - defp dump_key(group, key) do - group = group |> String.to_atom() - key = key |> String.to_atom() - + defp dump_key(group, key) when is_atom(group) and is_atom(key) do ConfigDB |> Repo.all() |> Enum.filter(fn x -> @@ -326,9 +347,7 @@ defp dump_key(group, key) do end) end - defp dump_group(group) do - group = group |> String.to_atom() - + defp dump_group(group) when is_atom(group) do ConfigDB |> Repo.all() |> Enum.filter(fn x -> @@ -337,4 +356,24 @@ defp dump_group(group) do end end) end + + defp group_exists?(group) when is_atom(group) do + result = + ConfigDB + |> Repo.all() + |> Enum.filter(fn x -> + if x.group == group do + x + end + end) + + unless result == [] do + true + else + false + end + end + + defp atomize(x) when is_atom(x), do: x + defp atomize(x) when is_binary(x), do: String.to_atom(x) end -- cgit v1.2.3 From 4bdfcf1682f1429e72102bf9f54ddee9e7ede0bc Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Fri, 27 Nov 2020 16:20:28 -0600 Subject: Transform strings to atoms for all cases, including when the atom is a module like Pleroma.Emails.Mailer --- lib/mix/tasks/pleroma/config.ex | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index 078a4110b..7ab15e60b 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -57,8 +57,8 @@ def run(["dump", group, key]) do with true <- Pleroma.Config.get([:configurable_from_database]) do start_pleroma() - group = atomize(group) - key = atomize(key) + group = maybe_atomize(group) + key = maybe_atomize(key) dump_key(group, key) else @@ -70,7 +70,7 @@ def run(["dump", group]) do with true <- Pleroma.Config.get([:configurable_from_database]) do start_pleroma() - group = atomize(group) + group = maybe_atomize(group) dump_group(group) else @@ -103,7 +103,7 @@ def run(["keys", group]) do with true <- Pleroma.Config.get([:configurable_from_database]) do start_pleroma() - group = atomize(group) + group = maybe_atomize(group) keys = ConfigDB @@ -148,7 +148,7 @@ def run(["delete", group]) do with true <- Pleroma.Config.get([:configurable_from_database]) do start_pleroma() - group = atomize(group) + group = maybe_atomize(group) if group_exists?(group) do shell_info("The following settings will be removed from ConfigDB:\n") @@ -178,8 +178,8 @@ def run(["delete", group, key]) do with true <- Pleroma.Config.get([:configurable_from_database]) do start_pleroma() - group = atomize(group) - key = atomize(key) + group = maybe_atomize(group) + key = maybe_atomize(key) if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do ConfigDB @@ -337,7 +337,7 @@ defp configdb_not_enabled do ) end - defp dump_key(group, key) when is_atom(group) and is_atom(key) do + defp dump_key(group, key) when is_atom(group) and is_atom(key) do ConfigDB |> Repo.all() |> Enum.filter(fn x -> @@ -374,6 +374,15 @@ defp group_exists?(group) when is_atom(group) do end end - defp atomize(x) when is_atom(x), do: x - defp atomize(x) when is_binary(x), do: String.to_atom(x) + def maybe_atomize(arg) when is_atom(arg), do: arg + + def maybe_atomize(arg) when is_binary(arg) do + chars = String.codepoints(arg) + + if "." in chars do + :"Elixir.#{arg}" + else + String.to_atom(arg) + end + end end -- cgit v1.2.3 From d4320e0daf7c732ba2c791cae697dea27c4919d2 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Fri, 27 Nov 2020 16:32:32 -0600 Subject: Both are really atoms --- lib/mix/tasks/pleroma/config.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index 7ab15e60b..a7c307f77 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -337,7 +337,7 @@ defp configdb_not_enabled do ) end - defp dump_key(group, key) when is_atom(group) and is_atom(key) do + defp dump_key(group, key) when is_atom(group) and is_atom(key) do ConfigDB |> Repo.all() |> Enum.filter(fn x -> -- cgit v1.2.3 From 0847e3e496624a97c7eb933cf69a92fd84677ce0 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Fri, 27 Nov 2020 16:32:46 -0600 Subject: Print whole config when resetting and include a scary looking message. --- lib/mix/tasks/pleroma/config.ex | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index a7c307f77..0c8170c9c 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -131,6 +131,15 @@ def run(["reset"]) do with true <- Pleroma.Config.get([:configurable_from_database]) do start_pleroma() + shell_info("The following settings will be permanently removed:") + + ConfigDB + |> Repo.all() + |> Enum.sort() + |> Enum.each(&dump(&1)) + + shell_error("THIS CANNOT BE UNDONE!") + if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;") Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;") -- cgit v1.2.3 From 570a923a3b77edc98c18c0cfb60e3a2d7bf2b2e8 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Sat, 28 Nov 2020 11:53:45 -0600 Subject: Update ConfigDB docs for new mix commands --- docs/configuration/howto_database_config.md | 89 +++++++++++++++-------------- 1 file changed, 47 insertions(+), 42 deletions(-) diff --git a/docs/configuration/howto_database_config.md b/docs/configuration/howto_database_config.md index d85b46bd1..b285190a3 100644 --- a/docs/configuration/howto_database_config.md +++ b/docs/configuration/howto_database_config.md @@ -8,17 +8,17 @@ The configuration of Pleroma has traditionally been managed with a config file, 1. Run the mix task to migrate to the database. You'll receive some debugging output and a few messages informing you of what happened. **Source:** - + ``` $ mix pleroma.config migrate_to_db ``` - + or - + **OTP:** - + *Note: OTP users need Pleroma to be running for `pleroma_ctl` commands to work* - + ``` $ ./bin/pleroma_ctl config migrate_to_db ``` @@ -27,28 +27,28 @@ The configuration of Pleroma has traditionally been managed with a config file, 10:04:34.155 [debug] QUERY OK source="config" db=1.6ms decode=2.0ms queue=33.5ms idle=0.0ms SELECT c0."id", c0."key", c0."group", c0."value", c0."inserted_at", c0."updated_at" FROM "config" AS c0 [] Migrating settings from file: /home/pleroma/config/dev.secret.exs - + 10:04:34.240 [debug] QUERY OK db=4.5ms queue=0.3ms idle=92.2ms TRUNCATE config; [] - + 10:04:34.244 [debug] QUERY OK db=2.8ms queue=0.3ms idle=97.2ms ALTER SEQUENCE config_id_seq RESTART; [] - + 10:04:34.256 [debug] QUERY OK source="config" db=0.8ms queue=1.4ms idle=109.8ms SELECT c0."id", c0."key", c0."group", c0."value", c0."inserted_at", c0."updated_at" FROM "config" AS c0 WHERE ((c0."group" = $1) AND (c0."key" = $2)) [":pleroma", ":instance"] - + 10:04:34.292 [debug] QUERY OK db=2.6ms queue=1.7ms idle=137.7ms INSERT INTO "config" ("group","key","value","inserted_at","updated_at") VALUES ($1,$2,$3,$4,$5) RETURNING "id" [":pleroma", ":instance", <<131, 108, 0, 0, 0, 1, 104, 2, 100, 0, 4, 110, 97, 109, 101, 109, 0, 0, 0, 7, 66, 108, 101, 114, 111, 109, 97, 106>>, ~N[2020-07-12 15:04:34], ~N[2020-07-12 15:04:34]] Settings for key instance migrated. Settings for group :pleroma migrated. ``` - + 2. It is recommended to backup your config file now. ``` cp config/dev.secret.exs config/dev.secret.exs.orig ``` - + 3. Edit your Pleroma config to enable database configuration: ``` @@ -76,17 +76,17 @@ The configuration of Pleroma has traditionally been managed with a config file, config :pleroma, Pleroma.Web.Endpoint, url: [host: "cool.pleroma.site", scheme: "https", port: 443] - + config :pleroma, Pleroma.Repo, adapter: Ecto.Adapters.Postgres, username: "pleroma", password: "MySecretPassword", database: "pleroma_prod", hostname: "localhost" - + config :pleroma, configurable_from_database: true ``` - + 5. Restart your instance and you can now access the Settings tab in AdminFE. @@ -95,15 +95,15 @@ The configuration of Pleroma has traditionally been managed with a config file, 1. Run the mix task to migrate back from the database. You'll receive some debugging output and a few messages informing you of what happened. **Source:** - + ``` $ mix pleroma.config migrate_from_db ``` - + or - + **OTP:** - + ``` $ ./bin/pleroma_ctl config migrate_from_db ``` @@ -111,7 +111,7 @@ The configuration of Pleroma has traditionally been managed with a config file, ``` 10:26:30.593 [debug] QUERY OK source="config" db=9.8ms decode=1.2ms queue=26.0ms idle=0.0ms SELECT c0."id", c0."key", c0."group", c0."value", c0."inserted_at", c0."updated_at" FROM "config" AS c0 [] - + 10:26:30.659 [debug] QUERY OK source="config" db=1.1ms idle=80.7ms SELECT c0."id", c0."key", c0."group", c0."value", c0."inserted_at", c0."updated_at" FROM "config" AS c0 [] Database configuration settings have been saved to config/dev.exported_from_db.secret.exs @@ -124,40 +124,45 @@ The configuration of Pleroma has traditionally been managed with a config file, ## Debugging ### Clearing database config -You can clear the database config by truncating the `config` table in the database. e.g., +You can clear the database config with the following command: -``` -psql -d pleroma_dev -pleroma_dev=# TRUNCATE config; -TRUNCATE TABLE -``` + **Source:** + + ``` + $ mix pleroma.config reset + ``` + + or + + **OTP:** + + ``` + $ ./bin/pleroma_ctl config reset + ``` Additionally, every time you migrate the configuration to the database the config table is automatically truncated to ensure a clean migration. ### Manually removing a setting If you encounter a situation where the server cannot run properly because of an invalid setting in the database and this is preventing you from accessing AdminFE, you can manually remove the offending setting if you know which one it is. -e.g., here is an example showing a minimal configuration in the database. Only the `config :pleroma, :instance` settings are in the table: +e.g., here is an example showing a the removal of the `config :pleroma, :instance` settings: -``` -$ mix pleroma.config keylist -The following configuration keys are set in ConfigDB: - -- instance - -``` + **Source:** -``` -$ mix pleroma.config show instance -config :pleroma, :instance, [name: "MyPleroma", description: "A fun place to hang out!", notify_email: "no-reply@mypleroma.com", email: "admin@mypleroma.com", account_activation_required: true] + ``` + $ mix pleroma.config delete pleroma instance + Are you sure you want to continue? [n] y + config :pleroma, :instance deleted from the ConfigDB. + ``` -``` + or -To delete the saved settings for `:instance`: + **OTP:** -``` -$ mix pleroma.config keydel instance -instance deleted from the ConfigDB. -``` + ``` + $ ./bin/pleroma_ctl config delete pleroma instance + Are you sure you want to continue? [n] y + config :pleroma, :instance deleted from the ConfigDB. + ``` Now the `config :pleroma, :instance` settings have been removed from the database. -- cgit v1.2.3 From d0cb73527f1bc21aa6bb6d21bfcdf58c406c5b0c Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Sat, 28 Nov 2020 12:05:01 -0600 Subject: Ensure scary warning starts on a new line --- lib/mix/tasks/pleroma/config.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index 0c8170c9c..fe0cd81f8 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -138,7 +138,7 @@ def run(["reset"]) do |> Enum.sort() |> Enum.each(&dump(&1)) - shell_error("THIS CANNOT BE UNDONE!") + shell_error("\nTHIS CANNOT BE UNDONE!") if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;") -- cgit v1.2.3 From cc2fc2e423bf7abf2e03a584754e82e1c140765b Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Sat, 28 Nov 2020 12:09:17 -0600 Subject: The debug output is no longer there by default --- docs/configuration/howto_database_config.md | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/docs/configuration/howto_database_config.md b/docs/configuration/howto_database_config.md index b285190a3..ae1462f9b 100644 --- a/docs/configuration/howto_database_config.md +++ b/docs/configuration/howto_database_config.md @@ -5,7 +5,7 @@ The configuration of Pleroma has traditionally been managed with a config file, ## Migration to database config -1. Run the mix task to migrate to the database. You'll receive some debugging output and a few messages informing you of what happened. +1. Run the mix task to migrate to the database. **Source:** @@ -24,21 +24,8 @@ The configuration of Pleroma has traditionally been managed with a config file, ``` ``` - 10:04:34.155 [debug] QUERY OK source="config" db=1.6ms decode=2.0ms queue=33.5ms idle=0.0ms - SELECT c0."id", c0."key", c0."group", c0."value", c0."inserted_at", c0."updated_at" FROM "config" AS c0 [] Migrating settings from file: /home/pleroma/config/dev.secret.exs - 10:04:34.240 [debug] QUERY OK db=4.5ms queue=0.3ms idle=92.2ms - TRUNCATE config; [] - - 10:04:34.244 [debug] QUERY OK db=2.8ms queue=0.3ms idle=97.2ms - ALTER SEQUENCE config_id_seq RESTART; [] - - 10:04:34.256 [debug] QUERY OK source="config" db=0.8ms queue=1.4ms idle=109.8ms - SELECT c0."id", c0."key", c0."group", c0."value", c0."inserted_at", c0."updated_at" FROM "config" AS c0 WHERE ((c0."group" = $1) AND (c0."key" = $2)) [":pleroma", ":instance"] - - 10:04:34.292 [debug] QUERY OK db=2.6ms queue=1.7ms idle=137.7ms - INSERT INTO "config" ("group","key","value","inserted_at","updated_at") VALUES ($1,$2,$3,$4,$5) RETURNING "id" [":pleroma", ":instance", <<131, 108, 0, 0, 0, 1, 104, 2, 100, 0, 4, 110, 97, 109, 101, 109, 0, 0, 0, 7, 66, 108, 101, 114, 111, 109, 97, 106>>, ~N[2020-07-12 15:04:34], ~N[2020-07-12 15:04:34]] Settings for key instance migrated. Settings for group :pleroma migrated. ``` -- cgit v1.2.3 From 6a97885ea30195b84b008391db26cc7d570f97cf Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Sat, 28 Nov 2020 12:19:00 -0600 Subject: Sync docs with mix commands --- docs/administration/CLI_tasks/config.md | 48 +++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/docs/administration/CLI_tasks/config.md b/docs/administration/CLI_tasks/config.md index 3572b5915..ea07ca293 100644 --- a/docs/administration/CLI_tasks/config.md +++ b/docs/administration/CLI_tasks/config.md @@ -72,36 +72,68 @@ To delete transferred settings from database optional flag `-d` can be used. ` Date: Sat, 28 Nov 2020 12:22:30 -0600 Subject: Remove unnecessary keys command --- lib/mix/tasks/pleroma/config.ex | 28 ---------------------------- 1 file changed, 28 deletions(-) diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index fe0cd81f8..f657adf46 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -99,34 +99,6 @@ def run(["groups"]) do end end - def run(["keys", group]) do - with true <- Pleroma.Config.get([:configurable_from_database]) do - start_pleroma() - - group = maybe_atomize(group) - - keys = - ConfigDB - |> Repo.all() - |> Enum.map(fn x -> - if x.group == group do - x.key - end - end) - |> Enum.sort() - |> Enum.uniq() - |> Enum.reject(fn x -> x == nil end) - - if length(keys) > 0 do - shell_info("The following configuration keys under :#{group} are set in ConfigDB:\r\n") - keys |> Enum.each(fn x -> shell_info("- #{x}") end) - shell_info("\r\n") - end - else - _ -> configdb_not_enabled() - end - end - def run(["reset"]) do with true <- Pleroma.Config.get([:configurable_from_database]) do start_pleroma() -- cgit v1.2.3 From 5135a8189f9e297354a1d9f61f3cb7454711923c Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Sat, 28 Nov 2020 12:24:37 -0600 Subject: Use inspect instead of faking the output --- lib/mix/tasks/pleroma/config.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index f657adf46..3e1449550 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -148,7 +148,7 @@ def run(["delete", group]) do shell_error("No changes made.") end else - shell_error("No settings in ConfigDB for :#{group}. Aborting.") + shell_error("No settings in ConfigDB for #{inspect(group)}. Aborting.") end else _ -> configdb_not_enabled() @@ -228,7 +228,7 @@ defp create(group, settings) do shell_info("Settings for key #{key} migrated.") end) - shell_info("Settings for group :#{group} migrated.") + shell_info("Settings for group #{inspect(group)} migrated.") end defp migrate_from_db(opts) do -- cgit v1.2.3 From 3e6d9187a7b826641a2a105f0b93944c54fdeec3 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Sat, 28 Nov 2020 13:32:28 -0600 Subject: Add tests for config dumping --- test/mix/tasks/pleroma/config_test.exs | 86 ++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/test/mix/tasks/pleroma/config_test.exs b/test/mix/tasks/pleroma/config_test.exs index f36648829..dfa04a508 100644 --- a/test/mix/tasks/pleroma/config_test.exs +++ b/test/mix/tasks/pleroma/config_test.exs @@ -186,4 +186,90 @@ test "load a settings with large values and pass to file", %{temp_file: temp_fil "#{header}\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 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 autofollowed_nicknames: [],\n max_pinned_statuses: 1,\n attachment_links: false,\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 + + test "dumping a specific group" do + insert(:config, + group: :pleroma, + key: :instance, + value: [ + name: "Pleroma Test" + ] + ) + + insert(:config, + group: :web_push_encryption, + key: :vapid_details, + value: [ + subject: "mailto:administrator@example.com", + public_key: + "BOsPL-_KjNnjj_RMvLeR3dTOrcndi4TbMR0cu56gLGfGaT5m1gXxSfRHOcC4Dd78ycQL1gdhtx13qgKHmTM5xAI", + private_key: "Ism6FNdS31nLCA94EfVbJbDdJXCxAZ8cZiB1JQPN_t4" + ] + ) + + Mix.Tasks.Pleroma.Config.run(["dump", "pleroma"]) + + assert_receive {:mix_shell, :info, + ["config :pleroma, :instance, [name: \"Pleroma Test\"]\r\n\r\n"]} + + refute_receive { + :mix_shell, + :info, + [ + "config :web_push_encryption, :vapid_details, [subject: \"mailto:administrator@example.com\", public_key: \"BOsPL-_KjNnjj_RMvLeR3dTOrcndi4TbMR0cu56gLGfGaT5m1gXxSfRHOcC4Dd78ycQL1gdhtx13qgKHmTM5xAI\", private_key: \"Ism6FNdS31nLCA94EfVbJbDdJXCxAZ8cZiB1JQPN_t4\"]\r\n\r\n" + ] + } + end + + test "dumping a specific key in a group" do + insert(:config, + group: :pleroma, + key: :instance, + value: [ + name: "Pleroma Test" + ] + ) + + insert(:config, + group: :pleroma, + key: Pleroma.Captcha, + value: [ + enabled: false + ] + ) + + Mix.Tasks.Pleroma.Config.run(["dump", "pleroma", "Pleroma.Captcha"]) + + refute_receive {:mix_shell, :info, + ["config :pleroma, :instance, [name: \"Pleroma Test\"]\r\n\r\n"]} + + assert_receive {:mix_shell, :info, + ["config :pleroma, Pleroma.Captcha, [enabled: false]\r\n\r\n"]} + end + + test "dumps all configuration successfully" do + insert(:config, + group: :pleroma, + key: :instance, + value: [ + name: "Pleroma Test" + ] + ) + + insert(:config, + group: :pleroma, + key: Pleroma.Captcha, + value: [ + enabled: false + ] + ) + + Mix.Tasks.Pleroma.Config.run(["dump"]) + + assert_receive {:mix_shell, :info, + ["config :pleroma, :instance, [name: \"Pleroma Test\"]\r\n\r\n"]} + + assert_receive {:mix_shell, :info, + ["config :pleroma, Pleroma.Captcha, [enabled: false]\r\n\r\n"]} + end end -- cgit v1.2.3 From 53a5ec195239b399c2bc072f754346eba3b3b6b2 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Sun, 29 Nov 2020 12:59:03 -0600 Subject: Left public during debugging --- lib/mix/tasks/pleroma/config.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index 3e1449550..a781f3bf1 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -355,9 +355,9 @@ defp group_exists?(group) when is_atom(group) do end end - def maybe_atomize(arg) when is_atom(arg), do: arg + defp maybe_atomize(arg) when is_atom(arg), do: arg - def maybe_atomize(arg) when is_binary(arg) do + defp maybe_atomize(arg) when is_binary(arg) do chars = String.codepoints(arg) if "." in chars do -- cgit v1.2.3 From a7b5280b5b620e3548bbd387752a04c918418f61 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Sun, 29 Nov 2020 13:29:36 -0600 Subject: Centralize check that configdb is enabled which now raises an exception --- lib/mix/tasks/pleroma/config.ex | 235 ++++++++++++++++++---------------------- 1 file changed, 106 insertions(+), 129 deletions(-) diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index a781f3bf1..df4ee55c1 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -14,11 +14,13 @@ defmodule Mix.Tasks.Pleroma.Config do @moduledoc File.read!("docs/administration/CLI_tasks/config.md") def run(["migrate_to_db"]) do + check_configdb() start_pleroma() migrate_to_db() end def run(["migrate_from_db" | options]) do + check_configdb() start_pleroma() {opts, _} = @@ -31,142 +33,101 @@ def run(["migrate_from_db" | options]) do end def run(["dump"]) do - with true <- Pleroma.Config.get([:configurable_from_database]) do - start_pleroma() + check_configdb() + start_pleroma() - header = config_header() + header = config_header() - settings = - ConfigDB - |> Repo.all() - |> Enum.sort() + settings = + ConfigDB + |> Repo.all() + |> Enum.sort() - unless settings == [] do - shell_info("#{header}") + unless settings == [] do + shell_info("#{header}") - settings |> Enum.each(&dump(&1)) - else - shell_error("No settings in ConfigDB.") - end + settings |> Enum.each(&dump(&1)) else - _ -> configdb_not_enabled() + shell_error("No settings in ConfigDB.") end end def run(["dump", group, key]) do - with true <- Pleroma.Config.get([:configurable_from_database]) do - start_pleroma() + check_configdb() + start_pleroma() - group = maybe_atomize(group) - key = maybe_atomize(key) + group = maybe_atomize(group) + key = maybe_atomize(key) - dump_key(group, key) - else - _ -> configdb_not_enabled() - end + dump_key(group, key) end def run(["dump", group]) do - with true <- Pleroma.Config.get([:configurable_from_database]) do - start_pleroma() + check_configdb() + start_pleroma() - group = maybe_atomize(group) + group = maybe_atomize(group) - dump_group(group) - else - _ -> configdb_not_enabled() - end + dump_group(group) end def run(["groups"]) do - with true <- Pleroma.Config.get([:configurable_from_database]) do - start_pleroma() + check_configdb() + start_pleroma() - groups = - ConfigDB - |> Repo.all() - |> Enum.map(fn x -> x.group end) - |> Enum.sort() - |> Enum.uniq() - - if length(groups) > 0 do - shell_info("The following configuration groups are set in ConfigDB:\r\n") - groups |> Enum.each(fn x -> shell_info("- #{x}") end) - shell_info("\r\n") - end - else - _ -> configdb_not_enabled() + groups = + ConfigDB + |> Repo.all() + |> Enum.map(fn x -> x.group end) + |> Enum.sort() + |> Enum.uniq() + + if length(groups) > 0 do + shell_info("The following configuration groups are set in ConfigDB:\r\n") + groups |> Enum.each(fn x -> shell_info("- #{x}") end) + shell_info("\r\n") end end def run(["reset"]) do - with true <- Pleroma.Config.get([:configurable_from_database]) do - start_pleroma() + check_configdb() + start_pleroma() - shell_info("The following settings will be permanently removed:") + shell_info("The following settings will be permanently removed:") - ConfigDB - |> Repo.all() - |> Enum.sort() - |> Enum.each(&dump(&1)) + ConfigDB + |> Repo.all() + |> Enum.sort() + |> Enum.each(&dump(&1)) - shell_error("\nTHIS CANNOT BE UNDONE!") + shell_error("\nTHIS CANNOT BE UNDONE!") - if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do - Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;") - Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;") + if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do + Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;") + Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;") - shell_info("The ConfigDB settings have been removed from the database.") - else - shell_error("No changes made.") - end + shell_info("The ConfigDB settings have been removed from the database.") else - _ -> configdb_not_enabled() + shell_error("No changes made.") end end def run(["delete", group]) do - with true <- Pleroma.Config.get([:configurable_from_database]) do - start_pleroma() - - group = maybe_atomize(group) - - if group_exists?(group) do - shell_info("The following settings will be removed from ConfigDB:\n") - - dump_group(group) + check_configdb() + start_pleroma() - if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do - ConfigDB - |> Repo.all() - |> Enum.filter(fn x -> - if x.group == group do - x |> delete(true) - end - end) - else - shell_error("No changes made.") - end - else - shell_error("No settings in ConfigDB for #{inspect(group)}. Aborting.") - end - else - _ -> configdb_not_enabled() - end - end + group = maybe_atomize(group) - def run(["delete", group, key]) do - with true <- Pleroma.Config.get([:configurable_from_database]) do - start_pleroma() + if group_exists?(group) do + shell_info("The following settings will be removed from ConfigDB:\n") - group = maybe_atomize(group) - key = maybe_atomize(key) + dump_group(group) if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do ConfigDB |> Repo.all() |> Enum.filter(fn x -> - if x.group == group and x.key == key do + if x.group == group do x |> delete(true) end end) @@ -174,14 +135,33 @@ def run(["delete", group, key]) do shell_error("No changes made.") end else - _ -> configdb_not_enabled() + shell_error("No settings in ConfigDB for #{inspect(group)}. Aborting.") + end + end + + def run(["delete", group, key]) do + check_configdb() + start_pleroma() + + group = maybe_atomize(group) + key = maybe_atomize(key) + + if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do + ConfigDB + |> Repo.all() + |> Enum.filter(fn x -> + if x.group == group and x.key == key do + x |> delete(true) + end + end) + else + shell_error("No changes made.") end end @spec migrate_to_db(Path.t() | nil) :: any() def migrate_to_db(file_path \\ nil) do - with true <- Pleroma.Config.get([:configurable_from_database]), - :ok <- Pleroma.Config.DeprecationWarnings.warn() do + with :ok <- Pleroma.Config.DeprecationWarnings.warn() do config_file = if file_path do file_path @@ -195,8 +175,7 @@ def migrate_to_db(file_path \\ nil) do do_migrate_to_db(config_file) else - :error -> deprecation_error() - _ -> migration_error() + _ -> deprecation_error() end end @@ -232,41 +211,31 @@ defp create(group, settings) do end defp migrate_from_db(opts) do - if Pleroma.Config.get([:configurable_from_database]) do - env = opts[:env] || Pleroma.Config.get(:env) - - config_path = - if Pleroma.Config.get(:release) do - :config_path - |> Pleroma.Config.get() - |> Path.dirname() - else - "config" - end - |> Path.join("#{env}.exported_from_db.secret.exs") + env = opts[:env] || Pleroma.Config.get(:env) - file = File.open!(config_path, [:write, :utf8]) + config_path = + if Pleroma.Config.get(:release) do + :config_path + |> Pleroma.Config.get() + |> Path.dirname() + else + "config" + end + |> Path.join("#{env}.exported_from_db.secret.exs") - IO.write(file, config_header()) + file = File.open!(config_path, [:write, :utf8]) - ConfigDB - |> Repo.all() - |> Enum.each(&write_and_delete(&1, file, opts[:delete])) + IO.write(file, config_header()) - :ok = File.close(file) - System.cmd("mix", ["format", config_path]) + ConfigDB + |> Repo.all() + |> Enum.each(&write_and_delete(&1, file, opts[:delete])) - shell_info( - "Database configuration settings have been exported to config/#{env}.exported_from_db.secret.exs" - ) - else - migration_error() - end - end + :ok = File.close(file) + System.cmd("mix", ["format", config_path]) - defp migration_error do - shell_error( - "Migration is not allowed in config. You can change this behavior by setting `config :pleroma, configurable_from_database: true`" + shell_info( + "Database configuration settings have been exported to config/#{env}.exported_from_db.secret.exs" ) end @@ -313,7 +282,7 @@ defp dump(%Pleroma.ConfigDB{} = config) do end defp configdb_not_enabled do - shell_error( + raise( "ConfigDB not enabled. Please check the value of :configurable_from_database in your configuration." ) end @@ -366,4 +335,12 @@ defp maybe_atomize(arg) when is_binary(arg) do String.to_atom(arg) end end + + defp check_configdb() do + with true <- Pleroma.Config.get([:configurable_from_database]) do + :ok + else + _ -> configdb_not_enabled() + end + end end -- cgit v1.2.3 From 13947999ad28eac6668a601bf957d2e64edda9d3 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Wed, 2 Dec 2020 12:33:34 -0600 Subject: Use a callback strategy to short circuit the functions and print a nice error --- lib/mix/tasks/pleroma/config.ex | 199 +++++++++++++++++---------------- test/mix/tasks/pleroma/config_test.exs | 177 ++++++++++++++++------------- 2 files changed, 205 insertions(+), 171 deletions(-) diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index df4ee55c1..d509f150e 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -14,149 +14,158 @@ defmodule Mix.Tasks.Pleroma.Config do @moduledoc File.read!("docs/administration/CLI_tasks/config.md") def run(["migrate_to_db"]) do - check_configdb() - start_pleroma() - migrate_to_db() + check_configdb(fn -> + start_pleroma() + migrate_to_db() + end) end def run(["migrate_from_db" | options]) do - check_configdb() - start_pleroma() + check_configdb(fn -> + start_pleroma() - {opts, _} = - OptionParser.parse!(options, - strict: [env: :string, delete: :boolean], - aliases: [d: :delete] - ) + {opts, _} = + OptionParser.parse!(options, + strict: [env: :string, delete: :boolean], + aliases: [d: :delete] + ) - migrate_from_db(opts) + migrate_from_db(opts) + end) end def run(["dump"]) do - check_configdb() - start_pleroma() + check_configdb(fn -> + start_pleroma() - header = config_header() + header = config_header() - settings = - ConfigDB - |> Repo.all() - |> Enum.sort() + settings = + ConfigDB + |> Repo.all() + |> Enum.sort() - unless settings == [] do - shell_info("#{header}") + unless settings == [] do + shell_info("#{header}") - settings |> Enum.each(&dump(&1)) - else - shell_error("No settings in ConfigDB.") - end + settings |> Enum.each(&dump(&1)) + else + shell_error("No settings in ConfigDB.") + end + end) end def run(["dump", group, key]) do - check_configdb() - start_pleroma() + check_configdb(fn -> + start_pleroma() - group = maybe_atomize(group) - key = maybe_atomize(key) + group = maybe_atomize(group) + key = maybe_atomize(key) - dump_key(group, key) + dump_key(group, key) + end) end def run(["dump", group]) do - check_configdb() - start_pleroma() + check_configdb(fn -> + start_pleroma() - group = maybe_atomize(group) + group = maybe_atomize(group) - dump_group(group) + dump_group(group) + end) end def run(["groups"]) do - check_configdb() - start_pleroma() - - groups = - ConfigDB - |> Repo.all() - |> Enum.map(fn x -> x.group end) - |> Enum.sort() - |> Enum.uniq() + check_configdb(fn -> + start_pleroma() - if length(groups) > 0 do - shell_info("The following configuration groups are set in ConfigDB:\r\n") - groups |> Enum.each(fn x -> shell_info("- #{x}") end) - shell_info("\r\n") - end + groups = + ConfigDB + |> Repo.all() + |> Enum.map(fn x -> x.group end) + |> Enum.sort() + |> Enum.uniq() + + if length(groups) > 0 do + shell_info("The following configuration groups are set in ConfigDB:\r\n") + groups |> Enum.each(fn x -> shell_info("- #{x}") end) + shell_info("\r\n") + end + end) end def run(["reset"]) do - check_configdb() - start_pleroma() + check_configdb(fn -> + start_pleroma() - shell_info("The following settings will be permanently removed:") + shell_info("The following settings will be permanently removed:") - ConfigDB - |> Repo.all() - |> Enum.sort() - |> Enum.each(&dump(&1)) + ConfigDB + |> Repo.all() + |> Enum.sort() + |> Enum.each(&dump(&1)) - shell_error("\nTHIS CANNOT BE UNDONE!") + shell_error("\nTHIS CANNOT BE UNDONE!") - if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do - Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;") - Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;") + if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do + Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;") + Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;") - shell_info("The ConfigDB settings have been removed from the database.") - else - shell_error("No changes made.") - end + shell_info("The ConfigDB settings have been removed from the database.") + else + shell_error("No changes made.") + end + end) end def run(["delete", group]) do - check_configdb() - start_pleroma() + check_configdb(fn -> + start_pleroma() - group = maybe_atomize(group) + group = maybe_atomize(group) - if group_exists?(group) do - shell_info("The following settings will be removed from ConfigDB:\n") + if group_exists?(group) do + shell_info("The following settings will be removed from ConfigDB:\n") - dump_group(group) + dump_group(group) + + if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do + ConfigDB + |> Repo.all() + |> Enum.filter(fn x -> + if x.group == group do + x |> delete(true) + end + end) + else + shell_error("No changes made.") + end + else + shell_error("No settings in ConfigDB for #{inspect(group)}. Aborting.") + end + end) + end + + def run(["delete", group, key]) do + check_configdb(fn -> + start_pleroma() + + group = maybe_atomize(group) + key = maybe_atomize(key) if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do ConfigDB |> Repo.all() |> Enum.filter(fn x -> - if x.group == group do + if x.group == group and x.key == key do x |> delete(true) end end) else shell_error("No changes made.") end - else - shell_error("No settings in ConfigDB for #{inspect(group)}. Aborting.") - end - end - - def run(["delete", group, key]) do - check_configdb() - start_pleroma() - - group = maybe_atomize(group) - key = maybe_atomize(key) - - if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do - ConfigDB - |> Repo.all() - |> Enum.filter(fn x -> - if x.group == group and x.key == key do - x |> delete(true) - end - end) - else - shell_error("No changes made.") - end + end) end @spec migrate_to_db(Path.t() | nil) :: any() @@ -282,7 +291,7 @@ defp dump(%Pleroma.ConfigDB{} = config) do end defp configdb_not_enabled do - raise( + shell_error( "ConfigDB not enabled. Please check the value of :configurable_from_database in your configuration." ) end @@ -336,9 +345,9 @@ defp maybe_atomize(arg) when is_binary(arg) do end end - defp check_configdb() do + defp check_configdb(callback) do with true <- Pleroma.Config.get([:configurable_from_database]) do - :ok + callback.() else _ -> configdb_not_enabled() end diff --git a/test/mix/tasks/pleroma/config_test.exs b/test/mix/tasks/pleroma/config_test.exs index dfa04a508..9d6d5ce15 100644 --- a/test/mix/tasks/pleroma/config_test.exs +++ b/test/mix/tasks/pleroma/config_test.exs @@ -22,8 +22,6 @@ defmodule Mix.Tasks.Pleroma.ConfigTest do :ok end - setup_all do: clear_config(:configurable_from_database, true) - test "error if file with custom settings doesn't exist" do Mix.Tasks.Pleroma.Config.migrate_to_db("config/not_existance_config_file.exs") @@ -36,6 +34,7 @@ test "error if file with custom settings doesn't exist" do describe "migrate_to_db/1" do setup do + clear_config(:configurable_from_database, true) initial = Application.get_env(:quack, :level) on_exit(fn -> Application.put_env(:quack, :level, initial) end) end @@ -83,6 +82,7 @@ test "config table is truncated before migration" do describe "with deletion temp file" do setup do + clear_config(:configurable_from_database, true) temp_file = "config/temp.exported_from_db.secret.exs" on_exit(fn -> @@ -187,89 +187,114 @@ test "load a settings with large values and pass to file", %{temp_file: temp_fil end end - test "dumping a specific group" do - insert(:config, - group: :pleroma, - key: :instance, - value: [ - name: "Pleroma Test" - ] - ) - - insert(:config, - group: :web_push_encryption, - key: :vapid_details, - value: [ - subject: "mailto:administrator@example.com", - public_key: - "BOsPL-_KjNnjj_RMvLeR3dTOrcndi4TbMR0cu56gLGfGaT5m1gXxSfRHOcC4Dd78ycQL1gdhtx13qgKHmTM5xAI", - private_key: "Ism6FNdS31nLCA94EfVbJbDdJXCxAZ8cZiB1JQPN_t4" - ] - ) - - Mix.Tasks.Pleroma.Config.run(["dump", "pleroma"]) + describe "operations on database config" do + setup do: clear_config(:configurable_from_database, true) - assert_receive {:mix_shell, :info, - ["config :pleroma, :instance, [name: \"Pleroma Test\"]\r\n\r\n"]} - - refute_receive { - :mix_shell, - :info, - [ - "config :web_push_encryption, :vapid_details, [subject: \"mailto:administrator@example.com\", public_key: \"BOsPL-_KjNnjj_RMvLeR3dTOrcndi4TbMR0cu56gLGfGaT5m1gXxSfRHOcC4Dd78ycQL1gdhtx13qgKHmTM5xAI\", private_key: \"Ism6FNdS31nLCA94EfVbJbDdJXCxAZ8cZiB1JQPN_t4\"]\r\n\r\n" - ] - } - end + test "dumping a specific group" do + insert(:config, + group: :pleroma, + key: :instance, + value: [ + name: "Pleroma Test" + ] + ) - test "dumping a specific key in a group" do - insert(:config, - group: :pleroma, - key: :instance, - value: [ - name: "Pleroma Test" - ] - ) + insert(:config, + group: :web_push_encryption, + key: :vapid_details, + value: [ + subject: "mailto:administrator@example.com", + public_key: + "BOsPL-_KjNnjj_RMvLeR3dTOrcndi4TbMR0cu56gLGfGaT5m1gXxSfRHOcC4Dd78ycQL1gdhtx13qgKHmTM5xAI", + private_key: "Ism6FNdS31nLCA94EfVbJbDdJXCxAZ8cZiB1JQPN_t4" + ] + ) - insert(:config, - group: :pleroma, - key: Pleroma.Captcha, - value: [ - enabled: false - ] - ) + Mix.Tasks.Pleroma.Config.run(["dump", "pleroma"]) - Mix.Tasks.Pleroma.Config.run(["dump", "pleroma", "Pleroma.Captcha"]) + assert_receive {:mix_shell, :info, + ["config :pleroma, :instance, [name: \"Pleroma Test\"]\r\n\r\n"]} - refute_receive {:mix_shell, :info, - ["config :pleroma, :instance, [name: \"Pleroma Test\"]\r\n\r\n"]} + refute_receive { + :mix_shell, + :info, + [ + "config :web_push_encryption, :vapid_details, [subject: \"mailto:administrator@example.com\", public_key: \"BOsPL-_KjNnjj_RMvLeR3dTOrcndi4TbMR0cu56gLGfGaT5m1gXxSfRHOcC4Dd78ycQL1gdhtx13qgKHmTM5xAI\", private_key: \"Ism6FNdS31nLCA94EfVbJbDdJXCxAZ8cZiB1JQPN_t4\"]\r\n\r\n" + ] + } + end - assert_receive {:mix_shell, :info, - ["config :pleroma, Pleroma.Captcha, [enabled: false]\r\n\r\n"]} + test "dumping a specific key in a group" do + insert(:config, + group: :pleroma, + key: :instance, + value: [ + name: "Pleroma Test" + ] + ) + + insert(:config, + group: :pleroma, + key: Pleroma.Captcha, + value: [ + enabled: false + ] + ) + + Mix.Tasks.Pleroma.Config.run(["dump", "pleroma", "Pleroma.Captcha"]) + + refute_receive {:mix_shell, :info, + ["config :pleroma, :instance, [name: \"Pleroma Test\"]\r\n\r\n"]} + + assert_receive {:mix_shell, :info, + ["config :pleroma, Pleroma.Captcha, [enabled: false]\r\n\r\n"]} + end + + test "dumps all configuration successfully" do + insert(:config, + group: :pleroma, + key: :instance, + value: [ + name: "Pleroma Test" + ] + ) + + insert(:config, + group: :pleroma, + key: Pleroma.Captcha, + value: [ + enabled: false + ] + ) + + Mix.Tasks.Pleroma.Config.run(["dump"]) + + assert_receive {:mix_shell, :info, + ["config :pleroma, :instance, [name: \"Pleroma Test\"]\r\n\r\n"]} + + assert_receive {:mix_shell, :info, + ["config :pleroma, Pleroma.Captcha, [enabled: false]\r\n\r\n"]} + end end - test "dumps all configuration successfully" do - insert(:config, - group: :pleroma, - key: :instance, - value: [ - name: "Pleroma Test" - ] - ) - - insert(:config, - group: :pleroma, - key: Pleroma.Captcha, - value: [ - enabled: false - ] - ) - - Mix.Tasks.Pleroma.Config.run(["dump"]) + describe "when configdb disabled" do + test "refuses to dump" do + clear_config(:configurable_from_database, false) - assert_receive {:mix_shell, :info, - ["config :pleroma, :instance, [name: \"Pleroma Test\"]\r\n\r\n"]} + insert(:config, + group: :pleroma, + key: :instance, + value: [ + name: "Pleroma Test" + ] + ) - assert_receive {:mix_shell, :info, - ["config :pleroma, Pleroma.Captcha, [enabled: false]\r\n\r\n"]} + Mix.Tasks.Pleroma.Config.run(["dump"]) + + assert_receive {:mix_shell, :error, + [ + "ConfigDB not enabled. Please check the value of :configurable_from_database in your configuration." + ]} + end end end -- cgit v1.2.3 From 25fab7da69e2a6019598132e5d776d7cebe42045 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Wed, 2 Dec 2020 13:00:07 -0600 Subject: No need for a separate functions here --- lib/mix/tasks/pleroma/config.ex | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index d509f150e..e53e21a0b 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -184,7 +184,8 @@ def migrate_to_db(file_path \\ nil) do do_migrate_to_db(config_file) else - _ -> deprecation_error() + _ -> + shell_error("Migration is not allowed until all deprecation warnings have been resolved.") end end @@ -248,10 +249,6 @@ defp migrate_from_db(opts) do ) end - defp deprecation_error do - shell_error("Migration is not allowed until all deprecation warnings have been resolved.") - end - if Code.ensure_loaded?(Config.Reader) do defp config_header, do: "import Config\r\n\r\n" defp read_file(config_file), do: Config.Reader.read_imports!(config_file) @@ -290,12 +287,6 @@ defp dump(%Pleroma.ConfigDB{} = config) do shell_info("config #{inspect(config.group)}, #{inspect(config.key)}, #{value}\r\n\r\n") end - defp configdb_not_enabled do - shell_error( - "ConfigDB not enabled. Please check the value of :configurable_from_database in your configuration." - ) - end - defp dump_key(group, key) when is_atom(group) and is_atom(key) do ConfigDB |> Repo.all() @@ -349,7 +340,10 @@ defp check_configdb(callback) do with true <- Pleroma.Config.get([:configurable_from_database]) do callback.() else - _ -> configdb_not_enabled() + _ -> + shell_error( + "ConfigDB not enabled. Please check the value of :configurable_from_database in your configuration." + ) end end end -- cgit v1.2.3 From 20a911f9f725088e841f2ebce220b26b1b4fe222 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Wed, 2 Dec 2020 14:22:59 -0600 Subject: Add comment for this mysterious behavior --- lib/mix/tasks/pleroma/config.ex | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index e53e21a0b..e2c4cc680 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -329,6 +329,8 @@ defp maybe_atomize(arg) when is_atom(arg), do: arg defp maybe_atomize(arg) when is_binary(arg) do chars = String.codepoints(arg) + # hack to make sure input like Pleroma.Mailer.Foo is formatted correctly + # for matching against values returned by Ecto if "." in chars do :"Elixir.#{arg}" else -- cgit v1.2.3 From e379ab8277f552d66737963a9c908ae3fc01c1ff Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Wed, 2 Dec 2020 16:24:32 -0600 Subject: Add --force flag for delete and reset commands Bunch of reorganization and consolidation --- docs/administration/CLI_tasks/config.md | 12 ++-- lib/mix/tasks/pleroma/config.ex | 110 ++++++++++++++++++++------------ test/mix/tasks/pleroma/config_test.exs | 95 +++++++++++++++++++++++++++ 3 files changed, 170 insertions(+), 47 deletions(-) diff --git a/docs/administration/CLI_tasks/config.md b/docs/administration/CLI_tasks/config.md index ea07ca293..000ed4d98 100644 --- a/docs/administration/CLI_tasks/config.md +++ b/docs/administration/CLI_tasks/config.md @@ -111,13 +111,13 @@ e.g., this deletes all the settings under `config :tesla` === "OTP" ```sh - ./bin/pleroma_ctl config delete tesla + ./bin/pleroma_ctl config delete [--force] tesla ``` === "From Source" ```sh - mix pleroma.config delete tesla + mix pleroma.config delete [--force] tesla ``` To delete values under a specific key: @@ -127,13 +127,13 @@ e.g., this deletes all the settings under `config :phoenix, :stacktrace_depth` === "OTP" ```sh - ./bin/pleroma_ctl config delete phoenix stacktrace_depth + ./bin/pleroma_ctl config delete [--force] phoenix stacktrace_depth ``` === "From Source" ```sh - mix pleroma.config delete phoenix stacktrace_depth + mix pleroma.config delete [--force] phoenix stacktrace_depth ``` ## Remove all settings from the database @@ -143,11 +143,11 @@ This forcibly removes all saved values in the database. === "OTP" ```sh - ./bin/pleroma_ctl config reset + ./bin/pleroma_ctl config [--force] reset ``` === "From Source" ```sh - mix pleroma.config reset + mix pleroma.config [--force] reset ``` diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index e2c4cc680..014782c35 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -95,7 +95,7 @@ def run(["groups"]) do end) end - def run(["reset"]) do + def run(["reset" | options]) do check_configdb(fn -> start_pleroma() @@ -108,7 +108,11 @@ def run(["reset"]) do shell_error("\nTHIS CANNOT BE UNDONE!") - if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do + proceed? = + "--force" in options or + shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) + + if proceed? do Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;") Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;") @@ -119,53 +123,46 @@ def run(["reset"]) do end) end - def run(["delete", group]) do - check_configdb(fn -> - start_pleroma() + def run(["delete", "--force", group, key]) do + start_pleroma() - group = maybe_atomize(group) + group = maybe_atomize(group) + key = maybe_atomize(key) - if group_exists?(group) do - shell_info("The following settings will be removed from ConfigDB:\n") + delete_key(group, key) + end - dump_group(group) + def run(["delete", "--force", group]) do + start_pleroma() - if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do - ConfigDB - |> Repo.all() - |> Enum.filter(fn x -> - if x.group == group do - x |> delete(true) - end - end) - else - shell_error("No changes made.") - end - else - shell_error("No settings in ConfigDB for #{inspect(group)}. Aborting.") - end - end) + group = maybe_atomize(group) + + delete_group(group) end def run(["delete", group, key]) do - check_configdb(fn -> - start_pleroma() + start_pleroma() - group = maybe_atomize(group) - key = maybe_atomize(key) + group = maybe_atomize(group) + key = maybe_atomize(key) - if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do - ConfigDB - |> Repo.all() - |> Enum.filter(fn x -> - if x.group == group and x.key == key do - x |> delete(true) - end - end) - else - shell_error("No changes made.") - end - end) + if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do + delete_key(group, key) + else + shell_error("No changes made.") + end + end + + def run(["delete", group]) do + start_pleroma() + + group = maybe_atomize(group) + + if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do + delete_group(group) + else + shell_error("No changes made.") + end end @spec migrate_to_db(Path.t() | nil) :: any() @@ -275,7 +272,7 @@ defp delete(config, true) do {:ok, _} = Repo.delete(config) shell_info( - "config #{inspect(config.group)}, #{inspect(config.key)} deleted from the ConfigDB." + "config #{inspect(config.group)}, #{inspect(config.key)} was deleted from the ConfigDB." ) end @@ -348,4 +345,35 @@ defp check_configdb(callback) do ) end end + + defp delete_key(group, key) do + check_configdb(fn -> + ConfigDB + |> Repo.all() + |> Enum.filter(fn x -> + if x.group == group and x.key == key do + x |> delete(true) + end + end) + end) + end + + defp delete_group(group) do + check_configdb(fn -> + with true <- group_exists?(group) do + shell_info("The following settings will be removed from ConfigDB:\n") + dump_group(group) + + ConfigDB + |> Repo.all() + |> Enum.filter(fn x -> + if x.group == group do + x |> delete(true) + end + end) + else + _ -> shell_error("No settings in ConfigDB for #{inspect(group)}. Aborting.") + end + end) + end end diff --git a/test/mix/tasks/pleroma/config_test.exs b/test/mix/tasks/pleroma/config_test.exs index 9d6d5ce15..3658b3179 100644 --- a/test/mix/tasks/pleroma/config_test.exs +++ b/test/mix/tasks/pleroma/config_test.exs @@ -297,4 +297,99 @@ test "refuses to dump" do ]} end end + + describe "destructive operations" do + setup do: clear_config(:configurable_from_database, true) + + test "deletes group of settings" do + insert(:config, + group: :pleroma, + key: :instance, + value: [ + name: "Pleroma Test" + ] + ) + + _config_before = Repo.all(ConfigDB) + + assert config_before = [ + %Pleroma.ConfigDB{ + group: :pleroma, + key: :instance, + value: [name: "Pleroma Test"] + } + ] + + Mix.Tasks.Pleroma.Config.run(["delete", "--force", "pleroma"]) + + config_after = Repo.all(ConfigDB) + + refute config_after == config_before + end + + test "deletes specified key" do + insert(:config, + group: :pleroma, + key: :instance, + value: [ + name: "Pleroma Test" + ] + ) + + insert(:config, + group: :pleroma, + key: Pleroma.Captcha, + value: [ + enabled: false + ] + ) + + _config_before = Repo.all(ConfigDB) + + assert config_before = [ + %Pleroma.ConfigDB{ + group: :pleroma, + key: :instance, + value: [name: "Pleroma Test"] + }, + %Pleroma.ConfigDB{ + group: :pleroma, + key: Pleroma.Captcha, + value: [enabled: false] + } + ] + + Mix.Tasks.Pleroma.Config.run(["delete", "--force", "pleroma", "Pleroma.Captcha"]) + + config_after = Repo.all(ConfigDB) + + refute config_after == config_before + end + + test "resets entire config" do + insert(:config, + group: :pleroma, + key: :instance, + value: [ + name: "Pleroma Test" + ] + ) + + _config_before = Repo.all(ConfigDB) + + assert config_before = [ + %Pleroma.ConfigDB{ + group: :pleroma, + key: :instance, + value: [name: "Pleroma Test"] + } + ] + + Mix.Tasks.Pleroma.Config.run(["reset", "--force"]) + + config_after = Repo.all(ConfigDB) + + assert config_after == [] + end + end end -- cgit v1.2.3 From 16bdc2bcd0600ae4c1fcb55eaa84824af01ee61e Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Wed, 2 Dec 2020 16:34:23 -0600 Subject: Make the --force flag for reset command consistent with the others and deduplicate db truncation --- lib/mix/tasks/pleroma/config.ex | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index 014782c35..ebaf2c623 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -95,7 +95,15 @@ def run(["groups"]) do end) end - def run(["reset" | options]) do + def run(["reset", "--force"]) do + check_configdb(fn -> + start_pleroma() + truncatedb() + shell_info("The ConfigDB settings have been removed from the database.") + end) + end + + def run(["reset"]) do check_configdb(fn -> start_pleroma() @@ -108,13 +116,8 @@ def run(["reset" | options]) do shell_error("\nTHIS CANNOT BE UNDONE!") - proceed? = - "--force" in options or - shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) - - if proceed? do - Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;") - Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;") + if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do + truncatedb() shell_info("The ConfigDB settings have been removed from the database.") else @@ -189,8 +192,7 @@ def migrate_to_db(file_path \\ nil) do defp do_migrate_to_db(config_file) do if File.exists?(config_file) do shell_info("Migrating settings from file: #{Path.expand(config_file)}") - Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;") - Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;") + truncatedb() custom_config = config_file @@ -376,4 +378,9 @@ defp delete_group(group) do end end) end + + defp truncatedb() do + Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;") + Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;") + end end -- cgit v1.2.3 From 95e908e4e2273a4b07218e45b46ecbeaa0f08e1c Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Thu, 3 Dec 2020 09:58:24 -0600 Subject: Credo --- lib/mix/tasks/pleroma/config.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index ebaf2c623..a6173e0e2 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -379,7 +379,7 @@ defp delete_group(group) do end) end - defp truncatedb() do + defp truncatedb do Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;") Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;") end -- cgit v1.2.3 From 60c4ac0f708b4a67d6168ed327327dcb13e7219f Mon Sep 17 00:00:00 2001 From: feld Date: Thu, 3 Dec 2020 16:03:14 +0000 Subject: Apply 6 suggestion(s) to 1 file(s) --- lib/mix/tasks/pleroma/config.ex | 61 ++++++++++++++--------------------------- 1 file changed, 20 insertions(+), 41 deletions(-) diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index a6173e0e2..f4bb84a13 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -62,7 +62,10 @@ def run(["dump", group, key]) do group = maybe_atomize(group) key = maybe_atomize(key) - dump_key(group, key) + %{group: group, key: key} + |> ConfigDB.get_by_params() + |> Repo.all() + |> Enum.each(&dump/1) end) end @@ -297,44 +300,27 @@ defp dump_key(group, key) when is_atom(group) and is_atom(key) do end defp dump_group(group) when is_atom(group) do - ConfigDB + %{group: group} + |> ConfigDB.get_by_params() |> Repo.all() - |> Enum.filter(fn x -> - if x.group == group do - x |> dump - end - end) + |> Enum.each(&dump/1) end - defp group_exists?(group) when is_atom(group) do - result = - ConfigDB + defp group_exists?(group) do + %{group: group} + |> ConfigDB.get_by_params() |> Repo.all() - |> Enum.filter(fn x -> - if x.group == group do - x - end - end) - - unless result == [] do - true - else - false - end + |> Enum.empty?() end defp maybe_atomize(arg) when is_atom(arg), do: arg defp maybe_atomize(arg) when is_binary(arg) do - chars = String.codepoints(arg) - - # hack to make sure input like Pleroma.Mailer.Foo is formatted correctly - # for matching against values returned by Ecto - if "." in chars do - :"Elixir.#{arg}" + if Pleroma.ConfigDB.module_name?(arg) do + String.to_existing_atom("Elixir." <> arg) else String.to_atom(arg) - end + end end defp check_configdb(callback) do @@ -350,13 +336,9 @@ defp check_configdb(callback) do defp delete_key(group, key) do check_configdb(fn -> - ConfigDB + ConfigDB.get_by_params(%{group: group, key: key}) |> Repo.all() - |> Enum.filter(fn x -> - if x.group == group and x.key == key do - x |> delete(true) - end - end) + |> Enum.each(&delete(&1, true)) end) end @@ -366,13 +348,10 @@ defp delete_group(group) do shell_info("The following settings will be removed from ConfigDB:\n") dump_group(group) - ConfigDB - |> Repo.all() - |> Enum.filter(fn x -> - if x.group == group do - x |> delete(true) - end - end) + ConfigDB.get_by_params(%{group: group}) + |> Repo.all() + |> Enum.each(&delete(&1, true)) + else _ -> shell_error("No settings in ConfigDB for #{inspect(group)}. Aborting.") end -- cgit v1.2.3 From 7fd4f4908bc31b3b4cc9d73a79169c3b3f08714c Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Thu, 3 Dec 2020 10:03:44 -0600 Subject: dump_key/2 no longer used --- lib/mix/tasks/pleroma/config.ex | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index f4bb84a13..137aef038 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -289,16 +289,6 @@ defp dump(%Pleroma.ConfigDB{} = config) do shell_info("config #{inspect(config.group)}, #{inspect(config.key)}, #{value}\r\n\r\n") end - defp dump_key(group, key) when is_atom(group) and is_atom(key) do - ConfigDB - |> Repo.all() - |> Enum.filter(fn x -> - if x.group == group && x.key == key do - x |> dump - end - end) - end - defp dump_group(group) when is_atom(group) do %{group: group} |> ConfigDB.get_by_params() -- cgit v1.2.3 From a02eb8839650ecbf8bcad9bd6d346fc280985cae Mon Sep 17 00:00:00 2001 From: Alexander Strizhakov Date: Thu, 3 Dec 2020 19:34:23 +0300 Subject: config_db search methods --- lib/mix/tasks/pleroma/config.ex | 30 +++++++++++++----------------- lib/pleroma/config_db.ex | 12 +++++++++++- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index 137aef038..63d8c46b5 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -62,9 +62,8 @@ def run(["dump", group, key]) do group = maybe_atomize(group) key = maybe_atomize(key) - %{group: group, key: key} - |> ConfigDB.get_by_params() - |> Repo.all() + group + |> ConfigDB.get_all_by_group_and_key(key) |> Enum.each(&dump/1) end) end @@ -290,17 +289,15 @@ defp dump(%Pleroma.ConfigDB{} = config) do end defp dump_group(group) when is_atom(group) do - %{group: group} - |> ConfigDB.get_by_params() - |> Repo.all() + group + |> ConfigDB.get_all_by_group() |> Enum.each(&dump/1) end defp group_exists?(group) do - %{group: group} - |> ConfigDB.get_by_params() - |> Repo.all() - |> Enum.empty?() + group + |> ConfigDB.get_all_by_group() + |> Enum.empty?() end defp maybe_atomize(arg) when is_atom(arg), do: arg @@ -310,7 +307,7 @@ defp maybe_atomize(arg) when is_binary(arg) do String.to_existing_atom("Elixir." <> arg) else String.to_atom(arg) - end + end end defp check_configdb(callback) do @@ -326,8 +323,8 @@ defp check_configdb(callback) do defp delete_key(group, key) do check_configdb(fn -> - ConfigDB.get_by_params(%{group: group, key: key}) - |> Repo.all() + group + |> ConfigDB.get_all_by_group_and_key(key) |> Enum.each(&delete(&1, true)) end) end @@ -338,10 +335,9 @@ defp delete_group(group) do shell_info("The following settings will be removed from ConfigDB:\n") dump_group(group) - ConfigDB.get_by_params(%{group: group}) - |> Repo.all() - |> Enum.each(&delete(&1, true)) - + group + |> ConfigDB.get_all_by_group() + |> Enum.each(&delete(&1, true)) else _ -> shell_error("No settings in ConfigDB for #{inspect(group)}. Aborting.") end diff --git a/lib/pleroma/config_db.ex b/lib/pleroma/config_db.ex index e5b7811aa..2c3c0cb5c 100644 --- a/lib/pleroma/config_db.ex +++ b/lib/pleroma/config_db.ex @@ -6,7 +6,7 @@ defmodule Pleroma.ConfigDB do use Ecto.Schema import Ecto.Changeset - import Ecto.Query, only: [select: 3] + import Ecto.Query, only: [select: 3, from: 2] import Pleroma.Web.Gettext alias __MODULE__ @@ -41,6 +41,16 @@ def get_all_as_keyword do end) end + @spec get_all_by_group(atom() | String.t()) :: [t()] + def get_all_by_group(group) do + from(c in ConfigDB, where: c.group == ^group) |> Repo.all() + end + + @spec get_all_by_group_and_key(atom() | String.t(), atom() | String.t()) :: [t()] + def get_all_by_group_and_key(group, key) do + from(c in ConfigDB, where: c.group == ^group and c.key == ^key) |> Repo.all() + end + @spec get_by_params(map()) :: ConfigDB.t() | nil def get_by_params(params), do: Repo.get_by(ConfigDB, params) -- cgit v1.2.3 From 4aad066091b63d88dcffa20458a097407da4f5b0 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Fri, 4 Dec 2020 11:04:53 -0600 Subject: Use Enum.any? to ensure we return true if there are results --- lib/mix/tasks/pleroma/config.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index 63d8c46b5..d2e9a3760 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -297,7 +297,7 @@ defp dump_group(group) when is_atom(group) do defp group_exists?(group) do group |> ConfigDB.get_all_by_group() - |> Enum.empty?() + |> Enum.any?() end defp maybe_atomize(arg) when is_atom(arg), do: arg -- cgit v1.2.3 From 685e5c8509b4c08bb74eab2438912031ab9b1c19 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Fri, 4 Dec 2020 11:09:13 -0600 Subject: Use Pleroma.ConfigDB.delete/1 instead of rolling our own --- lib/mix/tasks/pleroma/config.ex | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index d2e9a3760..7ec791b36 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -323,9 +323,7 @@ defp check_configdb(callback) do defp delete_key(group, key) do check_configdb(fn -> - group - |> ConfigDB.get_all_by_group_and_key(key) - |> Enum.each(&delete(&1, true)) + Pleroma.ConfigDB.delete(%{group: group, key: key}) end) end -- cgit v1.2.3 From 696d39c3dc32da1e3e163abb413f42d68c3a731f Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Fri, 4 Dec 2020 11:19:58 -0600 Subject: Fix deleting an entire group. Also utilize Pleroma.ConfigDB.delete/1 --- lib/mix/tasks/pleroma/config.ex | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index 7ec791b36..00e7be6f4 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -334,8 +334,10 @@ defp delete_group(group) do dump_group(group) group - |> ConfigDB.get_all_by_group() - |> Enum.each(&delete(&1, true)) + |> Pleroma.ConfigDB.get_all_by_group() + |> Enum.each(fn config -> + Pleroma.ConfigDB.delete(%{group: config.group, key: config.key}) + end) else _ -> shell_error("No settings in ConfigDB for #{inspect(group)}. Aborting.") end -- cgit v1.2.3 From 3bf5c5b0156e1357db22df8e377c5cd5c5c8ea5a Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Fri, 4 Dec 2020 11:30:48 -0600 Subject: Ensure deleting entire group prints out settings that will be removed before actually removing them --- lib/mix/tasks/pleroma/config.ex | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index 00e7be6f4..99dfd0dc3 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -142,7 +142,13 @@ def run(["delete", "--force", group]) do group = maybe_atomize(group) - delete_group(group) + with true <- group_exists?(group) do + shell_info("The following settings will be removed from ConfigDB:\n") + dump_group(group) + delete_group(group) + else + _ -> shell_error("No settings in ConfigDB for #{inspect(group)}. Aborting.") + end end def run(["delete", group, key]) do @@ -163,10 +169,17 @@ def run(["delete", group]) do group = maybe_atomize(group) - if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do - delete_group(group) + with true <- group_exists?(group) do + shell_info("The following settings will be removed from ConfigDB:\n") + dump_group(group) + + if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do + delete_group(group) + else + shell_error("No changes made.") + end else - shell_error("No changes made.") + _ -> shell_error("No settings in ConfigDB for #{inspect(group)}. Aborting.") end end @@ -329,18 +342,11 @@ defp delete_key(group, key) do defp delete_group(group) do check_configdb(fn -> - with true <- group_exists?(group) do - shell_info("The following settings will be removed from ConfigDB:\n") - dump_group(group) - - group - |> Pleroma.ConfigDB.get_all_by_group() - |> Enum.each(fn config -> - Pleroma.ConfigDB.delete(%{group: config.group, key: config.key}) - end) - else - _ -> shell_error("No settings in ConfigDB for #{inspect(group)}. Aborting.") - end + group + |> Pleroma.ConfigDB.get_all_by_group() + |> Enum.each(fn config -> + Pleroma.ConfigDB.delete(%{group: config.group, key: config.key}) + end) end) end -- cgit v1.2.3 From 9dfda37821d663c4b2f8e113336a517d694abee0 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Fri, 4 Dec 2020 11:37:49 -0600 Subject: More compact representation --- lib/mix/tasks/pleroma/config.ex | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index 99dfd0dc3..25f1ca05d 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -344,9 +344,7 @@ defp delete_group(group) do check_configdb(fn -> group |> Pleroma.ConfigDB.get_all_by_group() - |> Enum.each(fn config -> - Pleroma.ConfigDB.delete(%{group: config.group, key: config.key}) - end) + |> Enum.each(&ConfigDB.delete/1) end) end -- cgit v1.2.3 From 24673b6ca35c5600f0d2a8f5b8b89a402f387bf6 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Sat, 5 Dec 2020 08:41:15 -0600 Subject: Add entry announcing new ConfigDB mix tasks --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f4ef66408..7fe68f4b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Ability to view remote timelines, with ex. `/api/v1/timelines/public?instance=lain.com` and streams `public:remote` and `public:remote:media`. - The site title is now injected as a `title` tag like preloads or metadata. - Password reset tokens now are not accepted after a certain age. +- Mix tasks to help with displaying and removing ConfigDB entries. See `mix pleroma.config`
API Changes -- cgit v1.2.3 From e00c66714590948ef917909779772155e20a3c96 Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Sun, 6 Dec 2020 18:02:30 +0300 Subject: [#3174] Refactoring: ConfigDB fetching functions, ConfigDB tests. Minor fixes. --- lib/mix/tasks/pleroma/config.ex | 22 +- lib/pleroma/config_db.ex | 8 +- test/mix/tasks/pleroma/config_test.exs | 233 +++++++-------------- .../controllers/config_controller_test.exs | 4 +- .../controllers/relay_controller_test.exs | 2 +- 5 files changed, 92 insertions(+), 177 deletions(-) diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index 25f1ca05d..b5d802948 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -5,6 +5,7 @@ defmodule Mix.Tasks.Pleroma.Config do use Mix.Task + import Ecto.Query import Mix.Pleroma alias Pleroma.ConfigDB @@ -48,7 +49,7 @@ def run(["dump"]) do unless settings == [] do shell_info("#{header}") - settings |> Enum.each(&dump(&1)) + Enum.each(settings, &dump(&1)) else shell_error("No settings in ConfigDB.") end @@ -63,8 +64,8 @@ def run(["dump", group, key]) do key = maybe_atomize(key) group - |> ConfigDB.get_all_by_group_and_key(key) - |> Enum.each(&dump/1) + |> ConfigDB.get_by_group_and_key(key) + |> dump() end) end @@ -84,10 +85,9 @@ def run(["groups"]) do groups = ConfigDB + |> distinct([c], true) + |> select([c], c.group) |> Repo.all() - |> Enum.map(fn x -> x.group end) - |> Enum.sort() - |> Enum.uniq() if length(groups) > 0 do shell_info("The following configuration groups are set in ConfigDB:\r\n") @@ -295,12 +295,14 @@ defp delete(config, true) do defp delete(_config, _), do: :ok - defp dump(%Pleroma.ConfigDB{} = config) do + defp dump(%ConfigDB{} = config) do value = inspect(config.value, limit: :infinity) shell_info("config #{inspect(config.group)}, #{inspect(config.key)}, #{value}\r\n\r\n") end + defp dump(_), do: :noop + defp dump_group(group) when is_atom(group) do group |> ConfigDB.get_all_by_group() @@ -316,7 +318,7 @@ defp group_exists?(group) do defp maybe_atomize(arg) when is_atom(arg), do: arg defp maybe_atomize(arg) when is_binary(arg) do - if Pleroma.ConfigDB.module_name?(arg) do + if ConfigDB.module_name?(arg) do String.to_existing_atom("Elixir." <> arg) else String.to_atom(arg) @@ -336,14 +338,14 @@ defp check_configdb(callback) do defp delete_key(group, key) do check_configdb(fn -> - Pleroma.ConfigDB.delete(%{group: group, key: key}) + ConfigDB.delete(%{group: group, key: key}) end) end defp delete_group(group) do check_configdb(fn -> group - |> Pleroma.ConfigDB.get_all_by_group() + |> ConfigDB.get_all_by_group() |> Enum.each(&ConfigDB.delete/1) end) end diff --git a/lib/pleroma/config_db.ex b/lib/pleroma/config_db.ex index 2c3c0cb5c..8e8bb732f 100644 --- a/lib/pleroma/config_db.ex +++ b/lib/pleroma/config_db.ex @@ -46,13 +46,13 @@ def get_all_by_group(group) do from(c in ConfigDB, where: c.group == ^group) |> Repo.all() end - @spec get_all_by_group_and_key(atom() | String.t(), atom() | String.t()) :: [t()] - def get_all_by_group_and_key(group, key) do - from(c in ConfigDB, where: c.group == ^group and c.key == ^key) |> Repo.all() + @spec get_by_group_and_key(atom() | String.t(), atom() | String.t()) :: t() | nil + def get_by_group_and_key(group, key) do + get_by_params(%{group: group, key: key}) end @spec get_by_params(map()) :: ConfigDB.t() | nil - def get_by_params(params), do: Repo.get_by(ConfigDB, params) + def get_by_params(%{group: _, key: _} = params), do: Repo.get_by(ConfigDB, params) @spec changeset(ConfigDB.t(), map()) :: Changeset.t() def changeset(config, params \\ %{}) do diff --git a/test/mix/tasks/pleroma/config_test.exs b/test/mix/tasks/pleroma/config_test.exs index 3658b3179..1ea9f5790 100644 --- a/test/mix/tasks/pleroma/config_test.exs +++ b/test/mix/tasks/pleroma/config_test.exs @@ -7,6 +7,7 @@ defmodule Mix.Tasks.Pleroma.ConfigTest do import Pleroma.Factory + alias Mix.Tasks.Pleroma.Config, as: MixTask alias Pleroma.ConfigDB alias Pleroma.Repo @@ -22,29 +23,41 @@ defmodule Mix.Tasks.Pleroma.ConfigTest do :ok end + defp config_records do + ConfigDB + |> Repo.all() + |> Enum.sort() + end + + defp insert_config_record(group, key, value) do + insert(:config, + group: group, + key: key, + value: value + ) + end + test "error if file with custom settings doesn't exist" do - Mix.Tasks.Pleroma.Config.migrate_to_db("config/not_existance_config_file.exs") + MixTask.migrate_to_db("config/non_existent_config_file.exs") + + msg = + "To migrate settings, you must define custom settings in config/non_existent_config_file.exs." - assert_receive {:mix_shell, :info, - [ - "To migrate settings, you must define custom settings in config/not_existance_config_file.exs." - ]}, - 15 + assert_receive {:mix_shell, :info, [^msg]}, 15 end describe "migrate_to_db/1" do setup do clear_config(:configurable_from_database, true) - initial = Application.get_env(:quack, :level) - on_exit(fn -> Application.put_env(:quack, :level, initial) end) + clear_config([:quack, :level]) end @tag capture_log: true test "config migration refused when deprecated settings are found" do clear_config([:media_proxy, :whitelist], ["domain_without_scheme.com"]) - assert Repo.all(ConfigDB) == [] + assert config_records() == [] - Mix.Tasks.Pleroma.Config.migrate_to_db("test/fixtures/config/temp.secret.exs") + MixTask.migrate_to_db("test/fixtures/config/temp.secret.exs") assert_received {:mix_shell, :error, [message]} @@ -53,9 +66,9 @@ test "config migration refused when deprecated settings are found" do end test "filtered settings are migrated to db" do - assert Repo.all(ConfigDB) == [] + assert config_records() == [] - Mix.Tasks.Pleroma.Config.migrate_to_db("test/fixtures/config/temp.secret.exs") + MixTask.migrate_to_db("test/fixtures/config/temp.secret.exs") config1 = ConfigDB.get_by_params(%{group: ":pleroma", key: ":first_setting"}) config2 = ConfigDB.get_by_params(%{group: ":pleroma", key: ":second_setting"}) @@ -70,17 +83,17 @@ test "filtered settings are migrated to db" do end test "config table is truncated before migration" do - insert(:config, key: :first_setting, value: [key: "value", key2: ["Activity"]]) - assert Repo.aggregate(ConfigDB, :count, :id) == 1 + insert_config_record(:pleroma, :first_setting, key: "value", key2: ["Activity"]) + assert length(config_records()) == 1 - Mix.Tasks.Pleroma.Config.migrate_to_db("test/fixtures/config/temp.secret.exs") + MixTask.migrate_to_db("test/fixtures/config/temp.secret.exs") config = ConfigDB.get_by_params(%{group: ":pleroma", key: ":first_setting"}) assert config.value == [key: "value", key2: [Repo]] end end - describe "with deletion temp file" do + describe "with deletion of temp file" do setup do clear_config(:configurable_from_database, true) temp_file = "config/temp.exported_from_db.secret.exs" @@ -93,13 +106,13 @@ test "config table is truncated before migration" do end test "settings are migrated to file and deleted from db", %{temp_file: temp_file} do - insert(:config, key: :setting_first, value: [key: "value", key2: ["Activity"]]) - insert(:config, key: :setting_second, value: [key: "value2", key2: [Repo]]) - insert(:config, group: :quack, key: :level, value: :info) + insert_config_record(:pleroma, :setting_first, key: "value", key2: ["Activity"]) + insert_config_record(:pleroma, :setting_second, key: "value2", key2: [Repo]) + insert_config_record(:quack, :level, :info) - Mix.Tasks.Pleroma.Config.run(["migrate_from_db", "--env", "temp", "-d"]) + MixTask.run(["migrate_from_db", "--env", "temp", "-d"]) - assert Repo.all(ConfigDB) == [] + assert config_records() == [] file = File.read!(temp_file) assert file =~ "config :pleroma, :setting_first," @@ -169,9 +182,9 @@ test "load a settings with large values and pass to file", %{temp_file: temp_fil ] ) - Mix.Tasks.Pleroma.Config.run(["migrate_from_db", "--env", "temp", "-d"]) + MixTask.run(["migrate_from_db", "--env", "temp", "-d"]) - assert Repo.all(ConfigDB) == [] + assert config_records() == [] assert File.exists?(temp_file) {:ok, file} = File.read(temp_file) @@ -191,26 +204,16 @@ test "load a settings with large values and pass to file", %{temp_file: temp_fil setup do: clear_config(:configurable_from_database, true) test "dumping a specific group" do - insert(:config, - group: :pleroma, - key: :instance, - value: [ - name: "Pleroma Test" - ] - ) + insert_config_record(:pleroma, :instance, name: "Pleroma Test") - insert(:config, - group: :web_push_encryption, - key: :vapid_details, - value: [ - subject: "mailto:administrator@example.com", - public_key: - "BOsPL-_KjNnjj_RMvLeR3dTOrcndi4TbMR0cu56gLGfGaT5m1gXxSfRHOcC4Dd78ycQL1gdhtx13qgKHmTM5xAI", - private_key: "Ism6FNdS31nLCA94EfVbJbDdJXCxAZ8cZiB1JQPN_t4" - ] + insert_config_record(:web_push_encryption, :vapid_details, + subject: "mailto:administrator@example.com", + public_key: + "BOsPL-_KjNnjj_RMvLeR3dTOrcndi4TbMR0cu56gLGfGaT5m1gXxSfRHOcC4Dd78ycQL1gdhtx13qgKHmTM5xAI", + private_key: "Ism6FNdS31nLCA94EfVbJbDdJXCxAZ8cZiB1JQPN_t4" ) - Mix.Tasks.Pleroma.Config.run(["dump", "pleroma"]) + MixTask.run(["dump", "pleroma"]) assert_receive {:mix_shell, :info, ["config :pleroma, :instance, [name: \"Pleroma Test\"]\r\n\r\n"]} @@ -225,23 +228,10 @@ test "dumping a specific group" do end test "dumping a specific key in a group" do - insert(:config, - group: :pleroma, - key: :instance, - value: [ - name: "Pleroma Test" - ] - ) + insert_config_record(:pleroma, :instance, name: "Pleroma Test") + insert_config_record(:pleroma, Pleroma.Captcha, enabled: false) - insert(:config, - group: :pleroma, - key: Pleroma.Captcha, - value: [ - enabled: false - ] - ) - - Mix.Tasks.Pleroma.Config.run(["dump", "pleroma", "Pleroma.Captcha"]) + MixTask.run(["dump", "pleroma", "Pleroma.Captcha"]) refute_receive {:mix_shell, :info, ["config :pleroma, :instance, [name: \"Pleroma Test\"]\r\n\r\n"]} @@ -251,23 +241,10 @@ test "dumping a specific key in a group" do end test "dumps all configuration successfully" do - insert(:config, - group: :pleroma, - key: :instance, - value: [ - name: "Pleroma Test" - ] - ) + insert_config_record(:pleroma, :instance, name: "Pleroma Test") + insert_config_record(:pleroma, Pleroma.Captcha, enabled: false) - insert(:config, - group: :pleroma, - key: Pleroma.Captcha, - value: [ - enabled: false - ] - ) - - Mix.Tasks.Pleroma.Config.run(["dump"]) + MixTask.run(["dump"]) assert_receive {:mix_shell, :info, ["config :pleroma, :instance, [name: \"Pleroma Test\"]\r\n\r\n"]} @@ -281,115 +258,49 @@ test "dumps all configuration successfully" do test "refuses to dump" do clear_config(:configurable_from_database, false) - insert(:config, - group: :pleroma, - key: :instance, - value: [ - name: "Pleroma Test" - ] - ) + insert_config_record(:pleroma, :instance, name: "Pleroma Test") + + MixTask.run(["dump"]) - Mix.Tasks.Pleroma.Config.run(["dump"]) + msg = + "ConfigDB not enabled. Please check the value of :configurable_from_database in your configuration." - assert_receive {:mix_shell, :error, - [ - "ConfigDB not enabled. Please check the value of :configurable_from_database in your configuration." - ]} + assert_receive {:mix_shell, :error, [^msg]} end end describe "destructive operations" do setup do: clear_config(:configurable_from_database, true) - test "deletes group of settings" do - insert(:config, - group: :pleroma, - key: :instance, - value: [ - name: "Pleroma Test" - ] - ) - - _config_before = Repo.all(ConfigDB) + setup do + insert_config_record(:pleroma, :instance, name: "Pleroma Test") + insert_config_record(:pleroma, Pleroma.Captcha, enabled: false) + insert_config_record(:pleroma2, :key2, z: 1) - assert config_before = [ - %Pleroma.ConfigDB{ - group: :pleroma, - key: :instance, - value: [name: "Pleroma Test"] - } - ] + assert length(config_records()) == 3 - Mix.Tasks.Pleroma.Config.run(["delete", "--force", "pleroma"]) + :ok + end - config_after = Repo.all(ConfigDB) + test "deletes group of settings" do + MixTask.run(["delete", "--force", "pleroma"]) - refute config_after == config_before + assert [%ConfigDB{group: :pleroma2, key: :key2}] = config_records() end test "deletes specified key" do - insert(:config, - group: :pleroma, - key: :instance, - value: [ - name: "Pleroma Test" - ] - ) + MixTask.run(["delete", "--force", "pleroma", "Pleroma.Captcha"]) - insert(:config, - group: :pleroma, - key: Pleroma.Captcha, - value: [ - enabled: false - ] - ) - - _config_before = Repo.all(ConfigDB) - - assert config_before = [ - %Pleroma.ConfigDB{ - group: :pleroma, - key: :instance, - value: [name: "Pleroma Test"] - }, - %Pleroma.ConfigDB{ - group: :pleroma, - key: Pleroma.Captcha, - value: [enabled: false] - } - ] - - Mix.Tasks.Pleroma.Config.run(["delete", "--force", "pleroma", "Pleroma.Captcha"]) - - config_after = Repo.all(ConfigDB) - - refute config_after == config_before + assert [ + %ConfigDB{group: :pleroma, key: :instance}, + %ConfigDB{group: :pleroma2, key: :key2} + ] = config_records() end test "resets entire config" do - insert(:config, - group: :pleroma, - key: :instance, - value: [ - name: "Pleroma Test" - ] - ) - - _config_before = Repo.all(ConfigDB) - - assert config_before = [ - %Pleroma.ConfigDB{ - group: :pleroma, - key: :instance, - value: [name: "Pleroma Test"] - } - ] - - Mix.Tasks.Pleroma.Config.run(["reset", "--force"]) - - config_after = Repo.all(ConfigDB) + MixTask.run(["reset", "--force"]) - assert config_after == [] + assert config_records() == [] end end end diff --git a/test/pleroma/web/admin_api/controllers/config_controller_test.exs b/test/pleroma/web/admin_api/controllers/config_controller_test.exs index 4e897455f..276e827d1 100644 --- a/test/pleroma/web/admin_api/controllers/config_controller_test.exs +++ b/test/pleroma/web/admin_api/controllers/config_controller_test.exs @@ -162,7 +162,9 @@ test "with valid `admin_token` query parameter, skips OAuth scopes check" do end end - test "POST /api/pleroma/admin/config error", %{conn: conn} do + test "POST /api/pleroma/admin/config with configdb disabled", %{conn: conn} do + clear_config(:configurable_from_database, false) + conn = conn |> put_req_header("content-type", "application/json") diff --git a/test/pleroma/web/admin_api/controllers/relay_controller_test.exs b/test/pleroma/web/admin_api/controllers/relay_controller_test.exs index b4c5e7567..379067a62 100644 --- a/test/pleroma/web/admin_api/controllers/relay_controller_test.exs +++ b/test/pleroma/web/admin_api/controllers/relay_controller_test.exs @@ -60,7 +60,7 @@ test "GET /relay", %{conn: conn} do conn = get(conn, "/api/pleroma/admin/relay") - assert json_response_and_validate_schema(conn, 200)["relays"] == [ + assert json_response_and_validate_schema(conn, 200)["relays"] |> Enum.sort() == [ %{ "actor" => "http://mastodon.example.org/users/admin", "followed_back" => true -- cgit v1.2.3 From d817bae802c40bd2db9a88970cf24e98374b0af0 Mon Sep 17 00:00:00 2001 From: feld Date: Mon, 7 Dec 2020 17:13:29 +0000 Subject: Apply 1 suggestion(s) to 1 file(s) --- lib/mix/tasks/pleroma/config.ex | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index b5d802948..2ecad3578 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -316,6 +316,8 @@ defp group_exists?(group) do end defp maybe_atomize(arg) when is_atom(arg), do: arg + + defp maybe_atomize(":" <> arg), do: maybe_atomize(arg) defp maybe_atomize(arg) when is_binary(arg) do if ConfigDB.module_name?(arg) do -- cgit v1.2.3 From e3dd0d45b7f4c767ec826753f24c73fd6e07c12d Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Mon, 7 Dec 2020 11:21:06 -0600 Subject: Slip in a test to ensure we can use the atom syntax in mix task arguments --- test/mix/tasks/pleroma/config_test.exs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/mix/tasks/pleroma/config_test.exs b/test/mix/tasks/pleroma/config_test.exs index 1ea9f5790..0280d208d 100644 --- a/test/mix/tasks/pleroma/config_test.exs +++ b/test/mix/tasks/pleroma/config_test.exs @@ -225,6 +225,12 @@ test "dumping a specific group" do "config :web_push_encryption, :vapid_details, [subject: \"mailto:administrator@example.com\", public_key: \"BOsPL-_KjNnjj_RMvLeR3dTOrcndi4TbMR0cu56gLGfGaT5m1gXxSfRHOcC4Dd78ycQL1gdhtx13qgKHmTM5xAI\", private_key: \"Ism6FNdS31nLCA94EfVbJbDdJXCxAZ8cZiB1JQPN_t4\"]\r\n\r\n" ] } + + # Ensure operations work when using atom syntax + MixTask.run(["dump", ":pleroma"]) + + assert_receive {:mix_shell, :info, + ["config :pleroma, :instance, [name: \"Pleroma Test\"]\r\n\r\n"]} end test "dumping a specific key in a group" do -- cgit v1.2.3 From 61494b5245619eda38f05d010511df068280cff8 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Mon, 7 Dec 2020 11:22:07 -0600 Subject: Formatting --- lib/mix/tasks/pleroma/config.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index 2ecad3578..d1af0a60c 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -316,8 +316,8 @@ defp group_exists?(group) do end defp maybe_atomize(arg) when is_atom(arg), do: arg - - defp maybe_atomize(":" <> arg), do: maybe_atomize(arg) + + defp maybe_atomize(":" <> arg), do: maybe_atomize(arg) defp maybe_atomize(arg) when is_binary(arg) do if ConfigDB.module_name?(arg) do -- cgit v1.2.3 From 93428d7c11ce30d38fa23192c9a15e2e713a50be Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Mon, 7 Dec 2020 11:45:56 -0600 Subject: Print out settings that will be removed when specifying the group and key for consistency Fix error message when specified key doesn't exist --- lib/mix/tasks/pleroma/config.ex | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index d1af0a60c..d7e2e97e7 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -134,7 +134,18 @@ def run(["delete", "--force", group, key]) do group = maybe_atomize(group) key = maybe_atomize(key) - delete_key(group, key) + with true <- key_exists?(group, key) do + shell_info("The following settings will be removed from ConfigDB:\n") + + group + |> ConfigDB.get_by_group_and_key(key) + |> dump() + + delete_key(group, key) + else + _ -> + shell_error("No settings in ConfigDB for #{inspect(group)}, #{inspect(key)}. Aborting.") + end end def run(["delete", "--force", group]) do @@ -157,10 +168,21 @@ def run(["delete", group, key]) do group = maybe_atomize(group) key = maybe_atomize(key) - if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do - delete_key(group, key) + with true <- key_exists?(group, key) do + shell_info("The following settings will be removed from ConfigDB:\n") + + group + |> ConfigDB.get_by_group_and_key(key) + |> dump() + + if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do + delete_key(group, key) + else + shell_error("No changes made.") + end else - shell_error("No changes made.") + _ -> + shell_error("No settings in ConfigDB for #{inspect(group)}, #{inspect(key)}. Aborting.") end end @@ -315,6 +337,13 @@ defp group_exists?(group) do |> Enum.any?() end + defp key_exists?(group, key) do + group + |> ConfigDB.get_by_group_and_key(key) + |> is_nil + |> Kernel.!() + end + defp maybe_atomize(arg) when is_atom(arg), do: arg defp maybe_atomize(":" <> arg), do: maybe_atomize(arg) -- cgit v1.2.3 From e1a2e8b17cca0d9f50b72fcea0ec5ffb8e613db1 Mon Sep 17 00:00:00 2001 From: "Haelwenn (lanodan) Monnier" Date: Mon, 7 Dec 2020 20:09:34 +0100 Subject: instance: Do not fetch unreachable instances Closes: https://git.pleroma.social/pleroma/pleroma/-/issues/2346 --- lib/pleroma/instances/instance.ex | 13 +++++++++++-- test/pleroma/instances/instance_test.exs | 9 +++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/instances/instance.ex b/lib/pleroma/instances/instance.ex index df471a39d..c9ca3aac7 100644 --- a/lib/pleroma/instances/instance.ex +++ b/lib/pleroma/instances/instance.ex @@ -166,7 +166,8 @@ def get_or_update_favicon(%URI{host: host} = instance_uri) do defp scrape_favicon(%URI{} = instance_uri) do try do - with {:ok, %Tesla.Env{body: html}} <- + with {_, true} <- {:reachable, reachable?(instance_uri.host)}, + {:ok, %Tesla.Env{body: html}} <- Pleroma.HTTP.get(to_string(instance_uri), [{"accept", "text/html"}], pool: :media), {_, [favicon_rel | _]} when is_binary(favicon_rel) <- {:parse, @@ -175,7 +176,15 @@ defp scrape_favicon(%URI{} = instance_uri) do {:merge, URI.merge(instance_uri, favicon_rel) |> to_string()} do favicon else - _ -> nil + {:reachable, false} -> + Logger.debug( + "Instance.scrape_favicon(\"#{to_string(instance_uri)}\") ignored unreachable host" + ) + + nil + + _ -> + nil end rescue e -> diff --git a/test/pleroma/instances/instance_test.exs b/test/pleroma/instances/instance_test.exs index 4f0805100..2c6389e4f 100644 --- a/test/pleroma/instances/instance_test.exs +++ b/test/pleroma/instances/instance_test.exs @@ -3,6 +3,7 @@ # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.Instances.InstanceTest do + alias Pleroma.Instances alias Pleroma.Instances.Instance alias Pleroma.Repo @@ -148,5 +149,13 @@ test "Handles not getting a favicon URL properly" do ) end) =~ "Instance.scrape_favicon(\"https://no-favicon.example.org/\") error: " end + + test "Doesn't scrapes unreachable instances" do + instance = insert(:instance, unreachable_since: Instances.reachability_datetime_threshold()) + url = "https://" <> instance.host + + assert capture_log(fn -> assert nil == Instance.get_or_update_favicon(URI.parse(url)) end) =~ + "Instance.scrape_favicon(\"#{url}\") ignored unreachable host" + end end end -- cgit v1.2.3 From 1403798820da21660fb8787ffaf9f54817597636 Mon Sep 17 00:00:00 2001 From: "Haelwenn (lanodan) Monnier" Date: Mon, 7 Dec 2020 21:18:51 +0100 Subject: instance.reachable?: Limit to binary input --- lib/pleroma/instances/instance.ex | 2 +- test/pleroma/instances_test.exs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/pleroma/instances/instance.ex b/lib/pleroma/instances/instance.ex index c9ca3aac7..2e1696fe2 100644 --- a/lib/pleroma/instances/instance.ex +++ b/lib/pleroma/instances/instance.ex @@ -77,7 +77,7 @@ def reachable?(url_or_host) when is_binary(url_or_host) do ) end - def reachable?(_), do: true + def reachable?(url_or_host) when is_binary(url_or_host), do: true def set_reachable(url_or_host) when is_binary(url_or_host) do with host <- host(url_or_host), diff --git a/test/pleroma/instances_test.exs b/test/pleroma/instances_test.exs index d2618025c..5d0ce6237 100644 --- a/test/pleroma/instances_test.exs +++ b/test/pleroma/instances_test.exs @@ -32,9 +32,9 @@ test "returns `true` for host / url marked unreachable for less than `reachabili assert Instances.reachable?(URI.parse(url).host) end - test "returns true on non-binary input" do - assert Instances.reachable?(nil) - assert Instances.reachable?(1) + test "raises FunctionClauseError exception on non-binary input" do + assert_raise FunctionClauseError, fn -> Instances.reachable?(nil) end + assert_raise FunctionClauseError, fn -> Instances.reachable?(1) end end end -- cgit v1.2.3 From fb3fd692c66ca0f09c25067c2d024157144e1118 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Mon, 7 Dec 2020 16:36:44 -0600 Subject: Add a startup error for modified Repo pool_size --- lib/pleroma/application_requirements.ex | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/pleroma/application_requirements.ex b/lib/pleroma/application_requirements.ex index b977257a3..41f6c6e34 100644 --- a/lib/pleroma/application_requirements.ex +++ b/lib/pleroma/application_requirements.ex @@ -24,6 +24,7 @@ def verify! do |> check_migrations_applied!() |> check_welcome_message_config!() |> check_rum!() + |> check_repo_pool_size!() |> handle_result() end @@ -188,6 +189,24 @@ defp check_system_commands!(:ok) do defp check_system_commands!(result), do: result + defp check_repo_pool_size!(:ok) do + if Pleroma.Config.get([Pleroma.Repo, :pool_size], 10) != 10 and + not Pleroma.Config.get([:dangerzone, :override_repo_pool_size], false) do + Logger.error(""" + !!!CONFIG WARNING!!! + The database pool size has been altered from the recommended value of 10.\n + Please revert or ensure your database is tuned appropriately and then set\n + `config :pleroma, :dangerzone, override_repo_pool_size: true`. + """) + + {:error, "Repo.pool_size above recommended value."} + else + :ok + end + end + + defp check_repo_pool_size!(result), do: result + defp check_filter(filter, command_required) do filters = Config.get([Pleroma.Upload, :filters]) -- cgit v1.2.3 From 5b9b7b488807e86ecf3c648e8c6a1f1d86bf9806 Mon Sep 17 00:00:00 2001 From: lain Date: Tue, 8 Dec 2020 16:16:43 +0000 Subject: Apply 1 suggestion(s) to 1 file(s) --- lib/pleroma/application_requirements.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pleroma/application_requirements.ex b/lib/pleroma/application_requirements.ex index 41f6c6e34..2c1864ef1 100644 --- a/lib/pleroma/application_requirements.ex +++ b/lib/pleroma/application_requirements.ex @@ -199,7 +199,7 @@ defp check_repo_pool_size!(:ok) do `config :pleroma, :dangerzone, override_repo_pool_size: true`. """) - {:error, "Repo.pool_size above recommended value."} + {:error, "Repo.pool_size different than recommended value."} else :ok end -- cgit v1.2.3 From 50d16a9e27189800f69901c4e90aa6f41bdf3193 Mon Sep 17 00:00:00 2001 From: lain Date: Tue, 8 Dec 2020 17:30:10 +0100 Subject: ApplicationRequirements: Add test, more text for pool size. --- lib/pleroma/application_requirements.ex | 10 ++++++++-- test/pleroma/application_requirements_test.exs | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/application_requirements.ex b/lib/pleroma/application_requirements.ex index 2c1864ef1..e61576644 100644 --- a/lib/pleroma/application_requirements.ex +++ b/lib/pleroma/application_requirements.ex @@ -194,9 +194,15 @@ defp check_repo_pool_size!(:ok) do not Pleroma.Config.get([:dangerzone, :override_repo_pool_size], false) do Logger.error(""" !!!CONFIG WARNING!!! - The database pool size has been altered from the recommended value of 10.\n - Please revert or ensure your database is tuned appropriately and then set\n + + The database pool size has been altered from the recommended value of 10. + + Please revert or ensure your database is tuned appropriately and then set `config :pleroma, :dangerzone, override_repo_pool_size: true`. + + If you are experiencing database timeouts, please check the "Optimizing + your PostgreSQL performance" section in the documentation. If you still + encounter issues after that, please open an issue on the tracker. """) {:error, "Repo.pool_size different than recommended value."} diff --git a/test/pleroma/application_requirements_test.exs b/test/pleroma/application_requirements_test.exs index c505ae229..b432dbc37 100644 --- a/test/pleroma/application_requirements_test.exs +++ b/test/pleroma/application_requirements_test.exs @@ -12,6 +12,25 @@ defmodule Pleroma.ApplicationRequirementsTest do alias Pleroma.Config alias Pleroma.Repo + describe "check_repo_pool_size!/1" do + test "raises if the pool size is unexpected" do + clear_config([Pleroma.Repo, :pool_size], 11) + + assert_raise Pleroma.ApplicationRequirements.VerifyError, + "Repo.pool_size different than recommended value.", + fn -> + capture_log(&Pleroma.ApplicationRequirements.verify!/0) + end + end + + test "doesn't raise if the pool size is unexpected but the respective flag is set" do + clear_config([Pleroma.Repo, :pool_size], 11) + clear_config([:dangerzone, :override_repo_pool_size], true) + + assert Pleroma.ApplicationRequirements.verify!() == :ok + end + end + describe "check_welcome_message_config!/1" do setup do: clear_config([:welcome]) setup do: clear_config([Pleroma.Emails.Mailer]) -- cgit v1.2.3