revert ae54c06bb4bd2389d6eb1502b842c6b632e12e40
[akkoma] / lib / pleroma / application_requirements.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.ApplicationRequirements do
6 @moduledoc """
7 The module represents the collection of validations to runs before start server.
8 """
9
10 defmodule VerifyError, do: defexception([:message])
11
12 alias Pleroma.Config
13 alias Pleroma.Helpers.MediaHelper
14
15 import Ecto.Query
16
17 require Logger
18
19 @spec verify!() :: :ok | VerifyError.t()
20 def verify! do
21 :ok
22 |> check_system_commands!()
23 |> check_confirmation_accounts!()
24 |> check_migrations_applied!()
25 |> check_welcome_message_config!()
26 |> check_rum!()
27 |> check_repo_pool_size!()
28 |> handle_result()
29 end
30
31 defp handle_result(:ok), do: :ok
32 defp handle_result({:error, message}), do: raise(VerifyError, message: message)
33
34 defp check_welcome_message_config!(:ok) do
35 if Pleroma.Config.get([:welcome, :email, :enabled], false) and
36 not Pleroma.Emails.Mailer.enabled?() do
37 Logger.warn("""
38 To send welcome emails, you need to enable the mailer.
39 Welcome emails will NOT be sent with the current config.
40
41 Enable the mailer:
42 config :pleroma, Pleroma.Emails.Mailer, enabled: true
43 """)
44 end
45
46 :ok
47 end
48
49 defp check_welcome_message_config!(result), do: result
50
51 # Checks account confirmation email
52 #
53 def check_confirmation_accounts!(:ok) do
54 if Pleroma.Config.get([:instance, :account_activation_required]) &&
55 not Pleroma.Emails.Mailer.enabled?() do
56 Logger.warn("""
57 Account activation is required, but the mailer is disabled.
58 Users will NOT be able to confirm their accounts with this config.
59 Either disable account activation or enable the mailer.
60
61 Disable account activation:
62 config :pleroma, :instance, account_activation_required: false
63
64 Enable the mailer:
65 config :pleroma, Pleroma.Emails.Mailer, enabled: true
66 """)
67 end
68
69 :ok
70 end
71
72 def check_confirmation_accounts!(result), do: result
73
74 # Checks for pending migrations.
75 #
76 def check_migrations_applied!(:ok) do
77 unless Pleroma.Config.get(
78 [:i_am_aware_this_may_cause_data_loss, :disable_migration_check],
79 false
80 ) do
81 {_, res, _} =
82 Ecto.Migrator.with_repo(Pleroma.Repo, fn repo ->
83 down_migrations =
84 Ecto.Migrator.migrations(repo)
85 |> Enum.reject(fn
86 {:up, _, _} -> true
87 {:down, _, _} -> false
88 end)
89
90 if length(down_migrations) > 0 do
91 down_migrations_text =
92 Enum.map(down_migrations, fn {:down, id, name} -> "- #{name} (#{id})\n" end)
93
94 Logger.error(
95 "The following migrations were not applied:\n#{down_migrations_text}" <>
96 "If you want to start Pleroma anyway, set\n" <>
97 "config :pleroma, :i_am_aware_this_may_cause_data_loss, disable_migration_check: true"
98 )
99
100 {:error, "Unapplied Migrations detected"}
101 else
102 :ok
103 end
104 end)
105
106 res
107 else
108 :ok
109 end
110 end
111
112 def check_migrations_applied!(result), do: result
113
114 # Checks for settings of RUM indexes.
115 #
116 defp check_rum!(:ok) do
117 {_, res, _} =
118 Ecto.Migrator.with_repo(Pleroma.Repo, fn repo ->
119 migrate =
120 from(o in "columns",
121 where: o.table_name == "objects",
122 where: o.column_name == "fts_content"
123 )
124 |> repo.exists?(prefix: "information_schema")
125
126 setting = Pleroma.Config.get([:database, :rum_enabled], false)
127
128 do_check_rum!(setting, migrate)
129 end)
130
131 res
132 end
133
134 defp check_rum!(result), do: result
135
136 defp do_check_rum!(setting, migrate) do
137 case {setting, migrate} do
138 {true, false} ->
139 Logger.error(
140 "Use `RUM` index is enabled, but were not applied migrations for it.\n" <>
141 "If you want to start Pleroma anyway, set\n" <>
142 "config :pleroma, :database, rum_enabled: false\n" <>
143 "Otherwise apply the following migrations:\n" <>
144 "`mix ecto.migrate --migrations-path priv/repo/optional_migrations/rum_indexing/`"
145 )
146
147 {:error, "Unapplied RUM Migrations detected"}
148
149 {false, true} ->
150 Logger.error(
151 "Detected applied migrations to use `RUM` index, but `RUM` isn't enable in settings.\n" <>
152 "If you want to use `RUM`, set\n" <>
153 "config :pleroma, :database, rum_enabled: true\n" <>
154 "Otherwise roll `RUM` migrations back.\n" <>
155 "`mix ecto.rollback --migrations-path priv/repo/optional_migrations/rum_indexing/`"
156 )
157
158 {:error, "RUM Migrations detected"}
159
160 _ ->
161 :ok
162 end
163 end
164
165 defp check_system_commands!(:ok) do
166 filter_commands_statuses = [
167 check_filter(Pleroma.Upload.Filter.Exiftool, "exiftool"),
168 check_filter(Pleroma.Upload.Filter.Mogrify, "mogrify"),
169 check_filter(Pleroma.Upload.Filter.Mogrifun, "mogrify"),
170 check_filter(Pleroma.Upload.Filter.AnalyzeMetadata, "mogrify"),
171 check_filter(Pleroma.Upload.Filter.AnalyzeMetadata, "convert"),
172 check_filter(Pleroma.Upload.Filter.AnalyzeMetadata, "ffprobe")
173 ]
174
175 preview_proxy_commands_status =
176 if !Config.get([:media_preview_proxy, :enabled]) or
177 MediaHelper.missing_dependencies() == [] do
178 true
179 else
180 Logger.error(
181 "The following dependencies required by Media preview proxy " <>
182 "(which is currently enabled) are not installed: " <>
183 inspect(MediaHelper.missing_dependencies())
184 )
185
186 false
187 end
188
189 if Enum.all?([preview_proxy_commands_status | filter_commands_statuses], & &1) do
190 :ok
191 else
192 {:error,
193 "System commands missing. Check logs and see `docs/installation` for more details."}
194 end
195 end
196
197 defp check_repo_pool_size!(:ok) do
198 if Pleroma.Config.get([Pleroma.Repo, :pool_size], 10) != 10 and
199 not Pleroma.Config.get([:dangerzone, :override_repo_pool_size], false) do
200 Logger.error("""
201 !!!CONFIG WARNING!!!
202
203 The database pool size has been altered from the recommended value of 10.
204
205 Please revert or ensure your database is tuned appropriately and then set
206 `config :pleroma, :dangerzone, override_repo_pool_size: true`.
207
208 If you are experiencing database timeouts, please check the "Optimizing
209 your PostgreSQL performance" section in the documentation. If you still
210 encounter issues after that, please open an issue on the tracker.
211 """)
212
213 {:error, "Repo.pool_size different than recommended value."}
214 else
215 :ok
216 end
217 end
218
219 defp check_repo_pool_size!(result), do: result
220
221 defp check_filter(filter, command_required) do
222 filters = Config.get([Pleroma.Upload, :filters])
223
224 if filter in filters and not Pleroma.Utils.command_available?(command_required) do
225 Logger.error(
226 "#{filter} is specified in list of Pleroma.Upload filters, but the " <>
227 "#{command_required} command is not found"
228 )
229
230 false
231 else
232 true
233 end
234 end
235 end