More compact representation
[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 group
66 |> ConfigDB.get_all_by_group_and_key(key)
67 |> Enum.each(&dump/1)
68 end)
69 end
70
71 def run(["dump", group]) do
72 check_configdb(fn ->
73 start_pleroma()
74
75 group = maybe_atomize(group)
76
77 dump_group(group)
78 end)
79 end
80
81 def run(["groups"]) do
82 check_configdb(fn ->
83 start_pleroma()
84
85 groups =
86 ConfigDB
87 |> Repo.all()
88 |> Enum.map(fn x -> x.group end)
89 |> Enum.sort()
90 |> Enum.uniq()
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 delete_key(group, key)
138 end
139
140 def run(["delete", "--force", group]) do
141 start_pleroma()
142
143 group = maybe_atomize(group)
144
145 with true <- group_exists?(group) do
146 shell_info("The following settings will be removed from ConfigDB:\n")
147 dump_group(group)
148 delete_group(group)
149 else
150 _ -> shell_error("No settings in ConfigDB for #{inspect(group)}. Aborting.")
151 end
152 end
153
154 def run(["delete", group, key]) do
155 start_pleroma()
156
157 group = maybe_atomize(group)
158 key = maybe_atomize(key)
159
160 if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
161 delete_key(group, key)
162 else
163 shell_error("No changes made.")
164 end
165 end
166
167 def run(["delete", group]) do
168 start_pleroma()
169
170 group = maybe_atomize(group)
171
172 with true <- group_exists?(group) do
173 shell_info("The following settings will be removed from ConfigDB:\n")
174 dump_group(group)
175
176 if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
177 delete_group(group)
178 else
179 shell_error("No changes made.")
180 end
181 else
182 _ -> shell_error("No settings in ConfigDB for #{inspect(group)}. Aborting.")
183 end
184 end
185
186 @spec migrate_to_db(Path.t() | nil) :: any()
187 def migrate_to_db(file_path \\ nil) do
188 with :ok <- Pleroma.Config.DeprecationWarnings.warn() do
189 config_file =
190 if file_path do
191 file_path
192 else
193 if Pleroma.Config.get(:release) do
194 Pleroma.Config.get(:config_path)
195 else
196 "config/#{Pleroma.Config.get(:env)}.secret.exs"
197 end
198 end
199
200 do_migrate_to_db(config_file)
201 else
202 _ ->
203 shell_error("Migration is not allowed until all deprecation warnings have been resolved.")
204 end
205 end
206
207 defp do_migrate_to_db(config_file) do
208 if File.exists?(config_file) do
209 shell_info("Migrating settings from file: #{Path.expand(config_file)}")
210 truncatedb()
211
212 custom_config =
213 config_file
214 |> read_file()
215 |> elem(0)
216
217 custom_config
218 |> Keyword.keys()
219 |> Enum.each(&create(&1, custom_config))
220 else
221 shell_info("To migrate settings, you must define custom settings in #{config_file}.")
222 end
223 end
224
225 defp create(group, settings) do
226 group
227 |> Pleroma.Config.Loader.filter_group(settings)
228 |> Enum.each(fn {key, value} ->
229 {:ok, _} = ConfigDB.update_or_create(%{group: group, key: key, value: value})
230
231 shell_info("Settings for key #{key} migrated.")
232 end)
233
234 shell_info("Settings for group #{inspect(group)} migrated.")
235 end
236
237 defp migrate_from_db(opts) do
238 env = opts[:env] || Pleroma.Config.get(:env)
239
240 config_path =
241 if Pleroma.Config.get(:release) do
242 :config_path
243 |> Pleroma.Config.get()
244 |> Path.dirname()
245 else
246 "config"
247 end
248 |> Path.join("#{env}.exported_from_db.secret.exs")
249
250 file = File.open!(config_path, [:write, :utf8])
251
252 IO.write(file, config_header())
253
254 ConfigDB
255 |> Repo.all()
256 |> Enum.each(&write_and_delete(&1, file, opts[:delete]))
257
258 :ok = File.close(file)
259 System.cmd("mix", ["format", config_path])
260
261 shell_info(
262 "Database configuration settings have been exported to config/#{env}.exported_from_db.secret.exs"
263 )
264 end
265
266 if Code.ensure_loaded?(Config.Reader) do
267 defp config_header, do: "import Config\r\n\r\n"
268 defp read_file(config_file), do: Config.Reader.read_imports!(config_file)
269 else
270 defp config_header, do: "use Mix.Config\r\n\r\n"
271 defp read_file(config_file), do: Mix.Config.eval!(config_file)
272 end
273
274 defp write_and_delete(config, file, delete?) do
275 config
276 |> write(file)
277 |> delete(delete?)
278 end
279
280 defp write(config, file) do
281 value = inspect(config.value, limit: :infinity)
282
283 IO.write(file, "config #{inspect(config.group)}, #{inspect(config.key)}, #{value}\r\n\r\n")
284
285 config
286 end
287
288 defp delete(config, true) do
289 {:ok, _} = Repo.delete(config)
290
291 shell_info(
292 "config #{inspect(config.group)}, #{inspect(config.key)} was deleted from the ConfigDB."
293 )
294 end
295
296 defp delete(_config, _), do: :ok
297
298 defp dump(%Pleroma.ConfigDB{} = config) do
299 value = inspect(config.value, limit: :infinity)
300
301 shell_info("config #{inspect(config.group)}, #{inspect(config.key)}, #{value}\r\n\r\n")
302 end
303
304 defp dump_group(group) when is_atom(group) do
305 group
306 |> ConfigDB.get_all_by_group()
307 |> Enum.each(&dump/1)
308 end
309
310 defp group_exists?(group) do
311 group
312 |> ConfigDB.get_all_by_group()
313 |> Enum.any?()
314 end
315
316 defp maybe_atomize(arg) when is_atom(arg), do: arg
317
318 defp maybe_atomize(arg) when is_binary(arg) do
319 if Pleroma.ConfigDB.module_name?(arg) do
320 String.to_existing_atom("Elixir." <> arg)
321 else
322 String.to_atom(arg)
323 end
324 end
325
326 defp check_configdb(callback) do
327 with true <- Pleroma.Config.get([:configurable_from_database]) do
328 callback.()
329 else
330 _ ->
331 shell_error(
332 "ConfigDB not enabled. Please check the value of :configurable_from_database in your configuration."
333 )
334 end
335 end
336
337 defp delete_key(group, key) do
338 check_configdb(fn ->
339 Pleroma.ConfigDB.delete(%{group: group, key: key})
340 end)
341 end
342
343 defp delete_group(group) do
344 check_configdb(fn ->
345 group
346 |> Pleroma.ConfigDB.get_all_by_group()
347 |> Enum.each(&ConfigDB.delete/1)
348 end)
349 end
350
351 defp truncatedb do
352 Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;")
353 Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;")
354 end
355 end