2019-01-03 11:34:31 +01:00
|
|
|
# Portions of this file are derived from Pleroma:
|
|
|
|
# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
# Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/lib/mix/tasks/pleroma/instance.ex
|
|
|
|
|
|
|
|
defmodule Mix.Tasks.Mobilizon.Instance do
|
|
|
|
@moduledoc """
|
|
|
|
Generates a new config
|
|
|
|
|
|
|
|
## Usage
|
|
|
|
``mix mobilizon.instance gen [OPTION...]``
|
|
|
|
|
|
|
|
If any options are left unspecified, you will be prompted interactively.
|
|
|
|
|
|
|
|
## Options
|
|
|
|
|
|
|
|
- `-f`, `--force` - overwrite any output files
|
|
|
|
- `-o PATH`, `--output PATH` - the output file for the generated configuration
|
|
|
|
- `--output-psql PATH` - the output file for the generated PostgreSQL setup
|
|
|
|
- `--domain DOMAIN` - the domain of your instance
|
|
|
|
- `--instance-name INSTANCE_NAME` - the name of your instance
|
|
|
|
- `--admin-email ADMIN_EMAIL` - the email address of the instance admin
|
|
|
|
- `--dbhost HOSTNAME` - the hostname of the PostgreSQL database to use
|
|
|
|
- `--dbname DBNAME` - the name of the database to use
|
|
|
|
- `--dbuser DBUSER` - the user (aka role) to use for the database connection
|
|
|
|
- `--dbpass DBPASS` - the password to use for the database connection
|
2020-12-17 15:25:43 +01:00
|
|
|
- `--dbport DBPORT` - the database port
|
2020-08-04 21:46:17 +02:00
|
|
|
- `--listen-port PORT` - the port the app should listen to, defaults to 4000
|
2019-01-03 11:34:31 +01:00
|
|
|
"""
|
|
|
|
|
2019-09-22 16:26:23 +02:00
|
|
|
use Mix.Task
|
|
|
|
|
2020-10-30 15:16:01 +01:00
|
|
|
import Mix.Tasks.Mobilizon.Common
|
2019-09-22 16:26:23 +02:00
|
|
|
|
|
|
|
@preferred_cli_env "prod"
|
|
|
|
|
|
|
|
@shortdoc "Generates a new config"
|
2019-01-03 11:34:31 +01:00
|
|
|
def run(["gen" | options]) do
|
|
|
|
{options, [], []} =
|
|
|
|
OptionParser.parse(
|
|
|
|
options,
|
|
|
|
strict: [
|
|
|
|
force: :boolean,
|
|
|
|
output: :string,
|
|
|
|
output_psql: :string,
|
|
|
|
domain: :string,
|
|
|
|
instance_name: :string,
|
|
|
|
admin_email: :string,
|
|
|
|
dbhost: :string,
|
|
|
|
dbname: :string,
|
|
|
|
dbuser: :string,
|
|
|
|
dbpass: :string,
|
2020-08-04 21:46:17 +02:00
|
|
|
dbport: :integer,
|
|
|
|
listen_port: :string
|
2019-01-03 11:34:31 +01:00
|
|
|
],
|
|
|
|
aliases: [
|
|
|
|
o: :output,
|
|
|
|
f: :force
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
paths =
|
|
|
|
[config_path, psql_path] = [
|
2021-02-01 14:57:58 +01:00
|
|
|
Keyword.get(options, :output, "config/runtime.exs"),
|
2019-01-03 11:34:31 +01:00
|
|
|
Keyword.get(options, :output_psql, "setup_db.psql")
|
|
|
|
]
|
|
|
|
|
|
|
|
will_overwrite = Enum.filter(paths, &File.exists?/1)
|
|
|
|
proceed? = Enum.empty?(will_overwrite) or Keyword.get(options, :force, false)
|
|
|
|
|
2019-01-03 14:59:59 +01:00
|
|
|
if proceed? do
|
2019-01-03 11:34:31 +01:00
|
|
|
[domain, port | _] =
|
|
|
|
String.split(
|
2020-10-30 15:16:01 +01:00
|
|
|
get_option(
|
2019-01-03 11:34:31 +01:00
|
|
|
options,
|
|
|
|
:domain,
|
2020-06-10 09:38:06 +02:00
|
|
|
"What domain will your instance use? (e.g mobilizon.org)"
|
2019-01-03 11:34:31 +01:00
|
|
|
),
|
|
|
|
":"
|
|
|
|
) ++ [443]
|
|
|
|
|
|
|
|
name =
|
2020-10-30 15:16:01 +01:00
|
|
|
get_option(
|
2019-01-03 11:34:31 +01:00
|
|
|
options,
|
2020-08-04 21:22:38 +02:00
|
|
|
:instance_name,
|
2020-06-10 09:38:06 +02:00
|
|
|
"What is the name of your instance? (e.g. Mobilizon)"
|
2019-01-03 11:34:31 +01:00
|
|
|
)
|
|
|
|
|
2020-06-10 09:38:06 +02:00
|
|
|
email =
|
2020-10-30 15:16:01 +01:00
|
|
|
get_option(
|
2020-06-10 09:38:06 +02:00
|
|
|
options,
|
|
|
|
:admin_email,
|
|
|
|
"What's the address email will be send with?",
|
|
|
|
"noreply@#{domain}"
|
|
|
|
)
|
2019-01-03 11:34:31 +01:00
|
|
|
|
2020-10-30 15:16:01 +01:00
|
|
|
dbhost = get_option(options, :dbhost, "What is the hostname of your database?", "localhost")
|
2019-01-03 11:34:31 +01:00
|
|
|
|
|
|
|
dbname =
|
2020-10-30 15:16:01 +01:00
|
|
|
get_option(
|
2019-01-03 11:34:31 +01:00
|
|
|
options,
|
|
|
|
:dbname,
|
|
|
|
"What is the name of your database?",
|
|
|
|
"mobilizon_prod"
|
|
|
|
)
|
|
|
|
|
|
|
|
dbuser =
|
2020-10-30 15:16:01 +01:00
|
|
|
get_option(
|
2019-01-03 11:34:31 +01:00
|
|
|
options,
|
|
|
|
:dbuser,
|
|
|
|
"What is the user used to connect to your database?",
|
|
|
|
"mobilizon"
|
|
|
|
)
|
|
|
|
|
|
|
|
dbpass =
|
2020-10-30 15:16:01 +01:00
|
|
|
get_option(
|
2019-01-03 11:34:31 +01:00
|
|
|
options,
|
|
|
|
:dbpass,
|
|
|
|
"What is the password used to connect to your database?",
|
|
|
|
:crypto.strong_rand_bytes(64) |> Base.url_encode64() |> binary_part(0, 64),
|
|
|
|
"autogenerated"
|
|
|
|
)
|
|
|
|
|
2020-01-30 20:27:25 +01:00
|
|
|
listen_port =
|
2020-10-30 15:16:01 +01:00
|
|
|
get_option(
|
2020-01-30 20:27:25 +01:00
|
|
|
options,
|
|
|
|
:listen_port,
|
|
|
|
"What port will the app listen to (leave it if you are using the default setup with nginx)?",
|
|
|
|
4000
|
|
|
|
)
|
|
|
|
|
|
|
|
instance_secret = :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64)
|
|
|
|
auth_secret = :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64)
|
|
|
|
|
|
|
|
template_dir = Application.app_dir(:mobilizon, "priv") <> "/templates"
|
2019-01-03 11:34:31 +01:00
|
|
|
|
|
|
|
result_config =
|
|
|
|
EEx.eval_file(
|
2020-01-30 20:27:25 +01:00
|
|
|
"#{template_dir}/config.template.eex",
|
2019-01-03 11:34:31 +01:00
|
|
|
instance_domain: domain,
|
|
|
|
instance_port: port,
|
|
|
|
instance_email: email,
|
|
|
|
instance_name: name,
|
|
|
|
database_host: dbhost,
|
|
|
|
database_name: dbname,
|
|
|
|
database_port: Keyword.get(options, :dbport, 5432),
|
|
|
|
database_username: dbuser,
|
|
|
|
database_password: dbpass,
|
2020-01-30 20:27:25 +01:00
|
|
|
instance_secret: instance_secret,
|
|
|
|
auth_secret: auth_secret,
|
|
|
|
listen_port: listen_port
|
2019-01-03 11:34:31 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
result_psql =
|
|
|
|
EEx.eval_file(
|
2020-01-30 20:27:25 +01:00
|
|
|
"#{template_dir}/setup_db.eex",
|
2019-01-03 11:34:31 +01:00
|
|
|
database_name: dbname,
|
|
|
|
database_username: dbuser,
|
|
|
|
database_password: dbpass
|
|
|
|
)
|
|
|
|
|
2021-02-01 14:57:58 +01:00
|
|
|
with :ok <- write_config(config_path, result_config),
|
|
|
|
:ok <- write_psql(psql_path, result_psql) do
|
|
|
|
shell_info(
|
|
|
|
"\n" <>
|
|
|
|
"""
|
|
|
|
To get started:
|
|
|
|
1. Check the contents of the generated files.
|
|
|
|
2. Run `sudo -u postgres psql -f #{escape_sh_path(psql_path)} && rm #{
|
|
|
|
escape_sh_path(psql_path)
|
|
|
|
}`.
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
else
|
|
|
|
{:error, err} -> exit(err)
|
|
|
|
_ -> exit(:unknown_error)
|
|
|
|
end
|
2019-01-03 11:34:31 +01:00
|
|
|
else
|
2020-10-30 15:16:01 +01:00
|
|
|
shell_error(
|
2019-01-03 11:34:31 +01:00
|
|
|
"The task would have overwritten the following files:\n" <>
|
2019-01-03 14:59:59 +01:00
|
|
|
(will_overwrite |> Enum.map(&"- #{&1}\n") |> Enum.join("")) <>
|
2019-01-03 11:34:31 +01:00
|
|
|
"Rerun with `-f/--force` to overwrite them."
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2021-02-01 14:57:58 +01:00
|
|
|
|
|
|
|
defp write_config(config_path, result_config) do
|
|
|
|
shell_info("Writing config to #{config_path}.")
|
|
|
|
|
|
|
|
case File.write(config_path, result_config) do
|
|
|
|
:ok ->
|
|
|
|
:ok
|
|
|
|
|
|
|
|
{:error, err} ->
|
|
|
|
shell_error(
|
|
|
|
"\nERROR: Unable to write config file to #{config_path}. Make sure you have permissions on the destination.\n"
|
|
|
|
)
|
|
|
|
|
|
|
|
{:error, err}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp write_psql(psql_path, result_psql) do
|
|
|
|
shell_info("Writing #{psql_path}.")
|
|
|
|
|
|
|
|
case File.write(psql_path, result_psql) do
|
|
|
|
:ok ->
|
|
|
|
:ok
|
|
|
|
|
|
|
|
{:error, err} ->
|
|
|
|
shell_error(
|
|
|
|
"\nERROR: Unable to write psql file to #{psql_path}. Make sure you have permissions on the destination.\n"
|
|
|
|
)
|
|
|
|
|
|
|
|
{:error, err}
|
|
|
|
end
|
|
|
|
end
|
2019-01-03 11:34:31 +01:00
|
|
|
end
|