aboutsummaryrefslogtreecommitdiff
path: root/lib/mix
diff options
context:
space:
mode:
authorrinpatch <rinpatch@sdf.org>2020-01-30 22:16:55 +0300
committerrinpatch <rinpatch@sdf.org>2020-01-30 22:16:55 +0300
commit5b62acf6e9a38f8d14a9fb37cc85e646fb0169e3 (patch)
tree6677ba80418ffd34a78cba75498f2c1ab48ba14c /lib/mix
parent45180d4c6058f790475d8ff28018c912db105082 (diff)
parent946de2299cccebac6718e3a132397ff5c06c67ee (diff)
downloadpleroma-5b62acf6e9a38f8d14a9fb37cc85e646fb0169e3.tar.gz
Merge branch 'develop' into fix/disable-rate-limiter-for-socket-localhost
Diffstat (limited to 'lib/mix')
-rw-r--r--lib/mix/tasks/pleroma/config.ex158
-rw-r--r--lib/mix/tasks/pleroma/docs.ex2
-rw-r--r--lib/mix/tasks/pleroma/email.ex25
-rw-r--r--lib/mix/tasks/pleroma/emoji.ex2
-rw-r--r--lib/mix/tasks/pleroma/robotstxt.ex1
5 files changed, 146 insertions, 42 deletions
diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex
index 590c7a914..3e76d2c97 100644
--- a/lib/mix/tasks/pleroma/config.ex
+++ b/lib/mix/tasks/pleroma/config.ex
@@ -4,71 +4,147 @@
defmodule Mix.Tasks.Pleroma.Config do
use Mix.Task
+
import Mix.Pleroma
+
+ alias Pleroma.ConfigDB
alias Pleroma.Repo
- alias Pleroma.Web.AdminAPI.Config
+
@shortdoc "Manages the location of the config"
@moduledoc File.read!("docs/administration/CLI_tasks/config.md")
+
def run(["migrate_to_db"]) do
start_pleroma()
+ migrate_to_db()
+ end
+
+ def run(["migrate_from_db" | options]) do
+ start_pleroma()
- if Pleroma.Config.get([:instance, :dynamic_configuration]) do
- Application.get_all_env(:pleroma)
- |> Enum.reject(fn {k, _v} -> k in [Pleroma.Repo, :env] end)
- |> Enum.each(fn {k, v} ->
- key = to_string(k) |> String.replace("Elixir.", "")
+ {opts, _} =
+ OptionParser.parse!(options,
+ strict: [env: :string, delete: :boolean],
+ aliases: [d: :delete]
+ )
+
+ migrate_from_db(opts)
+ end
- key =
- if String.starts_with?(key, "Pleroma.") do
- key
+ @spec migrate_to_db(Path.t() | nil) :: any()
+ def migrate_to_db(file_path \\ nil) do
+ if Pleroma.Config.get([:configurable_from_database]) do
+ config_file =
+ if file_path do
+ file_path
+ else
+ if Pleroma.Config.get(:release) do
+ Pleroma.Config.get(:config_path)
else
- ":" <> key
+ "config/#{Pleroma.Config.get(:env)}.secret.exs"
end
+ end
- {:ok, _} = Config.update_or_create(%{group: "pleroma", key: key, value: v})
- Mix.shell().info("#{key} is migrated.")
- end)
-
- Mix.shell().info("Settings migrated.")
+ do_migrate_to_db(config_file)
else
- Mix.shell().info(
- "Migration is not allowed by config. You can change this behavior in instance settings."
- )
+ migration_error()
end
end
- def run(["migrate_from_db", env, delete?]) do
- start_pleroma()
+ defp do_migrate_to_db(config_file) do
+ if File.exists?(config_file) do
+ Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;")
+ Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;")
- delete? = if delete? == "true", do: true, else: false
+ custom_config =
+ config_file
+ |> read_file()
+ |> elem(0)
- if Pleroma.Config.get([:instance, :dynamic_configuration]) do
- config_path = "config/#{env}.exported_from_db.secret.exs"
+ custom_config
+ |> Keyword.keys()
+ |> Enum.each(&create(&1, custom_config))
+ else
+ shell_info("To migrate settings, you must define custom settings in #{config_file}.")
+ end
+ end
- {:ok, file} = File.open(config_path, [:write, :utf8])
- IO.write(file, "use Mix.Config\r\n")
+ defp create(group, settings) do
+ group
+ |> Pleroma.Config.Loader.filter_group(settings)
+ |> Enum.each(fn {key, value} ->
+ key = inspect(key)
+ {:ok, _} = ConfigDB.update_or_create(%{group: inspect(group), key: key, value: value})
- Repo.all(Config)
- |> Enum.each(fn config ->
- IO.write(
- file,
- "config :#{config.group}, #{config.key}, #{
- inspect(Config.from_binary(config.value), limit: :infinity)
- }\r\n\r\n"
- )
+ shell_info("Settings for key #{key} migrated.")
+ end)
+
+ shell_info("Settings for group :#{group} migrated.")
+ end
- if delete? do
- {:ok, _} = Repo.delete(config)
- Mix.shell().info("#{config.key} deleted from DB.")
+ defp migrate_from_db(opts) do
+ if Pleroma.Config.get([:configurable_from_database]) do
+ env = opts[:env] || "prod"
+
+ config_path =
+ if Pleroma.Config.get(:release) do
+ :config_path
+ |> Pleroma.Config.get()
+ |> Path.dirname()
+ else
+ "config"
end
- end)
+ |> Path.join("#{env}.exported_from_db.secret.exs")
+
+ file = File.open!(config_path, [:write, :utf8])
+
+ IO.write(file, config_header())
- File.close(file)
+ ConfigDB
+ |> Repo.all()
+ |> Enum.each(&write_and_delete(&1, file, opts[:delete]))
+
+ :ok = File.close(file)
System.cmd("mix", ["format", config_path])
else
- Mix.shell().info(
- "Migration is not allowed by config. You can change this behavior in instance settings."
- )
+ migration_error()
end
end
+
+ defp migration_error do
+ shell_error(
+ "Migration is not allowed in config. You can change this behavior by setting `configurable_from_database` to true."
+ )
+ end
+
+ if Code.ensure_loaded?(Config.Reader) do
+ defp config_header, do: "import Config\r\n\r\n"
+ defp read_file(config_file), do: Config.Reader.read_imports!(config_file)
+ else
+ defp config_header, do: "use Mix.Config\r\n\r\n"
+ defp read_file(config_file), do: Mix.Config.eval!(config_file)
+ end
+
+ defp write_and_delete(config, file, delete?) do
+ config
+ |> write(file)
+ |> delete(delete?)
+ end
+
+ defp write(config, file) do
+ value =
+ config.value
+ |> ConfigDB.from_binary()
+ |> inspect(limit: :infinity)
+
+ IO.write(file, "config #{config.group}, #{config.key}, #{value}\r\n\r\n")
+
+ config
+ end
+
+ defp delete(config, true) do
+ {:ok, _} = Repo.delete(config)
+ shell_info("#{config.key} deleted from DB.")
+ end
+
+ defp delete(_config, _), do: :ok
end
diff --git a/lib/mix/tasks/pleroma/docs.ex b/lib/mix/tasks/pleroma/docs.ex
index 0d2663648..3c870f876 100644
--- a/lib/mix/tasks/pleroma/docs.ex
+++ b/lib/mix/tasks/pleroma/docs.ex
@@ -28,7 +28,7 @@ defmodule Mix.Tasks.Pleroma.Docs do
defp do_run(implementation) do
start_pleroma()
- with {descriptions, _paths} <- Mix.Config.eval!("config/description.exs"),
+ with descriptions <- Pleroma.Config.Loader.load("config/description.exs"),
{:ok, file_path} <-
Pleroma.Docs.Generator.process(
implementation,
diff --git a/lib/mix/tasks/pleroma/email.ex b/lib/mix/tasks/pleroma/email.ex
new file mode 100644
index 000000000..2c3801429
--- /dev/null
+++ b/lib/mix/tasks/pleroma/email.ex
@@ -0,0 +1,25 @@
+defmodule Mix.Tasks.Pleroma.Email do
+ use Mix.Task
+
+ @shortdoc "Simple Email test"
+ @moduledoc File.read!("docs/administration/CLI_tasks/email.md")
+
+ def run(["test" | args]) do
+ Mix.Pleroma.start_pleroma()
+
+ {options, [], []} =
+ OptionParser.parse(
+ args,
+ strict: [
+ to: :string
+ ]
+ )
+
+ email = Pleroma.Emails.AdminEmail.test_email(options[:to])
+ {:ok, _} = Pleroma.Emails.Mailer.deliver(email)
+
+ Mix.shell().info(
+ "Test email has been sent to #{inspect(email.to)} from #{inspect(email.from)}"
+ )
+ end
+end
diff --git a/lib/mix/tasks/pleroma/emoji.ex b/lib/mix/tasks/pleroma/emoji.ex
index 35669af27..24d999707 100644
--- a/lib/mix/tasks/pleroma/emoji.ex
+++ b/lib/mix/tasks/pleroma/emoji.ex
@@ -9,6 +9,7 @@ defmodule Mix.Tasks.Pleroma.Emoji do
@moduledoc File.read!("docs/administration/CLI_tasks/emoji.md")
def run(["ls-packs" | args]) do
+ Mix.Pleroma.start_pleroma()
Application.ensure_all_started(:hackney)
{options, [], []} = parse_global_opts(args)
@@ -35,6 +36,7 @@ defmodule Mix.Tasks.Pleroma.Emoji do
end
def run(["get-packs" | args]) do
+ Mix.Pleroma.start_pleroma()
Application.ensure_all_started(:hackney)
{options, pack_names, []} = parse_global_opts(args)
diff --git a/lib/mix/tasks/pleroma/robotstxt.ex b/lib/mix/tasks/pleroma/robotstxt.ex
index 2128e1cd6..e99dd8502 100644
--- a/lib/mix/tasks/pleroma/robotstxt.ex
+++ b/lib/mix/tasks/pleroma/robotstxt.ex
@@ -18,6 +18,7 @@ defmodule Mix.Tasks.Pleroma.RobotsTxt do
"""
def run(["disallow_all"]) do
+ Mix.Pleroma.start_pleroma()
static_dir = Pleroma.Config.get([:instance, :static_dir], "instance/static/")
if !File.exists?(static_dir) do