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