38c6a6f1ddc88af1ecc0ac6c4194e87886acba12
[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 @spec migrate_to_db(Path.t() | nil) :: any()
34 def migrate_to_db(file_path \\ nil) do
35 if Pleroma.Config.get([:configurable_from_database]) do
36 config_file =
37 if file_path do
38 file_path
39 else
40 if Pleroma.Config.get(:release) do
41 Pleroma.Config.get(:config_path)
42 else
43 "config/#{Pleroma.Config.get(:env)}.secret.exs"
44 end
45 end
46
47 do_migrate_to_db(config_file)
48 else
49 migration_error()
50 end
51 end
52
53 defp do_migrate_to_db(config_file) do
54 if File.exists?(config_file) do
55 shell_info("Migrating settings from file: #{Path.expand(config_file)}")
56 Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;")
57 Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;")
58
59 custom_config =
60 config_file
61 |> read_file()
62 |> elem(0)
63
64 custom_config
65 |> Keyword.keys()
66 |> Enum.each(&create(&1, custom_config))
67 else
68 shell_info("To migrate settings, you must define custom settings in #{config_file}.")
69 end
70 end
71
72 defp create(group, settings) do
73 group
74 |> Pleroma.Config.Loader.filter_group(settings)
75 |> Enum.each(fn {key, value} ->
76 {:ok, _} = ConfigDB.update_or_create(%{group: group, key: key, value: value})
77
78 shell_info("Settings for key #{key} migrated.")
79 end)
80
81 shell_info("Settings for group :#{group} migrated.")
82 end
83
84 defp migrate_from_db(opts) do
85 if Pleroma.Config.get([:configurable_from_database]) do
86 env = opts[:env] || Mix.env()
87
88 config_path =
89 if Pleroma.Config.get(:release) do
90 :config_path
91 |> Pleroma.Config.get()
92 |> Path.dirname()
93 else
94 "config"
95 end
96 |> Path.join("#{env}.exported_from_db.secret.exs")
97
98 file = File.open!(config_path, [:write, :utf8])
99
100 IO.write(file, config_header())
101
102 ConfigDB
103 |> Repo.all()
104 |> Enum.each(&write_and_delete(&1, file, opts[:delete]))
105
106 :ok = File.close(file)
107 System.cmd("mix", ["format", config_path])
108 shell_info("Database configuration settings have been exported to config/#{env}.exported_from_db.secret.exs")
109 else
110 migration_error()
111 end
112 end
113
114 defp migration_error do
115 shell_error(
116 "Migration is not allowed in config. You can change this behavior by setting `config :pleroma, configurable_from_database: true`"
117 )
118 end
119
120 if Code.ensure_loaded?(Config.Reader) do
121 defp config_header, do: "import Config\r\n\r\n"
122 defp read_file(config_file), do: Config.Reader.read_imports!(config_file)
123 else
124 defp config_header, do: "use Mix.Config\r\n\r\n"
125 defp read_file(config_file), do: Mix.Config.eval!(config_file)
126 end
127
128 defp write_and_delete(config, file, delete?) do
129 config
130 |> write(file)
131 |> delete(delete?)
132 end
133
134 defp write(config, file) do
135 value = inspect(config.value, limit: :infinity)
136
137 IO.write(file, "config #{inspect(config.group)}, #{inspect(config.key)}, #{value}\r\n\r\n")
138
139 config
140 end
141
142 defp delete(config, true) do
143 {:ok, _} = Repo.delete(config)
144 shell_info("#{config.key} deleted from DB.")
145 end
146
147 defp delete(_config, _), do: :ok
148 end