revert non needable
[akkoma] / lib / mix / tasks / pleroma / config.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Mix.Tasks.Pleroma.Config do
6 use Mix.Task
7
8 import Mix.Pleroma
9
10 alias Pleroma.ConfigDB
11 alias Pleroma.Repo
12
13 require Logger
14
15 @shortdoc "Manages the location of the config"
16 @moduledoc File.read!("docs/administration/CLI_tasks/config.md")
17
18 def run(["migrate_to_db"]) do
19 # we want to save original logger level
20 start_pleroma()
21 migrate_to_db()
22 end
23
24 def run(["migrate_from_db" | options]) do
25 # TODO: add support for releases
26 start_pleroma()
27
28 {opts, _} =
29 OptionParser.parse!(options,
30 strict: [env: :string, delete_from_db: :boolean],
31 aliases: [d: :delete_from_db]
32 )
33
34 with {:active?, true} <-
35 {:active?, Pleroma.Config.get([:configurable_from_database])},
36 env_path when is_binary(env_path) <- opts[:env],
37 config_path <- "config/#{env_path}.exported_from_db.secret.exs",
38 {:ok, file} <- File.open(config_path, [:write, :utf8]) do
39 IO.write(file, "use Mix.Config\r\n")
40
41 ConfigDB
42 |> Repo.all()
43 |> Enum.each(&write_to_file_with_deletion(&1, file, opts[:delete_from_db]))
44
45 File.close(file)
46 System.cmd("mix", ["format", config_path])
47 else
48 {:active?, false} ->
49 Mix.shell().info(
50 "migration is not allowed by config. You can change this behavior in instance settings."
51 )
52
53 error ->
54 Mix.shell().info("error occuried while opening file. #{inspect(error)}")
55 end
56 end
57
58 @spec migrate_to_db(Path.t() | nil) :: any()
59 def migrate_to_db(file_path \\ nil) do
60 if Pleroma.Config.get([:configurable_from_database]) do
61 # TODO: add support for releases
62 config_file = file_path || "config/#{Pleroma.Config.get(:env)}.secret.exs"
63 do_migrate_to_db(config_file)
64 else
65 Mix.shell().info(
66 "migration is not allowed by config. You can change this behavior in instance settings."
67 )
68 end
69 end
70
71 if Code.ensure_loaded?(Config.Reader) do
72 defp read_file(config_file), do: Config.Reader.read_imports!(config_file)
73 else
74 defp read_file(config_file), do: Mix.Config.eval!(config_file)
75 end
76
77 defp do_migrate_to_db(config_file) do
78 if File.exists?(config_file) do
79 {custom_config, _paths} = read_file(config_file)
80
81 custom_config
82 |> Keyword.keys()
83 |> Enum.each(&create(&1, custom_config[&1]))
84 else
85 Logger.warn("to migrate settings, you must define custom settings in #{config_file}")
86 end
87 end
88
89 defp create(group, settings) do
90 Enum.reject(settings, fn {k, _v} ->
91 k in [Pleroma.Repo, Pleroma.Web.Endpoint, :env, :configurable_from_database] or
92 (group == :phoenix and k == :serve_endpoints)
93 end)
94 |> Enum.each(fn {key, value} ->
95 key = inspect(key)
96 {:ok, _} = ConfigDB.update_or_create(%{group: inspect(group), key: key, value: value})
97
98 Mix.shell().info("settings for key #{key} migrated.")
99 end)
100
101 Mix.shell().info("settings for group :#{group} migrated.")
102 end
103
104 defp write_to_file_with_deletion(config, file, with_deletion) do
105 IO.write(
106 file,
107 "config #{config.group}, #{config.key}, #{
108 inspect(ConfigDB.from_binary(config.value), limit: :infinity)
109 }\r\n\r\n"
110 )
111
112 if with_deletion do
113 {:ok, _} = Repo.delete(config)
114 Mix.shell().info("#{config.key} deleted from DB.")
115 end
116 end
117 end