New generate config task from Pleroma upstream & move tasks namespace
Little fixes and tests Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
28
lib/mix/tasks/mobilizon/common.ex
Normal file
28
lib/mix/tasks/mobilizon/common.ex
Normal file
@@ -0,0 +1,28 @@
|
||||
# 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/common.ex
|
||||
|
||||
defmodule Mix.Tasks.Mobilizon.Common do
|
||||
@moduledoc "Common functions to be reused in mix tasks"
|
||||
|
||||
def get_option(options, opt, prompt, defval \\ nil, defname \\ nil) do
|
||||
display = if defname || defval, do: "#{prompt} [#{defname || defval}]", else: "#{prompt}"
|
||||
|
||||
Keyword.get(options, opt) ||
|
||||
case Mix.shell().prompt(display) do
|
||||
"\n" ->
|
||||
case defval do
|
||||
nil -> get_option(options, opt, prompt, defval)
|
||||
defval -> defval
|
||||
end
|
||||
|
||||
opt ->
|
||||
opt |> String.trim()
|
||||
end
|
||||
end
|
||||
|
||||
def escape_sh_path(path) do
|
||||
~S(') <> String.replace(path, ~S('), ~S(\')) <> ~S(')
|
||||
end
|
||||
end
|
||||
30
lib/mix/tasks/mobilizon/create_bot.ex
Normal file
30
lib/mix/tasks/mobilizon/create_bot.ex
Normal file
@@ -0,0 +1,30 @@
|
||||
defmodule Mix.Tasks.Mobilizon.CreateBot do
|
||||
@moduledoc """
|
||||
Creates a bot from a source
|
||||
"""
|
||||
|
||||
use Mix.Task
|
||||
alias Mobilizon.Actors
|
||||
alias Mobilizon.Actors.Bot
|
||||
alias Mobilizon.Actors.User
|
||||
require Logger
|
||||
|
||||
@shortdoc "Register user"
|
||||
def run([email, name, summary, type, url]) do
|
||||
Mix.Task.run("app.start")
|
||||
|
||||
with {:ok, %User{} = user} <- Actors.get_user_by_email(email, true),
|
||||
actor <- Actors.register_bot_account(%{name: name, summary: summary}),
|
||||
{:ok, %Bot{} = bot} <-
|
||||
Actors.create_bot(%{
|
||||
"type" => type,
|
||||
"source" => url,
|
||||
"actor_id" => actor.id,
|
||||
"user_id" => user.id
|
||||
}) do
|
||||
bot
|
||||
else
|
||||
e -> Logger.error(inspect(e))
|
||||
end
|
||||
end
|
||||
end
|
||||
171
lib/mix/tasks/mobilizon/instance.ex
Normal file
171
lib/mix/tasks/mobilizon/instance.ex
Normal file
@@ -0,0 +1,171 @@
|
||||
# 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
|
||||
use Mix.Task
|
||||
alias Mix.Tasks.Mobilizon.Common
|
||||
|
||||
@shortdoc "Generates a new config"
|
||||
@preferred_cli_env "prod"
|
||||
@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
|
||||
"""
|
||||
|
||||
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,
|
||||
dbport: :integer
|
||||
],
|
||||
aliases: [
|
||||
o: :output,
|
||||
f: :force
|
||||
]
|
||||
)
|
||||
|
||||
paths =
|
||||
[config_path, psql_path] = [
|
||||
Keyword.get(options, :output, ".env"),
|
||||
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)
|
||||
|
||||
unless not proceed? do
|
||||
[domain, port | _] =
|
||||
String.split(
|
||||
Common.get_option(
|
||||
options,
|
||||
:domain,
|
||||
"What domain will your instance use? (e.g framameet.org)"
|
||||
),
|
||||
":"
|
||||
) ++ [443]
|
||||
|
||||
name =
|
||||
Common.get_option(
|
||||
options,
|
||||
:name,
|
||||
"What is the name of your instance? (e.g. Framameet)"
|
||||
)
|
||||
|
||||
email = Common.get_option(options, :admin_email, "What is your admin email address?")
|
||||
|
||||
dbhost =
|
||||
Common.get_option(options, :dbhost, "What is the hostname of your database?", "localhost")
|
||||
|
||||
dbname =
|
||||
Common.get_option(
|
||||
options,
|
||||
:dbname,
|
||||
"What is the name of your database?",
|
||||
"mobilizon_prod"
|
||||
)
|
||||
|
||||
dbuser =
|
||||
Common.get_option(
|
||||
options,
|
||||
:dbuser,
|
||||
"What is the user used to connect to your database?",
|
||||
"mobilizon"
|
||||
)
|
||||
|
||||
dbpass =
|
||||
Common.get_option(
|
||||
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"
|
||||
)
|
||||
|
||||
secret = :crypto.strong_rand_bytes(64) |> Base.encode64() |> binary_part(0, 64)
|
||||
|
||||
result_config =
|
||||
EEx.eval_file(
|
||||
".env.sample" |> Path.expand(__DIR__ <> "../../../../../"),
|
||||
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,
|
||||
version: Mobilizon.Mixfile.project() |> Keyword.get(:version),
|
||||
instance_secret: secret
|
||||
)
|
||||
|
||||
result_psql =
|
||||
EEx.eval_file(
|
||||
"support/postgresql/setup_db.psql" |> Path.expand(__DIR__ <> "../../../../../"),
|
||||
database_name: dbname,
|
||||
database_username: dbuser,
|
||||
database_password: dbpass
|
||||
)
|
||||
|
||||
Mix.shell().info(
|
||||
"Writing config to #{config_path}. You should rename it to .env.production, .env.dev or .env.test"
|
||||
)
|
||||
|
||||
File.write(config_path, result_config)
|
||||
Mix.shell().info("Writing #{psql_path}.")
|
||||
File.write(psql_path, result_psql)
|
||||
|
||||
Mix.shell().info(
|
||||
"\n" <>
|
||||
"""
|
||||
To get started:
|
||||
1. Check the contents of the generated files.
|
||||
2. Run `sudo -u postgres psql -f #{Common.escape_sh_path(psql_path)} && rm #{
|
||||
Common.escape_sh_path(psql_path)
|
||||
}`.
|
||||
""" <>
|
||||
if config_path in [".env.production", ".env.dev", ".env.test"] do
|
||||
""
|
||||
else
|
||||
"3. Run `mv #{Common.escape_sh_path(config_path)} '.env.production'`."
|
||||
end
|
||||
)
|
||||
else
|
||||
Mix.shell().error(
|
||||
"The task would have overwritten the following files:\n" <>
|
||||
(Enum.map(will_overwrite, &"- #{&1}\n") |> Enum.join("")) <>
|
||||
"Rerun with `-f/--force` to overwrite them."
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
20
lib/mix/tasks/mobilizon/toot.ex
Normal file
20
lib/mix/tasks/mobilizon/toot.ex
Normal file
@@ -0,0 +1,20 @@
|
||||
defmodule Mix.Tasks.Mobilizon.Toot do
|
||||
@moduledoc """
|
||||
Creates a bot from a source
|
||||
"""
|
||||
|
||||
use Mix.Task
|
||||
require Logger
|
||||
|
||||
@shortdoc "Toot to an user"
|
||||
def run([from, content]) do
|
||||
Mix.Task.run("app.start")
|
||||
|
||||
with {:ok, _} <- MobilizonWeb.API.Comments.create_comment(from, content) do
|
||||
Mix.shell().info("Tooted")
|
||||
else
|
||||
{:local_actor, _} -> Mix.shell().error("Failed to toot.\nActor #{from} doesn't exist")
|
||||
_ -> Mix.shell().error("Failed to toot.")
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user