Merge branch '394_user_tags' into 'develop'
[akkoma] / lib / mix / tasks / pleroma / instance.ex
1 defmodule Mix.Tasks.Pleroma.Instance do
2 use Mix.Task
3 alias Pleroma.{Repo, User}
4 alias Mix.Tasks.Pleroma.Common
5
6 @shortdoc "Manages Pleroma instance"
7 @moduledoc """
8 Manages Pleroma instance.
9
10 ## Generate a new instance config.
11
12 mix pleroma.instance gen [OPTION...]
13
14 If any options are left unspecified, you will be prompted interactively
15
16 ## Options
17
18 - `-f`, `--force` - overwrite any output files
19 - `-o PATH`, `--output PATH` - the output file for the generated configuration
20 - `--output-psql PATH` - the output file for the generated PostgreSQL setup
21 - `--domain DOMAIN` - the domain of your instance
22 - `--instance-name INSTANCE_NAME` - the name of your instance
23 - `--admin-email ADMIN_EMAIL` - the email address of the instance admin
24 - `--dbhost HOSTNAME` - the hostname of the PostgreSQL database to use
25 - `--dbname DBNAME` - the name of the database to use
26 - `--dbuser DBUSER` - the user (aka role) to use for the database connection
27 - `--dbpass DBPASS` - the password to use for the database connection
28 """
29
30 def run(["gen" | rest]) do
31 {options, [], []} =
32 OptionParser.parse(
33 rest,
34 strict: [
35 force: :boolean,
36 output: :string,
37 output_psql: :string,
38 domain: :string,
39 instance_name: :string,
40 admin_email: :string,
41 dbhost: :string,
42 dbname: :string,
43 dbuser: :string,
44 dbpass: :string
45 ],
46 aliases: [
47 o: :output,
48 f: :force
49 ]
50 )
51
52 paths =
53 [config_path, psql_path] = [
54 Keyword.get(options, :output, "config/generated_config.exs"),
55 Keyword.get(options, :output_psql, "config/setup_db.psql")
56 ]
57
58 will_overwrite = Enum.filter(paths, &File.exists?/1)
59 proceed? = Enum.empty?(will_overwrite) or Keyword.get(options, :force, false)
60
61 unless not proceed? do
62 domain =
63 Common.get_option(
64 options,
65 :domain,
66 "What domain will your instance use? (e.g pleroma.soykaf.com)"
67 )
68
69 name =
70 Common.get_option(
71 options,
72 :name,
73 "What is the name of your instance? (e.g. Pleroma/Soykaf)"
74 )
75
76 email = Common.get_option(options, :admin_email, "What is your admin email address?")
77
78 dbhost =
79 Common.get_option(options, :dbhost, "What is the hostname of your database?", "localhost")
80
81 dbname =
82 Common.get_option(options, :dbname, "What is the name of your database?", "pleroma_dev")
83
84 dbuser =
85 Common.get_option(
86 options,
87 :dbuser,
88 "What is the user used to connect to your database?",
89 "pleroma"
90 )
91
92 dbpass =
93 Common.get_option(
94 options,
95 :dbpass,
96 "What is the password used to connect to your database?",
97 :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64),
98 "autogenerated"
99 )
100
101 secret = :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64)
102 {web_push_public_key, web_push_private_key} = :crypto.generate_key(:ecdh, :prime256v1)
103
104 result_config =
105 EEx.eval_file(
106 "sample_config.eex" |> Path.expand(__DIR__),
107 domain: domain,
108 email: email,
109 name: name,
110 dbhost: dbhost,
111 dbname: dbname,
112 dbuser: dbuser,
113 dbpass: dbpass,
114 version: Pleroma.Mixfile.project() |> Keyword.get(:version),
115 secret: secret,
116 web_push_public_key: Base.url_encode64(web_push_public_key, padding: false),
117 web_push_private_key: Base.url_encode64(web_push_private_key, padding: false)
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 #{Common.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 #{Common.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 end