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)
91 group: ConfigDB.to_json_types(config.group),
92 key: ConfigDB.to_json_types(config.key),
93 value: ConfigDB.to_json_types(config.value),
96 |> Jason.Formatter.pretty_print()
98 File.write(fname, json)
99 shell_info("Wrote #{group}_#{key}.json")
103 def run(["load_from_file", fname]) do
107 json = File.read!(fname)
108 config = Jason.decode!(json)
109 group = ConfigDB.to_elixir_types(config["group"])
110 key = ConfigDB.to_elixir_types(config["key"])
111 value = ConfigDB.to_elixir_types(config["value"])
112 params = %{group: group, key: key, value: value}
114 ConfigDB.update_or_create(params)
115 shell_info("Loaded #{config["group"]}, #{config["key"]}")
119 def run(["groups"]) do
125 |> distinct([c], true)
126 |> select([c], c.group)
129 if length(groups) > 0 do
130 shell_info("The following configuration groups are set in ConfigDB:\r\n")
131 groups |> Enum.each(fn x -> shell_info("- #{x}") end)
137 def run(["reset", "--force"]) do
141 shell_info("The ConfigDB settings have been removed from the database.")
145 def run(["reset"]) do
149 shell_info("The following settings will be permanently removed:")
154 |> Enum.each(&dump(&1))
156 shell_error("\nTHIS CANNOT BE UNDONE!")
158 if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
161 shell_info("The ConfigDB settings have been removed from the database.")
163 shell_error("No changes made.")
168 def run(["delete", "--force", group, key]) do
171 group = maybe_atomize(group)
172 key = maybe_atomize(key)
174 with true <- key_exists?(group, key) do
175 shell_info("The following settings will be removed from ConfigDB:\n")
178 |> ConfigDB.get_by_group_and_key(key)
181 delete_key(group, key)
184 shell_error("No settings in ConfigDB for #{inspect(group)}, #{inspect(key)}. Aborting.")
188 def run(["delete", "--force", group]) do
191 group = maybe_atomize(group)
193 with true <- group_exists?(group) do
194 shell_info("The following settings will be removed from ConfigDB:\n")
198 _ -> shell_error("No settings in ConfigDB for #{inspect(group)}. Aborting.")
202 def run(["delete", group, key]) do
205 group = maybe_atomize(group)
206 key = maybe_atomize(key)
208 with true <- key_exists?(group, key) do
209 shell_info("The following settings will be removed from ConfigDB:\n")
212 |> ConfigDB.get_by_group_and_key(key)
215 if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
216 delete_key(group, key)
218 shell_error("No changes made.")
222 shell_error("No settings in ConfigDB for #{inspect(group)}, #{inspect(key)}. Aborting.")
226 def run(["delete", group]) do
229 group = maybe_atomize(group)
231 with true <- group_exists?(group) do
232 shell_info("The following settings will be removed from ConfigDB:\n")
235 if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
238 shell_error("No changes made.")
241 _ -> shell_error("No settings in ConfigDB for #{inspect(group)}. Aborting.")
245 @spec migrate_to_db(Path.t() | nil) :: any()
246 def migrate_to_db(file_path \\ nil) do
247 with :ok <- Pleroma.Config.DeprecationWarnings.warn() do
252 if Pleroma.Config.get(:release) do
253 Pleroma.Config.get(:config_path)
255 "config/#{Pleroma.Config.get(:env)}.secret.exs"
259 do_migrate_to_db(config_file)
262 shell_error("Migration is not allowed until all deprecation warnings have been resolved.")
266 defp do_migrate_to_db(config_file) do
267 if File.exists?(config_file) do
268 shell_info("Migrating settings from file: #{Path.expand(config_file)}")
278 |> Enum.each(&create(&1, custom_config))
280 shell_info("To migrate settings, you must define custom settings in #{config_file}.")
284 defp create(group, settings) do
286 |> Pleroma.Config.Loader.filter_group(settings)
287 |> Enum.each(fn {key, value} ->
288 {:ok, _} = ConfigDB.update_or_create(%{group: group, key: key, value: value})
290 shell_info("Settings for key #{key} migrated.")
293 shell_info("Settings for group #{inspect(group)} migrated.")
296 defp migrate_from_db(opts) do
297 env = opts[:env] || Pleroma.Config.get(:env)
299 filename = "#{env}.exported_from_db.secret.exs"
306 Pleroma.Config.get(:release) ->
308 |> Pleroma.Config.get()
314 |> Path.join(filename)
316 with {:ok, file} <- File.open(config_path, [:write, :utf8]) do
317 write_config(file, config_path, opts)
318 shell_info("Database configuration settings have been exported to #{config_path}")
321 shell_error("Impossible to save settings to this directory #{Path.dirname(config_path)}")
322 tmp_config_path = Path.join(System.tmp_dir!(), filename)
323 file = File.open!(tmp_config_path)
326 "Saving database configuration settings to #{tmp_config_path}. Copy it to the #{Path.dirname(config_path)} manually."
329 write_config(file, tmp_config_path, opts)
333 defp write_config(file, path, opts) do
334 IO.write(file, config_header())
338 |> Enum.each(&write_and_delete(&1, file, opts[:delete]))
340 :ok = File.close(file)
341 System.cmd("mix", ["format", path])
344 if Code.ensure_loaded?(Config.Reader) do
345 defp config_header, do: "import Config\r\n\r\n"
346 defp read_file(config_file), do: Config.Reader.read_imports!(config_file)
348 defp config_header, do: "use Mix.Config\r\n\r\n"
349 defp read_file(config_file), do: Mix.Config.eval!(config_file)
352 defp write_and_delete(config, file, delete?) do
358 defp write(config, file) do
359 value = inspect(config.value, limit: :infinity)
361 IO.write(file, "config #{inspect(config.group)}, #{inspect(config.key)}, #{value}\r\n\r\n")
366 defp delete(config, true) do
367 {:ok, _} = Repo.delete(config)
370 "config #{inspect(config.group)}, #{inspect(config.key)} was deleted from the ConfigDB."
374 defp delete(_config, _), do: :ok
376 defp dump(%ConfigDB{} = config) do
377 value = inspect(config.value, limit: :infinity)
379 shell_info("config #{inspect(config.group)}, #{inspect(config.key)}, #{value}\r\n\r\n")
382 defp dump(_), do: :noop
384 defp dump_group(group) when is_atom(group) do
386 |> ConfigDB.get_all_by_group()
387 |> Enum.each(&dump/1)
390 defp group_exists?(group) do
392 |> ConfigDB.get_all_by_group()
396 defp key_exists?(group, key) do
398 |> ConfigDB.get_by_group_and_key(key)
403 defp maybe_atomize(arg) when is_atom(arg), do: arg
405 defp maybe_atomize(":" <> arg), do: maybe_atomize(arg)
407 defp maybe_atomize(arg) when is_binary(arg) do
408 if ConfigDB.module_name?(arg) do
409 String.to_existing_atom("Elixir." <> arg)
415 defp check_configdb(callback) do
416 with true <- Pleroma.Config.get([:configurable_from_database]) do
421 "ConfigDB not enabled. Please check the value of :configurable_from_database in your configuration."
426 defp delete_key(group, key) do
428 ConfigDB.delete(%{group: group, key: key})
432 defp delete_group(group) do
435 |> ConfigDB.get_all_by_group()
436 |> Enum.each(&ConfigDB.delete/1)
441 Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;")
442 Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;")