Skip to content

Commit

Permalink
feat: show hash of downloaded backup files on site restore failure (#144
Browse files Browse the repository at this point in the history
)
  • Loading branch information
tanmoysrt authored Nov 28, 2024
1 parent 82fee5a commit be806a3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
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"

0 comments on commit be806a3

Please sign in to comment.