1 defmodule Mix.Tasks.Pleroma.Gen.Instance do
4 @shortdoc "Generates the configuration for a new instance"
6 Generates the configuration for a new instance.
8 If any options are left unspecified, you will be prompted interactively. This
9 means the simplest invocation would be
11 mix pleroma.gen.instance
15 - `-f`, `--force` - overwrite any output files
16 - `-o PATH`, `--output PATH` - the output file for the generated configuration
17 - `--output-psql PATH` - the output file for the generated PostgreSQL setup
18 - `--domain DOMAIN` - the domain of your instance
19 - `--instance-name INSTANCE_NAME` - the name of your instance
20 - `--admin-email ADMIN_EMAIL` - the email address of the instance admin
21 - `--dbhost HOSTNAME` - the hostname of the PostgreSQL database to use
22 - `--dbname DBNAME` - the name of the database to use
23 - `--dbuser DBUSER` - the user (aka role) to use for the database connection
24 - `--dbpass DBPASS` - the password to use for the database connection
36 instance_name: :string,
50 [config_path, psql_path] = [
51 Keyword.get(options, :output, "config/generated_config.exs"),
52 Keyword.get(options, :output_psql, "config/setup_db.psql")
55 will_overwrite = Enum.filter(paths, &File.exists?/1)
56 proceed? = Enum.empty?(will_overwrite) or Keyword.get(options, :force, false)
58 unless not proceed? do
60 Keyword.get(options, :domain) ||
61 Mix.shell().prompt("What domain will your instance use? (e.g. pleroma.soykaf.com)")
65 Keyword.get(options, :name) ||
66 Mix.shell().prompt("What is the name of your instance? (e.g. Pleroma/Soykaf)")
70 Keyword.get(options, :admin_email) ||
71 Mix.shell().prompt("What is your admin email address?")
75 Keyword.get(options, :dbhost) ||
76 case Mix.shell().prompt("What is the hostname of your database? [localhost]") do
78 dbhost -> dbhost |> String.trim()
82 Keyword.get(options, :dbname) ||
83 case Mix.shell().prompt("What is the name of your database? [pleroma_dev]") do
85 dbname -> dbname |> String.trim()
89 Keyword.get(options, :dbuser) ||
90 case Mix.shell().prompt("What is the user used to connect to your database? [pleroma]") do
92 dbuser -> dbuser |> String.trim()
96 Keyword.get(options, :dbpass) ||
97 case Mix.shell().prompt(
98 "What is the password used to connect to your database? [autogenerated]"
100 "\n" -> :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64)
101 dbpass -> dbpass |> String.trim()
104 secret = :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64)
108 "sample_config.eex" |> Path.expand(__DIR__),
116 version: Pleroma.Mixfile.project() |> Keyword.get(:version),
122 "sample_psql.eex" |> Path.expand(__DIR__),
129 "Writing config to #{config_path}. You should rename it to config/prod.secret.exs or config/dev.secret.exs."
132 File.write(config_path, result_config)
133 Mix.shell().info("Writing #{psql_path}.")
134 File.write(psql_path, result_psql)
140 1. Verify the contents of the generated files.
141 2. Run `sudo -u postgres psql -f #{escape_sh_path(psql_path)}`.
143 if config_path in ["config/dev.secret.exs", "config/prod.secret.exs"] do
146 "3. Run `mv #{escape_sh_path(config_path)} 'config/prod.secret.exs'`."
151 "The task would have overwritten the following files:\n" <>
152 (Enum.map(paths, &"- #{&1}\n") |> Enum.join("")) <>
153 "Rerun with `--force` to overwrite them."
158 defp escape_sh_path(path) do
159 ~S(') <> String.replace(path, ~S('), ~S(\')) <> ~S(')