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