1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Mix.Tasks.Pleroma.Config do
11 alias Pleroma.ConfigDB
14 @shortdoc "Manages the location of the config"
15 @moduledoc File.read!("docs/docs/administration/CLI_tasks/config.md")
17 def run(["migrate_to_db"]) do
24 def run(["migrate_from_db" | options]) do
29 OptionParser.parse!(options,
30 strict: [env: :string, delete: :boolean, path: :string],
42 header = config_header()
49 unless settings == [] do
50 shell_info("#{header}")
52 Enum.each(settings, &dump(&1))
54 shell_error("No settings in ConfigDB.")
59 def run(["dump", group, key]) do
63 group = maybe_atomize(group)
64 key = maybe_atomize(key)
67 |> ConfigDB.get_by_group_and_key(key)
72 def run(["dump", group]) do
76 group = maybe_atomize(group)
82 def run(["dump_to_file", group, key, fname]) do
86 group = maybe_atomize(group)
87 key = maybe_atomize(key)
89 config = ConfigDB.get_by_group_and_key(group, key)
93 group: ConfigDB.to_json_types(config.group),
94 key: ConfigDB.to_json_types(config.key),
95 value: ConfigDB.to_json_types(config.value)
98 |> Jason.Formatter.pretty_print()
100 File.write(fname, json)
101 shell_info("Wrote #{group}_#{key}.json")
105 def run(["load_from_file", fname]) do
109 json = File.read!(fname)
110 config = Jason.decode!(json)
111 group = ConfigDB.to_elixir_types(config["group"])
112 key = ConfigDB.to_elixir_types(config["key"])
113 value = ConfigDB.to_elixir_types(config["value"])
114 params = %{group: group, key: key, value: value}
116 ConfigDB.update_or_create(params)
117 shell_info("Loaded #{config["group"]}, #{config["key"]}")
121 def run(["groups"]) do
127 |> distinct([c], true)
128 |> select([c], c.group)
131 if length(groups) > 0 do
132 shell_info("The following configuration groups are set in ConfigDB:\r\n")
133 groups |> Enum.each(fn x -> shell_info("- #{x}") end)
139 def run(["reset", "--force"]) do
143 shell_info("The ConfigDB settings have been removed from the database.")
147 def run(["reset"]) do
151 shell_info("The following settings will be permanently removed:")
156 |> Enum.each(&dump(&1))
158 shell_error("\nTHIS CANNOT BE UNDONE!")
160 if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
163 shell_info("The ConfigDB settings have been removed from the database.")
165 shell_error("No changes made.")
170 def run(["delete", "--force", group, key]) do
173 group = maybe_atomize(group)
174 key = maybe_atomize(key)
176 with true <- key_exists?(group, key) do
177 shell_info("The following settings will be removed from ConfigDB:\n")
180 |> ConfigDB.get_by_group_and_key(key)
183 delete_key(group, key)
186 shell_error("No settings in ConfigDB for #{inspect(group)}, #{inspect(key)}. Aborting.")
190 def run(["delete", "--force", group]) do
193 group = maybe_atomize(group)
195 with true <- group_exists?(group) do
196 shell_info("The following settings will be removed from ConfigDB:\n")
200 _ -> shell_error("No settings in ConfigDB for #{inspect(group)}. Aborting.")
204 def run(["delete", group, key]) do
207 group = maybe_atomize(group)
208 key = maybe_atomize(key)
210 with true <- key_exists?(group, key) do
211 shell_info("The following settings will be removed from ConfigDB:\n")
214 |> ConfigDB.get_by_group_and_key(key)
217 if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
218 delete_key(group, key)
220 shell_error("No changes made.")
224 shell_error("No settings in ConfigDB for #{inspect(group)}, #{inspect(key)}. Aborting.")
228 def run(["delete", group]) do
231 group = maybe_atomize(group)
233 with true <- group_exists?(group) do
234 shell_info("The following settings will be removed from ConfigDB:\n")
237 if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
240 shell_error("No changes made.")
243 _ -> shell_error("No settings in ConfigDB for #{inspect(group)}. Aborting.")
247 @spec migrate_to_db(Path.t() | nil) :: any()
248 def migrate_to_db(file_path \\ nil) do
249 with :ok <- Pleroma.Config.DeprecationWarnings.warn() do
254 if Pleroma.Config.get(:release) do
255 Pleroma.Config.get(:config_path)
257 "config/#{Pleroma.Config.get(:env)}.secret.exs"
261 do_migrate_to_db(config_file)
264 shell_error("Migration is not allowed until all deprecation warnings have been resolved.")
268 defp do_migrate_to_db(config_file) do
269 if File.exists?(config_file) do
270 shell_info("Migrating settings from file: #{Path.expand(config_file)}")
280 |> Enum.each(&create(&1, custom_config))
282 shell_info("To migrate settings, you must define custom settings in #{config_file}.")
286 defp create(group, settings) do
288 |> Pleroma.Config.Loader.filter_group(settings)
289 |> Enum.each(fn {key, value} ->
290 {:ok, _} = ConfigDB.update_or_create(%{group: group, key: key, value: value})
292 shell_info("Settings for key #{key} migrated.")
295 shell_info("Settings for group #{inspect(group)} migrated.")
298 defp migrate_from_db(opts) do
299 env = opts[:env] || Pleroma.Config.get(:env)
301 filename = "#{env}.exported_from_db.secret.exs"
308 Pleroma.Config.get(:release) ->
310 |> Pleroma.Config.get()
316 |> Path.join(filename)
318 with {:ok, file} <- File.open(config_path, [:write, :utf8]) do
319 write_config(file, config_path, opts)
320 shell_info("Database configuration settings have been exported to #{config_path}")
323 shell_error("Impossible to save settings to this directory #{Path.dirname(config_path)}")
324 tmp_config_path = Path.join(System.tmp_dir!(), filename)
325 file = File.open!(tmp_config_path)
328 "Saving database configuration settings to #{tmp_config_path}. Copy it to the #{Path.dirname(config_path)} manually."
331 write_config(file, tmp_config_path, opts)
335 defp write_config(file, path, opts) do
336 IO.write(file, config_header())
340 |> Enum.each(&write_and_delete(&1, file, opts[:delete]))
342 :ok = File.close(file)
343 System.cmd("mix", ["format", path])
346 if Code.ensure_loaded?(Config.Reader) do
347 defp config_header, do: "import Config\r\n\r\n"
348 defp read_file(config_file), do: Config.Reader.read_imports!(config_file)
350 defp config_header, do: "use Mix.Config\r\n\r\n"
351 defp read_file(config_file), do: Mix.Config.eval!(config_file)
354 defp write_and_delete(config, file, delete?) do
360 defp write(config, file) do
361 value = inspect(config.value, limit: :infinity)
363 IO.write(file, "config #{inspect(config.group)}, #{inspect(config.key)}, #{value}\r\n\r\n")
368 defp delete(config, true) do
369 {:ok, _} = Repo.delete(config)
372 "config #{inspect(config.group)}, #{inspect(config.key)} was deleted from the ConfigDB."
376 defp delete(_config, _), do: :ok
378 defp dump(%ConfigDB{} = config) do
379 value = inspect(config.value, limit: :infinity)
381 shell_info("config #{inspect(config.group)}, #{inspect(config.key)}, #{value}\r\n\r\n")
384 defp dump(_), do: :noop
386 defp dump_group(group) when is_atom(group) do
388 |> ConfigDB.get_all_by_group()
389 |> Enum.each(&dump/1)
392 defp group_exists?(group) do
394 |> ConfigDB.get_all_by_group()
398 defp key_exists?(group, key) do
400 |> ConfigDB.get_by_group_and_key(key)
405 defp maybe_atomize(arg) when is_atom(arg), do: arg
407 defp maybe_atomize(":" <> arg), do: maybe_atomize(arg)
409 defp maybe_atomize(arg) when is_binary(arg) do
410 if ConfigDB.module_name?(arg) do
411 String.to_existing_atom("Elixir." <> arg)
417 defp check_configdb(callback) do
418 with true <- Pleroma.Config.get([:configurable_from_database]) do
423 "ConfigDB not enabled. Please check the value of :configurable_from_database in your configuration."
428 defp delete_key(group, key) do
430 ConfigDB.delete(%{group: group, key: key})
434 defp delete_group(group) do
437 |> ConfigDB.get_all_by_group()
438 |> Enum.each(&ConfigDB.delete/1)
443 Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;")
444 Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;")