Merge branch 'develop' into release-docs
[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 - `--indexable Y/N` - Allow/disallow indexing site by search engines
33 - `--db-configurable Y/N` - Allow/disallow configuring instance from admin part
34 - `--uploads-dir` - the directory uploads go in when using a local uploader
35 - `--static-dir` - the directory custom public files should be read from (custom emojis, frontend bundle overrides, robots.txt, etc.)
36 """
37
38 def run(["gen" | rest]) do
39 {options, [], []} =
40 OptionParser.parse(
41 rest,
42 strict: [
43 force: :boolean,
44 output: :string,
45 output_psql: :string,
46 domain: :string,
47 instance_name: :string,
48 admin_email: :string,
49 notify_email: :string,
50 dbhost: :string,
51 dbname: :string,
52 dbuser: :string,
53 dbpass: :string,
54 indexable: :string,
55 db_configurable: :string,
56 uploads_dir: :string,
57 static_dir: :string
58 ],
59 aliases: [
60 o: :output,
61 f: :force
62 ]
63 )
64
65 paths =
66 [config_path, psql_path] = [
67 Keyword.get(options, :output, "config/generated_config.exs"),
68 Keyword.get(options, :output_psql, "config/setup_db.psql")
69 ]
70
71 will_overwrite = Enum.filter(paths, &File.exists?/1)
72 proceed? = Enum.empty?(will_overwrite) or Keyword.get(options, :force, false)
73
74 if proceed? do
75 [domain, port | _] =
76 String.split(
77 get_option(
78 options,
79 :domain,
80 "What domain will your instance use? (e.g pleroma.soykaf.com)"
81 ),
82 ":"
83 ) ++ [443]
84
85 name =
86 get_option(
87 options,
88 :instance_name,
89 "What is the name of your instance? (e.g. Pleroma/Soykaf)"
90 )
91
92 email = get_option(options, :admin_email, "What is your admin email address?")
93
94 notify_email =
95 get_option(
96 options,
97 :notify_email,
98 "What email address do you want to use for sending email notifications?",
99 email
100 )
101
102 indexable =
103 get_option(
104 options,
105 :indexable,
106 "Do you want search engines to index your site? (y/n)",
107 "y"
108 ) === "y"
109
110 db_configurable? =
111 get_option(
112 options,
113 :db_configurable,
114 "Do you want to store the configuration in the database (allows controlling it from admin-fe)? (y/n)",
115 "y"
116 ) === "y"
117
118 dbhost = get_option(options, :dbhost, "What is the hostname of your database?", "localhost")
119
120 dbname = get_option(options, :dbname, "What is the name of your database?", "pleroma_dev")
121
122 dbuser =
123 get_option(
124 options,
125 :dbuser,
126 "What is the user used to connect to your database?",
127 "pleroma"
128 )
129
130 dbpass =
131 get_option(
132 options,
133 :dbpass,
134 "What is the password used to connect to your database?",
135 :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64),
136 "autogenerated"
137 )
138
139 uploads_dir =
140 get_option(
141 options,
142 :upload_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 signing_salt = :crypto.strong_rand_bytes(8) |> Base.encode64() |> binary_part(0, 8)
157 {web_push_public_key, web_push_private_key} = :crypto.generate_key(:ecdh, :prime256v1)
158
159 result_config =
160 EEx.eval_file(
161 "sample_config.eex" |> Path.expand(__DIR__),
162 domain: domain,
163 port: port,
164 email: email,
165 notify_email: notify_email,
166 name: name,
167 dbhost: dbhost,
168 dbname: dbname,
169 dbuser: dbuser,
170 dbpass: dbpass,
171 secret: secret,
172 signing_salt: signing_salt,
173 web_push_public_key: Base.url_encode64(web_push_public_key, padding: false),
174 web_push_private_key: Base.url_encode64(web_push_private_key, padding: false),
175 db_configurable?: db_configurable?,
176 static_dir: static_dir,
177 uploads_dir: uploads_dir
178 )
179
180 result_psql =
181 EEx.eval_file(
182 "sample_psql.eex" |> Path.expand(__DIR__),
183 dbname: dbname,
184 dbuser: dbuser,
185 dbpass: dbpass
186 )
187
188 shell_info(
189 "Writing config to #{config_path}. You should rename it to config/prod.secret.exs or config/dev.secret.exs."
190 )
191
192 File.write(config_path, result_config)
193 shell_info("Writing #{psql_path}.")
194 File.write(psql_path, result_psql)
195
196 write_robots_txt(indexable)
197
198 shell_info(
199 "\n" <>
200 """
201 To get started:
202 1. Verify the contents of the generated files.
203 2. Run `sudo -u postgres psql -f #{escape_sh_path(psql_path)}`.
204 """ <>
205 if config_path in ["config/dev.secret.exs", "config/prod.secret.exs"] do
206 ""
207 else
208 "3. Run `mv #{escape_sh_path(config_path)} 'config/prod.secret.exs'`."
209 end
210 )
211 else
212 shell_error(
213 "The task would have overwritten the following files:\n" <>
214 (Enum.map(paths, &"- #{&1}\n") |> Enum.join("")) <>
215 "Rerun with `--force` to overwrite them."
216 )
217 end
218 end
219
220 defp write_robots_txt(indexable) do
221 robots_txt =
222 EEx.eval_file(
223 Path.expand("robots_txt.eex", __DIR__),
224 indexable: indexable
225 )
226
227 static_dir = Pleroma.Config.get([:instance, :static_dir], "instance/static/")
228
229 unless File.exists?(static_dir) do
230 File.mkdir_p!(static_dir)
231 end
232
233 robots_txt_path = Path.join(static_dir, "robots.txt")
234
235 if File.exists?(robots_txt_path) do
236 File.cp!(robots_txt_path, "#{robots_txt_path}.bak")
237 shell_info("Backing up existing robots.txt to #{robots_txt_path}.bak")
238 end
239
240 File.write(robots_txt_path, robots_txt)
241 shell_info("Writing #{robots_txt_path}.")
242 end
243 end