Merge branch 'feature/905-dynamic-configuration-in-sql' into '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 alias Mix.Tasks.Pleroma.Common
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 Common.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 Common.get_option(
83 options,
84 :instance_name,
85 "What is the name of your instance? (e.g. Pleroma/Soykaf)"
86 )
87
88 email = Common.get_option(options, :admin_email, "What is your admin email address?")
89
90 notify_email =
91 Common.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 Common.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 Common.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 =
115 Common.get_option(options, :dbhost, "What is the hostname of your database?", "localhost")
116
117 dbname =
118 Common.get_option(options, :dbname, "What is the name of your database?", "pleroma_dev")
119
120 dbuser =
121 Common.get_option(
122 options,
123 :dbuser,
124 "What is the user used to connect to your database?",
125 "pleroma"
126 )
127
128 dbpass =
129 Common.get_option(
130 options,
131 :dbpass,
132 "What is the password used to connect to your database?",
133 :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64),
134 "autogenerated"
135 )
136
137 secret = :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64)
138 signing_salt = :crypto.strong_rand_bytes(8) |> Base.encode64() |> binary_part(0, 8)
139 {web_push_public_key, web_push_private_key} = :crypto.generate_key(:ecdh, :prime256v1)
140
141 result_config =
142 EEx.eval_file(
143 "sample_config.eex" |> Path.expand(__DIR__),
144 domain: domain,
145 port: port,
146 email: email,
147 notify_email: notify_email,
148 name: name,
149 dbhost: dbhost,
150 dbname: dbname,
151 dbuser: dbuser,
152 dbpass: dbpass,
153 version: Pleroma.Mixfile.project() |> Keyword.get(:version),
154 secret: secret,
155 signing_salt: signing_salt,
156 web_push_public_key: Base.url_encode64(web_push_public_key, padding: false),
157 web_push_private_key: Base.url_encode64(web_push_private_key, padding: false),
158 db_configurable?: db_configurable?
159 )
160
161 result_psql =
162 EEx.eval_file(
163 "sample_psql.eex" |> Path.expand(__DIR__),
164 dbname: dbname,
165 dbuser: dbuser,
166 dbpass: dbpass
167 )
168
169 Common.shell_info(
170 "Writing config to #{config_path}. You should rename it to config/prod.secret.exs or config/dev.secret.exs."
171 )
172
173 File.write(config_path, result_config)
174 Common.shell_info("Writing #{psql_path}.")
175 File.write(psql_path, result_psql)
176
177 write_robots_txt(indexable)
178
179 Common.shell_info(
180 "\n" <>
181 """
182 To get started:
183 1. Verify the contents of the generated files.
184 2. Run `sudo -u postgres psql -f #{Common.escape_sh_path(psql_path)}`.
185 """ <>
186 if config_path in ["config/dev.secret.exs", "config/prod.secret.exs"] do
187 ""
188 else
189 "3. Run `mv #{Common.escape_sh_path(config_path)} 'config/prod.secret.exs'`."
190 end
191 )
192 else
193 Common.shell_error(
194 "The task would have overwritten the following files:\n" <>
195 (Enum.map(paths, &"- #{&1}\n") |> Enum.join("")) <>
196 "Rerun with `--force` to overwrite them."
197 )
198 end
199 end
200
201 defp write_robots_txt(indexable) do
202 robots_txt =
203 EEx.eval_file(
204 Path.expand("robots_txt.eex", __DIR__),
205 indexable: indexable
206 )
207
208 static_dir = Pleroma.Config.get([:instance, :static_dir], "instance/static/")
209
210 unless File.exists?(static_dir) do
211 File.mkdir_p!(static_dir)
212 end
213
214 robots_txt_path = Path.join(static_dir, "robots.txt")
215
216 if File.exists?(robots_txt_path) do
217 File.cp!(robots_txt_path, "#{robots_txt_path}.bak")
218 Common.shell_info("Backing up existing robots.txt to #{robots_txt_path}.bak")
219 end
220
221 File.write(robots_txt_path, robots_txt)
222 Common.shell_info("Writing #{robots_txt_path}.")
223 end
224 end