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