Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Restore Site)!: Restore site config from backup #2213

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
38 changes: 21 additions & 17 deletions press/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@
"managed_database_config": self._get_managed_db_config(site),
}

if site.remote_config_file:
data["site_config"] = json.dumps(self._restore_site_config(site))

Check warning on line 181 in press/agent.py

View check run for this annotation

Codecov / codecov/patch

press/agent.py#L181

Added line #L181 was not covered by tests

return self.create_agent_job(
"Restore Site",
f"benches/{site.bench}/sites/{site.name}/restore",
Expand Down Expand Up @@ -246,22 +249,6 @@
site.check_enough_space_on_server()
apps = [app.app for app in site.apps]

def sanitized_site_config(site):
sanitized_config = {}
if site.remote_config_file:
from press.press.doctype.site_activity.site_activity import log_site_activity

site_config = frappe.get_doc("Remote File", site.remote_config_file)
new_config = site_config.get_content()
new_config["maintenance_mode"] = 0 # Don't allow deactivated sites to be created
sanitized_config = sanitize_config(new_config)
existing_config = json.loads(site.config)
existing_config.update(sanitized_config)
site._update_configuration(existing_config)
log_site_activity(site.name, "Update Configuration")

return json.dumps(sanitized_config)

public_link, private_link = None, None

if site.remote_public_file:
Expand All @@ -275,7 +262,7 @@
"name": site.name,
"mariadb_root_password": self._get_mariadb_root_password(site),
"admin_password": site.get_password("admin_password"),
"site_config": sanitized_site_config(site),
"site_config": json.dumps(self._restore_site_config(site) or {}),
"database": frappe.get_doc("Remote File", site.remote_database_file).download_link,
"public": public_link,
"private": private_link,
Expand All @@ -291,6 +278,23 @@
site=site.name,
)

def _restore_site_config(self, site: "Site"):
if not site.remote_config_file:
return None

Check warning on line 283 in press/agent.py

View check run for this annotation

Codecov / codecov/patch

press/agent.py#L283

Added line #L283 was not covered by tests

from press.press.doctype.site_activity.site_activity import log_site_activity

site_config = frappe.get_doc("Remote File", site.remote_config_file)
new_config: dict = site_config.get_content()
new_config["maintenance_mode"] = 0 # Don't allow deactivated sites to be created
new_config.pop("db_user", None) # Forcefully remove dangerous keys
sanitized_config = sanitize_config(new_config)
existing_config = json.loads(site.config)
existing_config.update(sanitized_config)
site._update_configuration(existing_config)
log_site_activity(site.name, "Update Configuration")
return sanitized_config

def install_app_site(self, site, app):
data = {"name": app}
return self.create_agent_job(
Expand Down
Loading