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()
39 shell_info("#{header}")
44 |> Enum.each(&dump(&1))
46 _ -> configdb_not_enabled()
50 def run(["dump" | args]) when is_list(args) and length(args) < 3 do
51 with true <- Pleroma.Config.get([:configurable_from_database]) do
54 if length(args) > 1 do
62 _ -> configdb_not_enabled()
66 def run(["groups"]) do
67 with true <- Pleroma.Config.get([:configurable_from_database]) do
73 |> Enum.map(fn x -> x.group end)
77 if length(groups) > 0 do
78 shell_info("The following configuration groups are set in ConfigDB:\r\n")
79 groups |> Enum.each(fn x -> shell_info("- #{x}") end)
83 _ -> configdb_not_enabled()
87 def run(["keys" | group]) do
88 with true <- Pleroma.Config.get([:configurable_from_database]) do
95 if x.group == group do
101 |> Enum.reject(fn x -> x == nil end)
103 if length(keys) > 0 do
104 shell_info("The following configuration keys under :#{group} are set in ConfigDB:\r\n")
105 keys |> Enum.each(fn x -> shell_info("- #{x}") end)
109 _ -> configdb_not_enabled()
113 def run(["reset"]) do
114 with true <- Pleroma.Config.get([:configurable_from_database]) do
117 Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;")
118 Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;")
120 shell_info("The ConfigDB settings have been removed from the database.")
122 _ -> configdb_not_enabled()
126 def run(["groupdel" | dbkey]) do
127 unless [] == dbkey do
128 with true <- Pleroma.Config.get([:configurable_from_database]) do
131 dbkey = dbkey |> List.first() |> String.to_atom()
135 |> Enum.filter(fn x ->
141 _ -> configdb_not_enabled()
145 "You must provide a group to delete. Use the groups command to get a list of valid configDB groups."
150 @spec migrate_to_db(Path.t() | nil) :: any()
151 def migrate_to_db(file_path \\ nil) do
152 with true <- Pleroma.Config.get([:configurable_from_database]),
153 :ok <- Pleroma.Config.DeprecationWarnings.warn() do
158 if Pleroma.Config.get(:release) do
159 Pleroma.Config.get(:config_path)
161 "config/#{Pleroma.Config.get(:env)}.secret.exs"
165 do_migrate_to_db(config_file)
167 :error -> deprecation_error()
168 _ -> migration_error()
172 defp do_migrate_to_db(config_file) do
173 if File.exists?(config_file) do
174 shell_info("Migrating settings from file: #{Path.expand(config_file)}")
175 Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;")
176 Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;")
185 |> Enum.each(&create(&1, custom_config))
187 shell_info("To migrate settings, you must define custom settings in #{config_file}.")
191 defp create(group, settings) do
193 |> Pleroma.Config.Loader.filter_group(settings)
194 |> Enum.each(fn {key, value} ->
195 {:ok, _} = ConfigDB.update_or_create(%{group: group, key: key, value: value})
197 shell_info("Settings for key #{key} migrated.")
200 shell_info("Settings for group :#{group} migrated.")
203 defp migrate_from_db(opts) do
204 if Pleroma.Config.get([:configurable_from_database]) do
205 env = opts[:env] || Pleroma.Config.get(:env)
208 if Pleroma.Config.get(:release) do
210 |> Pleroma.Config.get()
215 |> Path.join("#{env}.exported_from_db.secret.exs")
217 file = File.open!(config_path, [:write, :utf8])
219 IO.write(file, config_header())
223 |> Enum.each(&write_and_delete(&1, file, opts[:delete]))
225 :ok = File.close(file)
226 System.cmd("mix", ["format", config_path])
229 "Database configuration settings have been exported to config/#{env}.exported_from_db.secret.exs"
236 defp migration_error do
238 "Migration is not allowed in config. You can change this behavior by setting `config :pleroma, configurable_from_database: true`"
242 defp deprecation_error do
243 shell_error("Migration is not allowed until all deprecation warnings have been resolved.")
246 if Code.ensure_loaded?(Config.Reader) do
247 defp config_header, do: "import Config\r\n\r\n"
248 defp read_file(config_file), do: Config.Reader.read_imports!(config_file)
250 defp config_header, do: "use Mix.Config\r\n\r\n"
251 defp read_file(config_file), do: Mix.Config.eval!(config_file)
254 defp write_and_delete(config, file, delete?) do
260 defp write(config, file) do
261 value = inspect(config.value, limit: :infinity)
263 IO.write(file, "config #{inspect(config.group)}, #{inspect(config.key)}, #{value}\r\n\r\n")
268 defp delete(config, true) do
269 {:ok, _} = Repo.delete(config)
270 shell_info("#{config.key} deleted from the ConfigDB.")
273 defp delete(_config, _), do: :ok
275 defp dump(%Pleroma.ConfigDB{} = config) do
276 value = inspect(config.value, limit: :infinity)
278 shell_info("config #{inspect(config.group)}, #{inspect(config.key)}, #{value}\r\n\r\n")
281 defp configdb_not_enabled do
283 "ConfigDB not enabled. Please check the value of :configurable_from_database in your configuration."
287 defp dump_key(group, key) do
288 group = group |> String.to_atom()
289 key = key |> String.to_atom()
293 |> Enum.filter(fn x ->
294 if x.group == group && x.key == key do
300 defp dump_group(group) do
301 group = group |> String.to_atom()
305 |> Enum.filter(fn x ->
306 if x.group == group do