1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Mix.Tasks.Pleroma.Instance do
7 alias Mix.Tasks.Pleroma.Common
9 @shortdoc "Manages Pleroma instance"
11 Manages Pleroma instance.
13 ## Generate a new instance config.
15 mix pleroma.instance gen [OPTION...]
17 If any options are left unspecified, you will be prompted interactively
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
35 def run(["gen" | rest]) do
44 instance_name: :string,
46 notify_email: :string,
60 [config_path, psql_path] = [
61 Keyword.get(options, :output, "config/generated_config.exs"),
62 Keyword.get(options, :output_psql, "config/setup_db.psql")
65 will_overwrite = Enum.filter(paths, &File.exists?/1)
66 proceed? = Enum.empty?(will_overwrite) or Keyword.get(options, :force, false)
74 "What domain will your instance use? (e.g pleroma.soykaf.com)"
83 "What is the name of your instance? (e.g. Pleroma/Soykaf)"
86 email = Common.get_option(options, :admin_email, "What is your admin email address?")
92 "What email address do you want to use for sending email notifications?",
100 "Do you want search engines to index your site? (y/n)",
105 Common.get_option(options, :dbhost, "What is the hostname of your database?", "localhost")
108 Common.get_option(options, :dbname, "What is the name of your database?", "pleroma_dev")
114 "What is the user used to connect to your database?",
122 "What is the password used to connect to your database?",
123 :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64),
127 secret = :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64)
128 jwt_secret = :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64)
129 signing_salt = :crypto.strong_rand_bytes(8) |> Base.encode64() |> binary_part(0, 8)
130 {web_push_public_key, web_push_private_key} = :crypto.generate_key(:ecdh, :prime256v1)
134 "sample_config.eex" |> Path.expand(__DIR__),
138 notify_email: notify_email,
144 version: Pleroma.Mixfile.project() |> Keyword.get(:version),
146 jwt_secret: jwt_secret,
147 signing_salt: signing_salt,
148 web_push_public_key: Base.url_encode64(web_push_public_key, padding: false),
149 web_push_private_key: Base.url_encode64(web_push_private_key, padding: false)
154 "sample_psql.eex" |> Path.expand(__DIR__),
161 "Writing config to #{config_path}. You should rename it to config/prod.secret.exs or config/dev.secret.exs."
164 File.write(config_path, result_config)
165 Mix.shell().info("Writing #{psql_path}.")
166 File.write(psql_path, result_psql)
168 write_robots_txt(indexable)
174 1. Verify the contents of the generated files.
175 2. Run `sudo -u postgres psql -f #{Common.escape_sh_path(psql_path)}`.
177 if config_path in ["config/dev.secret.exs", "config/prod.secret.exs"] do
180 "3. Run `mv #{Common.escape_sh_path(config_path)} 'config/prod.secret.exs'`."
185 "The task would have overwritten the following files:\n" <>
186 (Enum.map(paths, &"- #{&1}\n") |> Enum.join("")) <>
187 "Rerun with `--force` to overwrite them."
192 defp write_robots_txt(indexable) do
195 Path.expand("robots_txt.eex", __DIR__),
199 static_dir = Pleroma.Config.get([:instance, :static_dir], "instance/static/")
201 unless File.exists?(static_dir) do
202 File.mkdir_p!(static_dir)
205 robots_txt_path = Path.join(static_dir, "robots.txt")
207 if File.exists?(robots_txt_path) do
208 File.cp!(robots_txt_path, "#{robots_txt_path}.bak")
209 Mix.shell().info("Backing up existing robots.txt to #{robots_txt_path}.bak")
212 File.write(robots_txt_path, robots_txt)
213 Mix.shell().info("Writing #{robots_txt_path}.")