spc-pleroma/lib/mix/tasks/pleroma/release_env.ex

77 lines
1.8 KiB
Elixir
Raw Normal View History

2020-07-14 04:31:21 +00:00
# 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
]
)
2020-07-16 05:52:14 +00:00
file_path =
2020-07-14 04:31:21 +00:00
get_option(
options,
:path,
"Environment file path",
2020-07-16 05:52:14 +00:00
"./config/pleroma.env"
2020-07-14 04:31:21 +00:00
)
2020-07-16 05:52:14 +00:00
env_path = Path.expand(file_path)
2020-07-14 04:31:21 +00:00
proceed? =
if File.exists?(env_path) do
get_option(
options,
:force,
2020-07-16 13:30:28 +00:00
"Environment file already exists. Do you want to overwrite the #{env_path} file? (y/n)",
2020-07-14 04:31:21 +00:00
"n"
) === "y"
else
true
end
if proceed? do
2020-07-16 05:52:14 +00:00
case do_generate(env_path) do
{:error, reason} ->
shell_error(
File.Error.message(%{action: "write to file", reason: reason, path: env_path})
)
2020-07-14 04:31:21 +00:00
2020-07-16 05:52:14 +00:00
_ ->
shell_info("\nThe file generated: #{env_path}.\n")
shell_info("""
2020-07-16 13:30:17 +00:00
WARNING: before start pleroma app please make sure to make the file read-only and non-modifiable.
2020-07-16 05:52:14 +00:00
Example:
chmod 0444 #{file_path}
chattr +i #{file_path}
""")
end
else
shell_info("\nThe file is exist. #{env_path}.\n")
2020-07-14 04:31:21 +00:00
end
end
def do_generate(path) do
content = "RELEASE_COOKIE=#{Base.encode32(:crypto.strong_rand_bytes(32))}"
File.mkdir_p!(Path.dirname(path))
2020-07-16 05:52:14 +00:00
File.write(path, content)
2020-07-14 04:31:21 +00:00
end
end