migrating config to tmp folder

This commit is contained in:
Alexander Strizhakov 2021-03-23 14:23:37 +03:00
parent 572363793f
commit 03843a5386
No known key found for this signature in database
GPG key ID: 022896A53AEF1381
3 changed files with 80 additions and 17 deletions

View file

@ -32,16 +32,20 @@
config :pleroma, configurable_from_database: false config :pleroma, configurable_from_database: false
``` ```
To delete transferred settings from database optional flag `-d` can be used. `<env>` is `prod` by default. Options:
- `<path>` - where to save migrated config. E.g. `--path=/tmp`. If file saved into non standart folder, you must manually copy file into directory where Pleroma can read it. For OTP install path will be `PLEROMA_CONFIG_PATH` or `/etc/pleroma`. For installation from source - `config` directory in the pleroma folder.
- `<env>` - environment, for which is migrated config. By default is `prod`.
- To delete transferred settings from database optional flag `-d` can be used
=== "OTP" === "OTP"
```sh ```sh
./bin/pleroma_ctl config migrate_from_db [--env=<env>] [-d] ./bin/pleroma_ctl config migrate_from_db [--env=<env>] [-d] [--path=<path>]
``` ```
=== "From Source" === "From Source"
```sh ```sh
mix pleroma.config migrate_from_db [--env=<env>] [-d] mix pleroma.config migrate_from_db [--env=<env>] [-d] [--path=<path>]
``` ```
## Dump all of the config settings defined in the database ## Dump all of the config settings defined in the database

View file

@ -27,7 +27,7 @@ def run(["migrate_from_db" | options]) do
{opts, _} = {opts, _} =
OptionParser.parse!(options, OptionParser.parse!(options,
strict: [env: :string, delete: :boolean], strict: [env: :string, delete: :boolean, path: :string],
aliases: [d: :delete] aliases: [d: :delete]
) )
@ -259,18 +259,43 @@ defp create(group, settings) do
defp migrate_from_db(opts) do defp migrate_from_db(opts) do
env = opts[:env] || Pleroma.Config.get(:env) env = opts[:env] || Pleroma.Config.get(:env)
filename = "#{env}.exported_from_db.secret.exs"
config_path = config_path =
if Pleroma.Config.get(:release) do cond do
:config_path opts[:path] ->
|> Pleroma.Config.get() opts[:path]
|> Path.dirname()
else Pleroma.Config.get(:release) ->
"config" :config_path
|> Pleroma.Config.get()
|> Path.dirname()
true ->
"config"
end end
|> Path.join("#{env}.exported_from_db.secret.exs") |> Path.join(filename)
file = File.open!(config_path, [:write, :utf8]) with {:ok, file} <- File.open(config_path, [:write, :utf8]) do
write_config(file, config_path, opts)
shell_info("Database configuration settings have been exported to #{config_path}")
else
_ ->
shell_error("Impossible to save settings to this directory #{Path.dirname(config_path)}")
tmp_config_path = Path.join("/tmp", filename)
file = File.open!(tmp_config_path)
shell_info(
"Saving database configuration settings to #{tmp_config_path}. Copy it to the #{
Path.dirname(config_path)
} manually."
)
write_config(file, tmp_config_path, opts)
end
end
defp write_config(file, path, opts) do
IO.write(file, config_header()) IO.write(file, config_header())
ConfigDB ConfigDB
@ -278,11 +303,7 @@ defp migrate_from_db(opts) do
|> Enum.each(&write_and_delete(&1, file, opts[:delete])) |> Enum.each(&write_and_delete(&1, file, opts[:delete]))
:ok = File.close(file) :ok = File.close(file)
System.cmd("mix", ["format", config_path]) System.cmd("mix", ["format", path])
shell_info(
"Database configuration settings have been exported to config/#{env}.exported_from_db.secret.exs"
)
end end
if Code.ensure_loaded?(Config.Reader) do if Code.ensure_loaded?(Config.Reader) do

View file

@ -200,6 +200,44 @@ test "load a settings with large values and pass to file", %{temp_file: temp_fil
end end
end end
describe "migrate_from_db/1" do
setup do: clear_config(:configurable_from_database, true)
setup do
insert_config_record(:pleroma, :setting_first, key: "value", key2: ["Activity"])
insert_config_record(:pleroma, :setting_second, key: "value2", key2: [Repo])
insert_config_record(:quack, :level, :info)
path = "test/instance_static"
file_path = Path.join(path, "temp.exported_from_db.secret.exs")
on_exit(fn -> File.rm!(file_path) end)
[file_path: file_path]
end
test "with path parameter", %{file_path: file_path} do
MixTask.run(["migrate_from_db", "--env", "temp", "--path", Path.dirname(file_path)])
file = File.read!(file_path)
assert file =~ "config :pleroma, :setting_first,"
assert file =~ "config :pleroma, :setting_second,"
assert file =~ "config :quack, :level, :info"
end
test "release", %{file_path: file_path} do
clear_config(:release, true)
clear_config(:config_path, file_path)
MixTask.run(["migrate_from_db", "--env", "temp"])
file = File.read!(file_path)
assert file =~ "config :pleroma, :setting_first,"
assert file =~ "config :pleroma, :setting_second,"
assert file =~ "config :quack, :level, :info"
end
end
describe "operations on database config" do describe "operations on database config" do
setup do: clear_config(:configurable_from_database, true) setup do: clear_config(:configurable_from_database, true)