2
0
mirror of https://github.com/frappe/bench.git synced 2025-01-06 23:44:03 +00:00

Merge pull request #1611 from revant/fix-1610

fix(easy-install): read values from env if exists
This commit is contained in:
Md Hussain Nagaria 2024-12-31 14:56:20 +05:30 committed by GitHub
commit 4c6ce6331d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,10 +2,10 @@
import argparse import argparse
import base64 import base64
import fileinput
import logging import logging
import os import os
import platform import platform
import shutil
import subprocess import subprocess
import sys import sys
import time import time
@ -73,7 +73,8 @@ def get_from_env(dir, file) -> Dict:
def write_to_env( def write_to_env(
wd: str, frappe_docker_dir: str,
out_file: str,
sites: List[str], sites: List[str],
db_pass: str, db_pass: str,
admin_pass: str, admin_pass: str,
@ -81,9 +82,11 @@ def write_to_env(
cronstring: str, cronstring: str,
erpnext_version: str = None, erpnext_version: str = None,
http_port: str = None, http_port: str = None,
custom_image: str = None,
custom_tag: str = None,
) -> None: ) -> None:
quoted_sites = ",".join([f"`{site}`" for site in sites]).strip(",") 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"] erpnext_version = erpnext_version or example_env["ERPNEXT_VERSION"]
env_file_lines = [ env_file_lines = [
# defaults to latest version of ERPNext # defaults to latest version of ERPNext
@ -98,13 +101,19 @@ def write_to_env(
f"SITE_ADMIN_PASS={admin_pass}\n", f"SITE_ADMIN_PASS={admin_pass}\n",
f"SITES={quoted_sites}\n", f"SITES={quoted_sites}\n",
"PULL_POLICY=missing\n", "PULL_POLICY=missing\n",
f'BACKUP_CRONSTRING="{cronstring}"', f'BACKUP_CRONSTRING="{cronstring}"\n',
] ]
if http_port: if http_port:
env_file_lines.append(f"HTTP_PUBLISH_PORT={http_port}\n") 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) f.writelines(env_file_lines)
@ -119,8 +128,12 @@ def generate_pass(length: int = 12) -> str:
return secrets.token_hex(math.ceil(length / 2))[:length] 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: 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( def start_prod(
@ -141,20 +154,37 @@ def start_prod(
os.path.expanduser("~"), os.path.expanduser("~"),
f"{project}-compose.yml", 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( 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, level=3,
) )
admin_pass = "" admin_pass = ""
db_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: with open(compose_file_name, "w") as f:
# Writing to compose file # 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() admin_pass = generate_pass()
db_pass = generate_pass(9) db_pass = generate_pass(9)
write_to_env( write_to_env(
wd=docker_repo_path, frappe_docker_dir=frappe_docker_dir,
out_file=env_file_path,
sites=sites, sites=sites,
db_pass=db_pass, db_pass=db_pass,
admin_pass=admin_pass, admin_pass=admin_pass,
@ -162,24 +192,31 @@ def start_prod(
cronstring=cronstring, cronstring=cronstring,
erpnext_version=version, erpnext_version=version,
http_port=http_port if not is_https and http_port else None, http_port=http_port if not is_https and http_port else None,
custom_image=custom_image,
custom_tag=custom_tag,
) )
cprint( cprint(
"\nA .env file is generated with basic configs. Please edit it to fit to your needs \n", "\nA .env file is generated with basic configs. Please edit it to fit to your needs \n",
level=3, level=3,
) )
with open( with open(
os.path.join(os.path.expanduser("~"), "passwords.txt"), "w" os.path.join(os.path.expanduser("~"), f"{project}-passwords.txt"), "w"
) as en: ) as en:
en.writelines(f"ADMINISTRATOR_PASSWORD={admin_pass}\n") en.writelines(f"ADMINISTRATOR_PASSWORD={admin_pass}\n")
en.writelines(f"MARIADB_ROOT_PASSWORD={db_pass}\n") en.writelines(f"MARIADB_ROOT_PASSWORD={db_pass}\n")
else: 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 [] sites = env["SITES"].replace("`", "").split(",") if env["SITES"] else []
db_pass = env["DB_PASSWORD"] db_pass = env["DB_PASSWORD"]
admin_pass = env["SITE_ADMIN_PASS"] admin_pass = env["SITE_ADMIN_PASS"]
email = env["LETSENCRYPT_EMAIL"] 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( write_to_env(
wd=docker_repo_path, frappe_docker_dir=frappe_docker_dir,
out_file=env_file_path,
sites=sites, sites=sites,
db_pass=db_pass, db_pass=db_pass,
admin_pass=admin_pass, admin_pass=admin_pass,
@ -187,6 +224,8 @@ def start_prod(
cronstring=cronstring, cronstring=cronstring,
erpnext_version=version, erpnext_version=version,
http_port=http_port if not is_https and http_port else None, http_port=http_port if not is_https and http_port else None,
custom_image=custom_image,
custom_tag=custom_tag,
) )
try: try:
@ -210,13 +249,13 @@ def start_prod(
"-f", "-f",
"overrides/compose.backup-cron.yaml", "overrides/compose.backup-cron.yaml",
"--env-file", "--env-file",
".env", env_file_path,
"config", "config",
] ]
subprocess.run( subprocess.run(
command, command,
cwd=docker_repo_path, cwd=frappe_docker_dir,
stdout=f, stdout=f,
check=True, check=True,
) )
@ -226,13 +265,6 @@ def start_prod(
cprint("\nGenerating Compose File failed\n") cprint("\nGenerating Compose File failed\n")
sys.exit(1) 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: try:
# Starting with generated compose file # Starting with generated compose file
command = [ command = [
@ -299,7 +331,7 @@ def setup_prod(
) )
passwords_file_path = os.path.join( passwords_file_path = os.path.join(
os.path.expanduser("~"), os.path.expanduser("~"),
"passwords.txt", f"{project}-passwords.txt",
) )
cprint(f"Passwords are stored in {passwords_file_path}", level=3) cprint(f"Passwords are stored in {passwords_file_path}", level=3)
@ -341,7 +373,7 @@ def setup_dev_instance(project: str):
] ]
subprocess.run( subprocess.run(
command, command,
cwd=os.path.join(os.getcwd(), "frappe_docker"), cwd=get_frappe_docker_path(),
check=True, check=True,
) )
cprint( cprint(
@ -543,6 +575,12 @@ def add_common_parser(parser: argparse.ArgumentParser):
"--version", "--version",
help="ERPNext or image version to install, defaults to latest stable", 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 return parser
@ -733,6 +771,14 @@ if __name__ == "__main__":
args = parser.parse_args() 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": if args.subcommand == "build":
build_image( build_image(
push=args.push, push=args.push,