From f58dc327394510bb1ce5c27fd7aa453354829aa1 Mon Sep 17 00:00:00 2001 From: Saurabh Date: Wed, 29 May 2024 18:49:47 +0530 Subject: [PATCH] feat: provision to setup site with managed DB --- press/agent.py | 68 ++++++++++++++++------------ press/press/doctype/bench/bench.py | 8 ++++ press/press/doctype/server/server.py | 8 +++- press/press/doctype/site/site.py | 21 --------- 4 files changed, 53 insertions(+), 52 deletions(-) diff --git a/press/agent.py b/press/agent.py index 154cac6f84b..d3d3f02bfeb 100644 --- a/press/agent.py +++ b/press/agent.py @@ -98,17 +98,45 @@ def update_bench_config(self, bench): "Update Bench Configuration", f"benches/{bench.name}/config", data, bench=bench.name ) + def _get_mariadb_root_password(self, site): + database_server, managed_database_service = frappe.get_cached_value( + "Bench", site.bench, ["database_server", "managed_database_service"] + ) + + if database_server: + doctype = "Database Server" + name = database_server + else: + doctype = "Managed Database Service" + name = managed_database_service + + return get_decrypted_password(doctype, name, "mariadb_root_password") + + def _get_managed_db_config(self, site): + managed_database_service = frappe.get_cached_value( + "Bench", site.bench, "managed_database_service" + ) + + if not managed_database_service: + return {} + + return frappe.get_cached_value( + "Managed Database Service", + managed_database_service, + ["database_host", "database_user", "port"], + as_dict=1, + ) + def new_site(self, site): apps = [app.app for app in site.apps] - database_server = frappe.db.get_value("Bench", site.bench, "database_server") + data = { "config": json.loads(site.config), "apps": apps, "name": site.name, - "mariadb_root_password": get_decrypted_password( - "Database Server", database_server, "mariadb_root_password" - ), + "mariadb_root_password": self._get_mariadb_root_password(site), "admin_password": site.get_password("admin_password"), + "managed_database_config": self._get_managed_db_config(site), } return self.create_agent_job( @@ -116,21 +144,11 @@ def new_site(self, site): ) def reinstall_site(self, site): - database_server = frappe.db.get_value("Bench", site.bench, "database_server") - - mariadb_root_password = ( - site.get_password("db_password") - if site.managed_database - else get_decrypted_password( - "Database Server", database_server, "mariadb_root_password" - ) - ) - data = { - "mariadb_root_user": site.db_user, - "mariadb_root_password": mariadb_root_password, + "mariadb_root_password": self._get_mariadb_root_password(site), "admin_password": site.get_password("admin_password"), "managed_database": site.managed_database, + "managed_database_config": self._get_managed_db_config(site), } return self.create_agent_job( @@ -144,29 +162,21 @@ def reinstall_site(self, site): def restore_site(self, site: "Site", skip_failing_patches=False): site.check_enough_space_on_server() apps = [app.app for app in site.apps] - database_server = frappe.db.get_value("Bench", site.bench, "database_server") public_link, private_link = None, None if site.remote_public_file: public_link = frappe.get_doc("Remote File", site.remote_public_file).download_link if site.remote_private_file: private_link = frappe.get_doc("Remote File", site.remote_private_file).download_link - if site.managed_database: - mariadb_root_password = site.get_password("db_password") - else: - mariadb_root_password = get_decrypted_password( - "Database Server", database_server, "mariadb_root_password" - ) data = { "apps": apps, - "mariadb_root_password": mariadb_root_password, + "mariadb_root_password": self._get_mariadb_root_password(site), "admin_password": site.get_password("admin_password"), "database": frappe.get_doc("Remote File", site.remote_database_file).download_link, "public": public_link, "private": private_link, "skip_failing_patches": skip_failing_patches, - "managed_database": site.managed_database, - "mariadb_root_user": site.db_user, + "managed_database_config": self._get_managed_db_config(site), } return self.create_agent_job( @@ -228,7 +238,6 @@ def sanitized_site_config(site): return json.dumps(sanitized_config) - database_server = frappe.db.get_value("Bench", site.bench, "database_server") public_link, private_link = None, None if site.remote_public_file: @@ -240,15 +249,14 @@ def sanitized_site_config(site): "config": json.loads(site.config), "apps": apps, "name": site.name, - "mariadb_root_password": get_decrypted_password( - "Database Server", database_server, "mariadb_root_password" - ), + "mariadb_root_password": self._get_mariadb_root_password(site), "admin_password": site.get_password("admin_password"), "site_config": sanitized_site_config(site), "database": frappe.get_doc("Remote File", site.remote_database_file).download_link, "public": public_link, "private": private_link, "skip_failing_patches": skip_failing_patches, + "managed_database_config": self._get_managed_db_config(site), } return self.create_agent_job( diff --git a/press/press/doctype/bench/bench.py b/press/press/doctype/bench/bench.py index 834497268e2..4b6ed57c405 100644 --- a/press/press/doctype/bench/bench.py +++ b/press/press/doctype/bench/bench.py @@ -66,6 +66,7 @@ class Bench(Document): is_code_server_enabled: DF.Check is_ssh_proxy_setup: DF.Check last_archive_failure: DF.Datetime | None + managed_database_service: DF.Link | None memory_high: DF.Int memory_max: DF.Int memory_swap: DF.Int @@ -201,6 +202,13 @@ def validate(self): "restart_supervisor_on_update": True, } + if not db_host and self.managed_database_service: + config["rds_db"] = 1 + config["db_host"] = self.managed_database_service + config["db_port"] = frappe.db.get_value( + "Managed Database Service", self.managed_database_service, "port" + ) + press_settings_common_site_config = frappe.db.get_single_value( "Press Settings", "bench_configuration" ) diff --git a/press/press/doctype/server/server.py b/press/press/doctype/server/server.py index fa95722b536..fe71d8766a3 100644 --- a/press/press/doctype/server/server.py +++ b/press/press/doctype/server/server.py @@ -868,6 +868,7 @@ class Server(BaseServer): hostname_abbreviation: DF.Data | None ignore_incidents: DF.Check ip: DF.Data | None + is_managed_database: DF.Check is_primary: DF.Check is_replication_setup: DF.Check is_self_hosted: DF.Check @@ -877,6 +878,7 @@ class Server(BaseServer): is_standalone: DF.Check is_standalone_setup: DF.Check is_upstream_setup: DF.Check + managed_database_service: DF.Link | None new_worker_allocation: DF.Check plan: DF.Link | None primary: DF.Link | None @@ -909,13 +911,17 @@ class Server(BaseServer): def on_update(self): # If Database Server is changed for the server then change it for all the benches - if not self.is_new() and self.has_value_changed("database_server"): + if not self.is_new() and ( + self.has_value_changed("database_server") + or self.has_value_changed("managed_database_service") + ): benches = frappe.get_all( "Bench", {"server": self.name, "status": ("!=", "Archived")} ) for bench in benches: bench = frappe.get_doc("Bench", bench) bench.database_server = self.database_server + bench.managed_database_service = self.managed_database_service bench.save() if not self.is_new() and self.has_value_changed("team"): diff --git a/press/press/doctype/site/site.py b/press/press/doctype/site/site.py index edc94077e67..552a5e1e1ee 100644 --- a/press/press/doctype/site/site.py +++ b/press/press/doctype/site/site.py @@ -99,12 +99,6 @@ class Site(Document, TagHelpers): database_access_password: DF.Password | None database_access_user: DF.Data | None database_name: DF.Data | None - database_service: DF.Literal["AWS RDS"] - db_host: DF.Data | None - db_name: DF.Data | None - db_password: DF.Password | None - db_port: DF.Data | None - db_user: DF.Data | None domain: DF.Link | None erpnext_consultant: DF.Link | None free: DF.Check @@ -115,7 +109,6 @@ class Site(Document, TagHelpers): is_database_access_enabled: DF.Check is_erpnext_setup: DF.Check is_standby: DF.Check - managed_database: DF.Check notify_email: DF.Data | None plan: DF.Link | None remote_config_file: DF.Link | None @@ -425,20 +418,6 @@ def update_config_preview(self): self.config = json.dumps(new_config, indent=4) - def _get_managed_db_config(self): - if self.managed_database: - return { - "managed_database": self.managed_database, - "db_host": self.db_host, - "db_user": self.db_user, - "db_name": self.db_name, - "db_password": self.get_password("db_password"), - "db_port": self.db_port, - "db_service": self.database_service, - } - - return {} - @dashboard_whitelist() @site_action(["Active"]) def install_app(self, app, plan=None):