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(["reset"]) do
103 with true <- Pleroma.Config.get([:configurable_from_database]) do
106 shell_info("The following settings will be permanently removed:")
111 |> Enum.each(&dump(&1))
113 shell_error("\nTHIS CANNOT BE UNDONE!")
115 if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
116 Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;")
117 Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;")
119 shell_info("The ConfigDB settings have been removed from the database.")
121 shell_error("No changes made.")
124 _ -> configdb_not_enabled()
128 def run(["delete", group]) do
129 with true <- Pleroma.Config.get([:configurable_from_database]) do
132 group = maybe_atomize(group)
134 if group_exists?(group) do
135 shell_info("The following settings will be removed from ConfigDB:\n")
139 if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
142 |> Enum.filter(fn x ->
143 if x.group == group do
148 shell_error("No changes made.")
151 shell_error("No settings in ConfigDB for #{inspect(group)}. Aborting.")
154 _ -> configdb_not_enabled()
158 def run(["delete", group, key]) do
159 with true <- Pleroma.Config.get([:configurable_from_database]) do
162 group = maybe_atomize(group)
163 key = maybe_atomize(key)
165 if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
168 |> Enum.filter(fn x ->
169 if x.group == group and x.key == key do
174 shell_error("No changes made.")
177 _ -> configdb_not_enabled()
181 @spec migrate_to_db(Path.t() | nil) :: any()
182 def migrate_to_db(file_path \\ nil) do
183 with true <- Pleroma.Config.get([:configurable_from_database]),
184 :ok <- Pleroma.Config.DeprecationWarnings.warn() do
189 if Pleroma.Config.get(:release) do
190 Pleroma.Config.get(:config_path)
192 "config/#{Pleroma.Config.get(:env)}.secret.exs"
196 do_migrate_to_db(config_file)
198 :error -> deprecation_error()
199 _ -> migration_error()
203 defp do_migrate_to_db(config_file) do
204 if File.exists?(config_file) do
205 shell_info("Migrating settings from file: #{Path.expand(config_file)}")
206 Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;")
207 Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;")
216 |> Enum.each(&create(&1, custom_config))
218 shell_info("To migrate settings, you must define custom settings in #{config_file}.")
222 defp create(group, settings) do
224 |> Pleroma.Config.Loader.filter_group(settings)
225 |> Enum.each(fn {key, value} ->
226 {:ok, _} = ConfigDB.update_or_create(%{group: group, key: key, value: value})
228 shell_info("Settings for key #{key} migrated.")
231 shell_info("Settings for group #{inspect(group)} migrated.")
234 defp migrate_from_db(opts) do
235 if Pleroma.Config.get([:configurable_from_database]) do
236 env = opts[:env] || Pleroma.Config.get(:env)
239 if Pleroma.Config.get(:release) do
241 |> Pleroma.Config.get()
246 |> Path.join("#{env}.exported_from_db.secret.exs")
248 file = File.open!(config_path, [:write, :utf8])
250 IO.write(file, config_header())
254 |> Enum.each(&write_and_delete(&1, file, opts[:delete]))
256 :ok = File.close(file)
257 System.cmd("mix", ["format", config_path])
260 "Database configuration settings have been exported to config/#{env}.exported_from_db.secret.exs"
267 defp migration_error do
269 "Migration is not allowed in config. You can change this behavior by setting `config :pleroma, configurable_from_database: true`"
273 defp deprecation_error do
274 shell_error("Migration is not allowed until all deprecation warnings have been resolved.")
277 if Code.ensure_loaded?(Config.Reader) do
278 defp config_header, do: "import Config\r\n\r\n"
279 defp read_file(config_file), do: Config.Reader.read_imports!(config_file)
281 defp config_header, do: "use Mix.Config\r\n\r\n"
282 defp read_file(config_file), do: Mix.Config.eval!(config_file)
285 defp write_and_delete(config, file, delete?) do
291 defp write(config, file) do
292 value = inspect(config.value, limit: :infinity)
294 IO.write(file, "config #{inspect(config.group)}, #{inspect(config.key)}, #{value}\r\n\r\n")
299 defp delete(config, true) do
300 {:ok, _} = Repo.delete(config)
303 "config #{inspect(config.group)}, #{inspect(config.key)} deleted from the ConfigDB."
307 defp delete(_config, _), do: :ok
309 defp dump(%Pleroma.ConfigDB{} = config) do
310 value = inspect(config.value, limit: :infinity)
312 shell_info("config #{inspect(config.group)}, #{inspect(config.key)}, #{value}\r\n\r\n")
315 defp configdb_not_enabled do
317 "ConfigDB not enabled. Please check the value of :configurable_from_database in your configuration."
321 defp dump_key(group, key) when is_atom(group) and is_atom(key) do
324 |> Enum.filter(fn x ->
325 if x.group == group && x.key == key do
331 defp dump_group(group) when is_atom(group) do
334 |> Enum.filter(fn x ->
335 if x.group == group do
341 defp group_exists?(group) when is_atom(group) do
345 |> Enum.filter(fn x ->
346 if x.group == group do
351 unless result == [] do
358 def maybe_atomize(arg) when is_atom(arg), do: arg
360 def maybe_atomize(arg) when is_binary(arg) do
361 chars = String.codepoints(arg)