Remove a useless binding from config template call
[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 secret: secret,
152 signing_salt: signing_salt,
153 web_push_public_key: Base.url_encode64(web_push_public_key, padding: false),
154 web_push_private_key: Base.url_encode64(web_push_private_key, padding: false),
155 db_configurable?: db_configurable?
156 )
157
158 result_psql =
159 EEx.eval_file(
160 "sample_psql.eex" |> Path.expand(__DIR__),
161 dbname: dbname,
162 dbuser: dbuser,
163 dbpass: dbpass
164 )
165
166 shell_info(
167 "Writing config to #{config_path}. You should rename it to config/prod.secret.exs or config/dev.secret.exs."
168 )
169
170 File.write(config_path, result_config)
171 shell_info("Writing #{psql_path}.")
172 File.write(psql_path, result_psql)
173
174 write_robots_txt(indexable)
175
176 shell_info(
177 "\n" <>
178 """
179 To get started:
180 1. Verify the contents of the generated files.
181 2. Run `sudo -u postgres psql -f #{escape_sh_path(psql_path)}`.
182 """ <>
183 if config_path in ["config/dev.secret.exs", "config/prod.secret.exs"] do
184 ""
185 else
186 "3. Run `mv #{escape_sh_path(config_path)} 'config/prod.secret.exs'`."
187 end
188 )
189 else
190 shell_error(
191 "The task would have overwritten the following files:\n" <>
192 (Enum.map(paths, &"- #{&1}\n") |> Enum.join("")) <>
193 "Rerun with `--force` to overwrite them."
194 )
195 end
196 end
197
198 defp write_robots_txt(indexable) do
199 robots_txt =
200 EEx.eval_file(
201 Path.expand("robots_txt.eex", __DIR__),
202 indexable: indexable
203 )
204
205 static_dir = Pleroma.Config.get([:instance, :static_dir], "instance/static/")
206
207 unless File.exists?(static_dir) do
208 File.mkdir_p!(static_dir)
209 end
210
211 robots_txt_path = Path.join(static_dir, "robots.txt")
212
213 if File.exists?(robots_txt_path) do
214 File.cp!(robots_txt_path, "#{robots_txt_path}.bak")
215 shell_info("Backing up existing robots.txt to #{robots_txt_path}.bak")
216 end
217
218 File.write(robots_txt_path, robots_txt)
219 shell_info("Writing #{robots_txt_path}.")
220 end
221 end