Skip to content
Draft
Show file tree
Hide file tree
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
60 changes: 60 additions & 0 deletions bench/tests/test_easy_install.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import importlib.util
import os
import tempfile
import unittest
from pathlib import Path


EASY_INSTALL_PATH = Path(__file__).resolve().parents[2] / "easy-install.py"
_spec = importlib.util.spec_from_file_location("easy_install", EASY_INSTALL_PATH)
easy_install = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(easy_install)


class TestEasyInstall(unittest.TestCase):
def _create_example_env(self, root: str):
with open(os.path.join(root, "example.env"), "w") as f:
f.write("ERPNEXT_VERSION=v16.0.0\n")

def test_write_to_env_sets_default_sites_rule(self):
with tempfile.TemporaryDirectory() as frappe_docker_dir, tempfile.NamedTemporaryFile() as out_file:
self._create_example_env(frappe_docker_dir)
easy_install.write_to_env(
frappe_docker_dir=frappe_docker_dir,
out_file=out_file.name,
sites=[],
db_pass="db_pass",
admin_pass="admin_pass",
email="test@example.com",
cronstring="@every 6h",
)

with open(out_file.name) as f:
env_data = f.read()

self.assertIn("SITES=`site1.localhost`", env_data)
self.assertIn("SITES_RULE=Host(`site1.localhost`)", env_data)

def test_write_to_env_preserves_provided_sites_rule(self):
with tempfile.TemporaryDirectory() as frappe_docker_dir, tempfile.NamedTemporaryFile() as out_file:
self._create_example_env(frappe_docker_dir)
custom_rule = "Host(`custom.localhost`)"
easy_install.write_to_env(
frappe_docker_dir=frappe_docker_dir,
out_file=out_file.name,
sites=["site1.localhost"],
db_pass="db_pass",
admin_pass="admin_pass",
email="test@example.com",
cronstring="@every 6h",
sites_rule=custom_rule,
)

with open(out_file.name) as f:
env_data = f.read()

self.assertIn(f"SITES_RULE={custom_rule}", env_data)


if __name__ == "__main__":
unittest.main()
6 changes: 6 additions & 0 deletions easy-install.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,11 @@ def write_to_env(
http_port: str = None,
custom_image: str = None,
custom_tag: str = None,
sites_rule: str = None,
) -> None:
sites = sites or ["site1.localhost"]
quoted_sites = ",".join([f"`{site}`" for site in sites]).strip(",")
sites_rule = sites_rule or " || ".join([f"Host(`{site}`)" for site in sites])
example_env = get_from_env(frappe_docker_dir, "example.env")
erpnext_version = erpnext_version or example_env["ERPNEXT_VERSION"]
env_file_lines = [
Expand All @@ -100,6 +103,7 @@ def write_to_env(
f"LETSENCRYPT_EMAIL={email}\n",
f"SITE_ADMIN_PASS={admin_pass}\n",
f"SITES={quoted_sites}\n",
f"SITES_RULE={sites_rule}\n",
"PULL_POLICY=missing\n",
f'BACKUP_CRONSTRING="{cronstring}"\n',
]
Expand Down Expand Up @@ -212,6 +216,7 @@ def start_prod(
email = env["LETSENCRYPT_EMAIL"]
custom_image = env.get("CUSTOM_IMAGE")
custom_tag = env.get("CUSTOM_TAG")
sites_rule = env.get("SITES_RULE")

version = env.get("ERPNEXT_VERSION", version)
write_to_env(
Expand All @@ -226,6 +231,7 @@ def start_prod(
http_port=http_port if not is_https and http_port else None,
custom_image=custom_image,
custom_tag=custom_tag,
sites_rule=sites_rule,
)

try:
Expand Down
Loading