Sync local logs to S3 bucket. Args: local_output_dir: Local directory containing logs to sync logger: Logger instance for debug output Raises: AssertionError: If required environment variables are not set RuntimeError: If aws s3 sync fails
(local_output_dir: Path, *, logger: Logger)
| 63 | |
| 64 | |
| 65 | def s3_log_sync(local_output_dir: Path, *, logger: Logger) -> None: |
| 66 | """Sync local logs to S3 bucket. |
| 67 | |
| 68 | Args: |
| 69 | local_output_dir: Local directory containing logs to sync |
| 70 | logger: Logger instance for debug output |
| 71 | |
| 72 | Raises: |
| 73 | AssertionError: If required environment variables are not set |
| 74 | RuntimeError: If aws s3 sync fails |
| 75 | """ |
| 76 | aws_s3_bucket = os.getenv("AWS_S3_BUCKET") |
| 77 | aws_s3_prefix = os.getenv("AWS_S3_PREFIX") |
| 78 | |
| 79 | assert aws_s3_bucket is not None, "AWS_S3_BUCKET environment variable must be set" |
| 80 | assert aws_s3_prefix is not None, "AWS_S3_PREFIX environment variable must be set" |
| 81 | |
| 82 | # Construct S3 path: s3://bucket/prefix/logs/relative_path |
| 83 | # where relative_path is local_output_dir relative to DIR_LOGS |
| 84 | try: |
| 85 | relative_path = local_output_dir.relative_to(LOCAL_LOG_DIR) |
| 86 | except ValueError: |
| 87 | # If local_output_dir is not under DIR_LOGS, use the full path |
| 88 | relative_path = local_output_dir |
| 89 | |
| 90 | s3_path = f"s3://{aws_s3_bucket}/{aws_s3_prefix}/{relative_path}" |
| 91 | |
| 92 | logger.debug(f"Syncing {local_output_dir} to {s3_path}") |
| 93 | |
| 94 | result = subprocess.run( |
| 95 | [ |
| 96 | "aws", |
| 97 | "s3", |
| 98 | "sync", |
| 99 | str(local_output_dir), |
| 100 | s3_path, |
| 101 | "--exclude", |
| 102 | "rounds", |
| 103 | "--exclude", |
| 104 | "rounds/*", |
| 105 | "--exclude", |
| 106 | "*/rounds", |
| 107 | "--exclude", |
| 108 | "*/rounds/*", |
| 109 | "--exclude", |
| 110 | "**/rounds", |
| 111 | "--exclude", |
| 112 | "**/rounds/*", |
| 113 | ], |
| 114 | capture_output=True, |
| 115 | text=True, |
| 116 | ) |
| 117 | |
| 118 | if result.returncode != 0: |
| 119 | logger.critical(f"❌ Failed to sync logs to S3: {result.stderr}\n{result.stdout}") |
| 120 | raise RuntimeError(f"Failed to sync logs to S3: {result.stderr}") |
| 121 | |
| 122 | logger.info(f"✅ Successfully synced logs to {s3_path}") |