logo

pleroma

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

application_requirements.ex (7229B)


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