don't migrate prometheus settings
[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 import Mix.Pleroma
8 alias Pleroma.Repo
9 alias Pleroma.Web.AdminAPI.Config
10 @shortdoc "Manages the location of the config"
11 @moduledoc File.read!("docs/administration/CLI_tasks/config.md")
12
13 @groups [
14 :pleroma,
15 :logger,
16 :quack,
17 :mime,
18 :tesla,
19 :phoenix,
20 :cors_plug,
21 :auto_linker,
22 :esshd,
23 :ueberauth,
24 :http_signatures,
25 :web_push_encryption,
26 :joken
27 ]
28
29 def run(["migrate_to_db"]) do
30 start_pleroma()
31
32 if Pleroma.Config.get([:configurable_from_database]) do
33 Enum.each(@groups, &load_and_create(&1))
34 else
35 Mix.shell().info(
36 "Migration is not allowed by config. You can change this behavior in instance settings."
37 )
38 end
39 end
40
41 def run(["migrate_from_db" | options]) do
42 start_pleroma()
43
44 {opts, _} =
45 OptionParser.parse!(options,
46 strict: [env: :string, delete_from_db: :boolean],
47 aliases: [d: :delete_from_db]
48 )
49
50 with {:active?, true} <-
51 {:active?, Pleroma.Config.get([:configurable_from_database])},
52 env_path when is_binary(env_path) <- opts[:env],
53 config_path <- "config/#{env_path}.exported_from_db.secret.exs",
54 {:ok, file} <- File.open(config_path, [:write, :utf8]) do
55 IO.write(file, "use Mix.Config\r\n")
56
57 Config
58 |> Repo.all()
59 |> Enum.each(&write_to_file_with_deletion(&1, file, opts[:delete_from_db]))
60
61 File.close(file)
62 System.cmd("mix", ["format", config_path])
63 else
64 {:active?, false} ->
65 Mix.shell().info(
66 "migration is not allowed by config. You can change this behavior in instance settings."
67 )
68
69 error ->
70 Mix.shell().info("error occuried while opening file. #{inspect(error)}")
71 end
72 end
73
74 defp load_and_create(group) do
75 group
76 |> Application.get_all_env()
77 |> Enum.reject(fn {k, _v} ->
78 k in [Pleroma.Repo, :env, :configurable_from_database] or
79 (group == :phoenix and k == :serve_endpoints)
80 end)
81 |> Enum.each(fn {key, value} ->
82 key = inspect(key)
83 {:ok, _} = Config.update_or_create(%{group: inspect(group), key: key, value: value})
84
85 Mix.shell().info("settings for key #{key} migrated.")
86 end)
87
88 Mix.shell().info("settings for group :#{group} migrated.")
89 end
90
91 defp write_to_file_with_deletion(config, file, with_deletion) do
92 IO.write(
93 file,
94 "config #{config.group}, #{config.key}, #{
95 inspect(Config.from_binary(config.value), limit: :infinity)
96 }\r\n\r\n"
97 )
98
99 if with_deletion do
100 {:ok, _} = Repo.delete(config)
101 Mix.shell().info("#{config.key} deleted from DB.")
102 end
103 end
104 end