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
21 def run(["migrate_from_db" | options]) do
25 OptionParser.parse!(options,
26 strict: [env: :string, delete: :boolean],
34 with true <- Pleroma.Config.get([:configurable_from_database]) do
37 header = config_header()
44 unless settings == [] do
45 shell_info("#{header}")
47 settings |> Enum.each(&dump(&1))
49 shell_error("No settings in ConfigDB.")
52 _ -> configdb_not_enabled()
56 def run(["dump", group, key]) do
57 with true <- Pleroma.Config.get([:configurable_from_database]) do
60 group = maybe_atomize(group)
61 key = maybe_atomize(key)
65 _ -> configdb_not_enabled()
69 def run(["dump", group]) do
70 with true <- Pleroma.Config.get([:configurable_from_database]) do
73 group = maybe_atomize(group)
77 _ -> configdb_not_enabled()
81 def run(["groups"]) do
82 with true <- Pleroma.Config.get([:configurable_from_database]) 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)
98 _ -> configdb_not_enabled()
102 def run(["keys", group]) do
103 with true <- Pleroma.Config.get([:configurable_from_database]) do
106 group = maybe_atomize(group)
112 if x.group == group do
118 |> Enum.reject(fn x -> x == nil end)
120 if length(keys) > 0 do
121 shell_info("The following configuration keys under :#{group} are set in ConfigDB:\r\n")
122 keys |> Enum.each(fn x -> shell_info("- #{x}") end)
126 _ -> configdb_not_enabled()
130 def run(["reset"]) do
131 with true <- Pleroma.Config.get([:configurable_from_database]) do
134 shell_info("The following settings will be permanently removed:")
139 |> Enum.each(&dump(&1))
141 shell_error("THIS CANNOT BE UNDONE!")
143 if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
144 Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;")
145 Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;")
147 shell_info("The ConfigDB settings have been removed from the database.")
149 shell_error("No changes made.")
152 _ -> configdb_not_enabled()
156 def run(["delete", group]) do
157 with true <- Pleroma.Config.get([:configurable_from_database]) do
160 group = maybe_atomize(group)
162 if group_exists?(group) do
163 shell_info("The following settings will be removed from ConfigDB:\n")
167 if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
170 |> Enum.filter(fn x ->
171 if x.group == group do
176 shell_error("No changes made.")
179 shell_error("No settings in ConfigDB for :#{group}. Aborting.")
182 _ -> configdb_not_enabled()
186 def run(["delete", group, key]) do
187 with true <- Pleroma.Config.get([:configurable_from_database]) do
190 group = maybe_atomize(group)
191 key = maybe_atomize(key)
193 if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
196 |> Enum.filter(fn x ->
197 if x.group == group and x.key == key do
202 shell_error("No changes made.")
205 _ -> configdb_not_enabled()
209 @spec migrate_to_db(Path.t() | nil) :: any()
210 def migrate_to_db(file_path \\ nil) do
211 with true <- Pleroma.Config.get([:configurable_from_database]),
212 :ok <- Pleroma.Config.DeprecationWarnings.warn() do
217 if Pleroma.Config.get(:release) do
218 Pleroma.Config.get(:config_path)
220 "config/#{Pleroma.Config.get(:env)}.secret.exs"
224 do_migrate_to_db(config_file)
226 :error -> deprecation_error()
227 _ -> migration_error()
231 defp do_migrate_to_db(config_file) do
232 if File.exists?(config_file) do
233 shell_info("Migrating settings from file: #{Path.expand(config_file)}")
234 Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;")
235 Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;")
244 |> Enum.each(&create(&1, custom_config))
246 shell_info("To migrate settings, you must define custom settings in #{config_file}.")
250 defp create(group, settings) do
252 |> Pleroma.Config.Loader.filter_group(settings)
253 |> Enum.each(fn {key, value} ->
254 {:ok, _} = ConfigDB.update_or_create(%{group: group, key: key, value: value})
256 shell_info("Settings for key #{key} migrated.")
259 shell_info("Settings for group :#{group} migrated.")
262 defp migrate_from_db(opts) do
263 if Pleroma.Config.get([:configurable_from_database]) do
264 env = opts[:env] || Pleroma.Config.get(:env)
267 if Pleroma.Config.get(:release) do
269 |> Pleroma.Config.get()
274 |> Path.join("#{env}.exported_from_db.secret.exs")
276 file = File.open!(config_path, [:write, :utf8])
278 IO.write(file, config_header())
282 |> Enum.each(&write_and_delete(&1, file, opts[:delete]))
284 :ok = File.close(file)
285 System.cmd("mix", ["format", config_path])
288 "Database configuration settings have been exported to config/#{env}.exported_from_db.secret.exs"
295 defp migration_error do
297 "Migration is not allowed in config. You can change this behavior by setting `config :pleroma, configurable_from_database: true`"
301 defp deprecation_error do
302 shell_error("Migration is not allowed until all deprecation warnings have been resolved.")
305 if Code.ensure_loaded?(Config.Reader) do
306 defp config_header, do: "import Config\r\n\r\n"
307 defp read_file(config_file), do: Config.Reader.read_imports!(config_file)
309 defp config_header, do: "use Mix.Config\r\n\r\n"
310 defp read_file(config_file), do: Mix.Config.eval!(config_file)
313 defp write_and_delete(config, file, delete?) do
319 defp write(config, file) do
320 value = inspect(config.value, limit: :infinity)
322 IO.write(file, "config #{inspect(config.group)}, #{inspect(config.key)}, #{value}\r\n\r\n")
327 defp delete(config, true) do
328 {:ok, _} = Repo.delete(config)
331 "config #{inspect(config.group)}, #{inspect(config.key)} deleted from the ConfigDB."
335 defp delete(_config, _), do: :ok
337 defp dump(%Pleroma.ConfigDB{} = config) do
338 value = inspect(config.value, limit: :infinity)
340 shell_info("config #{inspect(config.group)}, #{inspect(config.key)}, #{value}\r\n\r\n")
343 defp configdb_not_enabled do
345 "ConfigDB not enabled. Please check the value of :configurable_from_database in your configuration."
349 defp dump_key(group, key) when is_atom(group) and is_atom(key) do
352 |> Enum.filter(fn x ->
353 if x.group == group && x.key == key do
359 defp dump_group(group) when is_atom(group) do
362 |> Enum.filter(fn x ->
363 if x.group == group do
369 defp group_exists?(group) when is_atom(group) do
373 |> Enum.filter(fn x ->
374 if x.group == group do
379 unless result == [] do
386 def maybe_atomize(arg) when is_atom(arg), do: arg
388 def maybe_atomize(arg) when is_binary(arg) do
389 chars = String.codepoints(arg)