Skip to content

Commit

Permalink
move validate to constructor; use valueerror instead of backuperror
Browse files Browse the repository at this point in the history
  • Loading branch information
eimrek committed Jan 11, 2024
1 parent 94a37a5 commit 54c1e23
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions disk_objectstore/backup_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,10 @@ def __init__(
if "rsync" not in self.exes:
self.exes["rsync"] = "rsync"

self.validate()
# Validate the backup config inputs

def check_if_remote_accessible(self):
"""Check if remote host is accessible via ssh"""
self.logger.info(f"Checking if '{self.remote}' is accessible...")
success = self.run_cmd(["exit"])[0]
if not success:
raise BackupError(f"Remote '{self.remote}' is not accessible!")
self.logger.info("Success! '%s' is accessible!", self.remote)

def check_path_exists(self, path: Path) -> bool:
cmd = ["[", "-e", str(path), "]"]
return self.run_cmd(cmd)[0]

def validate(self):
"""Validate the backup config inputs"""
if self.keep < 0:
raise BackupError(
raise ValueError(
"Input validation failed: keep variable can't be negative!"
)

Expand All @@ -94,17 +80,27 @@ def validate(self):
if self.exes:
for _, path in self.exes.items():
if not is_exe_found(path):
raise BackupError(
f"Input validation failed: {path} not accessible."
)
raise ValueError(f"Input validation failed: {path} not accessible.")

if not self.check_path_exists(self.path):
success = self.run_cmd(["mkdir", str(self.path)])[0]
if not success:
raise BackupError(
raise ValueError(
f"Input validation failed: Couldn't access/create '{str(self.path)}'!"
)

def check_if_remote_accessible(self):
"""Check if remote host is accessible via ssh"""
self.logger.info(f"Checking if '{self.remote}' is accessible...")
success = self.run_cmd(["exit"])[0]
if not success:
raise BackupError(f"Remote '{self.remote}' is not accessible!")
self.logger.info("Success! '%s' is accessible!", self.remote)

def check_path_exists(self, path: Path) -> bool:
cmd = ["[", "-e", str(path), "]"]
return self.run_cmd(cmd)[0]

def run_cmd(
self,
args: list,
Expand Down

0 comments on commit 54c1e23

Please sign in to comment.