9da74ffcf3d80be602f6a50ed3a4982808e0159d
[akkoma] / lib / mix / tasks / pleroma / release_env.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Mix.Tasks.Pleroma.ReleaseEnv do
6 use Mix.Task
7 import Mix.Pleroma
8
9 @shortdoc "Generate Pleroma environment file."
10 @moduledoc File.read!("docs/administration/CLI_tasks/release_environments.md")
11
12 def run(["gen" | rest]) do
13 {options, [], []} =
14 OptionParser.parse(
15 rest,
16 strict: [
17 force: :boolean,
18 path: :string
19 ],
20 aliases: [
21 p: :path,
22 f: :force
23 ]
24 )
25
26 file_path =
27 get_option(
28 options,
29 :path,
30 "Environment file path",
31 "./config/pleroma.env"
32 )
33
34 env_path = Path.expand(file_path)
35
36 proceed? =
37 if File.exists?(env_path) do
38 get_option(
39 options,
40 :force,
41 "Environment file already exists. Do you want to overwrite the #{env_path} file? (y/n)",
42 "n"
43 ) === "y"
44 else
45 true
46 end
47
48 if proceed? do
49 case do_generate(env_path) do
50 {:error, reason} ->
51 shell_error(
52 File.Error.message(%{action: "write to file", reason: reason, path: env_path})
53 )
54
55 _ ->
56 shell_info("\nThe file generated: #{env_path}.\n")
57
58 shell_info("""
59 WARNING: before start pleroma app please make sure to make the file read-only and non-modifiable.
60 Example:
61 chmod 0444 #{file_path}
62 chattr +i #{file_path}
63 """)
64 end
65 else
66 shell_info("\nThe file is exist. #{env_path}.\n")
67 end
68 end
69
70 def do_generate(path) do
71 content = "RELEASE_COOKIE=#{Base.encode32(:crypto.strong_rand_bytes(32))}"
72
73 File.mkdir_p!(Path.dirname(path))
74 File.write(path, content)
75 end
76 end