1 defmodule Mix.Tasks.Pleroma.Instance do
3 alias Pleroma.{Repo, User}
5 @shortdoc "Manages Pleroma instance"
7 Manages Pleroma instance.
9 ## Generate a new instance.
11 mix pleroma.instance new [OPTION...]
13 If any options are left unspecified, you will be prompted interactively
17 - `-f`, `--force` - overwrite any output files
18 - `-o PATH`, `--output PATH` - the output file for the generated configuration
19 - `--output-psql PATH` - the output file for the generated PostgreSQL setup
20 - `--domain DOMAIN` - the domain of your instance
21 - `--instance-name INSTANCE_NAME` - the name of your instance
22 - `--admin-email ADMIN_EMAIL` - the email address of the instance admin
23 - `--dbhost HOSTNAME` - the hostname of the PostgreSQL database to use
24 - `--dbname DBNAME` - the name of the database to use
25 - `--dbuser DBUSER` - the user (aka role) to use for the database connection
26 - `--dbpass DBPASS` - the password to use for the database connection
29 def run(["new" | rest]) do
38 instance_name: :string,
52 [config_path, psql_path] = [
53 Keyword.get(options, :output, "config/generated_config.exs"),
54 Keyword.get(options, :output_psql, "config/setup_db.psql")
57 will_overwrite = Enum.filter(paths, &File.exists?/1)
58 proceed? = Enum.empty?(will_overwrite) or Keyword.get(options, :force, false)
60 unless not proceed? do
62 Keyword.get(options, :domain) ||
63 Mix.shell().prompt("What domain will your instance use? (e.g. pleroma.soykaf.com)")
67 Keyword.get(options, :name) ||
68 Mix.shell().prompt("What is the name of your instance? (e.g. Pleroma/Soykaf)")
72 Keyword.get(options, :admin_email) ||
73 Mix.shell().prompt("What is your admin email address?")
77 Keyword.get(options, :dbhost) ||
78 case Mix.shell().prompt("What is the hostname of your database? [localhost]") do
80 dbhost -> dbhost |> String.trim()
84 Keyword.get(options, :dbname) ||
85 case Mix.shell().prompt("What is the name of your database? [pleroma_dev]") do
87 dbname -> dbname |> String.trim()
91 Keyword.get(options, :dbuser) ||
92 case Mix.shell().prompt("What is the user used to connect to your database? [pleroma]") do
94 dbuser -> dbuser |> String.trim()
98 Keyword.get(options, :dbpass) ||
99 case Mix.shell().prompt(
100 "What is the password used to connect to your database? [autogenerated]"
102 "\n" -> :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64)
103 dbpass -> dbpass |> String.trim()
106 secret = :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64)
110 "sample_config.eex" |> Path.expand(__DIR__),
118 version: Pleroma.Mixfile.project() |> Keyword.get(:version),
124 "sample_psql.eex" |> Path.expand(__DIR__),
131 "Writing config to #{config_path}. You should rename it to config/prod.secret.exs or config/dev.secret.exs."
134 File.write(config_path, result_config)
135 Mix.shell().info("Writing #{psql_path}.")
136 File.write(psql_path, result_psql)
142 1. Verify the contents of the generated files.
143 2. Run `sudo -u postgres psql -f #{escape_sh_path(psql_path)}`.
145 if config_path in ["config/dev.secret.exs", "config/prod.secret.exs"] do
148 "3. Run `mv #{escape_sh_path(config_path)} 'config/prod.secret.exs'`."
153 "The task would have overwritten the following files:\n" <>
154 (Enum.map(paths, &"- #{&1}\n") |> Enum.join("")) <>
155 "Rerun with `--force` to overwrite them."
160 defp escape_sh_path(path) do
161 ~S(') <> String.replace(path, ~S('), ~S(\')) <> ~S(')