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)
66 |> ConfigDB.get_all_by_group_and_key(key)
71 def run(["dump", group]) do
75 group = maybe_atomize(group)
81 def run(["groups"]) do
88 |> Enum.map(fn x -> x.group end)
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 delete_key(group, key)
140 def run(["delete", "--force", group]) do
143 group = maybe_atomize(group)
145 with true <- group_exists?(group) do
146 shell_info("The following settings will be removed from ConfigDB:\n")
150 _ -> shell_error("No settings in ConfigDB for #{inspect(group)}. Aborting.")
154 def run(["delete", group, key]) do
157 group = maybe_atomize(group)
158 key = maybe_atomize(key)
160 if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
161 delete_key(group, key)
163 shell_error("No changes made.")
167 def run(["delete", group]) do
170 group = maybe_atomize(group)
172 with true <- group_exists?(group) do
173 shell_info("The following settings will be removed from ConfigDB:\n")
176 if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
179 shell_error("No changes made.")
182 _ -> shell_error("No settings in ConfigDB for #{inspect(group)}. Aborting.")
186 @spec migrate_to_db(Path.t() | nil) :: any()
187 def migrate_to_db(file_path \\ nil) do
188 with :ok <- Pleroma.Config.DeprecationWarnings.warn() do
193 if Pleroma.Config.get(:release) do
194 Pleroma.Config.get(:config_path)
196 "config/#{Pleroma.Config.get(:env)}.secret.exs"
200 do_migrate_to_db(config_file)
203 shell_error("Migration is not allowed until all deprecation warnings have been resolved.")
207 defp do_migrate_to_db(config_file) do
208 if File.exists?(config_file) do
209 shell_info("Migrating settings from file: #{Path.expand(config_file)}")
219 |> Enum.each(&create(&1, custom_config))
221 shell_info("To migrate settings, you must define custom settings in #{config_file}.")
225 defp create(group, settings) do
227 |> Pleroma.Config.Loader.filter_group(settings)
228 |> Enum.each(fn {key, value} ->
229 {:ok, _} = ConfigDB.update_or_create(%{group: group, key: key, value: value})
231 shell_info("Settings for key #{key} migrated.")
234 shell_info("Settings for group #{inspect(group)} migrated.")
237 defp migrate_from_db(opts) do
238 env = opts[:env] || Pleroma.Config.get(:env)
241 if Pleroma.Config.get(:release) do
243 |> Pleroma.Config.get()
248 |> Path.join("#{env}.exported_from_db.secret.exs")
250 file = File.open!(config_path, [:write, :utf8])
252 IO.write(file, config_header())
256 |> Enum.each(&write_and_delete(&1, file, opts[:delete]))
258 :ok = File.close(file)
259 System.cmd("mix", ["format", config_path])
262 "Database configuration settings have been exported to config/#{env}.exported_from_db.secret.exs"
266 if Code.ensure_loaded?(Config.Reader) do
267 defp config_header, do: "import Config\r\n\r\n"
268 defp read_file(config_file), do: Config.Reader.read_imports!(config_file)
270 defp config_header, do: "use Mix.Config\r\n\r\n"
271 defp read_file(config_file), do: Mix.Config.eval!(config_file)
274 defp write_and_delete(config, file, delete?) do
280 defp write(config, file) do
281 value = inspect(config.value, limit: :infinity)
283 IO.write(file, "config #{inspect(config.group)}, #{inspect(config.key)}, #{value}\r\n\r\n")
288 defp delete(config, true) do
289 {:ok, _} = Repo.delete(config)
292 "config #{inspect(config.group)}, #{inspect(config.key)} was deleted from the ConfigDB."
296 defp delete(_config, _), do: :ok
298 defp dump(%Pleroma.ConfigDB{} = config) do
299 value = inspect(config.value, limit: :infinity)
301 shell_info("config #{inspect(config.group)}, #{inspect(config.key)}, #{value}\r\n\r\n")
304 defp dump_group(group) when is_atom(group) do
306 |> ConfigDB.get_all_by_group()
307 |> Enum.each(&dump/1)
310 defp group_exists?(group) do
312 |> ConfigDB.get_all_by_group()
316 defp maybe_atomize(arg) when is_atom(arg), do: arg
318 defp maybe_atomize(arg) when is_binary(arg) do
319 if Pleroma.ConfigDB.module_name?(arg) do
320 String.to_existing_atom("Elixir." <> arg)
326 defp check_configdb(callback) do
327 with true <- Pleroma.Config.get([:configurable_from_database]) do
332 "ConfigDB not enabled. Please check the value of :configurable_from_database in your configuration."
337 defp delete_key(group, key) do
339 Pleroma.ConfigDB.delete(%{group: group, key: key})
343 defp delete_group(group) do
346 |> Pleroma.ConfigDB.get_all_by_group()
347 |> Enum.each(&ConfigDB.delete/1)
352 Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;")
353 Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;")