Merge branch 'develop' into admin-be
[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 @shortdoc "Manages the location of the config"
14 @moduledoc File.read!("docs/administration/CLI_tasks/config.md")
15
16 def run(["migrate_to_db"]) do
17 start_pleroma()
18 migrate_to_db()
19 end
20
21 def run(["migrate_from_db" | options]) do
22 # TODO: add support for releases
23 start_pleroma()
24
25 {opts, _} =
26 OptionParser.parse!(options,
27 strict: [env: :string, delete_from_db: :boolean],
28 aliases: [d: :delete_from_db]
29 )
30
31 with {:active?, true} <-
32 {:active?, Pleroma.Config.get([:configurable_from_database])},
33 env when is_binary(env) <- opts[:env] || "prod",
34 config_path <- config_path(env),
35 {:ok, file} <- File.open(config_path, [:write, :utf8]) do
36 IO.write(file, config_header())
37
38 ConfigDB
39 |> Repo.all()
40 |> Enum.each(&write_to_file_with_deletion(&1, file, opts[:delete_from_db]))
41
42 File.close(file)
43 System.cmd("mix", ["format", config_path])
44 else
45 {:active?, false} ->
46 shell_info(
47 "Migration is not allowed by config. You can change this behavior in instance settings."
48 )
49
50 error ->
51 shell_info("Error occuried while opening file. #{inspect(error)}")
52 end
53 end
54
55 defp config_path(env) do
56 path =
57 if Pleroma.Config.get(:release) do
58 :config_path
59 |> Pleroma.Config.get()
60 |> Path.dirname()
61 else
62 "config"
63 end
64
65 Path.join(path, "#{env}.exported_from_db.secret.exs")
66 end
67
68 @spec migrate_to_db(Path.t() | nil) :: any()
69 def migrate_to_db(file_path \\ nil) do
70 if Pleroma.Config.get([:configurable_from_database]) do
71 user_config_file =
72 if Pleroma.Config.get(:release),
73 do: Pleroma.Config.get(:config_path),
74 else: "config/#{Pleroma.Config.get(:env)}.secret.exs"
75
76 config_file = file_path || user_config_file
77 do_migrate_to_db(config_file)
78 else
79 shell_info(
80 "Migration is not allowed by config. You can change this behavior in instance settings."
81 )
82 end
83 end
84
85 if Code.ensure_loaded?(Config.Reader) do
86 defp config_header, do: "import Config\r\n\r\n"
87 defp read_file(config_file), do: Config.Reader.read_imports!(config_file)
88 else
89 defp config_header, do: "use Mix.Config\r\n\r\n"
90 defp read_file(config_file), do: Mix.Config.eval!(config_file)
91 end
92
93 defp do_migrate_to_db(config_file) do
94 if File.exists?(config_file) do
95 {custom_config, _paths} = read_file(config_file)
96
97 custom_config
98 |> Keyword.keys()
99 |> Enum.each(&create(&1, custom_config[&1]))
100 else
101 shell_info("To migrate settings, you must define custom settings in #{config_file}.")
102 end
103 end
104
105 defp create(group, settings) do
106 Enum.reject(settings, fn {k, _v} ->
107 k in [Pleroma.Repo, Pleroma.Web.Endpoint, :env, :configurable_from_database] or
108 (group == :phoenix and k == :serve_endpoints)
109 end)
110 |> Enum.each(fn {key, value} ->
111 key = inspect(key)
112 {:ok, _} = ConfigDB.update_or_create(%{group: inspect(group), key: key, value: value})
113
114 shell_info("Settings for key #{key} migrated.")
115 end)
116
117 shell_info("Settings for group :#{group} migrated.")
118 end
119
120 defp write_to_file_with_deletion(config, file, with_deletion) do
121 IO.write(
122 file,
123 "config #{config.group}, #{config.key}, #{
124 inspect(ConfigDB.from_binary(config.value), limit: :infinity)
125 }\r\n\r\n"
126 )
127
128 if with_deletion do
129 {:ok, _} = Repo.delete(config)
130 shell_info("#{config.key} deleted from DB.")
131 end
132 end
133 end