Skip to content

Commit

Permalink
feat: add generic option while setting up root domain and routing
Browse files Browse the repository at this point in the history
  • Loading branch information
saurabh6790 committed Aug 12, 2024
1 parent 944fc02 commit 4da0169
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 17 deletions.
22 changes: 20 additions & 2 deletions press/press/doctype/root_domain/root_domain.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,24 @@
// For license information, please see license.txt

frappe.ui.form.on('Root Domain', {
// refresh: function(frm) {
// }
refresh: function (frm) {
frm.trigger('set_mandatory_fields');
},

dns_provider: function (frm) {
frm.trigger('set_mandatory_fields');
},

set_mandatory_fields: function (frm) {
frm.set_df_property(
'aws_access_key_id',
'reqd',
frm.doc.dns_provider === 'AWS Route 53',
);
frm.set_df_property(
'aws_secret_access_key',
'reqd',
frm.doc.dns_provider === 'AWS Route 53',
);
},
});
36 changes: 25 additions & 11 deletions press/press/doctype/root_domain/root_domain.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"engine": "InnoDB",
"field_order": [
"default_cluster",
"column_break_4",
"column_break_exzq",
"obtain_wildcard_tls",
"section_break_hdal",
"dns_provider",
"aws_access_key_id",
"aws_secret_access_key"
Expand All @@ -20,31 +22,41 @@
"in_list_view": 1,
"in_standard_filter": 1,
"label": "DNS Provider",
"options": "AWS Route 53",
"options": "AWS Route 53\nGeneric",
"reqd": 1
},
{
"fieldname": "column_break_4",
"fieldtype": "Column Break"
},
{
"fieldname": "aws_access_key_id",
"fieldtype": "Data",
"label": "AWS Access Key ID",
"reqd": 1
"label": "AWS Access Key ID"
},
{
"fieldname": "aws_secret_access_key",
"fieldtype": "Password",
"label": "AWS Secret Access Key",
"reqd": 1
"label": "AWS Secret Access Key"
},
{
"fieldname": "default_cluster",
"fieldtype": "Link",
"label": "Default Cluster",
"options": "Cluster",
"reqd": 1
},
{
"fieldname": "column_break_exzq",
"fieldtype": "Column Break"
},
{
"default": "1",
"fieldname": "obtain_wildcard_tls",
"fieldtype": "Check",
"label": "Obtain Wildcard TLS",
"no_copy": 1,
"set_only_once": 1
},
{
"fieldname": "section_break_hdal",
"fieldtype": "Section Break"
}
],
"index_web_pages_for_search": 1,
Expand Down Expand Up @@ -80,10 +92,11 @@
"link_fieldname": "domain"
}
],
"modified": "2021-03-25 11:42:01.377832",
"modified": "2024-08-09 17:58:48.193519",
"modified_by": "Administrator",
"module": "Press",
"name": "Root Domain",
"naming_rule": "Set by user",
"owner": "Administrator",
"permissions": [
{
Expand All @@ -101,5 +114,6 @@
],
"sort_field": "modified",
"sort_order": "DESC",
"states": [],
"track_changes": 1
}
24 changes: 20 additions & 4 deletions press/press/doctype/root_domain/root_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@ class RootDomain(Document):
if TYPE_CHECKING:
from frappe.types import DF

aws_access_key_id: DF.Data
aws_secret_access_key: DF.Password
aws_access_key_id: DF.Data | None
aws_secret_access_key: DF.Password | None
default_cluster: DF.Link
dns_provider: DF.Literal["AWS Route 53"]
dns_provider: DF.Literal["AWS Route 53", "Generic"]
obtain_wildcard_tls: DF.Check
# end: auto-generated types

def after_insert(self):
if not frappe.db.exists("TLS Certificate", {"wildcard": True, "domain": self.name}):
if self.obtain_wildcard_tls and not frappe.db.exists(
"TLS Certificate", {"wildcard": True, "domain": self.name}
):
frappe.enqueue_doc(
self.doctype,
self.name,
Expand All @@ -55,6 +58,12 @@ def obtain_root_domain_tls_certificate(self):
except Exception:
log_error("Root Domain TLS Certificate Exception")

@property
def generic(self):
if not hasattr(self, "_generic"):
self._generic = self.dns_provider == "Generic"
return self._generic

@property
def boto3_client(self):
if not hasattr(self, "_boto3_client"):
Expand Down Expand Up @@ -128,6 +137,9 @@ def remove_unused_cname_records(self):
self.delete_dns_records(to_delete)

def update_dns_records_for_sites(self, sites: list[str], proxy_server: str):
if self.generic:
return # No need to create DNS records for generic domains

# update records in batches of 500
batch_size = 500
for i in range(0, len(sites), batch_size):
Expand All @@ -154,4 +166,8 @@ def cleanup_cname_records():
domains = frappe.get_all("Root Domain", pluck="name")
for domain_name in domains:
domain = frappe.get_doc("Root Domain", domain_name)

if domain.generic:
continue # No need to create DNS records for generic domains

domain.remove_unused_cname_records()
4 changes: 4 additions & 0 deletions press/press/doctype/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ def after_insert(self):
def create_dns_record(self):
try:
domain = frappe.get_doc("Root Domain", self.domain)

if domain.generic:
return # No need to create DNS records for generic domains

client = boto3.client(
"route53",
aws_access_key_id=domain.aws_access_key_id,
Expand Down
4 changes: 4 additions & 0 deletions press/utils/dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ def create_dns_record(doc, record_name=None):
"""Check if site needs dns records and creates one."""
domain = frappe.get_doc("Root Domain", doc.domain)
is_standalone = frappe.get_value("Server", doc.server, "is_standalone")

if domain.generic:
return # No need to create DNS records for generic domains

if doc.cluster == domain.default_cluster and not is_standalone:
return

Expand Down

0 comments on commit 4da0169

Please sign in to comment.