Centralize check that configdb is enabled which now raises an exception

This commit is contained in:
Mark Felder 2020-11-29 13:29:36 -06:00
parent 53a5ec1952
commit a7b5280b5b
1 changed files with 105 additions and 128 deletions

View File

@ -14,11 +14,13 @@ defmodule Mix.Tasks.Pleroma.Config do
@moduledoc File.read!("docs/administration/CLI_tasks/config.md") @moduledoc File.read!("docs/administration/CLI_tasks/config.md")
def run(["migrate_to_db"]) do def run(["migrate_to_db"]) do
check_configdb()
start_pleroma() start_pleroma()
migrate_to_db() migrate_to_db()
end end
def run(["migrate_from_db" | options]) do def run(["migrate_from_db" | options]) do
check_configdb()
start_pleroma() start_pleroma()
{opts, _} = {opts, _} =
@ -31,142 +33,101 @@ def run(["migrate_from_db" | options]) do
end end
def run(["dump"]) do def run(["dump"]) do
with true <- Pleroma.Config.get([:configurable_from_database]) do check_configdb()
start_pleroma() start_pleroma()
header = config_header() header = config_header()
settings = settings =
ConfigDB ConfigDB
|> Repo.all() |> Repo.all()
|> Enum.sort() |> Enum.sort()
unless settings == [] do unless settings == [] do
shell_info("#{header}") shell_info("#{header}")
settings |> Enum.each(&dump(&1)) settings |> Enum.each(&dump(&1))
else
shell_error("No settings in ConfigDB.")
end
else else
_ -> configdb_not_enabled() shell_error("No settings in ConfigDB.")
end end
end end
def run(["dump", group, key]) do def run(["dump", group, key]) do
with true <- Pleroma.Config.get([:configurable_from_database]) do check_configdb()
start_pleroma() start_pleroma()
group = maybe_atomize(group) group = maybe_atomize(group)
key = maybe_atomize(key) key = maybe_atomize(key)
dump_key(group, key) dump_key(group, key)
else
_ -> configdb_not_enabled()
end
end end
def run(["dump", group]) do def run(["dump", group]) do
with true <- Pleroma.Config.get([:configurable_from_database]) do check_configdb()
start_pleroma() start_pleroma()
group = maybe_atomize(group) group = maybe_atomize(group)
dump_group(group) dump_group(group)
else
_ -> configdb_not_enabled()
end
end end
def run(["groups"]) do def run(["groups"]) do
with true <- Pleroma.Config.get([:configurable_from_database]) do check_configdb()
start_pleroma() start_pleroma()
groups = groups =
ConfigDB ConfigDB
|> Repo.all() |> Repo.all()
|> Enum.map(fn x -> x.group end) |> Enum.map(fn x -> x.group end)
|> Enum.sort() |> Enum.sort()
|> Enum.uniq() |> Enum.uniq()
if length(groups) > 0 do if length(groups) > 0 do
shell_info("The following configuration groups are set in ConfigDB:\r\n") shell_info("The following configuration groups are set in ConfigDB:\r\n")
groups |> Enum.each(fn x -> shell_info("- #{x}") end) groups |> Enum.each(fn x -> shell_info("- #{x}") end)
shell_info("\r\n") shell_info("\r\n")
end
else
_ -> configdb_not_enabled()
end end
end end
def run(["reset"]) do def run(["reset"]) do
with true <- Pleroma.Config.get([:configurable_from_database]) do check_configdb()
start_pleroma() start_pleroma()
shell_info("The following settings will be permanently removed:") shell_info("The following settings will be permanently removed:")
ConfigDB ConfigDB
|> Repo.all() |> Repo.all()
|> Enum.sort() |> Enum.sort()
|> Enum.each(&dump(&1)) |> 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 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, "TRUNCATE config;")
Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;") 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_error("No changes made.")
end
else else
_ -> configdb_not_enabled() shell_error("No changes made.")
end end
end end
def run(["delete", group]) do def run(["delete", group]) do
with true <- Pleroma.Config.get([:configurable_from_database]) do check_configdb()
start_pleroma() start_pleroma()
group = maybe_atomize(group) group = maybe_atomize(group)
if group_exists?(group) do if group_exists?(group) do
shell_info("The following settings will be removed from ConfigDB:\n") 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
else
_ -> configdb_not_enabled()
end
end
def run(["delete", group, key]) do
with true <- Pleroma.Config.get([:configurable_from_database]) do
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 if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
ConfigDB ConfigDB
|> Repo.all() |> Repo.all()
|> Enum.filter(fn x -> |> Enum.filter(fn x ->
if x.group == group and x.key == key do if x.group == group do
x |> delete(true) x |> delete(true)
end end
end) end)
@ -174,14 +135,33 @@ def run(["delete", group, key]) do
shell_error("No changes made.") shell_error("No changes made.")
end end
else 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
end end
@spec migrate_to_db(Path.t() | nil) :: any() @spec migrate_to_db(Path.t() | nil) :: any()
def migrate_to_db(file_path \\ nil) do def migrate_to_db(file_path \\ nil) do
with true <- Pleroma.Config.get([:configurable_from_database]), with :ok <- Pleroma.Config.DeprecationWarnings.warn() do
:ok <- Pleroma.Config.DeprecationWarnings.warn() do
config_file = config_file =
if file_path do if file_path do
file_path file_path
@ -195,8 +175,7 @@ def migrate_to_db(file_path \\ nil) do
do_migrate_to_db(config_file) do_migrate_to_db(config_file)
else else
:error -> deprecation_error() _ -> deprecation_error()
_ -> migration_error()
end end
end end
@ -232,41 +211,31 @@ defp create(group, settings) do
end end
defp migrate_from_db(opts) do defp migrate_from_db(opts) do
if Pleroma.Config.get([:configurable_from_database]) do env = opts[:env] || Pleroma.Config.get(:env)
env = opts[:env] || Pleroma.Config.get(:env)
config_path = config_path =
if Pleroma.Config.get(:release) do if Pleroma.Config.get(:release) do
:config_path :config_path
|> Pleroma.Config.get() |> Pleroma.Config.get()
|> Path.dirname() |> Path.dirname()
else else
"config" "config"
end end
|> Path.join("#{env}.exported_from_db.secret.exs") |> Path.join("#{env}.exported_from_db.secret.exs")
file = File.open!(config_path, [:write, :utf8]) file = File.open!(config_path, [:write, :utf8])
IO.write(file, config_header()) IO.write(file, config_header())
ConfigDB ConfigDB
|> Repo.all() |> Repo.all()
|> Enum.each(&write_and_delete(&1, file, opts[:delete])) |> Enum.each(&write_and_delete(&1, file, opts[:delete]))
:ok = File.close(file) :ok = File.close(file)
System.cmd("mix", ["format", config_path]) System.cmd("mix", ["format", config_path])
shell_info( shell_info(
"Database configuration settings have been exported to config/#{env}.exported_from_db.secret.exs" "Database configuration settings have been exported to config/#{env}.exported_from_db.secret.exs"
)
else
migration_error()
end
end
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`"
) )
end end
@ -313,7 +282,7 @@ defp dump(%Pleroma.ConfigDB{} = config) do
end end
defp configdb_not_enabled do defp configdb_not_enabled do
shell_error( raise(
"ConfigDB not enabled. Please check the value of :configurable_from_database in your configuration." "ConfigDB not enabled. Please check the value of :configurable_from_database in your configuration."
) )
end end
@ -366,4 +335,12 @@ defp maybe_atomize(arg) when is_binary(arg) do
String.to_atom(arg) String.to_atom(arg)
end end
end end
defp check_configdb() do
with true <- Pleroma.Config.get([:configurable_from_database]) do
:ok
else
_ -> configdb_not_enabled()
end
end
end end