Merge pull request 'use postgres 15 in CI' (#411) from ci-postgres-15 into develop
[akkoma] / lib / mix / tasks / pleroma / config.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Mix.Tasks.Pleroma.Config do
6 use Mix.Task
7
8 import Ecto.Query
9 import Mix.Pleroma
10
11 alias Pleroma.ConfigDB
12 alias Pleroma.Repo
13
14 @shortdoc "Manages the location of the config"
15 @moduledoc File.read!("docs/docs/administration/CLI_tasks/config.md")
16
17 def run(["migrate_to_db"]) do
18 check_configdb(fn ->
19 start_pleroma()
20 migrate_to_db()
21 end)
22 end
23
24 def run(["migrate_from_db" | options]) do
25 check_configdb(fn ->
26 start_pleroma()
27
28 {opts, _} =
29 OptionParser.parse!(options,
30 strict: [env: :string, delete: :boolean, path: :string],
31 aliases: [d: :delete]
32 )
33
34 migrate_from_db(opts)
35 end)
36 end
37
38 def run(["dump"]) do
39 check_configdb(fn ->
40 start_pleroma()
41
42 header = config_header()
43
44 settings =
45 ConfigDB
46 |> Repo.all()
47 |> Enum.sort()
48
49 unless settings == [] do
50 shell_info("#{header}")
51
52 Enum.each(settings, &dump(&1))
53 else
54 shell_error("No settings in ConfigDB.")
55 end
56 end)
57 end
58
59 def run(["dump", group, key]) do
60 check_configdb(fn ->
61 start_pleroma()
62
63 group = maybe_atomize(group)
64 key = maybe_atomize(key)
65
66 group
67 |> ConfigDB.get_by_group_and_key(key)
68 |> dump()
69 end)
70 end
71
72 def run(["dump", group]) do
73 check_configdb(fn ->
74 start_pleroma()
75
76 group = maybe_atomize(group)
77
78 dump_group(group)
79 end)
80 end
81
82 def run(["dump_to_file", group, key, fname]) do
83 check_configdb(fn ->
84 start_pleroma()
85
86 group = maybe_atomize(group)
87 key = maybe_atomize(key)
88
89 config = ConfigDB.get_by_group_and_key(group, key)
90
91 json =
92 %{
93 group: ConfigDB.to_json_types(config.group),
94 key: ConfigDB.to_json_types(config.key),
95 value: ConfigDB.to_json_types(config.value)
96 }
97 |> Jason.encode!()
98 |> Jason.Formatter.pretty_print()
99
100 File.write(fname, json)
101 shell_info("Wrote #{group}_#{key}.json")
102 end)
103 end
104
105 def run(["load_from_file", fname]) do
106 check_configdb(fn ->
107 start_pleroma()
108
109 json = File.read!(fname)
110 config = Jason.decode!(json)
111 group = ConfigDB.to_elixir_types(config["group"])
112 key = ConfigDB.to_elixir_types(config["key"])
113 value = ConfigDB.to_elixir_types(config["value"])
114 params = %{group: group, key: key, value: value}
115
116 ConfigDB.update_or_create(params)
117 shell_info("Loaded #{config["group"]}, #{config["key"]}")
118 end)
119 end
120
121 def run(["groups"]) do
122 check_configdb(fn ->
123 start_pleroma()
124
125 groups =
126 ConfigDB
127 |> distinct([c], true)
128 |> select([c], c.group)
129 |> Repo.all()
130
131 if length(groups) > 0 do
132 shell_info("The following configuration groups are set in ConfigDB:\r\n")
133 groups |> Enum.each(fn x -> shell_info("- #{x}") end)
134 shell_info("\r\n")
135 end
136 end)
137 end
138
139 def run(["reset", "--force"]) do
140 check_configdb(fn ->
141 start_pleroma()
142 truncatedb()
143 shell_info("The ConfigDB settings have been removed from the database.")
144 end)
145 end
146
147 def run(["reset"]) do
148 check_configdb(fn ->
149 start_pleroma()
150
151 shell_info("The following settings will be permanently removed:")
152
153 ConfigDB
154 |> Repo.all()
155 |> Enum.sort()
156 |> Enum.each(&dump(&1))
157
158 shell_error("\nTHIS CANNOT BE UNDONE!")
159
160 if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
161 truncatedb()
162
163 shell_info("The ConfigDB settings have been removed from the database.")
164 else
165 shell_error("No changes made.")
166 end
167 end)
168 end
169
170 def run(["delete", "--force", group, key]) do
171 start_pleroma()
172
173 group = maybe_atomize(group)
174 key = maybe_atomize(key)
175
176 with true <- key_exists?(group, key) do
177 shell_info("The following settings will be removed from ConfigDB:\n")
178
179 group
180 |> ConfigDB.get_by_group_and_key(key)
181 |> dump()
182
183 delete_key(group, key)
184 else
185 _ ->
186 shell_error("No settings in ConfigDB for #{inspect(group)}, #{inspect(key)}. Aborting.")
187 end
188 end
189
190 def run(["delete", "--force", group]) do
191 start_pleroma()
192
193 group = maybe_atomize(group)
194
195 with true <- group_exists?(group) do
196 shell_info("The following settings will be removed from ConfigDB:\n")
197 dump_group(group)
198 delete_group(group)
199 else
200 _ -> shell_error("No settings in ConfigDB for #{inspect(group)}. Aborting.")
201 end
202 end
203
204 def run(["delete", group, key]) do
205 start_pleroma()
206
207 group = maybe_atomize(group)
208 key = maybe_atomize(key)
209
210 with true <- key_exists?(group, key) do
211 shell_info("The following settings will be removed from ConfigDB:\n")
212
213 group
214 |> ConfigDB.get_by_group_and_key(key)
215 |> dump()
216
217 if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
218 delete_key(group, key)
219 else
220 shell_error("No changes made.")
221 end
222 else
223 _ ->
224 shell_error("No settings in ConfigDB for #{inspect(group)}, #{inspect(key)}. Aborting.")
225 end
226 end
227
228 def run(["delete", group]) do
229 start_pleroma()
230
231 group = maybe_atomize(group)
232
233 with true <- group_exists?(group) do
234 shell_info("The following settings will be removed from ConfigDB:\n")
235 dump_group(group)
236
237 if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
238 delete_group(group)
239 else
240 shell_error("No changes made.")
241 end
242 else
243 _ -> shell_error("No settings in ConfigDB for #{inspect(group)}. Aborting.")
244 end
245 end
246
247 @spec migrate_to_db(Path.t() | nil) :: any()
248 def migrate_to_db(file_path \\ nil) do
249 with :ok <- Pleroma.Config.DeprecationWarnings.warn() do
250 config_file =
251 if file_path do
252 file_path
253 else
254 if Pleroma.Config.get(:release) do
255 Pleroma.Config.get(:config_path)
256 else
257 "config/#{Pleroma.Config.get(:env)}.secret.exs"
258 end
259 end
260
261 do_migrate_to_db(config_file)
262 else
263 _ ->
264 shell_error("Migration is not allowed until all deprecation warnings have been resolved.")
265 end
266 end
267
268 defp do_migrate_to_db(config_file) do
269 if File.exists?(config_file) do
270 shell_info("Migrating settings from file: #{Path.expand(config_file)}")
271 truncatedb()
272
273 custom_config =
274 config_file
275 |> read_file()
276 |> elem(0)
277
278 custom_config
279 |> Keyword.keys()
280 |> Enum.each(&create(&1, custom_config))
281 else
282 shell_info("To migrate settings, you must define custom settings in #{config_file}.")
283 end
284 end
285
286 defp create(group, settings) do
287 group
288 |> Pleroma.Config.Loader.filter_group(settings)
289 |> Enum.each(fn {key, value} ->
290 {:ok, _} = ConfigDB.update_or_create(%{group: group, key: key, value: value})
291
292 shell_info("Settings for key #{key} migrated.")
293 end)
294
295 shell_info("Settings for group #{inspect(group)} migrated.")
296 end
297
298 defp migrate_from_db(opts) do
299 env = opts[:env] || Pleroma.Config.get(:env)
300
301 filename = "#{env}.exported_from_db.secret.exs"
302
303 config_path =
304 cond do
305 opts[:path] ->
306 opts[:path]
307
308 Pleroma.Config.get(:release) ->
309 :config_path
310 |> Pleroma.Config.get()
311 |> Path.dirname()
312
313 true ->
314 "config"
315 end
316 |> Path.join(filename)
317
318 with {:ok, file} <- File.open(config_path, [:write, :utf8]) do
319 write_config(file, config_path, opts)
320 shell_info("Database configuration settings have been exported to #{config_path}")
321 else
322 _ ->
323 shell_error("Impossible to save settings to this directory #{Path.dirname(config_path)}")
324 tmp_config_path = Path.join(System.tmp_dir!(), filename)
325 file = File.open!(tmp_config_path)
326
327 shell_info(
328 "Saving database configuration settings to #{tmp_config_path}. Copy it to the #{Path.dirname(config_path)} manually."
329 )
330
331 write_config(file, tmp_config_path, opts)
332 end
333 end
334
335 defp write_config(file, path, opts) do
336 IO.write(file, config_header())
337
338 ConfigDB
339 |> Repo.all()
340 |> Enum.each(&write_and_delete(&1, file, opts[:delete]))
341
342 :ok = File.close(file)
343 System.cmd("mix", ["format", path])
344 end
345
346 if Code.ensure_loaded?(Config.Reader) do
347 defp config_header, do: "import Config\r\n\r\n"
348 defp read_file(config_file), do: Config.Reader.read_imports!(config_file)
349 else
350 defp config_header, do: "use Mix.Config\r\n\r\n"
351 defp read_file(config_file), do: Mix.Config.eval!(config_file)
352 end
353
354 defp write_and_delete(config, file, delete?) do
355 config
356 |> write(file)
357 |> delete(delete?)
358 end
359
360 defp write(config, file) do
361 value = inspect(config.value, limit: :infinity)
362
363 IO.write(file, "config #{inspect(config.group)}, #{inspect(config.key)}, #{value}\r\n\r\n")
364
365 config
366 end
367
368 defp delete(config, true) do
369 {:ok, _} = Repo.delete(config)
370
371 shell_info(
372 "config #{inspect(config.group)}, #{inspect(config.key)} was deleted from the ConfigDB."
373 )
374 end
375
376 defp delete(_config, _), do: :ok
377
378 defp dump(%ConfigDB{} = config) do
379 value = inspect(config.value, limit: :infinity)
380
381 shell_info("config #{inspect(config.group)}, #{inspect(config.key)}, #{value}\r\n\r\n")
382 end
383
384 defp dump(_), do: :noop
385
386 defp dump_group(group) when is_atom(group) do
387 group
388 |> ConfigDB.get_all_by_group()
389 |> Enum.each(&dump/1)
390 end
391
392 defp group_exists?(group) do
393 group
394 |> ConfigDB.get_all_by_group()
395 |> Enum.any?()
396 end
397
398 defp key_exists?(group, key) do
399 group
400 |> ConfigDB.get_by_group_and_key(key)
401 |> is_nil
402 |> Kernel.!()
403 end
404
405 defp maybe_atomize(arg) when is_atom(arg), do: arg
406
407 defp maybe_atomize(":" <> arg), do: maybe_atomize(arg)
408
409 defp maybe_atomize(arg) when is_binary(arg) do
410 if ConfigDB.module_name?(arg) do
411 String.to_existing_atom("Elixir." <> arg)
412 else
413 String.to_atom(arg)
414 end
415 end
416
417 defp check_configdb(callback) do
418 with true <- Pleroma.Config.get([:configurable_from_database]) do
419 callback.()
420 else
421 _ ->
422 shell_error(
423 "ConfigDB not enabled. Please check the value of :configurable_from_database in your configuration."
424 )
425 end
426 end
427
428 defp delete_key(group, key) do
429 check_configdb(fn ->
430 ConfigDB.delete(%{group: group, key: key})
431 end)
432 end
433
434 defp delete_group(group) do
435 check_configdb(fn ->
436 group
437 |> ConfigDB.get_all_by_group()
438 |> Enum.each(&ConfigDB.delete/1)
439 end)
440 end
441
442 defp truncatedb do
443 Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;")
444 Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;")
445 end
446 end