Merge branch 'feature/configdb-mix-tasks-refactoring' into 'feature/configdb-mix...
[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 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 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(%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(_), do: :noop
305
306 defp dump_group(group) when is_atom(group) do
307 group
308 |> ConfigDB.get_all_by_group()
309 |> Enum.each(&dump/1)
310 end
311
312 defp group_exists?(group) do
313 group
314 |> ConfigDB.get_all_by_group()
315 |> Enum.any?()
316 end
317
318 defp maybe_atomize(arg) when is_atom(arg), do: arg
319
320 defp maybe_atomize(arg) when is_binary(arg) do
321 if ConfigDB.module_name?(arg) do
322 String.to_existing_atom("Elixir." <> arg)
323 else
324 String.to_atom(arg)
325 end
326 end
327
328 defp check_configdb(callback) do
329 with true <- Pleroma.Config.get([:configurable_from_database]) do
330 callback.()
331 else
332 _ ->
333 shell_error(
334 "ConfigDB not enabled. Please check the value of :configurable_from_database in your configuration."
335 )
336 end
337 end
338
339 defp delete_key(group, key) do
340 check_configdb(fn ->
341 ConfigDB.delete(%{group: group, key: key})
342 end)
343 end
344
345 defp delete_group(group) do
346 check_configdb(fn ->
347 group
348 |> ConfigDB.get_all_by_group()
349 |> Enum.each(&ConfigDB.delete/1)
350 end)
351 end
352
353 defp truncatedb do
354 Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;")
355 Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;")
356 end
357 end