Skip to content

Commit

Permalink
feat: provision to setup site with managed DB
Browse files Browse the repository at this point in the history
  • Loading branch information
saurabh6790 committed May 29, 2024
1 parent 0721553 commit f58dc32
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 52 deletions.
68 changes: 38 additions & 30 deletions press/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,39 +98,57 @@ 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(
"New Site", f"benches/{site.bench}/sites", data, bench=site.bench, site=site.name
)

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(
Expand All @@ -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(
Expand Down Expand Up @@ -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:
Expand All @@ -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(
Expand Down
8 changes: 8 additions & 0 deletions press/press/doctype/bench/bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
)
Expand Down
8 changes: 7 additions & 1 deletion press/press/doctype/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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"):
Expand Down
21 changes: 0 additions & 21 deletions press/press/doctype/site/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit f58dc32

Please sign in to comment.