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