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