From 2753285b7722fdb47f0ebb2180e997cf72f65d1a Mon Sep 17 00:00:00 2001 From: Alex S Date: Sun, 29 Sep 2019 11:17:38 +0300 Subject: config editing through database --- lib/mix/tasks/pleroma/config.ex | 107 +++++++++++++++++++++++++--------------- 1 file changed, 67 insertions(+), 40 deletions(-) (limited to 'lib/mix/tasks') diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index 590c7a914..bb126463c 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -9,27 +9,29 @@ defmodule Mix.Tasks.Pleroma.Config do alias Pleroma.Web.AdminAPI.Config @shortdoc "Manages the location of the config" @moduledoc File.read!("docs/administration/CLI_tasks/config.md") + + @groups [ + :pleroma, + :logger, + :quack, + :mime, + :tesla, + :phoenix, + :cors_plug, + :auto_linker, + :esshd, + :ueberauth, + :prometheus, + :http_signatures, + :web_push_encryption, + :joken + ] + def run(["migrate_to_db"]) do start_pleroma() if Pleroma.Config.get([:instance, :dynamic_configuration]) do - Application.get_all_env(:pleroma) - |> Enum.reject(fn {k, _v} -> k in [Pleroma.Repo, :env] end) - |> Enum.each(fn {k, v} -> - key = to_string(k) |> String.replace("Elixir.", "") - - key = - if String.starts_with?(key, "Pleroma.") do - key - else - ":" <> key - end - - {:ok, _} = Config.update_or_create(%{group: "pleroma", key: key, value: v}) - Mix.shell().info("#{key} is migrated.") - end) - - Mix.shell().info("Settings migrated.") + Enum.each(@groups, &load_and_create(&1)) else Mix.shell().info( "Migration is not allowed by config. You can change this behavior in instance settings." @@ -37,38 +39,63 @@ defmodule Mix.Tasks.Pleroma.Config do end end - def run(["migrate_from_db", env, delete?]) do + def run(["migrate_from_db" | options]) do start_pleroma() - delete? = if delete? == "true", do: true, else: false - - if Pleroma.Config.get([:instance, :dynamic_configuration]) do - config_path = "config/#{env}.exported_from_db.secret.exs" + {opts, _} = + OptionParser.parse!(options, + strict: [env: :string, delete_from_db: :boolean], + aliases: [d: :delete_from_db] + ) - {:ok, file} = File.open(config_path, [:write, :utf8]) + with {:active?, true} <- {:active?, Pleroma.Config.get([:instance, :dynamic_configuration])}, + env_path when is_binary(env_path) <- opts[:env], + config_path <- "config/#{env_path}.exported_from_db.secret.exs", + {:ok, file} <- File.open(config_path, [:write, :utf8]) do IO.write(file, "use Mix.Config\r\n") - Repo.all(Config) - |> Enum.each(fn config -> - IO.write( - file, - "config :#{config.group}, #{config.key}, #{ - inspect(Config.from_binary(config.value), limit: :infinity) - }\r\n\r\n" - ) - - if delete? do - {:ok, _} = Repo.delete(config) - Mix.shell().info("#{config.key} deleted from DB.") - end - end) + Config + |> Repo.all() + |> Enum.each(&write_to_file_with_deletion(&1, file, opts[:delete_from_db])) File.close(file) System.cmd("mix", ["format", config_path]) else - Mix.shell().info( - "Migration is not allowed by config. You can change this behavior in instance settings." - ) + {:active?, false} -> + Mix.shell().info( + "migration is not allowed by config. You can change this behavior in instance settings." + ) + + error -> + Mix.shell().info("error occuried while opening file. #{inspect(error)}") + end + end + + defp load_and_create(group) do + group + |> Application.get_all_env() + |> Enum.reject(fn {k, _v} -> k in [Pleroma.Repo, :env] end) + |> Enum.each(fn {key, value} -> + key_str = inspect(key) + + {:ok, _} = Config.update_or_create(%{group: ":#{group}", key: key_str, value: value}) + Mix.shell().info("settings for key #{key_str} migrated.") + end) + + Mix.shell().info("settings for group :#{group} migrated.") + end + + defp write_to_file_with_deletion(config, file, with_deletion) do + IO.write( + file, + "config #{config.group}, #{config.key}, #{ + inspect(Config.from_binary(config.value), limit: :infinity) + }\r\n\r\n" + ) + + if with_deletion do + {:ok, _} = Repo.delete(config) + Mix.shell().info("#{config.key} deleted from DB.") end end end -- cgit v1.2.3 From 0656816c77875d87d64d89e0e549f73104104cfb Mon Sep 17 00:00:00 2001 From: Alexander Date: Fri, 6 Dec 2019 08:21:30 +0300 Subject: tests for setttings without an explicit key --- lib/mix/tasks/pleroma/config.ex | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lib/mix/tasks') diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index bb126463c..cef02b864 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -76,10 +76,10 @@ defmodule Mix.Tasks.Pleroma.Config do |> Application.get_all_env() |> Enum.reject(fn {k, _v} -> k in [Pleroma.Repo, :env] end) |> Enum.each(fn {key, value} -> - key_str = inspect(key) + key = inspect(key) + {:ok, _} = Config.update_or_create(%{group: inspect(group), key: key, value: value}) - {:ok, _} = Config.update_or_create(%{group: ":#{group}", key: key_str, value: value}) - Mix.shell().info("settings for key #{key_str} migrated.") + Mix.shell().info("settings for key #{key} migrated.") end) Mix.shell().info("settings for group :#{group} migrated.") -- cgit v1.2.3 From a71393dd29488eb86d6da23250b05a9b5b04eb81 Mon Sep 17 00:00:00 2001 From: Alexander Date: Tue, 10 Dec 2019 12:00:40 +0300 Subject: fix for endpoints after env update --- lib/mix/tasks/pleroma/config.ex | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'lib/mix/tasks') diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index cef02b864..257a0dfe5 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -74,7 +74,9 @@ defmodule Mix.Tasks.Pleroma.Config do defp load_and_create(group) do group |> Application.get_all_env() - |> Enum.reject(fn {k, _v} -> k in [Pleroma.Repo, :env] end) + |> Enum.reject(fn {k, _v} -> + k in [Pleroma.Repo, :env] or (group == :phoenix and k == :serve_endpoints) + end) |> Enum.each(fn {key, value} -> key = inspect(key) {:ok, _} = Config.update_or_create(%{group: inspect(group), key: key, value: value}) -- cgit v1.2.3 From 7d128ca2083d83486a05d8c4456aa4090006e781 Mon Sep 17 00:00:00 2001 From: Alexander Strizhakov Date: Fri, 10 Jan 2020 19:34:19 +0300 Subject: dynamic_configuration renaming and moving it from instance settings --- lib/mix/tasks/pleroma/config.ex | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'lib/mix/tasks') diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index 257a0dfe5..3f80af5a7 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -30,7 +30,7 @@ defmodule Mix.Tasks.Pleroma.Config do def run(["migrate_to_db"]) do start_pleroma() - if Pleroma.Config.get([:instance, :dynamic_configuration]) do + if Pleroma.Config.get([:configurable_from_database]) do Enum.each(@groups, &load_and_create(&1)) else Mix.shell().info( @@ -48,7 +48,8 @@ defmodule Mix.Tasks.Pleroma.Config do aliases: [d: :delete_from_db] ) - with {:active?, true} <- {:active?, Pleroma.Config.get([:instance, :dynamic_configuration])}, + with {:active?, true} <- + {:active?, Pleroma.Config.get([:configurable_from_database])}, env_path when is_binary(env_path) <- opts[:env], config_path <- "config/#{env_path}.exported_from_db.secret.exs", {:ok, file} <- File.open(config_path, [:write, :utf8]) do -- cgit v1.2.3 From eadb674c41dea27346aad43fa5514aa4f461273b Mon Sep 17 00:00:00 2001 From: Alexander Strizhakov Date: Fri, 10 Jan 2020 19:44:30 +0300 Subject: don't migrate configurable_from_database setting --- lib/mix/tasks/pleroma/config.ex | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib/mix/tasks') diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index 3f80af5a7..e7c6e8810 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -76,7 +76,8 @@ defmodule Mix.Tasks.Pleroma.Config do group |> Application.get_all_env() |> Enum.reject(fn {k, _v} -> - k in [Pleroma.Repo, :env] or (group == :phoenix and k == :serve_endpoints) + k in [Pleroma.Repo, :env, :configurable_from_database] or + (group == :phoenix and k == :serve_endpoints) end) |> Enum.each(fn {key, value} -> key = inspect(key) -- cgit v1.2.3 From 3453b27015dfa26bc6fd59899541eadfce2cacee Mon Sep 17 00:00:00 2001 From: Alexander Strizhakov Date: Fri, 10 Jan 2020 21:24:06 +0300 Subject: don't migrate prometheus settings --- lib/mix/tasks/pleroma/config.ex | 1 - 1 file changed, 1 deletion(-) (limited to 'lib/mix/tasks') diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index e7c6e8810..b69c49b45 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -21,7 +21,6 @@ defmodule Mix.Tasks.Pleroma.Config do :auto_linker, :esshd, :ueberauth, - :prometheus, :http_signatures, :web_push_encryption, :joken -- cgit v1.2.3 From 66de2b159b2e03fcac7fece01d39e1238e02dbf7 Mon Sep 17 00:00:00 2001 From: Alexander Strizhakov Date: Wed, 15 Jan 2020 17:08:45 +0300 Subject: don't migrate Pleroma.Web.EndPoint settings don't set Logger level to all tasks --- lib/mix/tasks/pleroma/config.ex | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'lib/mix/tasks') diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index b69c49b45..92487dd51 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -27,7 +27,8 @@ defmodule Mix.Tasks.Pleroma.Config do ] def run(["migrate_to_db"]) do - start_pleroma() + # we want to save original logger level + start_pleroma(false) if Pleroma.Config.get([:configurable_from_database]) do Enum.each(@groups, &load_and_create(&1)) @@ -75,7 +76,7 @@ defmodule Mix.Tasks.Pleroma.Config do group |> Application.get_all_env() |> Enum.reject(fn {k, _v} -> - k in [Pleroma.Repo, :env, :configurable_from_database] or + k in [Pleroma.Repo, Pleroma.Web.Endpoint, :env, :configurable_from_database] or (group == :phoenix and k == :serve_endpoints) end) |> Enum.each(fn {key, value} -> -- cgit v1.2.3 From 29155137fdae15fccfaa68fb9c954e98078ce0c4 Mon Sep 17 00:00:00 2001 From: Alexander Strizhakov Date: Thu, 16 Jan 2020 08:50:27 +0300 Subject: renaming Pleroma.Web.AdminAPI.Config -> Pleroma.ConfigDB --- lib/mix/tasks/pleroma/config.ex | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'lib/mix/tasks') diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index 92487dd51..57952aeba 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -5,8 +5,8 @@ defmodule Mix.Tasks.Pleroma.Config do use Mix.Task import Mix.Pleroma + alias Pleroma.ConfigDB alias Pleroma.Repo - alias Pleroma.Web.AdminAPI.Config @shortdoc "Manages the location of the config" @moduledoc File.read!("docs/administration/CLI_tasks/config.md") @@ -55,7 +55,7 @@ defmodule Mix.Tasks.Pleroma.Config do {:ok, file} <- File.open(config_path, [:write, :utf8]) do IO.write(file, "use Mix.Config\r\n") - Config + ConfigDB |> Repo.all() |> Enum.each(&write_to_file_with_deletion(&1, file, opts[:delete_from_db])) @@ -81,7 +81,7 @@ defmodule Mix.Tasks.Pleroma.Config do end) |> Enum.each(fn {key, value} -> key = inspect(key) - {:ok, _} = Config.update_or_create(%{group: inspect(group), key: key, value: value}) + {:ok, _} = ConfigDB.update_or_create(%{group: inspect(group), key: key, value: value}) Mix.shell().info("settings for key #{key} migrated.") end) @@ -93,7 +93,7 @@ defmodule Mix.Tasks.Pleroma.Config do IO.write( file, "config #{config.group}, #{config.key}, #{ - inspect(Config.from_binary(config.value), limit: :infinity) + inspect(ConfigDB.from_binary(config.value), limit: :infinity) }\r\n\r\n" ) -- cgit v1.2.3 From 60ba2339a244290f7353e8026032b1a5d185227c Mon Sep 17 00:00:00 2001 From: Alexander Strizhakov Date: Fri, 17 Jan 2020 11:45:44 +0300 Subject: saving to DB only added by user settings --- lib/mix/tasks/pleroma/config.ex | 65 +++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 28 deletions(-) (limited to 'lib/mix/tasks') diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index 57952aeba..3157e7b20 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -4,42 +4,25 @@ defmodule Mix.Tasks.Pleroma.Config do use Mix.Task + import Mix.Pleroma + alias Pleroma.ConfigDB alias Pleroma.Repo + + require Logger + @shortdoc "Manages the location of the config" @moduledoc File.read!("docs/administration/CLI_tasks/config.md") - @groups [ - :pleroma, - :logger, - :quack, - :mime, - :tesla, - :phoenix, - :cors_plug, - :auto_linker, - :esshd, - :ueberauth, - :http_signatures, - :web_push_encryption, - :joken - ] - def run(["migrate_to_db"]) do # we want to save original logger level start_pleroma(false) - - if Pleroma.Config.get([:configurable_from_database]) do - Enum.each(@groups, &load_and_create(&1)) - else - Mix.shell().info( - "Migration is not allowed by config. You can change this behavior in instance settings." - ) - end + migrate_to_db() end def run(["migrate_from_db" | options]) do + # TODO: add support for releases start_pleroma() {opts, _} = @@ -72,10 +55,36 @@ defmodule Mix.Tasks.Pleroma.Config do end end - defp load_and_create(group) do - group - |> Application.get_all_env() - |> Enum.reject(fn {k, _v} -> + @spec migrate_to_db(Path.t() | nil) :: any() + def migrate_to_db(file_path \\ nil) do + if Pleroma.Config.get([:configurable_from_database]) do + # TODO: add support for releases + config_file = file_path || "config/#{Pleroma.Config.get(:env)}.secret.exs" + do_migrate_to_db(config_file) + else + Mix.shell().info( + "migration is not allowed by config. You can change this behavior in instance settings." + ) + end + end + + defp do_migrate_to_db(config_file) do + if File.exists?(config_file) do + {custom_config, _paths} = + if Code.ensure_loaded?(Config.Reader), + do: Config.Reader.read_imports!(config_file), + else: Mix.Config.eval!(config_file) + + custom_config + |> Keyword.keys() + |> Enum.each(&create(&1, custom_config[&1])) + else + Logger.warn("to migrate settings, you must define custom settings in #{config_file}") + end + end + + defp create(group, settings) do + Enum.reject(settings, fn {k, _v} -> k in [Pleroma.Repo, Pleroma.Web.Endpoint, :env, :configurable_from_database] or (group == :phoenix and k == :serve_endpoints) end) -- cgit v1.2.3 From b310feeccb03a9bc232b49d93fb03805e3d5c2d8 Mon Sep 17 00:00:00 2001 From: Alexander Strizhakov Date: Fri, 17 Jan 2020 15:00:20 +0300 Subject: compile fix --- lib/mix/tasks/pleroma/config.ex | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'lib/mix/tasks') diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index 3157e7b20..d2abbd885 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -68,12 +68,15 @@ defmodule Mix.Tasks.Pleroma.Config do end end + if Code.ensure_loaded?(Config.Reader) do + defp read_file(config_file), do: Config.Reader.read_imports!(config_file) + else + defp read_file(config_file), do: Mix.Config.eval!(config_file) + end + defp do_migrate_to_db(config_file) do if File.exists?(config_file) do - {custom_config, _paths} = - if Code.ensure_loaded?(Config.Reader), - do: Config.Reader.read_imports!(config_file), - else: Mix.Config.eval!(config_file) + {custom_config, _paths} = read_file(config_file) custom_config |> Keyword.keys() -- cgit v1.2.3 From ce027fd0ef1080b03e7982e5939b0c7db6e3b783 Mon Sep 17 00:00:00 2001 From: Alexander Strizhakov Date: Fri, 17 Jan 2020 15:13:24 +0300 Subject: revert non needable --- lib/mix/tasks/pleroma/config.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/mix/tasks') diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index d2abbd885..43039ae77 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -17,7 +17,7 @@ defmodule Mix.Tasks.Pleroma.Config do def run(["migrate_to_db"]) do # we want to save original logger level - start_pleroma(false) + start_pleroma() migrate_to_db() end -- cgit v1.2.3 From 7676ed82397d73a20aad1ae4b47690923ddfb162 Mon Sep 17 00:00:00 2001 From: Alexander Strizhakov Date: Fri, 17 Jan 2020 16:28:44 +0300 Subject: some clean up --- lib/mix/tasks/pleroma/config.ex | 1 - lib/mix/tasks/pleroma/docs.ex | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) (limited to 'lib/mix/tasks') diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index 43039ae77..148d18141 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -16,7 +16,6 @@ defmodule Mix.Tasks.Pleroma.Config do @moduledoc File.read!("docs/administration/CLI_tasks/config.md") def run(["migrate_to_db"]) do - # we want to save original logger level start_pleroma() migrate_to_db() end diff --git a/lib/mix/tasks/pleroma/docs.ex b/lib/mix/tasks/pleroma/docs.ex index 0d2663648..3c870f876 100644 --- a/lib/mix/tasks/pleroma/docs.ex +++ b/lib/mix/tasks/pleroma/docs.ex @@ -28,7 +28,7 @@ defmodule Mix.Tasks.Pleroma.Docs do defp do_run(implementation) do start_pleroma() - with {descriptions, _paths} <- Mix.Config.eval!("config/description.exs"), + with descriptions <- Pleroma.Config.Loader.load("config/description.exs"), {:ok, file_path} <- Pleroma.Docs.Generator.process( implementation, -- cgit v1.2.3 From efb8ef5abee1a8defa2bfba40ad1065db4c09ddf Mon Sep 17 00:00:00 2001 From: Alexander Strizhakov Date: Sat, 18 Jan 2020 16:55:33 +0300 Subject: releases support --- lib/mix/tasks/pleroma/config.ex | 49 +++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 16 deletions(-) (limited to 'lib/mix/tasks') diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index 148d18141..715b72dbe 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -10,8 +10,6 @@ defmodule Mix.Tasks.Pleroma.Config do alias Pleroma.ConfigDB alias Pleroma.Repo - require Logger - @shortdoc "Manages the location of the config" @moduledoc File.read!("docs/administration/CLI_tasks/config.md") @@ -32,10 +30,10 @@ defmodule Mix.Tasks.Pleroma.Config do with {:active?, true} <- {:active?, Pleroma.Config.get([:configurable_from_database])}, - env_path when is_binary(env_path) <- opts[:env], - config_path <- "config/#{env_path}.exported_from_db.secret.exs", + env when is_binary(env) <- opts[:env] || "prod", + config_path <- config_path(env), {:ok, file} <- File.open(config_path, [:write, :utf8]) do - IO.write(file, "use Mix.Config\r\n") + IO.write(file, config_header()) ConfigDB |> Repo.all() @@ -45,31 +43,50 @@ defmodule Mix.Tasks.Pleroma.Config do System.cmd("mix", ["format", config_path]) else {:active?, false} -> - Mix.shell().info( - "migration is not allowed by config. You can change this behavior in instance settings." + shell_info( + "Migration is not allowed by config. You can change this behavior in instance settings." ) error -> - Mix.shell().info("error occuried while opening file. #{inspect(error)}") + shell_info("Error occuried while opening file. #{inspect(error)}") end end + defp config_path(env) do + path = + if Pleroma.Config.get(:release) do + :config_path + |> Pleroma.Config.get() + |> Path.dirname() + else + "config" + end + + Path.join(path, "#{env}.exported_from_db.secret.exs") + end + @spec migrate_to_db(Path.t() | nil) :: any() def migrate_to_db(file_path \\ nil) do if Pleroma.Config.get([:configurable_from_database]) do - # TODO: add support for releases - config_file = file_path || "config/#{Pleroma.Config.get(:env)}.secret.exs" + user_config_file = + if Pleroma.Config.get(:release), + do: Pleroma.Config.get(:config_path), + else: "config/#{Pleroma.Config.get(:env)}.secret.exs" + + config_file = file_path || user_config_file do_migrate_to_db(config_file) else - Mix.shell().info( - "migration is not allowed by config. You can change this behavior in instance settings." + shell_info( + "Migration is not allowed by config. You can change this behavior in instance settings." ) end 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) else + defp config_header, do: "use Mix.Config\r\n\r\n" defp read_file(config_file), do: Mix.Config.eval!(config_file) end @@ -81,7 +98,7 @@ defmodule Mix.Tasks.Pleroma.Config do |> Keyword.keys() |> Enum.each(&create(&1, custom_config[&1])) else - Logger.warn("to migrate settings, you must define custom settings in #{config_file}") + shell_info("To migrate settings, you must define custom settings in #{config_file}.") end end @@ -94,10 +111,10 @@ defmodule Mix.Tasks.Pleroma.Config do key = inspect(key) {:ok, _} = ConfigDB.update_or_create(%{group: inspect(group), key: key, value: value}) - Mix.shell().info("settings for key #{key} migrated.") + shell_info("Settings for key #{key} migrated.") end) - Mix.shell().info("settings for group :#{group} migrated.") + shell_info("Settings for group :#{group} migrated.") end defp write_to_file_with_deletion(config, file, with_deletion) do @@ -110,7 +127,7 @@ defmodule Mix.Tasks.Pleroma.Config do if with_deletion do {:ok, _} = Repo.delete(config) - Mix.shell().info("#{config.key} deleted from DB.") + shell_info("#{config.key} deleted from DB.") end end end -- cgit v1.2.3 From f01ab6cd29aaae39fef6a95ec8490223fb692499 Mon Sep 17 00:00:00 2001 From: Alexander Strizhakov Date: Tue, 21 Jan 2020 17:49:22 +0300 Subject: some refactor and tests --- lib/mix/tasks/pleroma/config.ex | 154 ++++++++++++++++++++++------------------ 1 file changed, 84 insertions(+), 70 deletions(-) (limited to 'lib/mix/tasks') diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index 715b72dbe..861832451 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -19,94 +19,55 @@ defmodule Mix.Tasks.Pleroma.Config do end def run(["migrate_from_db" | options]) do - # TODO: add support for releases start_pleroma() {opts, _} = OptionParser.parse!(options, - strict: [env: :string, delete_from_db: :boolean], - aliases: [d: :delete_from_db] + strict: [env: :string, delete: :boolean], + aliases: [d: :delete] ) - with {:active?, true} <- - {:active?, Pleroma.Config.get([:configurable_from_database])}, - env when is_binary(env) <- opts[:env] || "prod", - config_path <- config_path(env), - {:ok, file} <- File.open(config_path, [:write, :utf8]) do - IO.write(file, config_header()) - - ConfigDB - |> Repo.all() - |> Enum.each(&write_to_file_with_deletion(&1, file, opts[:delete_from_db])) - - File.close(file) - System.cmd("mix", ["format", config_path]) - else - {:active?, false} -> - shell_info( - "Migration is not allowed by config. You can change this behavior in instance settings." - ) - - error -> - shell_info("Error occuried while opening file. #{inspect(error)}") - end - end - - defp config_path(env) do - path = - if Pleroma.Config.get(:release) do - :config_path - |> Pleroma.Config.get() - |> Path.dirname() - else - "config" - end - - Path.join(path, "#{env}.exported_from_db.secret.exs") + migrate_from_db(opts) end @spec migrate_to_db(Path.t() | nil) :: any() def migrate_to_db(file_path \\ nil) do if Pleroma.Config.get([:configurable_from_database]) do - user_config_file = - if Pleroma.Config.get(:release), - do: Pleroma.Config.get(:config_path), - else: "config/#{Pleroma.Config.get(:env)}.secret.exs" + config_file = + if file_path do + file_path + else + if Pleroma.Config.get(:release) do + Pleroma.Config.get(:config_path) + else + "config/#{Pleroma.Config.get(:env)}.secret.exs" + end + end - config_file = file_path || user_config_file do_migrate_to_db(config_file) else - shell_info( - "Migration is not allowed by config. You can change this behavior in instance settings." - ) + migration_error() end 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) - else - defp config_header, do: "use Mix.Config\r\n\r\n" - defp read_file(config_file), do: Mix.Config.eval!(config_file) - end - defp do_migrate_to_db(config_file) do if File.exists?(config_file) do - {custom_config, _paths} = read_file(config_file) + custom_config = + config_file + |> read_file() + |> elem(0) custom_config |> Keyword.keys() - |> Enum.each(&create(&1, custom_config[&1])) + |> Enum.each(&create(&1, custom_config)) else shell_info("To migrate settings, you must define custom settings in #{config_file}.") end end defp create(group, settings) do - Enum.reject(settings, fn {k, _v} -> - k in [Pleroma.Repo, Pleroma.Web.Endpoint, :env, :configurable_from_database] or - (group == :phoenix and k == :serve_endpoints) - end) + group + |> Pleroma.Config.Loader.filter_group(settings) |> Enum.each(fn {key, value} -> key = inspect(key) {:ok, _} = ConfigDB.update_or_create(%{group: inspect(group), key: key, value: value}) @@ -117,17 +78,70 @@ defmodule Mix.Tasks.Pleroma.Config do shell_info("Settings for group :#{group} migrated.") end - defp write_to_file_with_deletion(config, file, with_deletion) do - IO.write( - file, - "config #{config.group}, #{config.key}, #{ - inspect(ConfigDB.from_binary(config.value), limit: :infinity) - }\r\n\r\n" - ) + defp migrate_from_db(opts) do + if Pleroma.Config.get([:configurable_from_database]) do + env = opts[:env] || "prod" + + 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") + + file = File.open!(config_path, [:write, :utf8]) + + IO.write(file, config_header()) + + ConfigDB + |> Repo.all() + |> Enum.each(&write_and_delete(&1, file, opts[:delete])) - if with_deletion do - {:ok, _} = Repo.delete(config) - shell_info("#{config.key} deleted from DB.") + :ok = File.close(file) + System.cmd("mix", ["format", config_path]) + else + migration_error() end end + + defp migration_error do + shell_error( + "Migration is not allowed in config. You can change this behavior by setting `configurable_from_database` to true." + ) + 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) + else + defp config_header, do: "use Mix.Config\r\n\r\n" + defp read_file(config_file), do: Mix.Config.eval!(config_file) + end + + defp write_and_delete(config, file, delete?) do + config + |> write(file) + |> delete(delete?) + end + + defp write(config, file) do + value = + config.value + |> ConfigDB.from_binary() + |> inspect(limit: :infinity) + + IO.write(file, "config #{config.group}, #{config.key}, #{value}\r\n\r\n") + + config + end + + defp delete(config, true) do + {:ok, _} = Repo.delete(config) + shell_info("#{config.key} deleted from DB.") + end + + defp delete(_config, _), do: :ok end -- cgit v1.2.3