2019-07-09 23:13:23 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-06-14 09:45:05 -06:00
|
|
|
defmodule Mix.Tasks.Pleroma.Config do
|
|
|
|
use Mix.Task
|
2019-06-19 17:05:19 -06:00
|
|
|
import Mix.Pleroma
|
2019-06-14 09:45:05 -06:00
|
|
|
alias Pleroma.Repo
|
|
|
|
alias Pleroma.Web.AdminAPI.Config
|
|
|
|
@shortdoc "Manages the location of the config"
|
|
|
|
@moduledoc """
|
|
|
|
Manages the location of the config.
|
|
|
|
|
|
|
|
## Transfers config from file to DB.
|
|
|
|
|
|
|
|
mix pleroma.config migrate_to_db
|
|
|
|
|
2019-07-30 10:36:05 -06:00
|
|
|
## Transfers config from DB to file `config/env.exported_from_db.secret.exs`
|
2019-06-14 09:45:05 -06:00
|
|
|
|
|
|
|
mix pleroma.config migrate_from_db ENV
|
|
|
|
"""
|
|
|
|
|
|
|
|
def run(["migrate_to_db"]) do
|
2019-06-19 17:05:19 -06:00
|
|
|
start_pleroma()
|
2019-06-14 09:45:05 -06:00
|
|
|
|
|
|
|
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.", "")
|
2019-07-15 06:45:27 -06:00
|
|
|
|
|
|
|
key =
|
|
|
|
if String.starts_with?(key, "Pleroma.") do
|
|
|
|
key
|
|
|
|
else
|
|
|
|
":" <> key
|
|
|
|
end
|
|
|
|
|
2019-06-22 23:16:16 -06:00
|
|
|
{:ok, _} = Config.update_or_create(%{group: "pleroma", key: key, value: v})
|
2019-06-14 09:45:05 -06:00
|
|
|
Mix.shell().info("#{key} is migrated.")
|
|
|
|
end)
|
|
|
|
|
|
|
|
Mix.shell().info("Settings migrated.")
|
|
|
|
else
|
|
|
|
Mix.shell().info(
|
|
|
|
"Migration is not allowed by config. You can change this behavior in instance settings."
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-26 22:19:44 -06:00
|
|
|
def run(["migrate_from_db", env, delete?]) do
|
2019-06-19 17:05:19 -06:00
|
|
|
start_pleroma()
|
2019-06-14 09:45:05 -06:00
|
|
|
|
2019-06-26 22:19:44 -06:00
|
|
|
delete? = if delete? == "true", do: true, else: false
|
|
|
|
|
2019-06-14 09:45:05 -06:00
|
|
|
if Pleroma.Config.get([:instance, :dynamic_configuration]) do
|
2019-06-20 11:43:57 -06:00
|
|
|
config_path = "config/#{env}.exported_from_db.secret.exs"
|
2019-06-14 09:45:05 -06:00
|
|
|
|
|
|
|
{:ok, file} = File.open(config_path, [:write])
|
2019-06-20 11:43:57 -06:00
|
|
|
IO.write(file, "use Mix.Config\r\n")
|
2019-06-14 09:45:05 -06:00
|
|
|
|
|
|
|
Repo.all(Config)
|
|
|
|
|> Enum.each(fn config ->
|
|
|
|
IO.write(
|
|
|
|
file,
|
2019-07-15 06:45:27 -06:00
|
|
|
"config :#{config.group}, #{config.key}, #{inspect(Config.from_binary(config.value))}\r\n\r\n"
|
2019-06-14 09:45:05 -06:00
|
|
|
)
|
|
|
|
|
2019-06-26 22:19:44 -06:00
|
|
|
if delete? do
|
|
|
|
{:ok, _} = Repo.delete(config)
|
|
|
|
Mix.shell().info("#{config.key} deleted from DB.")
|
|
|
|
end
|
2019-06-14 09:45:05 -06:00
|
|
|
end)
|
|
|
|
|
|
|
|
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."
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|