Move invite task to pleroma namespace
[akkoma] / lib / mix / tasks / pleroma / gen_instance.ex
1 defmodule Mix.Tasks.Pleroma.Gen.Instance do
2 use Mix.Task
3
4 @shortdoc "Generates the configuration for a new instance"
5 @moduledoc """
6 Generates the configuration for a new instance.
7
8 If any options are left unspecified, you will be prompted interactively. This
9 means the simplest invocation would be
10
11 mix pleroma.gen.instance
12
13 ## Options
14
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
25 """
26
27 def run(rest) do
28 {options, [], []} =
29 OptionParser.parse(
30 rest,
31 strict: [
32 force: :boolean,
33 output: :string,
34 output_psql: :string,
35 domain: :string,
36 instance_name: :string,
37 admin_email: :string,
38 dbhost: :string,
39 dbname: :string,
40 dbuser: :string,
41 dbpass: :string
42 ],
43 aliases: [
44 o: :output,
45 f: :force
46 ]
47 )
48
49 paths =
50 [config_path, psql_path] = [
51 Keyword.get(options, :output, "config/generated_config.exs"),
52 Keyword.get(options, :output_psql, "config/setup_db.psql")
53 ]
54
55 will_overwrite = Enum.filter(paths, &File.exists?/1)
56 proceed? = Enum.empty?(will_overwrite) or Keyword.get(options, :force, false)
57
58 unless not proceed? do
59 domain =
60 Keyword.get(options, :domain) ||
61 Mix.shell().prompt("What domain will your instance use? (e.g. pleroma.soykaf.com)")
62 |> String.trim()
63
64 name =
65 Keyword.get(options, :name) ||
66 Mix.shell().prompt("What is the name of your instance? (e.g. Pleroma/Soykaf)")
67 |> String.trim()
68
69 email =
70 Keyword.get(options, :admin_email) ||
71 Mix.shell().prompt("What is your admin email address?")
72 |> String.trim()
73
74 dbhost =
75 Keyword.get(options, :dbhost) ||
76 case Mix.shell().prompt("What is the hostname of your database? [localhost]") do
77 "\n" -> "localhost"
78 dbhost -> dbhost |> String.trim()
79 end
80
81 dbname =
82 Keyword.get(options, :dbname) ||
83 case Mix.shell().prompt("What is the name of your database? [pleroma_dev]") do
84 "\n" -> "pleroma_dev"
85 dbname -> dbname |> String.trim()
86 end
87
88 dbuser =
89 Keyword.get(options, :dbuser) ||
90 case Mix.shell().prompt("What is the user used to connect to your database? [pleroma]") do
91 "\n" -> "pleroma"
92 dbuser -> dbuser |> String.trim()
93 end
94
95 dbpass =
96 Keyword.get(options, :dbpass) ||
97 case Mix.shell().prompt(
98 "What is the password used to connect to your database? [autogenerated]"
99 ) do
100 "\n" -> :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64)
101 dbpass -> dbpass |> String.trim()
102 end
103
104 secret = :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64)
105
106 result_config =
107 EEx.eval_file(
108 "sample_config.eex" |> Path.expand(__DIR__),
109 domain: domain,
110 email: email,
111 name: name,
112 dbhost: dbhost,
113 dbname: dbname,
114 dbuser: dbuser,
115 dbpass: dbpass,
116 version: Pleroma.Mixfile.project() |> Keyword.get(:version),
117 secret: secret
118 )
119
120 result_psql =
121 EEx.eval_file(
122 "sample_psql.eex" |> Path.expand(__DIR__),
123 dbname: dbname,
124 dbuser: dbuser,
125 dbpass: dbpass
126 )
127
128 Mix.shell().info(
129 "Writing config to #{config_path}. You should rename it to config/prod.secret.exs or config/dev.secret.exs."
130 )
131
132 File.write(config_path, result_config)
133 Mix.shell().info("Writing #{psql_path}.")
134 File.write(psql_path, result_psql)
135
136 Mix.shell().info(
137 "\n" <>
138 """
139 To get started:
140 1. Verify the contents of the generated files.
141 2. Run `sudo -u postgres psql -f #{escape_sh_path(psql_path)}`.
142 """ <>
143 if config_path in ["config/dev.secret.exs", "config/prod.secret.exs"] do
144 ""
145 else
146 "3. Run `mv #{escape_sh_path(config_path)} 'config/prod.secret.exs'`."
147 end
148 )
149 else
150 Mix.shell().error(
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."
154 )
155 end
156 end
157
158 defp escape_sh_path(path) do
159 ~S(') <> String.replace(path, ~S('), ~S(\')) <> ~S(')
160 end
161 end