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