25f94ecebd433a7d625f41a53380add0463821fc
[akkoma] / lib / mix / tasks / pleroma / instance.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Mix.Tasks.Pleroma.Instance do
6 use Mix.Task
7 import Mix.Pleroma
8
9 @shortdoc "Manages Pleroma instance"
10
11 def run(["gen" | rest]) do
12 {options, [], []} =
13 OptionParser.parse(
14 rest,
15 strict: [
16 force: :boolean,
17 output: :string,
18 output_psql: :string,
19 domain: :string,
20 instance_name: :string,
21 admin_email: :string,
22 notify_email: :string,
23 dbhost: :string,
24 dbname: :string,
25 dbuser: :string,
26 dbpass: :string,
27 rum: :string,
28 indexable: :string,
29 db_configurable: :string,
30 uploads_dir: :string,
31 static_dir: :string,
32 listen_ip: :string,
33 listen_port: :string
34 ],
35 aliases: [
36 o: :output,
37 f: :force
38 ]
39 )
40
41 paths =
42 [config_path, psql_path] = [
43 Keyword.get(options, :output, "config/generated_config.exs"),
44 Keyword.get(options, :output_psql, "config/setup_db.psql")
45 ]
46
47 will_overwrite = Enum.filter(paths, &File.exists?/1)
48 proceed? = Enum.empty?(will_overwrite) or Keyword.get(options, :force, false)
49
50 if proceed? do
51 [domain, port | _] =
52 String.split(
53 get_option(
54 options,
55 :domain,
56 "What domain will your instance use? (e.g pleroma.soykaf.com)"
57 ),
58 ":"
59 ) ++ [443]
60
61 name =
62 get_option(
63 options,
64 :instance_name,
65 "What is the name of your instance? (e.g. Pleroma/Soykaf)"
66 )
67
68 email = get_option(options, :admin_email, "What is your admin email address?")
69
70 notify_email =
71 get_option(
72 options,
73 :notify_email,
74 "What email address do you want to use for sending email notifications?",
75 email
76 )
77
78 indexable =
79 get_option(
80 options,
81 :indexable,
82 "Do you want search engines to index your site? (y/n)",
83 "y"
84 ) === "y"
85
86 db_configurable? =
87 get_option(
88 options,
89 :db_configurable,
90 "Do you want to store the configuration in the database (allows controlling it from admin-fe)? (y/n)",
91 "n"
92 ) === "y"
93
94 dbhost = get_option(options, :dbhost, "What is the hostname of your database?", "localhost")
95
96 dbname = get_option(options, :dbname, "What is the name of your database?", "pleroma")
97
98 dbuser =
99 get_option(
100 options,
101 :dbuser,
102 "What is the user used to connect to your database?",
103 "pleroma"
104 )
105
106 dbpass =
107 get_option(
108 options,
109 :dbpass,
110 "What is the password used to connect to your database?",
111 :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64),
112 "autogenerated"
113 )
114
115 rum_enabled =
116 get_option(
117 options,
118 :rum,
119 "Would you like to use RUM indices?",
120 "n"
121 ) === "y"
122
123 listen_port =
124 get_option(
125 options,
126 :listen_port,
127 "What port will the app listen to (leave it if you are using the default setup with nginx)?",
128 4000
129 )
130
131 listen_ip =
132 get_option(
133 options,
134 :listen_ip,
135 "What ip will the app listen to (leave it if you are using the default setup with nginx)?",
136 "127.0.0.1"
137 )
138
139 uploads_dir =
140 get_option(
141 options,
142 :uploads_dir,
143 "What directory should media uploads go in (when using the local uploader)?",
144 Pleroma.Config.get([Pleroma.Uploaders.Local, :uploads])
145 )
146
147 static_dir =
148 get_option(
149 options,
150 :static_dir,
151 "What directory should custom public files be read from (custom emojis, frontend bundle overrides, robots.txt, etc.)?",
152 Pleroma.Config.get([:instance, :static_dir])
153 )
154
155 secret = :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64)
156 jwt_secret = :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64)
157 signing_salt = :crypto.strong_rand_bytes(8) |> Base.encode64() |> binary_part(0, 8)
158 {web_push_public_key, web_push_private_key} = :crypto.generate_key(:ecdh, :prime256v1)
159 template_dir = Application.app_dir(:pleroma, "priv") <> "/templates"
160
161 result_config =
162 EEx.eval_file(
163 template_dir <> "/sample_config.eex",
164 domain: domain,
165 port: port,
166 email: email,
167 notify_email: notify_email,
168 name: name,
169 dbhost: dbhost,
170 dbname: dbname,
171 dbuser: dbuser,
172 dbpass: dbpass,
173 secret: secret,
174 jwt_secret: jwt_secret,
175 signing_salt: signing_salt,
176 web_push_public_key: Base.url_encode64(web_push_public_key, padding: false),
177 web_push_private_key: Base.url_encode64(web_push_private_key, padding: false),
178 db_configurable?: db_configurable?,
179 static_dir: static_dir,
180 uploads_dir: uploads_dir,
181 rum_enabled: rum_enabled,
182 listen_ip: listen_ip,
183 listen_port: listen_port
184 )
185
186 result_psql =
187 EEx.eval_file(
188 template_dir <> "/sample_psql.eex",
189 dbname: dbname,
190 dbuser: dbuser,
191 dbpass: dbpass,
192 rum_enabled: rum_enabled
193 )
194
195 shell_info("Writing config to #{config_path}.")
196
197 File.write(config_path, result_config)
198 shell_info("Writing the postgres script to #{psql_path}.")
199 File.write(psql_path, result_psql)
200
201 write_robots_txt(indexable, template_dir)
202
203 shell_info(
204 "\n All files successfully written! Refer to the installation instructions for your platform for next steps"
205 )
206 else
207 shell_error(
208 "The task would have overwritten the following files:\n" <>
209 (Enum.map(paths, &"- #{&1}\n") |> Enum.join("")) <>
210 "Rerun with `--force` to overwrite them."
211 )
212 end
213 end
214
215 defp write_robots_txt(indexable, template_dir) do
216 robots_txt =
217 EEx.eval_file(
218 template_dir <> "/robots_txt.eex",
219 indexable: indexable
220 )
221
222 static_dir = Pleroma.Config.get([:instance, :static_dir], "instance/static/")
223
224 unless File.exists?(static_dir) do
225 File.mkdir_p!(static_dir)
226 end
227
228 robots_txt_path = Path.join(static_dir, "robots.txt")
229
230 if File.exists?(robots_txt_path) do
231 File.cp!(robots_txt_path, "#{robots_txt_path}.bak")
232 shell_info("Backing up existing robots.txt to #{robots_txt_path}.bak")
233 end
234
235 File.write(robots_txt_path, robots_txt)
236 shell_info("Writing #{robots_txt_path}.")
237 end
238 end