Refactor common functions to common.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 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(options, :name, "What is the name of your instance? (e.g. Pleroma/Soykaf)")
71
72 email = Common.get_option(options, :admin_email, "What is your admin email address?")
73
74 dbhost = Common.get_option(options, :dbhost, "What is the hostname of your database?", "localhost")
75
76 dbname = Common.get_option(options, :dbname, "What is the name of your database?", "pleroma_dev")
77
78 dbuser =
79 Common.get_option(
80 options,
81 :dbuser,
82 "What is the user used to connect to your database?",
83 "pleroma"
84 )
85
86 dbpass =
87 Common.get_option(
88 options,
89 :dbpass,
90 "What is the password used to connect to your database?",
91 :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64),
92 "autogenerated"
93 )
94
95 secret = :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64)
96
97 result_config =
98 EEx.eval_file(
99 "sample_config.eex" |> Path.expand(__DIR__),
100 domain: domain,
101 email: email,
102 name: name,
103 dbhost: dbhost,
104 dbname: dbname,
105 dbuser: dbuser,
106 dbpass: dbpass,
107 version: Pleroma.Mixfile.project() |> Keyword.get(:version),
108 secret: secret
109 )
110
111 result_psql =
112 EEx.eval_file(
113 "sample_psql.eex" |> Path.expand(__DIR__),
114 dbname: dbname,
115 dbuser: dbuser,
116 dbpass: dbpass
117 )
118
119 Mix.shell().info(
120 "Writing config to #{config_path}. You should rename it to config/prod.secret.exs or config/dev.secret.exs."
121 )
122
123 File.write(config_path, result_config)
124 Mix.shell().info("Writing #{psql_path}.")
125 File.write(psql_path, result_psql)
126
127 Mix.shell().info(
128 "\n" <>
129 """
130 To get started:
131 1. Verify the contents of the generated files.
132 2. Run `sudo -u postgres psql -f #{Common.escape_sh_path(psql_path)}`.
133 """ <>
134 if config_path in ["config/dev.secret.exs", "config/prod.secret.exs"] do
135 ""
136 else
137 "3. Run `mv #{Common.escape_sh_path(config_path)} 'config/prod.secret.exs'`."
138 end
139 )
140 else
141 Mix.shell().error(
142 "The task would have overwritten the following files:\n" <>
143 (Enum.map(paths, &"- #{&1}\n") |> Enum.join("")) <>
144 "Rerun with `--force` to overwrite them."
145 )
146 end
147 end
148
149
150
151 end