logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://anongit.hacktivis.me/git/pleroma.git/

application_requirements.ex (7714B)


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