Merge branch 'fix/instance-gen-custom-static' into 'develop'
[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 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. Pleroma/Soykaf)"
69 )
70
71 email = get_option(options, :admin_email, "What is your admin email address?")
72
73 notify_email =
74 get_option(
75 options,
76 :notify_email,
77 "What email address do you want to use for sending email notifications?",
78 email
79 )
80
81 indexable =
82 get_option(
83 options,
84 :indexable,
85 "Do you want search engines to index your site? (y/n)",
86 "y"
87 ) === "y"
88
89 db_configurable? =
90 get_option(
91 options,
92 :db_configurable,
93 "Do you want to store the configuration in the database (allows controlling it from admin-fe)? (y/n)",
94 "n"
95 ) === "y"
96
97 dbhost = get_option(options, :dbhost, "What is the hostname of your database?", "localhost")
98
99 dbname = get_option(options, :dbname, "What is the name of your database?", "pleroma")
100
101 dbuser =
102 get_option(
103 options,
104 :dbuser,
105 "What is the user used to connect to your database?",
106 "pleroma"
107 )
108
109 dbpass =
110 get_option(
111 options,
112 :dbpass,
113 "What is the password used to connect to your database?",
114 :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64),
115 "autogenerated"
116 )
117
118 rum_enabled =
119 get_option(
120 options,
121 :rum,
122 "Would you like to use RUM indices?",
123 "n"
124 ) === "y"
125
126 listen_port =
127 get_option(
128 options,
129 :listen_port,
130 "What port will the app listen to (leave it if you are using the default setup with nginx)?",
131 4000
132 )
133
134 listen_ip =
135 get_option(
136 options,
137 :listen_ip,
138 "What ip will the app listen to (leave it if you are using the default setup with nginx)?",
139 "127.0.0.1"
140 )
141
142 uploads_dir =
143 get_option(
144 options,
145 :uploads_dir,
146 "What directory should media uploads go in (when using the local uploader)?",
147 Pleroma.Config.get([Pleroma.Uploaders.Local, :uploads])
148 )
149
150 static_dir =
151 get_option(
152 options,
153 :static_dir,
154 "What directory should custom public files be read from (custom emojis, frontend bundle overrides, robots.txt, etc.)?",
155 Pleroma.Config.get([:instance, :static_dir])
156 )
157
158 Config.put([:instance, :static_dir], static_dir)
159
160 secret = :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64)
161 jwt_secret = :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64)
162 signing_salt = :crypto.strong_rand_bytes(8) |> Base.encode64() |> binary_part(0, 8)
163 {web_push_public_key, web_push_private_key} = :crypto.generate_key(:ecdh, :prime256v1)
164 template_dir = Application.app_dir(:pleroma, "priv") <> "/templates"
165
166 result_config =
167 EEx.eval_file(
168 template_dir <> "/sample_config.eex",
169 domain: domain,
170 port: port,
171 email: email,
172 notify_email: notify_email,
173 name: name,
174 dbhost: dbhost,
175 dbname: dbname,
176 dbuser: dbuser,
177 dbpass: dbpass,
178 secret: secret,
179 jwt_secret: jwt_secret,
180 signing_salt: signing_salt,
181 web_push_public_key: Base.url_encode64(web_push_public_key, padding: false),
182 web_push_private_key: Base.url_encode64(web_push_private_key, padding: false),
183 db_configurable?: db_configurable?,
184 static_dir: static_dir,
185 uploads_dir: uploads_dir,
186 rum_enabled: rum_enabled,
187 listen_ip: listen_ip,
188 listen_port: listen_port
189 )
190
191 result_psql =
192 EEx.eval_file(
193 template_dir <> "/sample_psql.eex",
194 dbname: dbname,
195 dbuser: dbuser,
196 dbpass: dbpass,
197 rum_enabled: rum_enabled
198 )
199
200 shell_info("Writing config to #{config_path}.")
201
202 File.write(config_path, result_config)
203 shell_info("Writing the postgres script to #{psql_path}.")
204 File.write(psql_path, result_psql)
205
206 write_robots_txt(indexable, template_dir)
207
208 shell_info(
209 "\n All files successfully written! Refer to the installation instructions for your platform for next steps"
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, template_dir) do
221 robots_txt =
222 EEx.eval_file(
223 template_dir <> "/robots_txt.eex",
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