Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

238 lines
7.1KB

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