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