Make the --force flag for reset command consistent with the others and deduplicate...
[akkoma] / lib / mix / tasks / pleroma / config.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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 Mix.Pleroma
9
10 alias Pleroma.ConfigDB
11 alias Pleroma.Repo
12
13 @shortdoc "Manages the location of the config"
14 @moduledoc File.read!("docs/administration/CLI_tasks/config.md")
15
16 def run(["migrate_to_db"]) do
17 check_configdb(fn ->
18 start_pleroma()
19 migrate_to_db()
20 end)
21 end
22
23 def run(["migrate_from_db" | options]) do
24 check_configdb(fn ->
25 start_pleroma()
26
27 {opts, _} =
28 OptionParser.parse!(options,
29 strict: [env: :string, delete: :boolean],
30 aliases: [d: :delete]
31 )
32
33 migrate_from_db(opts)
34 end)
35 end
36
37 def run(["dump"]) do
38 check_configdb(fn ->
39 start_pleroma()
40
41 header = config_header()
42
43 settings =
44 ConfigDB
45 |> Repo.all()
46 |> Enum.sort()
47
48 unless settings == [] do
49 shell_info("#{header}")
50
51 settings |> Enum.each(&dump(&1))
52 else
53 shell_error("No settings in ConfigDB.")
54 end
55 end)
56 end
57
58 def run(["dump", group, key]) do
59 check_configdb(fn ->
60 start_pleroma()
61
62 group = maybe_atomize(group)
63 key = maybe_atomize(key)
64
65 dump_key(group, key)
66 end)
67 end
68
69 def run(["dump", group]) do
70 check_configdb(fn ->
71 start_pleroma()
72
73 group = maybe_atomize(group)
74
75 dump_group(group)
76 end)
77 end
78
79 def run(["groups"]) do
80 check_configdb(fn ->
81 start_pleroma()
82
83 groups =
84 ConfigDB
85 |> Repo.all()
86 |> Enum.map(fn x -> x.group end)
87 |> Enum.sort()
88 |> Enum.uniq()
89
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)
93 shell_info("\r\n")
94 end
95 end)
96 end
97
98 def run(["reset", "--force"]) do
99 check_configdb(fn ->
100 start_pleroma()
101 truncatedb()
102 shell_info("The ConfigDB settings have been removed from the database.")
103 end)
104 end
105
106 def run(["reset"]) do
107 check_configdb(fn ->
108 start_pleroma()
109
110 shell_info("The following settings will be permanently removed:")
111
112 ConfigDB
113 |> Repo.all()
114 |> Enum.sort()
115 |> Enum.each(&dump(&1))
116
117 shell_error("\nTHIS CANNOT BE UNDONE!")
118
119 if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
120 truncatedb()
121
122 shell_info("The ConfigDB settings have been removed from the database.")
123 else
124 shell_error("No changes made.")
125 end
126 end)
127 end
128
129 def run(["delete", "--force", group, key]) do
130 start_pleroma()
131
132 group = maybe_atomize(group)
133 key = maybe_atomize(key)
134
135 delete_key(group, key)
136 end
137
138 def run(["delete", "--force", group]) do
139 start_pleroma()
140
141 group = maybe_atomize(group)
142
143 delete_group(group)
144 end
145
146 def run(["delete", group, key]) do
147 start_pleroma()
148
149 group = maybe_atomize(group)
150 key = maybe_atomize(key)
151
152 if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
153 delete_key(group, key)
154 else
155 shell_error("No changes made.")
156 end
157 end
158
159 def run(["delete", group]) do
160 start_pleroma()
161
162 group = maybe_atomize(group)
163
164 if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
165 delete_group(group)
166 else
167 shell_error("No changes made.")
168 end
169 end
170
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
174 config_file =
175 if file_path do
176 file_path
177 else
178 if Pleroma.Config.get(:release) do
179 Pleroma.Config.get(:config_path)
180 else
181 "config/#{Pleroma.Config.get(:env)}.secret.exs"
182 end
183 end
184
185 do_migrate_to_db(config_file)
186 else
187 _ ->
188 shell_error("Migration is not allowed until all deprecation warnings have been resolved.")
189 end
190 end
191
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 truncatedb()
196
197 custom_config =
198 config_file
199 |> read_file()
200 |> elem(0)
201
202 custom_config
203 |> Keyword.keys()
204 |> Enum.each(&create(&1, custom_config))
205 else
206 shell_info("To migrate settings, you must define custom settings in #{config_file}.")
207 end
208 end
209
210 defp create(group, settings) do
211 group
212 |> Pleroma.Config.Loader.filter_group(settings)
213 |> Enum.each(fn {key, value} ->
214 {:ok, _} = ConfigDB.update_or_create(%{group: group, key: key, value: value})
215
216 shell_info("Settings for key #{key} migrated.")
217 end)
218
219 shell_info("Settings for group #{inspect(group)} migrated.")
220 end
221
222 defp migrate_from_db(opts) do
223 env = opts[:env] || Pleroma.Config.get(:env)
224
225 config_path =
226 if Pleroma.Config.get(:release) do
227 :config_path
228 |> Pleroma.Config.get()
229 |> Path.dirname()
230 else
231 "config"
232 end
233 |> Path.join("#{env}.exported_from_db.secret.exs")
234
235 file = File.open!(config_path, [:write, :utf8])
236
237 IO.write(file, config_header())
238
239 ConfigDB
240 |> Repo.all()
241 |> Enum.each(&write_and_delete(&1, file, opts[:delete]))
242
243 :ok = File.close(file)
244 System.cmd("mix", ["format", config_path])
245
246 shell_info(
247 "Database configuration settings have been exported to config/#{env}.exported_from_db.secret.exs"
248 )
249 end
250
251 if Code.ensure_loaded?(Config.Reader) do
252 defp config_header, do: "import Config\r\n\r\n"
253 defp read_file(config_file), do: Config.Reader.read_imports!(config_file)
254 else
255 defp config_header, do: "use Mix.Config\r\n\r\n"
256 defp read_file(config_file), do: Mix.Config.eval!(config_file)
257 end
258
259 defp write_and_delete(config, file, delete?) do
260 config
261 |> write(file)
262 |> delete(delete?)
263 end
264
265 defp write(config, file) do
266 value = inspect(config.value, limit: :infinity)
267
268 IO.write(file, "config #{inspect(config.group)}, #{inspect(config.key)}, #{value}\r\n\r\n")
269
270 config
271 end
272
273 defp delete(config, true) do
274 {:ok, _} = Repo.delete(config)
275
276 shell_info(
277 "config #{inspect(config.group)}, #{inspect(config.key)} was deleted from the ConfigDB."
278 )
279 end
280
281 defp delete(_config, _), do: :ok
282
283 defp dump(%Pleroma.ConfigDB{} = config) do
284 value = inspect(config.value, limit: :infinity)
285
286 shell_info("config #{inspect(config.group)}, #{inspect(config.key)}, #{value}\r\n\r\n")
287 end
288
289 defp dump_key(group, key) when is_atom(group) and is_atom(key) do
290 ConfigDB
291 |> Repo.all()
292 |> Enum.filter(fn x ->
293 if x.group == group && x.key == key do
294 x |> dump
295 end
296 end)
297 end
298
299 defp dump_group(group) when is_atom(group) do
300 ConfigDB
301 |> Repo.all()
302 |> Enum.filter(fn x ->
303 if x.group == group do
304 x |> dump
305 end
306 end)
307 end
308
309 defp group_exists?(group) when is_atom(group) do
310 result =
311 ConfigDB
312 |> Repo.all()
313 |> Enum.filter(fn x ->
314 if x.group == group do
315 x
316 end
317 end)
318
319 unless result == [] do
320 true
321 else
322 false
323 end
324 end
325
326 defp maybe_atomize(arg) when is_atom(arg), do: arg
327
328 defp maybe_atomize(arg) when is_binary(arg) do
329 chars = String.codepoints(arg)
330
331 # hack to make sure input like Pleroma.Mailer.Foo is formatted correctly
332 # for matching against values returned by Ecto
333 if "." in chars do
334 :"Elixir.#{arg}"
335 else
336 String.to_atom(arg)
337 end
338 end
339
340 defp check_configdb(callback) do
341 with true <- Pleroma.Config.get([:configurable_from_database]) do
342 callback.()
343 else
344 _ ->
345 shell_error(
346 "ConfigDB not enabled. Please check the value of :configurable_from_database in your configuration."
347 )
348 end
349 end
350
351 defp delete_key(group, key) do
352 check_configdb(fn ->
353 ConfigDB
354 |> Repo.all()
355 |> Enum.filter(fn x ->
356 if x.group == group and x.key == key do
357 x |> delete(true)
358 end
359 end)
360 end)
361 end
362
363 defp delete_group(group) do
364 check_configdb(fn ->
365 with true <- group_exists?(group) do
366 shell_info("The following settings will be removed from ConfigDB:\n")
367 dump_group(group)
368
369 ConfigDB
370 |> Repo.all()
371 |> Enum.filter(fn x ->
372 if x.group == group do
373 x |> delete(true)
374 end
375 end)
376 else
377 _ -> shell_error("No settings in ConfigDB for #{inspect(group)}. Aborting.")
378 end
379 end)
380 end
381
382 defp truncatedb() do
383 Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;")
384 Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;")
385 end
386 end