1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Mix.Tasks.Pleroma.Config do
10 alias Pleroma.ConfigDB
13 @shortdoc "Manages the location of the config"
14 @moduledoc File.read!("docs/administration/CLI_tasks/config.md")
16 def run(["migrate_to_db"]) do
23 def run(["migrate_from_db" | options]) do
28 OptionParser.parse!(options,
29 strict: [env: :string, delete: :boolean],
41 header = config_header()
48 unless settings == [] do
49 shell_info("#{header}")
51 settings |> Enum.each(&dump(&1))
53 shell_error("No settings in ConfigDB.")
58 def run(["dump", group, key]) do
62 group = maybe_atomize(group)
63 key = maybe_atomize(key)
69 def run(["dump", group]) do
73 group = maybe_atomize(group)
79 def run(["groups"]) do
86 |> Enum.map(fn x -> x.group end)
90 if length(groups) > 0 do
91 shell_info("The following configuration groups are set in ConfigDB:\r\n")
92 groups |> Enum.each(fn x -> shell_info("- #{x}") end)
98 def run(["reset", "--force"]) do
102 shell_info("The ConfigDB settings have been removed from the database.")
106 def run(["reset"]) do
110 shell_info("The following settings will be permanently removed:")
115 |> Enum.each(&dump(&1))
117 shell_error("\nTHIS CANNOT BE UNDONE!")
119 if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
122 shell_info("The ConfigDB settings have been removed from the database.")
124 shell_error("No changes made.")
129 def run(["delete", "--force", group, key]) do
132 group = maybe_atomize(group)
133 key = maybe_atomize(key)
135 delete_key(group, key)
138 def run(["delete", "--force", group]) do
141 group = maybe_atomize(group)
146 def run(["delete", group, key]) do
149 group = maybe_atomize(group)
150 key = maybe_atomize(key)
152 if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
153 delete_key(group, key)
155 shell_error("No changes made.")
159 def run(["delete", group]) do
162 group = maybe_atomize(group)
164 if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
167 shell_error("No changes made.")
171 @spec migrate_to_db(Path.t() | nil) :: any()
172 def migrate_to_db(file_path \\ nil) do
173 with :ok <- Pleroma.Config.DeprecationWarnings.warn() do
178 if Pleroma.Config.get(:release) do
179 Pleroma.Config.get(:config_path)
181 "config/#{Pleroma.Config.get(:env)}.secret.exs"
185 do_migrate_to_db(config_file)
188 shell_error("Migration is not allowed until all deprecation warnings have been resolved.")
192 defp do_migrate_to_db(config_file) do
193 if File.exists?(config_file) do
194 shell_info("Migrating settings from file: #{Path.expand(config_file)}")
204 |> Enum.each(&create(&1, custom_config))
206 shell_info("To migrate settings, you must define custom settings in #{config_file}.")
210 defp create(group, settings) do
212 |> Pleroma.Config.Loader.filter_group(settings)
213 |> Enum.each(fn {key, value} ->
214 {:ok, _} = ConfigDB.update_or_create(%{group: group, key: key, value: value})
216 shell_info("Settings for key #{key} migrated.")
219 shell_info("Settings for group #{inspect(group)} migrated.")
222 defp migrate_from_db(opts) do
223 env = opts[:env] || Pleroma.Config.get(:env)
226 if Pleroma.Config.get(:release) do
228 |> Pleroma.Config.get()
233 |> Path.join("#{env}.exported_from_db.secret.exs")
235 file = File.open!(config_path, [:write, :utf8])
237 IO.write(file, config_header())
241 |> Enum.each(&write_and_delete(&1, file, opts[:delete]))
243 :ok = File.close(file)
244 System.cmd("mix", ["format", config_path])
247 "Database configuration settings have been exported to config/#{env}.exported_from_db.secret.exs"
251 if Code.ensure_loaded?(Config.Reader) do
252 defp config_header, do: "import Config\r\n\r\n"
253 defp read_file(config_file), do: Config.Reader.read_imports!(config_file)
255 defp config_header, do: "use Mix.Config\r\n\r\n"
256 defp read_file(config_file), do: Mix.Config.eval!(config_file)
259 defp write_and_delete(config, file, delete?) do
265 defp write(config, file) do
266 value = inspect(config.value, limit: :infinity)
268 IO.write(file, "config #{inspect(config.group)}, #{inspect(config.key)}, #{value}\r\n\r\n")
273 defp delete(config, true) do
274 {:ok, _} = Repo.delete(config)
277 "config #{inspect(config.group)}, #{inspect(config.key)} was deleted from the ConfigDB."
281 defp delete(_config, _), do: :ok
283 defp dump(%Pleroma.ConfigDB{} = config) do
284 value = inspect(config.value, limit: :infinity)
286 shell_info("config #{inspect(config.group)}, #{inspect(config.key)}, #{value}\r\n\r\n")
289 defp dump_key(group, key) when is_atom(group) and is_atom(key) do
292 |> Enum.filter(fn x ->
293 if x.group == group && x.key == key do
299 defp dump_group(group) when is_atom(group) do
302 |> Enum.filter(fn x ->
303 if x.group == group do
309 defp group_exists?(group) when is_atom(group) do
313 |> Enum.filter(fn x ->
314 if x.group == group do
319 unless result == [] do
326 defp maybe_atomize(arg) when is_atom(arg), do: arg
328 defp maybe_atomize(arg) when is_binary(arg) do
329 chars = String.codepoints(arg)
331 # hack to make sure input like Pleroma.Mailer.Foo is formatted correctly
332 # for matching against values returned by Ecto
340 defp check_configdb(callback) do
341 with true <- Pleroma.Config.get([:configurable_from_database]) do
346 "ConfigDB not enabled. Please check the value of :configurable_from_database in your configuration."
351 defp delete_key(group, key) do
355 |> Enum.filter(fn x ->
356 if x.group == group and x.key == key do
363 defp delete_group(group) do
365 with true <- group_exists?(group) do
366 shell_info("The following settings will be removed from ConfigDB:\n")
371 |> Enum.filter(fn x ->
372 if x.group == group do
377 _ -> shell_error("No settings in ConfigDB for #{inspect(group)}. Aborting.")
383 Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;")
384 Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;")