summaryrefslogtreecommitdiff
path: root/lib/mix/tasks/pleroma/release_env.ex
blob: 9da74ffcf3d80be602f6a50ed3a4982808e0159d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Mix.Tasks.Pleroma.ReleaseEnv do
  use Mix.Task
  import Mix.Pleroma

  @shortdoc "Generate Pleroma environment file."
  @moduledoc File.read!("docs/administration/CLI_tasks/release_environments.md")

  def run(["gen" | rest]) do
    {options, [], []} =
      OptionParser.parse(
        rest,
        strict: [
          force: :boolean,
          path: :string
        ],
        aliases: [
          p: :path,
          f: :force
        ]
      )

    file_path =
      get_option(
        options,
        :path,
        "Environment file path",
        "./config/pleroma.env"
      )

    env_path = Path.expand(file_path)

    proceed? =
      if File.exists?(env_path) do
        get_option(
          options,
          :force,
          "Environment file already exists. Do you want to overwrite the #{env_path} file? (y/n)",
          "n"
        ) === "y"
      else
        true
      end

    if proceed? do
      case do_generate(env_path) do
        {:error, reason} ->
          shell_error(
            File.Error.message(%{action: "write to file", reason: reason, path: env_path})
          )

        _ ->
          shell_info("\nThe file generated: #{env_path}.\n")

          shell_info("""
          WARNING: before start pleroma app please make sure to make the file read-only and non-modifiable.
            Example:
              chmod 0444 #{file_path}
              chattr +i #{file_path}
          """)
      end
    else
      shell_info("\nThe file is exist. #{env_path}.\n")
    end
  end

  def do_generate(path) do
    content = "RELEASE_COOKIE=#{Base.encode32(:crypto.strong_rand_bytes(32))}"

    File.mkdir_p!(Path.dirname(path))
    File.write(path, content)
  end
end