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/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(["groups"]) do
88 |> distinct([c], true)
89 |> select([c], c.group)
92 if length(groups) > 0 do
93 shell_info("The following configuration groups are set in ConfigDB:\r\n")
94 groups |> Enum.each(fn x -> shell_info("- #{x}") end)
100 def run(["reset", "--force"]) do
104 shell_info("The ConfigDB settings have been removed from the database.")
108 def run(["reset"]) do
112 shell_info("The following settings will be permanently removed:")
117 |> Enum.each(&dump(&1))
119 shell_error("\nTHIS CANNOT BE UNDONE!")
121 if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
124 shell_info("The ConfigDB settings have been removed from the database.")
126 shell_error("No changes made.")
131 def run(["delete", "--force", group, key]) do
134 group = maybe_atomize(group)
135 key = maybe_atomize(key)
137 with true <- key_exists?(group, key) do
138 shell_info("The following settings will be removed from ConfigDB:\n")
141 |> ConfigDB.get_by_group_and_key(key)
144 delete_key(group, key)
147 shell_error("No settings in ConfigDB for #{inspect(group)}, #{inspect(key)}. Aborting.")
151 def run(["delete", "--force", group]) do
154 group = maybe_atomize(group)
156 with true <- group_exists?(group) do
157 shell_info("The following settings will be removed from ConfigDB:\n")
161 _ -> shell_error("No settings in ConfigDB for #{inspect(group)}. Aborting.")
165 def run(["delete", group, key]) do
168 group = maybe_atomize(group)
169 key = maybe_atomize(key)
171 with true <- key_exists?(group, key) do
172 shell_info("The following settings will be removed from ConfigDB:\n")
175 |> ConfigDB.get_by_group_and_key(key)
178 if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
179 delete_key(group, key)
181 shell_error("No changes made.")
185 shell_error("No settings in ConfigDB for #{inspect(group)}, #{inspect(key)}. Aborting.")
189 def run(["delete", group]) do
192 group = maybe_atomize(group)
194 with true <- group_exists?(group) do
195 shell_info("The following settings will be removed from ConfigDB:\n")
198 if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
201 shell_error("No changes made.")
204 _ -> shell_error("No settings in ConfigDB for #{inspect(group)}. Aborting.")
208 @spec migrate_to_db(Path.t() | nil) :: any()
209 def migrate_to_db(file_path \\ nil) do
210 with :ok <- Pleroma.Config.DeprecationWarnings.warn() do
215 if Pleroma.Config.get(:release) do
216 Pleroma.Config.get(:config_path)
218 "config/#{Pleroma.Config.get(:env)}.secret.exs"
222 do_migrate_to_db(config_file)
225 shell_error("Migration is not allowed until all deprecation warnings have been resolved.")
229 defp do_migrate_to_db(config_file) do
230 if File.exists?(config_file) do
231 shell_info("Migrating settings from file: #{Path.expand(config_file)}")
241 |> Enum.each(&create(&1, custom_config))
243 shell_info("To migrate settings, you must define custom settings in #{config_file}.")
247 defp create(group, settings) do
249 |> Pleroma.Config.Loader.filter_group(settings)
250 |> Enum.each(fn {key, value} ->
251 {:ok, _} = ConfigDB.update_or_create(%{group: group, key: key, value: value})
253 shell_info("Settings for key #{key} migrated.")
256 shell_info("Settings for group #{inspect(group)} migrated.")
259 defp migrate_from_db(opts) do
260 env = opts[:env] || Pleroma.Config.get(:env)
262 filename = "#{env}.exported_from_db.secret.exs"
269 Pleroma.Config.get(:release) ->
271 |> Pleroma.Config.get()
277 |> Path.join(filename)
279 with {:ok, file} <- File.open(config_path, [:write, :utf8]) do
280 write_config(file, config_path, opts)
281 shell_info("Database configuration settings have been exported to #{config_path}")
284 shell_error("Impossible to save settings to this directory #{Path.dirname(config_path)}")
285 tmp_config_path = Path.join(System.tmp_dir!(), filename)
286 file = File.open!(tmp_config_path)
289 "Saving database configuration settings to #{tmp_config_path}. Copy it to the #{
290 Path.dirname(config_path)
294 write_config(file, tmp_config_path, opts)
298 defp write_config(file, path, opts) do
299 IO.write(file, config_header())
303 |> Enum.each(&write_and_delete(&1, file, opts[:delete]))
305 :ok = File.close(file)
306 System.cmd("mix", ["format", path])
309 if Code.ensure_loaded?(Config.Reader) do
310 defp config_header, do: "import Config\r\n\r\n"
311 defp read_file(config_file), do: Config.Reader.read_imports!(config_file)
313 defp config_header, do: "use Mix.Config\r\n\r\n"
314 defp read_file(config_file), do: Mix.Config.eval!(config_file)
317 defp write_and_delete(config, file, delete?) do
323 defp write(config, file) do
324 value = inspect(config.value, limit: :infinity)
326 IO.write(file, "config #{inspect(config.group)}, #{inspect(config.key)}, #{value}\r\n\r\n")
331 defp delete(config, true) do
332 {:ok, _} = Repo.delete(config)
335 "config #{inspect(config.group)}, #{inspect(config.key)} was deleted from the ConfigDB."
339 defp delete(_config, _), do: :ok
341 defp dump(%ConfigDB{} = config) do
342 value = inspect(config.value, limit: :infinity)
344 shell_info("config #{inspect(config.group)}, #{inspect(config.key)}, #{value}\r\n\r\n")
347 defp dump(_), do: :noop
349 defp dump_group(group) when is_atom(group) do
351 |> ConfigDB.get_all_by_group()
352 |> Enum.each(&dump/1)
355 defp group_exists?(group) do
357 |> ConfigDB.get_all_by_group()
361 defp key_exists?(group, key) do
363 |> ConfigDB.get_by_group_and_key(key)
368 defp maybe_atomize(arg) when is_atom(arg), do: arg
370 defp maybe_atomize(":" <> arg), do: maybe_atomize(arg)
372 defp maybe_atomize(arg) when is_binary(arg) do
373 if ConfigDB.module_name?(arg) do
374 String.to_existing_atom("Elixir." <> arg)
380 defp check_configdb(callback) do
381 with true <- Pleroma.Config.get([:configurable_from_database]) do
386 "ConfigDB not enabled. Please check the value of :configurable_from_database in your configuration."
391 defp delete_key(group, key) do
393 ConfigDB.delete(%{group: group, key: key})
397 defp delete_group(group) do
400 |> ConfigDB.get_all_by_group()
401 |> Enum.each(&ConfigDB.delete/1)
406 Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;")
407 Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;")