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: show hash of downloaded backup files on restore failure #144

Merged
merged 6 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
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
25 changes: 24 additions & 1 deletion agent/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from agent.base import AgentException, Base
from agent.database import Database
from agent.job import job, step
from agent.utils import b2mb, get_size
from agent.utils import b2mb, compute_file_hash, get_size

if TYPE_CHECKING:
from agent.bench import Bench
Expand Down Expand Up @@ -129,6 +129,26 @@ def restore(
finally:
self.bench.drop_mariadb_user(self.name, mariadb_root_password, self.database)

@step("Checksum of Downloaded Backup Files")
def calculate_checksum_of_backup_files(self, database_file, public_file, private_file):
database_file_sha256 = compute_file_hash(database_file, algorithm="sha256", raise_exception=False)

data = f"""Database File
> File Name - {os.path.basename(database_file)}
> SHA256 Checksum - {database_file_sha256}\n"""
if public_file:
public_file_sha256 = compute_file_hash(public_file, algorithm="sha256", raise_exception=False)
data += f"""\nPublic File
> File Name - {os.path.basename(public_file)}
> SHA256 Checksum - {public_file_sha256}\n"""
if private_file:
private_file_sha256 = compute_file_hash(private_file, algorithm="sha256", raise_exception=False)
data += f"""\nPrivate File
> File Name - {os.path.basename(private_file)}
> SHA256 Checksum - {private_file_sha256}\n"""

return {"output": data}

@job("Restore Site")
def restore_job(
self,
Expand All @@ -149,6 +169,9 @@ def restore_job(
files["public"],
files["private"],
)
except Exception:
self.calculate_checksum_of_backup_files(files["database"], files["public"], files["private"])
raise
finally:
self.bench.delete_downloaded_files(files["directory"])
self.uninstall_unavailable_apps(apps)
Expand Down
22 changes: 22 additions & 0 deletions agent/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import hashlib
import os
from datetime import datetime, timedelta
from math import ceil
Expand Down Expand Up @@ -116,3 +117,24 @@ def end_execution(
res["status"] = status or "Success"
res["output"] = output or res["output"]
return res


def compute_file_hash(file_path, algorithm="sha256", raise_exception=True):
try:
"""Compute the hash of a file using the specified algorithm."""
hash_func = hashlib.new(algorithm)

with open(file_path, "rb") as file:
# read in 10MB chunks
while chunk := file.read(10000000):
hash_func.update(chunk)

return hash_func.hexdigest()
except FileNotFoundError:
if raise_exception:
raise
return "File does not exist"
except Exception:
if raise_exception:
raise
return "Failed to compute hash"