7575f0ef847a7b414d6d9cec97d5c50c029d1033
[akkoma] / lib / mix / pleroma.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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 ]
17 @cachex_children ["object", "user", "scrubber", "web_resp"]
18 @doc "Common functions to be reused in mix tasks"
19 def start_pleroma do
20 Pleroma.Config.Holder.save_default()
21 Pleroma.Config.Oban.warn()
22 Pleroma.Application.limiters_setup()
23 Application.put_env(:phoenix, :serve_endpoints, false, persistent: true)
24
25 unless System.get_env("DEBUG") do
26 Logger.remove_backend(:console)
27 end
28
29 adapter = Application.get_env(:tesla, :adapter)
30
31 apps =
32 if adapter == Tesla.Adapter.Gun do
33 [:gun | @apps]
34 else
35 [:hackney | @apps]
36 end
37
38 Enum.each(apps, &Application.ensure_all_started/1)
39
40 children =
41 [
42 Pleroma.Repo,
43 {Pleroma.Config.TransferTask, false},
44 Pleroma.Web.Endpoint,
45 {Oban, Pleroma.Config.get(Oban)}
46 ] ++
47 http_children(adapter)
48
49 cachex_children = Enum.map(@cachex_children, &Pleroma.Application.build_cachex(&1, []))
50
51 Supervisor.start_link(children ++ cachex_children,
52 strategy: :one_for_one,
53 name: Pleroma.Supervisor
54 )
55
56 if Pleroma.Config.get(:env) not in [:test, :benchmark] do
57 pleroma_rebooted?()
58 end
59 end
60
61 defp pleroma_rebooted? do
62 if Restarter.Pleroma.rebooted?() do
63 :ok
64 else
65 Process.sleep(10)
66 pleroma_rebooted?()
67 end
68 end
69
70 def load_pleroma do
71 Application.load(:pleroma)
72 end
73
74 def get_option(options, opt, prompt, defval \\ nil, defname \\ nil) do
75 Keyword.get(options, opt) || shell_prompt(prompt, defval, defname)
76 end
77
78 def shell_prompt(prompt, defval \\ nil, defname \\ nil) do
79 prompt_message = "#{prompt} [#{defname || defval}] "
80
81 input =
82 if mix_shell?(),
83 do: Mix.shell().prompt(prompt_message),
84 else: :io.get_line(prompt_message)
85
86 case input do
87 "\n" ->
88 case defval do
89 nil ->
90 shell_prompt(prompt, defval, defname)
91
92 defval ->
93 defval
94 end
95
96 input ->
97 String.trim(input)
98 end
99 end
100
101 def shell_info(message) do
102 if mix_shell?(),
103 do: Mix.shell().info(message),
104 else: IO.puts(message)
105 end
106
107 def shell_error(message) do
108 if mix_shell?(),
109 do: Mix.shell().error(message),
110 else: IO.puts(:stderr, message)
111 end
112
113 @doc "Performs a safe check whether `Mix.shell/0` is available (does not raise if Mix is not loaded)"
114 def mix_shell?, do: :erlang.function_exported(Mix, :shell, 0)
115
116 def escape_sh_path(path) do
117 ~S(') <> String.replace(path, ~S('), ~S(\')) <> ~S(')
118 end
119
120 defp http_children(Tesla.Adapter.Gun) do
121 Pleroma.Gun.ConnectionPool.children() ++
122 [{Task, &Pleroma.HTTP.AdapterHelper.Gun.limiter_setup/0}]
123 end
124
125 defp http_children(_), do: []
126 end