diff --git a/press/press/doctype/analytics_server/analytics_server.py b/press/press/doctype/analytics_server/analytics_server.py index b4bc1c93d60..5e438bb671f 100644 --- a/press/press/doctype/analytics_server/analytics_server.py +++ b/press/press/doctype/analytics_server/analytics_server.py @@ -59,10 +59,7 @@ def validate_plausible_password(self): def _setup_server(self): agent_repository_url = self.get_agent_repository_url() - certificate_name = frappe.db.get_value( - "TLS Certificate", {"wildcard": True, "domain": self.domain}, "name" - ) - certificate = frappe.get_doc("TLS Certificate", certificate_name) + certificate = self.get_certificate() log_server = frappe.db.get_single_value("Press Settings", "log_server") if log_server: diff --git a/press/press/doctype/database_server/database_server.py b/press/press/doctype/database_server/database_server.py index 77e0b1c1eb9..ece8aff2fa0 100644 --- a/press/press/doctype/database_server/database_server.py +++ b/press/press/doctype/database_server/database_server.py @@ -393,11 +393,7 @@ def _setup_server(self): self.save() def _get_config(self): - certificate_name = frappe.db.get_value( - "TLS Certificate", {"wildcard": True, "domain": self.domain}, "name" - ) - certificate = frappe.get_doc("TLS Certificate", certificate_name) - + certificate = self.get_certificate() log_server = frappe.db.get_single_value("Press Settings", "log_server") if log_server: kibana_password = frappe.get_doc("Log Server", log_server).get_password( diff --git a/press/press/doctype/log_server/log_server.py b/press/press/doctype/log_server/log_server.py index afd963fa520..13b1b4c703c 100644 --- a/press/press/doctype/log_server/log_server.py +++ b/press/press/doctype/log_server/log_server.py @@ -54,10 +54,8 @@ def _setup_server(self): agent_repository_url = self.get_agent_repository_url() kibana_password = self.get_password("kibana_password") monitoring_password = self.get_password("monitoring_password") - certificate_name = frappe.db.get_value( - "TLS Certificate", {"wildcard": True, "domain": self.domain}, "name" - ) - certificate = frappe.get_doc("TLS Certificate", certificate_name) + certificate = self.get_certificate() + try: ansible = Ansible( playbook="log.yml", diff --git a/press/press/doctype/monitor_server/monitor_server.py b/press/press/doctype/monitor_server/monitor_server.py index 070e32c3302..69927bb7d3a 100644 --- a/press/press/doctype/monitor_server/monitor_server.py +++ b/press/press/doctype/monitor_server/monitor_server.py @@ -58,10 +58,7 @@ def _setup_server(self): agent_password = self.get_password("agent_password") agent_repository_url = self.get_agent_repository_url() monitoring_password = self.get_password("monitoring_password") - certificate_name = frappe.db.get_value( - "TLS Certificate", {"wildcard": True, "domain": self.domain}, "name" - ) - certificate = frappe.get_doc("TLS Certificate", certificate_name) + certificate = self.get_certificate() registries = [] for registry in frappe.get_all("Registry Server"): diff --git a/press/press/doctype/proxy_server/proxy_server.py b/press/press/doctype/proxy_server/proxy_server.py index 58fec53db86..da2c4ce2c81 100644 --- a/press/press/doctype/proxy_server/proxy_server.py +++ b/press/press/doctype/proxy_server/proxy_server.py @@ -103,10 +103,8 @@ def get_wildcard_domains(self): if domain.domain == self.domain: # self.domain certs are symlinks continue - certificate_name = frappe.db.get_value( - "TLS Certificate", {"wildcard": True, "domain": domain.domain}, "name" - ) - certificate = frappe.get_doc("TLS Certificate", certificate_name) + + certificate = self.get_certificate() wildcard_domains.append( { "domain": domain.domain, diff --git a/press/press/doctype/registry_server/registry_server.py b/press/press/doctype/registry_server/registry_server.py index e427fadd7df..13bc5f4fb25 100644 --- a/press/press/doctype/registry_server/registry_server.py +++ b/press/press/doctype/registry_server/registry_server.py @@ -61,10 +61,8 @@ def _setup_server(self): agent_password = self.get_password("agent_password") agent_repository_url = self.get_agent_repository_url() monitoring_password = self.get_password("monitoring_password") - certificate_name = frappe.db.get_value( - "TLS Certificate", {"wildcard": True, "domain": self.domain}, "name" - ) - certificate = frappe.get_doc("TLS Certificate", certificate_name) + certificate = self.get_certificate() + try: ansible = Ansible( playbook="registry.yml", diff --git a/press/press/doctype/server/server.py b/press/press/doctype/server/server.py index 2a548df6dc4..6d1cb6a1d7c 100644 --- a/press/press/doctype/server/server.py +++ b/press/press/doctype/server/server.py @@ -779,29 +779,6 @@ def run_press_job(self, job_name, arguments=None): } ).insert() - def get_certificate(self): - certificate_name = frappe.db.get_value( - "TLS Certificate", {"wildcard": True, "domain": self.domain}, "name" - ) - - if not certificate_name and self.is_self_hosted: - certificate_name = frappe.db.get_value( - "TLS Certificate", {"domain": f"{self.name}"}, "name" - ) - - if not certificate_name: - self_hosted_server = frappe.db.get_value( - "Self Hosted Server", {"server": self.name}, ["hostname", "domain"], as_dict=1 - ) - - certificate_name = frappe.db.get_value( - "TLS Certificate", - {"domain": f"{self_hosted_server.hostname}.{self_hosted_server.domain}"}, - "name", - ) - - return frappe.get_doc("TLS Certificate", certificate_name) - def get_log_server(self): log_server = frappe.db.get_single_value("Press Settings", "log_server") if log_server: @@ -1114,6 +1091,30 @@ def get_primary_frappe_public_key(self): return frappe.db.get_value(self.doctype, self.primary, "frappe_public_key") frappe.throw(f"Failed to fetch {primary.name}'s Frappe public key") + def get_certificate(self): + certificate_name = frappe.db.get_value( + "TLS Certificate", {"wildcard": True, "domain": self.domain}, "name" + ) + + if not certificate_name: + if hasattr(self, "is_self_hosted") and self.is_self_hosted: + certificate_name = frappe.db.get_value( + "TLS Certificate", {"domain": {self.name}}, "name" + ) + + if not certificate_name: + self_hosted_server = frappe.db.get_value( + "Self Hosted Server", {"server": self.name}, ["hostname", "domain"], as_dict=1 + ) + + certificate_name = frappe.db.get_value( + "TLS Certificate", + {"domain": f"{self_hosted_server.hostname}.{self_hosted_server.domain}"}, + "name", + ) + + return frappe.get_doc("TLS Certificate", certificate_name) + class Server(BaseServer): # begin: auto-generated types diff --git a/press/press/doctype/trace_server/trace_server.py b/press/press/doctype/trace_server/trace_server.py index 1e433fb7c8d..740e27b6170 100644 --- a/press/press/doctype/trace_server/trace_server.py +++ b/press/press/doctype/trace_server/trace_server.py @@ -58,10 +58,7 @@ def validate_sentry_admin_password(self): def _setup_server(self): agent_repository_url = self.get_agent_repository_url() - certificate_name = frappe.db.get_value( - "TLS Certificate", {"wildcard": True, "domain": self.domain}, "name" - ) - certificate = frappe.get_doc("TLS Certificate", certificate_name) + certificate = self.get_certificate() log_server = frappe.db.get_single_value("Press Settings", "log_server") if log_server: