f4a6bcf631ac8f05eb33abeecf8e1790279eadc8
[akkoma] / lib / mix / pleroma.ex
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
5 defmodule Mix.Pleroma do
6 @apps [
7 :restarter,
8 :ecto,
9 :ecto_sql,
10 :postgrex,
11 :db_connection,
12 :cachex,
13 :flake_id,
14 :swoosh,
15 :timex,
16 :fast_html,
17 :oban
18 ]
19 @cachex_children ["object", "user", "scrubber", "web_resp"]
20 @doc "Common functions to be reused in mix tasks"
21 def start_pleroma do
22 Pleroma.Config.Holder.save_default()
23 Pleroma.Config.Oban.warn()
24 Pleroma.Application.limiters_setup()
25 Application.put_env(:phoenix, :serve_endpoints, false, persistent: true)
26 Finch.start_link(name: MyFinch)
27
28 unless System.get_env("DEBUG") do
29 Logger.remove_backend(:console)
30 end
31
32 Enum.each(@apps, &Application.ensure_all_started/1)
33
34 oban_config = [
35 crontab: [],
36 repo: Pleroma.Repo,
37 log: false,
38 queues: [],
39 plugins: []
40 ]
41
42 children =
43 [
44 Pleroma.Repo,
45 Pleroma.Emoji,
46 {Pleroma.Config.TransferTask, false},
47 Pleroma.Web.Endpoint,
48 {Oban, oban_config},
49 {Majic.Pool,
50 [name: Pleroma.MajicPool, pool_size: Pleroma.Config.get([:majic_pool, :size], 2)]}
51 ] ++
52 elasticsearch_children()
53
54 cachex_children = Enum.map(@cachex_children, &Pleroma.Application.build_cachex(&1, []))
55
56 Supervisor.start_link(children ++ cachex_children,
57 strategy: :one_for_one,
58 name: Pleroma.Supervisor
59 )
60
61 if Pleroma.Config.get(:env) not in [:test, :benchmark] do
62 pleroma_rebooted?()
63 end
64 end
65
66 defp pleroma_rebooted? do
67 if Restarter.Pleroma.rebooted?() do
68 :ok
69 else
70 Process.sleep(10)
71 pleroma_rebooted?()
72 end
73 end
74
75 def load_pleroma do
76 Application.load(:pleroma)
77 end
78
79 def get_option(options, opt, prompt, defval \\ nil, defname \\ nil) do
80 Keyword.get(options, opt) || shell_prompt(prompt, defval, defname)
81 end
82
83 def shell_prompt(prompt, defval \\ nil, defname \\ nil) do
84 prompt_message = "#{prompt} [#{defname || defval}] "
85
86 input =
87 if mix_shell?(),
88 do: Mix.shell().prompt(prompt_message),
89 else: :io.get_line(prompt_message)
90
91 case input do
92 "\n" ->
93 case defval do
94 nil ->
95 shell_prompt(prompt, defval, defname)
96
97 defval ->
98 defval
99 end
100
101 input ->
102 String.trim(input)
103 end
104 end
105
106 def shell_info(message) do
107 if mix_shell?(),
108 do: Mix.shell().info(message),
109 else: IO.puts(message)
110 end
111
112 def shell_error(message) do
113 if mix_shell?(),
114 do: Mix.shell().error(message),
115 else: IO.puts(:stderr, message)
116 end
117
118 @doc "Performs a safe check whether `Mix.shell/0` is available (does not raise if Mix is not loaded)"
119 def mix_shell?, do: :erlang.function_exported(Mix, :shell, 0)
120
121 def escape_sh_path(path) do
122 ~S(') <> String.replace(path, ~S('), ~S(\')) <> ~S(')
123 end
124
125 def elasticsearch_children do
126 config = Pleroma.Config.get([Pleroma.Search, :module])
127
128 if config == Pleroma.Search.Elasticsearch do
129 [Pleroma.Search.Elasticsearch.Cluster]
130 else
131 []
132 end
133 end
134 end