Lint fix
[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 Keyword.get(options, :domain) ||
63 Mix.shell().prompt("What domain will your instance use? (e.g. pleroma.soykaf.com)")
64 |> String.trim()
65
66 name =
67 Keyword.get(options, :name) ||
68 Mix.shell().prompt("What is the name of your instance? (e.g. Pleroma/Soykaf)")
69 |> String.trim()
70
71 email =
72 Keyword.get(options, :admin_email) ||
73 Mix.shell().prompt("What is your admin email address?")
74 |> String.trim()
75
76 dbhost =
77 Keyword.get(options, :dbhost) ||
78 case Mix.shell().prompt("What is the hostname of your database? [localhost]") do
79 "\n" -> "localhost"
80 dbhost -> dbhost |> String.trim()
81 end
82
83 dbname =
84 Keyword.get(options, :dbname) ||
85 case Mix.shell().prompt("What is the name of your database? [pleroma_dev]") do
86 "\n" -> "pleroma_dev"
87 dbname -> dbname |> String.trim()
88 end
89
90 dbuser =
91 Keyword.get(options, :dbuser) ||
92 case Mix.shell().prompt("What is the user used to connect to your database? [pleroma]") do
93 "\n" -> "pleroma"
94 dbuser -> dbuser |> String.trim()
95 end
96
97 dbpass =
98 Keyword.get(options, :dbpass) ||
99 case Mix.shell().prompt(
100 "What is the password used to connect to your database? [autogenerated]"
101 ) do
102 "\n" -> :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64)
103 dbpass -> dbpass |> String.trim()
104 end
105
106 secret = :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64)
107
108 result_config =
109 EEx.eval_file(
110 "sample_config.eex" |> Path.expand(__DIR__),
111 domain: domain,
112 email: email,
113 name: name,
114 dbhost: dbhost,
115 dbname: dbname,
116 dbuser: dbuser,
117 dbpass: dbpass,
118 version: Pleroma.Mixfile.project() |> Keyword.get(:version),
119 secret: secret
120 )
121
122 result_psql =
123 EEx.eval_file(
124 "sample_psql.eex" |> Path.expand(__DIR__),
125 dbname: dbname,
126 dbuser: dbuser,
127 dbpass: dbpass
128 )
129
130 Mix.shell().info(
131 "Writing config to #{config_path}. You should rename it to config/prod.secret.exs or config/dev.secret.exs."
132 )
133
134 File.write(config_path, result_config)
135 Mix.shell().info("Writing #{psql_path}.")
136 File.write(psql_path, result_psql)
137
138 Mix.shell().info(
139 "\n" <>
140 """
141 To get started:
142 1. Verify the contents of the generated files.
143 2. Run `sudo -u postgres psql -f #{escape_sh_path(psql_path)}`.
144 """ <>
145 if config_path in ["config/dev.secret.exs", "config/prod.secret.exs"] do
146 ""
147 else
148 "3. Run `mv #{escape_sh_path(config_path)} 'config/prod.secret.exs'`."
149 end
150 )
151 else
152 Mix.shell().error(
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."
156 )
157 end
158 end
159
160 defp escape_sh_path(path) do
161 ~S(') <> String.replace(path, ~S('), ~S(\')) <> ~S(')
162 end
163 end