releases support
This commit is contained in:
parent
e699861690
commit
efb8ef5abe
6 changed files with 103 additions and 57 deletions
|
|
@ -10,8 +10,6 @@ defmodule Mix.Tasks.Pleroma.Config do
|
|||
alias Pleroma.ConfigDB
|
||||
alias Pleroma.Repo
|
||||
|
||||
require Logger
|
||||
|
||||
@shortdoc "Manages the location of the config"
|
||||
@moduledoc File.read!("docs/administration/CLI_tasks/config.md")
|
||||
|
||||
|
|
@ -32,10 +30,10 @@ defmodule Mix.Tasks.Pleroma.Config do
|
|||
|
||||
with {:active?, true} <-
|
||||
{:active?, Pleroma.Config.get([:configurable_from_database])},
|
||||
env_path when is_binary(env_path) <- opts[:env],
|
||||
config_path <- "config/#{env_path}.exported_from_db.secret.exs",
|
||||
env when is_binary(env) <- opts[:env] || "prod",
|
||||
config_path <- config_path(env),
|
||||
{:ok, file} <- File.open(config_path, [:write, :utf8]) do
|
||||
IO.write(file, "use Mix.Config\r\n")
|
||||
IO.write(file, config_header())
|
||||
|
||||
ConfigDB
|
||||
|> Repo.all()
|
||||
|
|
@ -45,31 +43,50 @@ defmodule Mix.Tasks.Pleroma.Config do
|
|||
System.cmd("mix", ["format", config_path])
|
||||
else
|
||||
{:active?, false} ->
|
||||
Mix.shell().info(
|
||||
"migration is not allowed by config. You can change this behavior in instance settings."
|
||||
shell_info(
|
||||
"Migration is not allowed by config. You can change this behavior in instance settings."
|
||||
)
|
||||
|
||||
error ->
|
||||
Mix.shell().info("error occuried while opening file. #{inspect(error)}")
|
||||
shell_info("Error occuried while opening file. #{inspect(error)}")
|
||||
end
|
||||
end
|
||||
|
||||
defp config_path(env) do
|
||||
path =
|
||||
if Pleroma.Config.get(:release) do
|
||||
:config_path
|
||||
|> Pleroma.Config.get()
|
||||
|> Path.dirname()
|
||||
else
|
||||
"config"
|
||||
end
|
||||
|
||||
Path.join(path, "#{env}.exported_from_db.secret.exs")
|
||||
end
|
||||
|
||||
@spec migrate_to_db(Path.t() | nil) :: any()
|
||||
def migrate_to_db(file_path \\ nil) do
|
||||
if Pleroma.Config.get([:configurable_from_database]) do
|
||||
# TODO: add support for releases
|
||||
config_file = file_path || "config/#{Pleroma.Config.get(:env)}.secret.exs"
|
||||
user_config_file =
|
||||
if Pleroma.Config.get(:release),
|
||||
do: Pleroma.Config.get(:config_path),
|
||||
else: "config/#{Pleroma.Config.get(:env)}.secret.exs"
|
||||
|
||||
config_file = file_path || user_config_file
|
||||
do_migrate_to_db(config_file)
|
||||
else
|
||||
Mix.shell().info(
|
||||
"migration is not allowed by config. You can change this behavior in instance settings."
|
||||
shell_info(
|
||||
"Migration is not allowed by config. You can change this behavior in instance settings."
|
||||
)
|
||||
end
|
||||
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
|
||||
|
||||
|
|
@ -81,7 +98,7 @@ defmodule Mix.Tasks.Pleroma.Config do
|
|||
|> Keyword.keys()
|
||||
|> Enum.each(&create(&1, custom_config[&1]))
|
||||
else
|
||||
Logger.warn("to migrate settings, you must define custom settings in #{config_file}")
|
||||
shell_info("To migrate settings, you must define custom settings in #{config_file}.")
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -94,10 +111,10 @@ defmodule Mix.Tasks.Pleroma.Config do
|
|||
key = inspect(key)
|
||||
{:ok, _} = ConfigDB.update_or_create(%{group: inspect(group), key: key, value: value})
|
||||
|
||||
Mix.shell().info("settings for key #{key} migrated.")
|
||||
shell_info("Settings for key #{key} migrated.")
|
||||
end)
|
||||
|
||||
Mix.shell().info("settings for group :#{group} migrated.")
|
||||
shell_info("Settings for group :#{group} migrated.")
|
||||
end
|
||||
|
||||
defp write_to_file_with_deletion(config, file, with_deletion) do
|
||||
|
|
@ -110,7 +127,7 @@ defmodule Mix.Tasks.Pleroma.Config do
|
|||
|
||||
if with_deletion do
|
||||
{:ok, _} = Repo.delete(config)
|
||||
Mix.shell().info("#{config.key} deleted from DB.")
|
||||
shell_info("#{config.key} deleted from DB.")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,36 +1,5 @@
|
|||
defmodule Pleroma.Config.Loader do
|
||||
# TODO: add support for releases
|
||||
if Code.ensure_loaded?(Config.Reader) do
|
||||
@spec load() :: map()
|
||||
def load do
|
||||
config = load("config/config.exs")
|
||||
env_config = load("config/#{Mix.env()}.exs")
|
||||
|
||||
Config.Reader.merge(config, env_config)
|
||||
end
|
||||
|
||||
@spec load(Path.t()) :: keyword()
|
||||
def load(path), do: Config.Reader.read!(path)
|
||||
else
|
||||
# support for Elixir less than 1.9
|
||||
@spec load() :: map()
|
||||
def load do
|
||||
config = load("config/config.exs")
|
||||
env_config = load("config/#{Mix.env()}.exs")
|
||||
|
||||
Mix.Config.merge(config, env_config)
|
||||
end
|
||||
|
||||
@spec load(Path.t()) :: keyword()
|
||||
def load(path) do
|
||||
{config, _paths} = Mix.Config.eval!(path)
|
||||
config
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defmodule Pleroma.Config.Holder do
|
||||
@config Pleroma.Config.Loader.load()
|
||||
@config Pleroma.Config.Loader.load_and_merge()
|
||||
|
||||
@spec config() :: keyword()
|
||||
def config do
|
||||
|
|
|
|||
39
lib/pleroma/config/loader.ex
Normal file
39
lib/pleroma/config/loader.ex
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
defmodule Pleroma.Config.Loader do
|
||||
@paths ["config/config.exs", "config/#{Mix.env()}.exs"]
|
||||
|
||||
if Code.ensure_loaded?(Config.Reader) do
|
||||
@spec load(Path.t()) :: keyword()
|
||||
def load(path), do: Config.Reader.read!(path)
|
||||
|
||||
defp do_merge(conf1, conf2), do: Config.Reader.merge(conf1, conf2)
|
||||
else
|
||||
# support for Elixir less than 1.9
|
||||
@spec load(Path.t()) :: keyword()
|
||||
def load(path) do
|
||||
{config, _paths} = Mix.Config.eval!(path)
|
||||
config
|
||||
end
|
||||
|
||||
defp do_merge(conf1, conf2), do: Mix.Config.merge(conf1, conf2)
|
||||
end
|
||||
|
||||
@spec load_and_merge() :: keyword()
|
||||
def load_and_merge do
|
||||
all_paths =
|
||||
if Pleroma.Config.get(:release),
|
||||
do: @paths ++ ["config/releases.exs"],
|
||||
else: @paths
|
||||
|
||||
all_paths
|
||||
|> Enum.map(&load(&1))
|
||||
|> merge()
|
||||
end
|
||||
|
||||
@spec merge([keyword()], keyword()) :: keyword()
|
||||
def merge(configs, acc \\ [])
|
||||
def merge([], acc), do: acc
|
||||
|
||||
def merge([config | others], acc) do
|
||||
merge(others, do_merge(acc, config))
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue