Purge Rejected Follow requests in daily task (#334)
[akkoma] / lib / mix / tasks / pleroma / instance.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.Tasks.Pleroma.Instance do
6 use Mix.Task
7 import Mix.Pleroma
8
9 alias Pleroma.Config
10
11 @shortdoc "Manages Pleroma instance"
12 @moduledoc File.read!("docs/docs/administration/CLI_tasks/instance.md")
13
14 def run(["gen" | rest]) do
15 {options, [], []} =
16 OptionParser.parse(
17 rest,
18 strict: [
19 force: :boolean,
20 output: :string,
21 output_psql: :string,
22 domain: :string,
23 instance_name: :string,
24 admin_email: :string,
25 notify_email: :string,
26 dbhost: :string,
27 dbname: :string,
28 dbuser: :string,
29 dbpass: :string,
30 rum: :string,
31 indexable: :string,
32 db_configurable: :string,
33 uploads_dir: :string,
34 static_dir: :string,
35 listen_ip: :string,
36 listen_port: :string,
37 strip_uploads: :string,
38 anonymize_uploads: :string,
39 dedupe_uploads: :string
40 ],
41 aliases: [
42 o: :output,
43 f: :force
44 ]
45 )
46
47 paths =
48 [config_path, psql_path] = [
49 Keyword.get(options, :output, "config/generated_config.exs"),
50 Keyword.get(options, :output_psql, "config/setup_db.psql")
51 ]
52
53 will_overwrite = Enum.filter(paths, &File.exists?/1)
54 proceed? = Enum.empty?(will_overwrite) or Keyword.get(options, :force, false)
55
56 if proceed? do
57 [domain, port | _] =
58 String.split(
59 get_option(
60 options,
61 :domain,
62 "What domain will your instance use? (e.g akkoma.example.com)"
63 ),
64 ":"
65 ) ++ [443]
66
67 name =
68 get_option(
69 options,
70 :instance_name,
71 "What is the name of your instance? (e.g. The Corndog Emporium)",
72 domain
73 )
74
75 email = get_option(options, :admin_email, "What is your admin email address?")
76
77 notify_email =
78 get_option(
79 options,
80 :notify_email,
81 "What email address do you want to use for sending email notifications?",
82 email
83 )
84
85 indexable =
86 get_option(
87 options,
88 :indexable,
89 "Do you want search engines to index your site? (y/n)",
90 "y"
91 ) === "y"
92
93 db_configurable? =
94 get_option(
95 options,
96 :db_configurable,
97 "Do you want to store the configuration in the database (allows controlling it from admin-fe)? (y/n)",
98 "n"
99 ) === "y"
100
101 dbhost = get_option(options, :dbhost, "What is the hostname of your database?", "localhost")
102
103 dbname = get_option(options, :dbname, "What is the name of your database?", "akkoma")
104
105 dbuser =
106 get_option(
107 options,
108 :dbuser,
109 "What is the user used to connect to your database?",
110 "akkoma"
111 )
112
113 dbpass =
114 get_option(
115 options,
116 :dbpass,
117 "What is the password used to connect to your database?",
118 :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64),
119 "autogenerated"
120 )
121
122 rum_enabled =
123 get_option(
124 options,
125 :rum,
126 "Would you like to use RUM indices?",
127 "n"
128 ) === "y"
129
130 listen_port =
131 get_option(
132 options,
133 :listen_port,
134 "What port will the app listen to (leave it if you are using the default setup with nginx)?",
135 4000
136 )
137
138 listen_ip =
139 get_option(
140 options,
141 :listen_ip,
142 "What ip will the app listen to (leave it if you are using the default setup with nginx)?",
143 "127.0.0.1"
144 )
145
146 uploads_dir =
147 get_option(
148 options,
149 :uploads_dir,
150 "What directory should media uploads go in (when using the local uploader)?",
151 Config.get([Pleroma.Uploaders.Local, :uploads])
152 )
153 |> Path.expand()
154
155 static_dir =
156 get_option(
157 options,
158 :static_dir,
159 "What directory should custom public files be read from (custom emojis, frontend bundle overrides, robots.txt, etc.)?",
160 Config.get([:instance, :static_dir])
161 )
162 |> Path.expand()
163
164 {strip_uploads_message, strip_uploads_default} =
165 if Pleroma.Utils.command_available?("exiftool") do
166 {"Do you want to strip location (GPS) data from uploaded images? This requires exiftool, it was detected as installed. (y/n)",
167 "y"}
168 else
169 {"Do you want to strip location (GPS) data from uploaded images? This requires exiftool, it was detected as not installed, please install it if you answer yes. (y/n)",
170 "n"}
171 end
172
173 strip_uploads =
174 get_option(
175 options,
176 :strip_uploads,
177 strip_uploads_message,
178 strip_uploads_default
179 ) === "y"
180
181 anonymize_uploads =
182 get_option(
183 options,
184 :anonymize_uploads,
185 "Do you want to anonymize the filenames of uploads? (y/n)",
186 "n"
187 ) === "y"
188
189 dedupe_uploads =
190 get_option(
191 options,
192 :dedupe_uploads,
193 "Do you want to deduplicate uploaded files? (y/n)",
194 "n"
195 ) === "y"
196
197 Config.put([:instance, :static_dir], static_dir)
198
199 secret = :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64)
200 jwt_secret = :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64)
201 signing_salt = :crypto.strong_rand_bytes(8) |> Base.encode64() |> binary_part(0, 8)
202 lv_signing_salt = :crypto.strong_rand_bytes(8) |> Base.encode64() |> binary_part(0, 8)
203 {web_push_public_key, web_push_private_key} = :crypto.generate_key(:ecdh, :prime256v1)
204 template_dir = Application.app_dir(:pleroma, "priv") <> "/templates"
205
206 result_config =
207 EEx.eval_file(
208 template_dir <> "/sample_config.eex",
209 domain: domain,
210 port: port,
211 email: email,
212 notify_email: notify_email,
213 name: name,
214 dbhost: dbhost,
215 dbname: dbname,
216 dbuser: dbuser,
217 dbpass: dbpass,
218 secret: secret,
219 jwt_secret: jwt_secret,
220 signing_salt: signing_salt,
221 lv_signing_salt: lv_signing_salt,
222 web_push_public_key: Base.url_encode64(web_push_public_key, padding: false),
223 web_push_private_key: Base.url_encode64(web_push_private_key, padding: false),
224 db_configurable?: db_configurable?,
225 static_dir: static_dir,
226 uploads_dir: uploads_dir,
227 rum_enabled: rum_enabled,
228 listen_ip: listen_ip,
229 listen_port: listen_port,
230 upload_filters:
231 upload_filters(%{
232 strip: strip_uploads,
233 anonymize: anonymize_uploads,
234 dedupe: dedupe_uploads
235 })
236 )
237
238 result_psql =
239 EEx.eval_file(
240 template_dir <> "/sample_psql.eex",
241 dbname: dbname,
242 dbuser: dbuser,
243 dbpass: dbpass,
244 rum_enabled: rum_enabled
245 )
246
247 config_dir = Path.dirname(config_path)
248 psql_dir = Path.dirname(psql_path)
249
250 [config_dir, psql_dir, static_dir, uploads_dir]
251 |> Enum.reject(&File.exists?/1)
252 |> Enum.map(&File.mkdir_p!/1)
253
254 shell_info("Writing config to #{config_path}.")
255
256 File.write(config_path, result_config)
257 shell_info("Writing the postgres script to #{psql_path}.")
258 File.write(psql_path, result_psql)
259
260 write_robots_txt(static_dir, indexable, template_dir)
261
262 shell_info(
263 "\n All files successfully written! Refer to the installation instructions for your platform for next steps."
264 )
265
266 if db_configurable? do
267 shell_info(
268 " Please transfer your config to the database after running database migrations. Refer to \"Transfering the config to/from the database\" section of the docs for more information."
269 )
270 end
271 else
272 shell_error(
273 "The task would have overwritten the following files:\n" <>
274 (Enum.map(will_overwrite, &"- #{&1}\n") |> Enum.join("")) <>
275 "Rerun with `--force` to overwrite them."
276 )
277 end
278 end
279
280 defp write_robots_txt(static_dir, indexable, template_dir) do
281 robots_txt =
282 EEx.eval_file(
283 template_dir <> "/robots_txt.eex",
284 indexable: indexable
285 )
286
287 robots_txt_path = Path.join(static_dir, "robots.txt")
288
289 if File.exists?(robots_txt_path) do
290 File.cp!(robots_txt_path, "#{robots_txt_path}.bak")
291 shell_info("Backing up existing robots.txt to #{robots_txt_path}.bak")
292 end
293
294 File.write(robots_txt_path, robots_txt)
295 shell_info("Writing #{robots_txt_path}.")
296 end
297
298 defp upload_filters(filters) when is_map(filters) do
299 enabled_filters =
300 if filters.strip do
301 [Pleroma.Upload.Filter.Exiftool]
302 else
303 []
304 end
305
306 enabled_filters =
307 if filters.anonymize do
308 enabled_filters ++ [Pleroma.Upload.Filter.AnonymizeFilename]
309 else
310 enabled_filters
311 end
312
313 enabled_filters =
314 if filters.dedupe do
315 enabled_filters ++ [Pleroma.Upload.Filter.Dedupe]
316 else
317 enabled_filters
318 end
319
320 enabled_filters
321 end
322
323 defp upload_filters(_), do: []
324 end