Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(easy-install): read values from env if exists #1611

Merged
merged 3 commits into from
Dec 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 70 additions & 24 deletions easy-install.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import argparse
import base64
import fileinput
import logging
import os
import platform
import shutil
import subprocess
import sys
import time
Expand Down Expand Up @@ -73,17 +73,20 @@ def get_from_env(dir, file) -> Dict:


def write_to_env(
wd: str,
frappe_docker_dir: str,
out_file: str,
sites: List[str],
db_pass: str,
admin_pass: str,
email: str,
cronstring: str,
erpnext_version: str = None,
http_port: str = None,
custom_image: str = None,
custom_tag: str = None,
) -> None:
quoted_sites = ",".join([f"`{site}`" for site in sites]).strip(",")
example_env = get_from_env(wd, "example.env")
example_env = get_from_env(frappe_docker_dir, "example.env")
erpnext_version = erpnext_version or example_env["ERPNEXT_VERSION"]
env_file_lines = [
# defaults to latest version of ERPNext
Expand All @@ -98,13 +101,19 @@ def write_to_env(
f"SITE_ADMIN_PASS={admin_pass}\n",
f"SITES={quoted_sites}\n",
"PULL_POLICY=missing\n",
f'BACKUP_CRONSTRING="{cronstring}"',
f'BACKUP_CRONSTRING="{cronstring}"\n',
]

if http_port:
env_file_lines.append(f"HTTP_PUBLISH_PORT={http_port}\n")

with open(os.path.join(wd, ".env"), "w") as f:
if custom_image:
env_file_lines.append(f"CUSTOM_IMAGE={custom_image}\n")

if custom_tag:
env_file_lines.append(f"CUSTOM_TAG={custom_tag}\n")

with open(os.path.join(out_file), "w") as f:
f.writelines(env_file_lines)


Expand All @@ -119,8 +128,12 @@ def generate_pass(length: int = 12) -> str:
return secrets.token_hex(math.ceil(length / 2))[:length]


def get_frappe_docker_path():
return os.path.join(os.getcwd(), "frappe_docker")


def check_repo_exists() -> bool:
return os.path.exists(os.path.join(os.getcwd(), "frappe_docker"))
return os.path.exists(get_frappe_docker_path())


def start_prod(
Expand All @@ -141,52 +154,78 @@ def start_prod(
os.path.expanduser("~"),
f"{project}-compose.yml",
)
docker_repo_path = os.path.join(os.getcwd(), "frappe_docker")

env_file_dir = os.path.expanduser("~")
env_file_name = f"{project}.env"
env_file_path = os.path.join(
os.path.expanduser("~"),
env_file_name,
)

frappe_docker_dir = get_frappe_docker_path()

cprint(
"\nPlease refer to .example.env file in the frappe_docker folder to know which keys to set\n\n",
f"\nPlease refer to {env_file_path} to know which keys to set\n\n",
level=3,
)
admin_pass = ""
db_pass = ""
custom_image = None
custom_tag = None

if image:
custom_image = image
custom_tag = version

with open(compose_file_name, "w") as f:
# Writing to compose file
if not os.path.exists(os.path.join(docker_repo_path, ".env")):
if not os.path.exists(env_file_path):
admin_pass = generate_pass()
db_pass = generate_pass(9)
write_to_env(
wd=docker_repo_path,
frappe_docker_dir=frappe_docker_dir,
out_file=env_file_path,
sites=sites,
db_pass=db_pass,
admin_pass=admin_pass,
email=email,
cronstring=cronstring,
erpnext_version=version,
http_port=http_port if not is_https and http_port else None,
custom_image=custom_image,
custom_tag=custom_tag,
)
cprint(
"\nA .env file is generated with basic configs. Please edit it to fit to your needs \n",
level=3,
)
with open(
os.path.join(os.path.expanduser("~"), "passwords.txt"), "w"
os.path.join(os.path.expanduser("~"), f"{project}-passwords.txt"), "w"
) as en:
en.writelines(f"ADMINISTRATOR_PASSWORD={admin_pass}\n")
en.writelines(f"MARIADB_ROOT_PASSWORD={db_pass}\n")
else:
env = get_from_env(docker_repo_path, ".env")
env = get_from_env(env_file_dir, env_file_name)
sites = env["SITES"].replace("`", "").split(",") if env["SITES"] else []
db_pass = env["DB_PASSWORD"]
admin_pass = env["SITE_ADMIN_PASS"]
email = env["LETSENCRYPT_EMAIL"]
custom_image = env.get("CUSTOM_IMAGE")
custom_tag = env.get("CUSTOM_TAG")

version = env.get("ERPNEXT_VERSION", version)
write_to_env(
wd=docker_repo_path,
frappe_docker_dir=frappe_docker_dir,
out_file=env_file_path,
sites=sites,
db_pass=db_pass,
admin_pass=admin_pass,
email=email,
cronstring=cronstring,
erpnext_version=version,
http_port=http_port if not is_https and http_port else None,
custom_image=custom_image,
custom_tag=custom_tag,
)

try:
Expand All @@ -210,13 +249,13 @@ def start_prod(
"-f",
"overrides/compose.backup-cron.yaml",
"--env-file",
".env",
env_file_path,
"config",
]

subprocess.run(
command,
cwd=docker_repo_path,
cwd=frappe_docker_dir,
stdout=f,
check=True,
)
Expand All @@ -226,13 +265,6 @@ def start_prod(
cprint("\nGenerating Compose File failed\n")
sys.exit(1)

# Use custom image
if image:
for line in fileinput.input(compose_file_name, inplace=True):
if "image: frappe/erpnext" in line:
line = line.replace("image: frappe/erpnext", f"image: {image}")
sys.stdout.write(line)

try:
# Starting with generated compose file
command = [
Expand Down Expand Up @@ -299,7 +331,7 @@ def setup_prod(
)
passwords_file_path = os.path.join(
os.path.expanduser("~"),
"passwords.txt",
f"{project}-passwords.txt",
)
cprint(f"Passwords are stored in {passwords_file_path}", level=3)

Expand Down Expand Up @@ -341,7 +373,7 @@ def setup_dev_instance(project: str):
]
subprocess.run(
command,
cwd=os.path.join(os.getcwd(), "frappe_docker"),
cwd=get_frappe_docker_path(),
check=True,
)
cprint(
Expand Down Expand Up @@ -543,6 +575,12 @@ def add_common_parser(parser: argparse.ArgumentParser):
"--version",
help="ERPNext or image version to install, defaults to latest stable",
)
parser.add_argument(
"-l",
"--force-pull",
action="store_true",
help="Force pull frappe_docker",
)
return parser


Expand Down Expand Up @@ -733,6 +771,14 @@ def get_args_parser():

args = parser.parse_args()

if (
args.subcommand != "exec"
and args.force_pull
and os.path.exists(get_frappe_docker_path())
):
cprint("\nForce pull frappe_docker again\n", level=2)
shutil.rmtree(get_frappe_docker_path(), ignore_errors=True)

if args.subcommand == "build":
build_image(
push=args.push,
Expand Down
Loading