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)
102 shell_info("The following settings will be permanently removed:")
107 |> Enum.each(&dump(&1))
109 shell_error("\nTHIS CANNOT BE UNDONE!")
111 if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
112 Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;")
113 Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;")
115 shell_info("The ConfigDB settings have been removed from the database.")
117 shell_error("No changes made.")
122 def run(["delete", group]) do
126 group = maybe_atomize(group)
128 if group_exists?(group) do
129 shell_info("The following settings will be removed from ConfigDB:\n")
133 if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
136 |> Enum.filter(fn x ->
137 if x.group == group do
142 shell_error("No changes made.")
145 shell_error("No settings in ConfigDB for #{inspect(group)}. Aborting.")
150 def run(["delete", group, key]) do
154 group = maybe_atomize(group)
155 key = maybe_atomize(key)
157 if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
160 |> Enum.filter(fn x ->
161 if x.group == group and x.key == key do
166 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)}")
195 Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;")
196 Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;")
205 |> Enum.each(&create(&1, custom_config))
207 shell_info("To migrate settings, you must define custom settings in #{config_file}.")
211 defp create(group, settings) do
213 |> Pleroma.Config.Loader.filter_group(settings)
214 |> Enum.each(fn {key, value} ->
215 {:ok, _} = ConfigDB.update_or_create(%{group: group, key: key, value: value})
217 shell_info("Settings for key #{key} migrated.")
220 shell_info("Settings for group #{inspect(group)} migrated.")
223 defp migrate_from_db(opts) do
224 env = opts[:env] || Pleroma.Config.get(:env)
227 if Pleroma.Config.get(:release) do
229 |> Pleroma.Config.get()
234 |> Path.join("#{env}.exported_from_db.secret.exs")
236 file = File.open!(config_path, [:write, :utf8])
238 IO.write(file, config_header())
242 |> Enum.each(&write_and_delete(&1, file, opts[:delete]))
244 :ok = File.close(file)
245 System.cmd("mix", ["format", config_path])
248 "Database configuration settings have been exported to config/#{env}.exported_from_db.secret.exs"
252 if Code.ensure_loaded?(Config.Reader) do
253 defp config_header, do: "import Config\r\n\r\n"
254 defp read_file(config_file), do: Config.Reader.read_imports!(config_file)
256 defp config_header, do: "use Mix.Config\r\n\r\n"
257 defp read_file(config_file), do: Mix.Config.eval!(config_file)
260 defp write_and_delete(config, file, delete?) do
266 defp write(config, file) do
267 value = inspect(config.value, limit: :infinity)
269 IO.write(file, "config #{inspect(config.group)}, #{inspect(config.key)}, #{value}\r\n\r\n")
274 defp delete(config, true) do
275 {:ok, _} = Repo.delete(config)
278 "config #{inspect(config.group)}, #{inspect(config.key)} deleted from the ConfigDB."
282 defp delete(_config, _), do: :ok
284 defp dump(%Pleroma.ConfigDB{} = config) do
285 value = inspect(config.value, limit: :infinity)
287 shell_info("config #{inspect(config.group)}, #{inspect(config.key)}, #{value}\r\n\r\n")
290 defp dump_key(group, key) when is_atom(group) and is_atom(key) do
293 |> Enum.filter(fn x ->
294 if x.group == group && x.key == key do
300 defp dump_group(group) when is_atom(group) do
303 |> Enum.filter(fn x ->
304 if x.group == group do
310 defp group_exists?(group) when is_atom(group) do
314 |> Enum.filter(fn x ->
315 if x.group == group do
320 unless result == [] do
327 defp maybe_atomize(arg) when is_atom(arg), do: arg
329 defp maybe_atomize(arg) when is_binary(arg) do
330 chars = String.codepoints(arg)
339 defp check_configdb(callback) do
340 with true <- Pleroma.Config.get([:configurable_from_database]) do
345 "ConfigDB not enabled. Please check the value of :configurable_from_database in your configuration."