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.

231 lines
6.8KB

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