Add comment for this mysterious behavior
[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"]) do
99 check_configdb(fn ->
100 start_pleroma()
101
102 shell_info("The following settings will be permanently removed:")
103
104 ConfigDB
105 |> Repo.all()
106 |> Enum.sort()
107 |> Enum.each(&dump(&1))
108
109 shell_error("\nTHIS CANNOT BE UNDONE!")
110
111 if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
112 Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;")
113 Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;")
114
115 shell_info("The ConfigDB settings have been removed from the database.")
116 else
117 shell_error("No changes made.")
118 end
119 end)
120 end
121
122 def run(["delete", group]) do
123 check_configdb(fn ->
124 start_pleroma()
125
126 group = maybe_atomize(group)
127
128 if group_exists?(group) do
129 shell_info("The following settings will be removed from ConfigDB:\n")
130
131 dump_group(group)
132
133 if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
134 ConfigDB
135 |> Repo.all()
136 |> Enum.filter(fn x ->
137 if x.group == group do
138 x |> delete(true)
139 end
140 end)
141 else
142 shell_error("No changes made.")
143 end
144 else
145 shell_error("No settings in ConfigDB for #{inspect(group)}. Aborting.")
146 end
147 end)
148 end
149
150 def run(["delete", group, key]) do
151 check_configdb(fn ->
152 start_pleroma()
153
154 group = maybe_atomize(group)
155 key = maybe_atomize(key)
156
157 if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
158 ConfigDB
159 |> Repo.all()
160 |> Enum.filter(fn x ->
161 if x.group == group and x.key == key do
162 x |> delete(true)
163 end
164 end)
165 else
166 shell_error("No changes made.")
167 end
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 Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;")
196 Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;")
197
198 custom_config =
199 config_file
200 |> read_file()
201 |> elem(0)
202
203 custom_config
204 |> Keyword.keys()
205 |> Enum.each(&create(&1, custom_config))
206 else
207 shell_info("To migrate settings, you must define custom settings in #{config_file}.")
208 end
209 end
210
211 defp create(group, settings) do
212 group
213 |> Pleroma.Config.Loader.filter_group(settings)
214 |> Enum.each(fn {key, value} ->
215 {:ok, _} = ConfigDB.update_or_create(%{group: group, key: key, value: value})
216
217 shell_info("Settings for key #{key} migrated.")
218 end)
219
220 shell_info("Settings for group #{inspect(group)} migrated.")
221 end
222
223 defp migrate_from_db(opts) do
224 env = opts[:env] || Pleroma.Config.get(:env)
225
226 config_path =
227 if Pleroma.Config.get(:release) do
228 :config_path
229 |> Pleroma.Config.get()
230 |> Path.dirname()
231 else
232 "config"
233 end
234 |> Path.join("#{env}.exported_from_db.secret.exs")
235
236 file = File.open!(config_path, [:write, :utf8])
237
238 IO.write(file, config_header())
239
240 ConfigDB
241 |> Repo.all()
242 |> Enum.each(&write_and_delete(&1, file, opts[:delete]))
243
244 :ok = File.close(file)
245 System.cmd("mix", ["format", config_path])
246
247 shell_info(
248 "Database configuration settings have been exported to config/#{env}.exported_from_db.secret.exs"
249 )
250 end
251
252 if Code.ensure_loaded?(Config.Reader) do
253 defp config_header, do: "import Config\r\n\r\n"
254 defp read_file(config_file), do: Config.Reader.read_imports!(config_file)
255 else
256 defp config_header, do: "use Mix.Config\r\n\r\n"
257 defp read_file(config_file), do: Mix.Config.eval!(config_file)
258 end
259
260 defp write_and_delete(config, file, delete?) do
261 config
262 |> write(file)
263 |> delete(delete?)
264 end
265
266 defp write(config, file) do
267 value = inspect(config.value, limit: :infinity)
268
269 IO.write(file, "config #{inspect(config.group)}, #{inspect(config.key)}, #{value}\r\n\r\n")
270
271 config
272 end
273
274 defp delete(config, true) do
275 {:ok, _} = Repo.delete(config)
276
277 shell_info(
278 "config #{inspect(config.group)}, #{inspect(config.key)} deleted from the ConfigDB."
279 )
280 end
281
282 defp delete(_config, _), do: :ok
283
284 defp dump(%Pleroma.ConfigDB{} = config) do
285 value = inspect(config.value, limit: :infinity)
286
287 shell_info("config #{inspect(config.group)}, #{inspect(config.key)}, #{value}\r\n\r\n")
288 end
289
290 defp dump_key(group, key) when is_atom(group) and is_atom(key) do
291 ConfigDB
292 |> Repo.all()
293 |> Enum.filter(fn x ->
294 if x.group == group && x.key == key do
295 x |> dump
296 end
297 end)
298 end
299
300 defp dump_group(group) when is_atom(group) do
301 ConfigDB
302 |> Repo.all()
303 |> Enum.filter(fn x ->
304 if x.group == group do
305 x |> dump
306 end
307 end)
308 end
309
310 defp group_exists?(group) when is_atom(group) do
311 result =
312 ConfigDB
313 |> Repo.all()
314 |> Enum.filter(fn x ->
315 if x.group == group do
316 x
317 end
318 end)
319
320 unless result == [] do
321 true
322 else
323 false
324 end
325 end
326
327 defp maybe_atomize(arg) when is_atom(arg), do: arg
328
329 defp maybe_atomize(arg) when is_binary(arg) do
330 chars = String.codepoints(arg)
331
332 # hack to make sure input like Pleroma.Mailer.Foo is formatted correctly
333 # for matching against values returned by Ecto
334 if "." in chars do
335 :"Elixir.#{arg}"
336 else
337 String.to_atom(arg)
338 end
339 end
340
341 defp check_configdb(callback) do
342 with true <- Pleroma.Config.get([:configurable_from_database]) do
343 callback.()
344 else
345 _ ->
346 shell_error(
347 "ConfigDB not enabled. Please check the value of :configurable_from_database in your configuration."
348 )
349 end
350 end
351 end