Merge branch 'chores/bump-copyright' 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/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],
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(["groups"]) do
83 check_configdb(fn ->
84 start_pleroma()
85
86 groups =
87 ConfigDB
88 |> distinct([c], true)
89 |> select([c], c.group)
90 |> Repo.all()
91
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)
95 shell_info("\r\n")
96 end
97 end)
98 end
99
100 def run(["reset", "--force"]) do
101 check_configdb(fn ->
102 start_pleroma()
103 truncatedb()
104 shell_info("The ConfigDB settings have been removed from the database.")
105 end)
106 end
107
108 def run(["reset"]) do
109 check_configdb(fn ->
110 start_pleroma()
111
112 shell_info("The following settings will be permanently removed:")
113
114 ConfigDB
115 |> Repo.all()
116 |> Enum.sort()
117 |> Enum.each(&dump(&1))
118
119 shell_error("\nTHIS CANNOT BE UNDONE!")
120
121 if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
122 truncatedb()
123
124 shell_info("The ConfigDB settings have been removed from the database.")
125 else
126 shell_error("No changes made.")
127 end
128 end)
129 end
130
131 def run(["delete", "--force", group, key]) do
132 start_pleroma()
133
134 group = maybe_atomize(group)
135 key = maybe_atomize(key)
136
137 with true <- key_exists?(group, key) do
138 shell_info("The following settings will be removed from ConfigDB:\n")
139
140 group
141 |> ConfigDB.get_by_group_and_key(key)
142 |> dump()
143
144 delete_key(group, key)
145 else
146 _ ->
147 shell_error("No settings in ConfigDB for #{inspect(group)}, #{inspect(key)}. Aborting.")
148 end
149 end
150
151 def run(["delete", "--force", group]) do
152 start_pleroma()
153
154 group = maybe_atomize(group)
155
156 with true <- group_exists?(group) do
157 shell_info("The following settings will be removed from ConfigDB:\n")
158 dump_group(group)
159 delete_group(group)
160 else
161 _ -> shell_error("No settings in ConfigDB for #{inspect(group)}. Aborting.")
162 end
163 end
164
165 def run(["delete", group, key]) do
166 start_pleroma()
167
168 group = maybe_atomize(group)
169 key = maybe_atomize(key)
170
171 with true <- key_exists?(group, key) do
172 shell_info("The following settings will be removed from ConfigDB:\n")
173
174 group
175 |> ConfigDB.get_by_group_and_key(key)
176 |> dump()
177
178 if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
179 delete_key(group, key)
180 else
181 shell_error("No changes made.")
182 end
183 else
184 _ ->
185 shell_error("No settings in ConfigDB for #{inspect(group)}, #{inspect(key)}. Aborting.")
186 end
187 end
188
189 def run(["delete", group]) do
190 start_pleroma()
191
192 group = maybe_atomize(group)
193
194 with true <- group_exists?(group) do
195 shell_info("The following settings will be removed from ConfigDB:\n")
196 dump_group(group)
197
198 if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
199 delete_group(group)
200 else
201 shell_error("No changes made.")
202 end
203 else
204 _ -> shell_error("No settings in ConfigDB for #{inspect(group)}. Aborting.")
205 end
206 end
207
208 @spec migrate_to_db(Path.t() | nil) :: any()
209 def migrate_to_db(file_path \\ nil) do
210 with :ok <- Pleroma.Config.DeprecationWarnings.warn() do
211 config_file =
212 if file_path do
213 file_path
214 else
215 if Pleroma.Config.get(:release) do
216 Pleroma.Config.get(:config_path)
217 else
218 "config/#{Pleroma.Config.get(:env)}.secret.exs"
219 end
220 end
221
222 do_migrate_to_db(config_file)
223 else
224 _ ->
225 shell_error("Migration is not allowed until all deprecation warnings have been resolved.")
226 end
227 end
228
229 defp do_migrate_to_db(config_file) do
230 if File.exists?(config_file) do
231 shell_info("Migrating settings from file: #{Path.expand(config_file)}")
232 truncatedb()
233
234 custom_config =
235 config_file
236 |> read_file()
237 |> elem(0)
238
239 custom_config
240 |> Keyword.keys()
241 |> Enum.each(&create(&1, custom_config))
242 else
243 shell_info("To migrate settings, you must define custom settings in #{config_file}.")
244 end
245 end
246
247 defp create(group, settings) do
248 group
249 |> Pleroma.Config.Loader.filter_group(settings)
250 |> Enum.each(fn {key, value} ->
251 {:ok, _} = ConfigDB.update_or_create(%{group: group, key: key, value: value})
252
253 shell_info("Settings for key #{key} migrated.")
254 end)
255
256 shell_info("Settings for group #{inspect(group)} migrated.")
257 end
258
259 defp migrate_from_db(opts) do
260 env = opts[:env] || Pleroma.Config.get(:env)
261
262 config_path =
263 if Pleroma.Config.get(:release) do
264 :config_path
265 |> Pleroma.Config.get()
266 |> Path.dirname()
267 else
268 "config"
269 end
270 |> Path.join("#{env}.exported_from_db.secret.exs")
271
272 file = File.open!(config_path, [:write, :utf8])
273
274 IO.write(file, config_header())
275
276 ConfigDB
277 |> Repo.all()
278 |> Enum.each(&write_and_delete(&1, file, opts[:delete]))
279
280 :ok = File.close(file)
281 System.cmd("mix", ["format", config_path])
282
283 shell_info(
284 "Database configuration settings have been exported to config/#{env}.exported_from_db.secret.exs"
285 )
286 end
287
288 if Code.ensure_loaded?(Config.Reader) do
289 defp config_header, do: "import Config\r\n\r\n"
290 defp read_file(config_file), do: Config.Reader.read_imports!(config_file)
291 else
292 defp config_header, do: "use Mix.Config\r\n\r\n"
293 defp read_file(config_file), do: Mix.Config.eval!(config_file)
294 end
295
296 defp write_and_delete(config, file, delete?) do
297 config
298 |> write(file)
299 |> delete(delete?)
300 end
301
302 defp write(config, file) do
303 value = inspect(config.value, limit: :infinity)
304
305 IO.write(file, "config #{inspect(config.group)}, #{inspect(config.key)}, #{value}\r\n\r\n")
306
307 config
308 end
309
310 defp delete(config, true) do
311 {:ok, _} = Repo.delete(config)
312
313 shell_info(
314 "config #{inspect(config.group)}, #{inspect(config.key)} was deleted from the ConfigDB."
315 )
316 end
317
318 defp delete(_config, _), do: :ok
319
320 defp dump(%ConfigDB{} = config) do
321 value = inspect(config.value, limit: :infinity)
322
323 shell_info("config #{inspect(config.group)}, #{inspect(config.key)}, #{value}\r\n\r\n")
324 end
325
326 defp dump(_), do: :noop
327
328 defp dump_group(group) when is_atom(group) do
329 group
330 |> ConfigDB.get_all_by_group()
331 |> Enum.each(&dump/1)
332 end
333
334 defp group_exists?(group) do
335 group
336 |> ConfigDB.get_all_by_group()
337 |> Enum.any?()
338 end
339
340 defp key_exists?(group, key) do
341 group
342 |> ConfigDB.get_by_group_and_key(key)
343 |> is_nil
344 |> Kernel.!()
345 end
346
347 defp maybe_atomize(arg) when is_atom(arg), do: arg
348
349 defp maybe_atomize(":" <> arg), do: maybe_atomize(arg)
350
351 defp maybe_atomize(arg) when is_binary(arg) do
352 if ConfigDB.module_name?(arg) do
353 String.to_existing_atom("Elixir." <> arg)
354 else
355 String.to_atom(arg)
356 end
357 end
358
359 defp check_configdb(callback) do
360 with true <- Pleroma.Config.get([:configurable_from_database]) do
361 callback.()
362 else
363 _ ->
364 shell_error(
365 "ConfigDB not enabled. Please check the value of :configurable_from_database in your configuration."
366 )
367 end
368 end
369
370 defp delete_key(group, key) do
371 check_configdb(fn ->
372 ConfigDB.delete(%{group: group, key: key})
373 end)
374 end
375
376 defp delete_group(group) do
377 check_configdb(fn ->
378 group
379 |> ConfigDB.get_all_by_group()
380 |> Enum.each(&ConfigDB.delete/1)
381 end)
382 end
383
384 defp truncatedb do
385 Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;")
386 Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;")
387 end
388 end