Merge branch 'develop' into oembed_provider
[akkoma] / lib / mix / tasks / pleroma / instance.ex
1 defmodule Mix.Tasks.Pleroma.Instance do
2 use Mix.Task
3 alias Mix.Tasks.Pleroma.Common
4
5 @shortdoc "Manages Pleroma instance"
6 @moduledoc """
7 Manages Pleroma instance.
8
9 ## Generate a new instance config.
10
11 mix pleroma.instance gen [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(["gen" | 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, port | _] =
62 String.split(
63 Common.get_option(
64 options,
65 :domain,
66 "What domain will your instance use? (e.g pleroma.soykaf.com)"
67 ),
68 ":"
69 ) ++ [443]
70
71 name =
72 Common.get_option(
73 options,
74 :name,
75 "What is the name of your instance? (e.g. Pleroma/Soykaf)"
76 )
77
78 email = Common.get_option(options, :admin_email, "What is your admin email address?")
79
80 dbhost =
81 Common.get_option(options, :dbhost, "What is the hostname of your database?", "localhost")
82
83 dbname =
84 Common.get_option(options, :dbname, "What is the name of your database?", "pleroma_dev")
85
86 dbuser =
87 Common.get_option(
88 options,
89 :dbuser,
90 "What is the user used to connect to your database?",
91 "pleroma"
92 )
93
94 dbpass =
95 Common.get_option(
96 options,
97 :dbpass,
98 "What is the password used to connect to your database?",
99 :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64),
100 "autogenerated"
101 )
102
103 secret = :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64)
104 {web_push_public_key, web_push_private_key} = :crypto.generate_key(:ecdh, :prime256v1)
105
106 result_config =
107 EEx.eval_file(
108 "sample_config.eex" |> Path.expand(__DIR__),
109 domain: domain,
110 port: port,
111 email: email,
112 name: name,
113 dbhost: dbhost,
114 dbname: dbname,
115 dbuser: dbuser,
116 dbpass: dbpass,
117 version: Pleroma.Mixfile.project() |> Keyword.get(:version),
118 secret: secret,
119 web_push_public_key: Base.url_encode64(web_push_public_key, padding: false),
120 web_push_private_key: Base.url_encode64(web_push_private_key, padding: false)
121 )
122
123 result_psql =
124 EEx.eval_file(
125 "sample_psql.eex" |> Path.expand(__DIR__),
126 dbname: dbname,
127 dbuser: dbuser,
128 dbpass: dbpass
129 )
130
131 Mix.shell().info(
132 "Writing config to #{config_path}. You should rename it to config/prod.secret.exs or config/dev.secret.exs."
133 )
134
135 File.write(config_path, result_config)
136 Mix.shell().info("Writing #{psql_path}.")
137 File.write(psql_path, result_psql)
138
139 Mix.shell().info(
140 "\n" <>
141 """
142 To get started:
143 1. Verify the contents of the generated files.
144 2. Run `sudo -u postgres psql -f #{Common.escape_sh_path(psql_path)}`.
145 """ <>
146 if config_path in ["config/dev.secret.exs", "config/prod.secret.exs"] do
147 ""
148 else
149 "3. Run `mv #{Common.escape_sh_path(config_path)} 'config/prod.secret.exs'`."
150 end
151 )
152 else
153 Mix.shell().error(
154 "The task would have overwritten the following files:\n" <>
155 (Enum.map(paths, &"- #{&1}\n") |> Enum.join("")) <>
156 "Rerun with `--force` to overwrite them."
157 )
158 end
159 end
160 end