2c1864ef115d468dee9cf5652d99f27fd558e7fe
[akkoma] / lib / pleroma / application_requirements.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 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.error("""
38 To send welcome email do you need to enable mail.
39 \nconfig :pleroma, Pleroma.Emails.Mailer, enabled: true
40 """)
41
42 {:error, "The mail disabled."}
43 else
44 :ok
45 end
46 end
47
48 defp check_welcome_message_config!(result), do: result
49
50 # Checks account confirmation email
51 #
52 def check_confirmation_accounts!(:ok) do
53 if Pleroma.Config.get([:instance, :account_activation_required]) &&
54 not Pleroma.Config.get([Pleroma.Emails.Mailer, :enabled]) do
55 Logger.error(
56 "Account activation enabled, but no Mailer settings enabled.\n" <>
57 "Please set config :pleroma, :instance, account_activation_required: false\n" <>
58 "Otherwise setup and enable Mailer."
59 )
60
61 {:error,
62 "Account activation enabled, but Mailer is disabled. Cannot send confirmation emails."}
63 else
64 :ok
65 end
66 end
67
68 def check_confirmation_accounts!(result), do: result
69
70 # Checks for pending migrations.
71 #
72 def check_migrations_applied!(:ok) do
73 unless Pleroma.Config.get(
74 [:i_am_aware_this_may_cause_data_loss, :disable_migration_check],
75 false
76 ) do
77 {_, res, _} =
78 Ecto.Migrator.with_repo(Pleroma.Repo, fn repo ->
79 down_migrations =
80 Ecto.Migrator.migrations(repo)
81 |> Enum.reject(fn
82 {:up, _, _} -> true
83 {:down, _, _} -> false
84 end)
85
86 if length(down_migrations) > 0 do
87 down_migrations_text =
88 Enum.map(down_migrations, fn {:down, id, name} -> "- #{name} (#{id})\n" end)
89
90 Logger.error(
91 "The following migrations were not applied:\n#{down_migrations_text}" <>
92 "If you want to start Pleroma anyway, set\n" <>
93 "config :pleroma, :i_am_aware_this_may_cause_data_loss, disable_migration_check: true"
94 )
95
96 {:error, "Unapplied Migrations detected"}
97 else
98 :ok
99 end
100 end)
101
102 res
103 else
104 :ok
105 end
106 end
107
108 def check_migrations_applied!(result), do: result
109
110 # Checks for settings of RUM indexes.
111 #
112 defp check_rum!(:ok) do
113 {_, res, _} =
114 Ecto.Migrator.with_repo(Pleroma.Repo, fn repo ->
115 migrate =
116 from(o in "columns",
117 where: o.table_name == "objects",
118 where: o.column_name == "fts_content"
119 )
120 |> repo.exists?(prefix: "information_schema")
121
122 setting = Pleroma.Config.get([:database, :rum_enabled], false)
123
124 do_check_rum!(setting, migrate)
125 end)
126
127 res
128 end
129
130 defp check_rum!(result), do: result
131
132 defp do_check_rum!(setting, migrate) do
133 case {setting, migrate} do
134 {true, false} ->
135 Logger.error(
136 "Use `RUM` index is enabled, but were not applied migrations for it.\n" <>
137 "If you want to start Pleroma anyway, set\n" <>
138 "config :pleroma, :database, rum_enabled: false\n" <>
139 "Otherwise apply the following migrations:\n" <>
140 "`mix ecto.migrate --migrations-path priv/repo/optional_migrations/rum_indexing/`"
141 )
142
143 {:error, "Unapplied RUM Migrations detected"}
144
145 {false, true} ->
146 Logger.error(
147 "Detected applied migrations to use `RUM` index, but `RUM` isn't enable in settings.\n" <>
148 "If you want to use `RUM`, set\n" <>
149 "config :pleroma, :database, rum_enabled: true\n" <>
150 "Otherwise roll `RUM` migrations back.\n" <>
151 "`mix ecto.rollback --migrations-path priv/repo/optional_migrations/rum_indexing/`"
152 )
153
154 {:error, "RUM Migrations detected"}
155
156 _ ->
157 :ok
158 end
159 end
160
161 defp check_system_commands!(:ok) do
162 filter_commands_statuses = [
163 check_filter(Pleroma.Upload.Filters.Exiftool, "exiftool"),
164 check_filter(Pleroma.Upload.Filters.Mogrify, "mogrify"),
165 check_filter(Pleroma.Upload.Filters.Mogrifun, "mogrify")
166 ]
167
168 preview_proxy_commands_status =
169 if !Config.get([:media_preview_proxy, :enabled]) or
170 MediaHelper.missing_dependencies() == [] do
171 true
172 else
173 Logger.error(
174 "The following dependencies required by Media preview proxy " <>
175 "(which is currently enabled) are not installed: " <>
176 inspect(MediaHelper.missing_dependencies())
177 )
178
179 false
180 end
181
182 if Enum.all?([preview_proxy_commands_status | filter_commands_statuses], & &1) do
183 :ok
184 else
185 {:error,
186 "System commands missing. Check logs and see `docs/installation` for more details."}
187 end
188 end
189
190 defp check_system_commands!(result), do: result
191
192 defp check_repo_pool_size!(:ok) do
193 if Pleroma.Config.get([Pleroma.Repo, :pool_size], 10) != 10 and
194 not Pleroma.Config.get([:dangerzone, :override_repo_pool_size], false) do
195 Logger.error("""
196 !!!CONFIG WARNING!!!
197 The database pool size has been altered from the recommended value of 10.\n
198 Please revert or ensure your database is tuned appropriately and then set\n
199 `config :pleroma, :dangerzone, override_repo_pool_size: true`.
200 """)
201
202 {:error, "Repo.pool_size different than recommended value."}
203 else
204 :ok
205 end
206 end
207
208 defp check_repo_pool_size!(result), do: result
209
210 defp check_filter(filter, command_required) do
211 filters = Config.get([Pleroma.Upload, :filters])
212
213 if filter in filters and not Pleroma.Utils.command_available?(command_required) do
214 Logger.error(
215 "#{filter} is specified in list of Pleroma.Upload filters, but the " <>
216 "#{command_required} command is not found"
217 )
218
219 false
220 else
221 true
222 end
223 end
224 end