-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from glrs/feature/report-transfer
Add Report Transfer utilities [used in 10x for now]
- Loading branch information
Showing
3 changed files
with
157 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import subprocess | ||
from pathlib import Path | ||
from typing import Optional | ||
|
||
from lib.core_utils.config_loader import configs | ||
from lib.core_utils.logging_utils import custom_logger | ||
|
||
logging = custom_logger(__name__.split(".")[-1]) | ||
|
||
|
||
def transfer_report( | ||
report_path: Path, project_id: str, sample_id: Optional[str] = None | ||
) -> bool: | ||
try: | ||
report_transfer_config = configs["report_transfer"] | ||
server = report_transfer_config["server"] | ||
user = report_transfer_config["user"] | ||
destination_path = report_transfer_config["destination"] | ||
ssh_key = report_transfer_config.get("ssh_key") | ||
except KeyError as e: | ||
missing_key = e.args[0] | ||
logging.error(f"Missing configuration for report transfer: '{missing_key}'") | ||
logging.warning("Report transfer will not be attempted. Handle manually...") | ||
return False | ||
|
||
if sample_id: | ||
remote_path = f"{user}@{server}:{destination_path}/{project_id}/{sample_id}/" | ||
else: | ||
remote_path = f"{user}@{server}:{destination_path}/{project_id}/" | ||
|
||
rsync_command = [ | ||
"rsync", | ||
"-avz", | ||
"-e", | ||
f"ssh -i {ssh_key}" if ssh_key else "ssh", | ||
str(report_path), | ||
remote_path, | ||
] | ||
|
||
try: | ||
# Execute the rsync command | ||
result = subprocess.run( | ||
rsync_command, | ||
check=True, | ||
text=True, | ||
capture_output=True, | ||
) | ||
|
||
logging.info(f"Report transferred successfully to {remote_path}") | ||
return True | ||
except subprocess.CalledProcessError as e: | ||
logging.error(f"Failed to transfer report:\n{e.stderr.strip()}") | ||
return False | ||
except Exception as e: | ||
logging.error(f"Unexpected error during report transfer: {e}") | ||
logging.error(f"RSYNC output: {result.stdout}") | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters