Merge branch 'develop' into frontend-switcher-9000
[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
27 proxy_url = Pleroma.Config.get([:http, :proxy_url])
28 proxy = Pleroma.HTTP.AdapterHelper.format_proxy(proxy_url)
29
30 finch_config =
31 [:http, :adapter]
32 |> Pleroma.Config.get([])
33 |> Pleroma.HTTP.AdapterHelper.maybe_add_proxy_pool(proxy)
34 |> Keyword.put(:name, MyFinch)
35
36 unless System.get_env("DEBUG") do
37 Logger.remove_backend(:console)
38 end
39
40 Enum.each(@apps, &Application.ensure_all_started/1)
41
42 oban_config = [
43 crontab: [],
44 repo: Pleroma.Repo,
45 log: false,
46 queues: [],
47 plugins: []
48 ]
49
50 children =
51 [
52 Pleroma.Repo,
53 Pleroma.Emoji,
54 {Pleroma.Config.TransferTask, false},
55 Pleroma.Web.Endpoint,
56 {Finch, finch_config},
57 {Oban, oban_config},
58 {Majic.Pool,
59 [name: Pleroma.MajicPool, pool_size: Pleroma.Config.get([:majic_pool, :size], 2)]}
60 ] ++
61 elasticsearch_children()
62
63 cachex_children = Enum.map(@cachex_children, &Pleroma.Application.build_cachex(&1, []))
64
65 Supervisor.start_link(children ++ cachex_children,
66 strategy: :one_for_one,
67 name: Pleroma.Supervisor
68 )
69
70 if Pleroma.Config.get(:env) not in [:test, :benchmark] do
71 pleroma_rebooted?()
72 end
73 end
74
75 defp pleroma_rebooted? do
76 if Restarter.Pleroma.rebooted?() do
77 :ok
78 else
79 Process.sleep(10)
80 pleroma_rebooted?()
81 end
82 end
83
84 def load_pleroma do
85 Application.load(:pleroma)
86 end
87
88 def get_option(options, opt, prompt, defval \\ nil, defname \\ nil) do
89 Keyword.get(options, opt) || shell_prompt(prompt, defval, defname)
90 end
91
92 def shell_prompt(prompt, defval \\ nil, defname \\ nil) do
93 prompt_message = "#{prompt} [#{defname || defval}] "
94
95 input =
96 if mix_shell?(),
97 do: Mix.shell().prompt(prompt_message),
98 else: :io.get_line(prompt_message)
99
100 case input do
101 "\n" ->
102 case defval do
103 nil ->
104 shell_prompt(prompt, defval, defname)
105
106 defval ->
107 defval
108 end
109
110 input ->
111 String.trim(input)
112 end
113 end
114
115 def shell_info(message) do
116 if mix_shell?(),
117 do: Mix.shell().info(message),
118 else: IO.puts(message)
119 end
120
121 def shell_error(message) do
122 if mix_shell?(),
123 do: Mix.shell().error(message),
124 else: IO.puts(:stderr, message)
125 end
126
127 @doc "Performs a safe check whether `Mix.shell/0` is available (does not raise if Mix is not loaded)"
128 def mix_shell?, do: :erlang.function_exported(Mix, :shell, 0)
129
130 def escape_sh_path(path) do
131 ~S(') <> String.replace(path, ~S('), ~S(\')) <> ~S(')
132 end
133
134 def elasticsearch_children do
135 config = Pleroma.Config.get([Pleroma.Search, :module])
136
137 if config == Pleroma.Search.Elasticsearch do
138 [Pleroma.Search.Elasticsearch.Cluster]
139 else
140 []
141 end
142 end
143 end