Merge develop
[akkoma] / lib / mix / tasks / pleroma / instance.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 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 @moduledoc """
11 Manages Pleroma instance.
12
13 ## Generate a new instance config.
14
15 mix pleroma.instance gen [OPTION...]
16
17 If any options are left unspecified, you will be prompted interactively
18
19 ## Options
20
21 - `-f`, `--force` - overwrite any output files
22 - `-o PATH`, `--output PATH` - the output file for the generated configuration
23 - `--output-psql PATH` - the output file for the generated PostgreSQL setup
24 - `--domain DOMAIN` - the domain of your instance
25 - `--instance-name INSTANCE_NAME` - the name of your instance
26 - `--admin-email ADMIN_EMAIL` - the email address of the instance admin
27 - `--notify-email NOTIFY_EMAIL` - email address for notifications
28 - `--dbhost HOSTNAME` - the hostname of the PostgreSQL database to use
29 - `--dbname DBNAME` - the name of the database to use
30 - `--dbuser DBUSER` - the user (aka role) to use for the database connection
31 - `--dbpass DBPASS` - the password to use for the database connection
32 - `--rum Y/N` - Whether to enable RUM indexes
33 - `--indexable Y/N` - Allow/disallow indexing site by search engines
34 - `--db-configurable Y/N` - Allow/disallow configuring instance from admin part
35 - `--uploads-dir` - the directory uploads go in when using a local uploader
36 - `--static-dir` - the directory custom public files should be read from (custom emojis, frontend bundle overrides, robots.txt, etc.)
37 """
38
39 def run(["gen" | rest]) do
40 {options, [], []} =
41 OptionParser.parse(
42 rest,
43 strict: [
44 force: :boolean,
45 output: :string,
46 output_psql: :string,
47 domain: :string,
48 instance_name: :string,
49 admin_email: :string,
50 notify_email: :string,
51 dbhost: :string,
52 dbname: :string,
53 dbuser: :string,
54 dbpass: :string,
55 rum: :string,
56 indexable: :string,
57 db_configurable: :string,
58 uploads_dir: :string,
59 static_dir: :string
60 ],
61 aliases: [
62 o: :output,
63 f: :force
64 ]
65 )
66
67 paths =
68 [config_path, psql_path] = [
69 Keyword.get(options, :output, "config/generated_config.exs"),
70 Keyword.get(options, :output_psql, "config/setup_db.psql")
71 ]
72
73 will_overwrite = Enum.filter(paths, &File.exists?/1)
74 proceed? = Enum.empty?(will_overwrite) or Keyword.get(options, :force, false)
75
76 if proceed? do
77 [domain, port | _] =
78 String.split(
79 get_option(
80 options,
81 :domain,
82 "What domain will your instance use? (e.g pleroma.soykaf.com)"
83 ),
84 ":"
85 ) ++ [443]
86
87 name =
88 get_option(
89 options,
90 :instance_name,
91 "What is the name of your instance? (e.g. Pleroma/Soykaf)"
92 )
93
94 email = get_option(options, :admin_email, "What is your admin email address?")
95
96 notify_email =
97 get_option(
98 options,
99 :notify_email,
100 "What email address do you want to use for sending email notifications?",
101 email
102 )
103
104 indexable =
105 get_option(
106 options,
107 :indexable,
108 "Do you want search engines to index your site? (y/n)",
109 "y"
110 ) === "y"
111
112 db_configurable? =
113 get_option(
114 options,
115 :db_configurable,
116 "Do you want to store the configuration in the database (allows controlling it from admin-fe)? (y/n)",
117 "n"
118 ) === "y"
119
120 dbhost = get_option(options, :dbhost, "What is the hostname of your database?", "localhost")
121
122 dbname = get_option(options, :dbname, "What is the name of your database?", "pleroma")
123
124 dbuser =
125 get_option(
126 options,
127 :dbuser,
128 "What is the user used to connect to your database?",
129 "pleroma"
130 )
131
132 dbpass =
133 get_option(
134 options,
135 :dbpass,
136 "What is the password used to connect to your database?",
137 :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64),
138 "autogenerated"
139 )
140
141 rum_enabled =
142 get_option(
143 options,
144 :rum,
145 "Would you like to use RUM indices?",
146 "n"
147 ) === "y"
148
149 uploads_dir =
150 get_option(
151 options,
152 :uploads_dir,
153 "What directory should media uploads go in (when using the local uploader)?",
154 Pleroma.Config.get([Pleroma.Uploaders.Local, :uploads])
155 )
156
157 static_dir =
158 get_option(
159 options,
160 :static_dir,
161 "What directory should custom public files be read from (custom emojis, frontend bundle overrides, robots.txt, etc.)?",
162 Pleroma.Config.get([:instance, :static_dir])
163 )
164
165 secret = :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64)
166 jwt_secret = :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64)
167 signing_salt = :crypto.strong_rand_bytes(8) |> Base.encode64() |> binary_part(0, 8)
168 {web_push_public_key, web_push_private_key} = :crypto.generate_key(:ecdh, :prime256v1)
169 template_dir = Application.app_dir(:pleroma, "priv") <> "/templates"
170
171 result_config =
172 EEx.eval_file(
173 template_dir <> "/sample_config.eex",
174 domain: domain,
175 port: port,
176 email: email,
177 notify_email: notify_email,
178 name: name,
179 dbhost: dbhost,
180 dbname: dbname,
181 dbuser: dbuser,
182 dbpass: dbpass,
183 secret: secret,
184 jwt_secret: jwt_secret,
185 signing_salt: signing_salt,
186 web_push_public_key: Base.url_encode64(web_push_public_key, padding: false),
187 web_push_private_key: Base.url_encode64(web_push_private_key, padding: false),
188 db_configurable?: db_configurable?,
189 static_dir: static_dir,
190 uploads_dir: uploads_dir,
191 rum_enabled: rum_enabled
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(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 else
215 shell_error(
216 "The task would have overwritten the following files:\n" <>
217 (Enum.map(paths, &"- #{&1}\n") |> Enum.join("")) <>
218 "Rerun with `--force` to overwrite them."
219 )
220 end
221 end
222
223 defp write_robots_txt(indexable, template_dir) do
224 robots_txt =
225 EEx.eval_file(
226 template_dir <> "/robots_txt.eex",
227 indexable: indexable
228 )
229
230 static_dir = Pleroma.Config.get([:instance, :static_dir], "instance/static/")
231
232 unless File.exists?(static_dir) do
233 File.mkdir_p!(static_dir)
234 end
235
236 robots_txt_path = Path.join(static_dir, "robots.txt")
237
238 if File.exists?(robots_txt_path) do
239 File.cp!(robots_txt_path, "#{robots_txt_path}.bak")
240 shell_info("Backing up existing robots.txt to #{robots_txt_path}.bak")
241 end
242
243 File.write(robots_txt_path, robots_txt)
244 shell_info("Writing #{robots_txt_path}.")
245 end
246 end