summaryrefslogtreecommitdiff
path: root/lib/pleroma/application_requirements.ex
blob: a56311a65e623f7352ea5e2013e5646ae306cfef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.ApplicationRequirements do
  @moduledoc """
  The module represents the collection of validations to runs before start server.
  """

  defmodule VerifyError, do: defexception([:message])

  alias Pleroma.Config
  alias Pleroma.Helpers.MediaHelper

  import Ecto.Query

  require Logger

  @spec verify!() :: :ok | VerifyError.t()
  def verify! do
    :ok
    |> check_system_commands!()
    |> check_confirmation_accounts!()
    |> check_migrations_applied!()
    |> check_welcome_message_config!()
    |> check_rum!()
    |> check_repo_pool_size!()
    |> handle_result()
  end

  defp handle_result(:ok), do: :ok
  defp handle_result({:error, message}), do: raise(VerifyError, message: message)

  defp check_welcome_message_config!(:ok) do
    if Pleroma.Config.get([:welcome, :email, :enabled], false) and
         not Pleroma.Emails.Mailer.enabled?() do
      Logger.warn("""
      To send welcome emails, you need to enable the mailer.
      Welcome emails will NOT be sent with the current config.

      Enable the mailer:
        config :pleroma, Pleroma.Emails.Mailer, enabled: true
      """)
    end

    :ok
  end

  defp check_welcome_message_config!(result), do: result

  # Checks account confirmation email
  #
  def check_confirmation_accounts!(:ok) do
    if Pleroma.Config.get([:instance, :account_activation_required]) &&
         not Pleroma.Emails.Mailer.enabled?() do
      Logger.warn("""
      Account activation is required, but the mailer is disabled.
      Users will NOT be able to confirm their accounts with this config.
      Either disable account activation or enable the mailer.

      Disable account activation:
        config :pleroma, :instance, account_activation_required: false

      Enable the mailer:
        config :pleroma, Pleroma.Emails.Mailer, enabled: true
      """)
    end

    :ok
  end

  def check_confirmation_accounts!(result), do: result

  # Checks for pending migrations.
  #
  def check_migrations_applied!(:ok) do
    unless Pleroma.Config.get(
             [:i_am_aware_this_may_cause_data_loss, :disable_migration_check],
             false
           ) do
      {_, res, _} =
        Ecto.Migrator.with_repo(Pleroma.Repo, fn repo ->
          down_migrations =
            Ecto.Migrator.migrations(repo)
            |> Enum.reject(fn
              {:up, _, _} -> true
              {:down, _, _} -> false
            end)

          if length(down_migrations) > 0 do
            down_migrations_text =
              Enum.map(down_migrations, fn {:down, id, name} -> "- #{name} (#{id})\n" end)

            Logger.error(
              "The following migrations were not applied:\n#{down_migrations_text}" <>
                "If you want to start Pleroma anyway, set\n" <>
                "config :pleroma, :i_am_aware_this_may_cause_data_loss, disable_migration_check: true"
            )

            {:error, "Unapplied Migrations detected"}
          else
            :ok
          end
        end)

      res
    else
      :ok
    end
  end

  def check_migrations_applied!(result), do: result

  # Checks for settings of RUM indexes.
  #
  defp check_rum!(:ok) do
    {_, res, _} =
      Ecto.Migrator.with_repo(Pleroma.Repo, fn repo ->
        migrate =
          from(o in "columns",
            where: o.table_name == "objects",
            where: o.column_name == "fts_content"
          )
          |> repo.exists?(prefix: "information_schema")

        setting = Pleroma.Config.get([:database, :rum_enabled], false)

        do_check_rum!(setting, migrate)
      end)

    res
  end

  defp check_rum!(result), do: result

  defp do_check_rum!(setting, migrate) do
    case {setting, migrate} do
      {true, false} ->
        Logger.error(
          "Use `RUM` index is enabled, but were not applied migrations for it.\n" <>
            "If you want to start Pleroma anyway, set\n" <>
            "config :pleroma, :database, rum_enabled: false\n" <>
            "Otherwise apply the following migrations:\n" <>
            "`mix ecto.migrate --migrations-path priv/repo/optional_migrations/rum_indexing/`"
        )

        {:error, "Unapplied RUM Migrations detected"}

      {false, true} ->
        Logger.error(
          "Detected applied migrations to use `RUM` index, but `RUM` isn't enable in settings.\n" <>
            "If you want to use `RUM`, set\n" <>
            "config :pleroma, :database, rum_enabled: true\n" <>
            "Otherwise roll `RUM` migrations back.\n" <>
            "`mix ecto.rollback --migrations-path priv/repo/optional_migrations/rum_indexing/`"
        )

        {:error, "RUM Migrations detected"}

      _ ->
        :ok
    end
  end

  defp check_system_commands!(:ok) do
    filter_commands_statuses = [
      check_filter(Pleroma.Upload.Filter.Exiftool, "exiftool"),
      check_filter(Pleroma.Upload.Filter.Mogrify, "mogrify"),
      check_filter(Pleroma.Upload.Filter.Mogrifun, "mogrify"),
      check_filter(Pleroma.Upload.Filter.AnalyzeMetadata, "mogrify"),
      check_filter(Pleroma.Upload.Filter.AnalyzeMetadata, "convert"),
      check_filter(Pleroma.Upload.Filter.AnalyzeMetadata, "ffprobe")
    ]

    preview_proxy_commands_status =
      if !Config.get([:media_preview_proxy, :enabled]) or
           MediaHelper.missing_dependencies() == [] do
        true
      else
        Logger.error(
          "The following dependencies required by Media preview proxy " <>
            "(which is currently enabled) are not installed: " <>
            inspect(MediaHelper.missing_dependencies())
        )

        false
      end

    if Enum.all?([preview_proxy_commands_status | filter_commands_statuses], & &1) do
      :ok
    else
      {:error,
       "System commands missing. Check logs and see `docs/installation` for more details."}
    end
  end

  defp check_system_commands!(result), do: result

  defp check_repo_pool_size!(:ok) do
    if Pleroma.Config.get([Pleroma.Repo, :pool_size], 10) != 10 and
         not Pleroma.Config.get([:dangerzone, :override_repo_pool_size], false) do
      Logger.error("""
      !!!CONFIG WARNING!!!

      The database pool size has been altered from the recommended value of 10.

      Please revert or ensure your database is tuned appropriately and then set
      `config :pleroma, :dangerzone, override_repo_pool_size: true`.

      If you are experiencing database timeouts, please check the "Optimizing
      your PostgreSQL performance" section in the documentation. If you still
      encounter issues after that, please open an issue on the tracker.
      """)

      {:error, "Repo.pool_size different than recommended value."}
    else
      :ok
    end
  end

  defp check_repo_pool_size!(result), do: result

  defp check_filter(filter, command_required) do
    filters = Config.get([Pleroma.Upload, :filters])

    if filter in filters and not Pleroma.Utils.command_available?(command_required) do
      Logger.error(
        "#{filter} is specified in list of Pleroma.Upload filters, but the " <>
          "#{command_required} command is not found"
      )

      false
    else
      true
    end
  end
end