-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel_rsync.py
More file actions
570 lines (490 loc) · 19.2 KB
/
Copy pathparallel_rsync.py
File metadata and controls
570 lines (490 loc) · 19.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
#!/usr/bin/env python3
import argparse
import logging
import re
import shlex
import subprocess
import sys
import threading
from concurrent.futures import ThreadPoolExecutor, as_completed
from pathlib import Path
import yaml
_RICH_AVAILABLE = False
try:
from rich.console import Console
from rich.progress import (
BarColumn,
Progress,
SpinnerColumn,
TaskID,
TaskProgressColumn,
TextColumn,
TimeElapsedColumn,
)
from rich.table import Table
from rich.text import Text
_RICH_AVAILABLE = True
except ImportError:
pass
_PROGRESS_RE = re.compile(
r"^\s*"
r"(?P<bytes>[\d,]+)\s+" # bytes transferred
r"(?P<pct>\d+)%\s+" # percentage
r"(?P<speed>\S+/s)\s+" # transfer speed
r"(?P<eta>\S+)" # ETA
r"(?:\s+\(xfr#(?P<xfr>\d+)" # files transferred
r",\s*(?:to-chk|ir-chk)="
r"(?P<remaining>\d+)/(?P<total>\d+)\))?" # remaining/total files
)
def _parse_progress_line(line: str) -> dict | None:
"""Parse a single rsync --info=progress2 line into a dict."""
m = _PROGRESS_RE.search(line)
if not m:
return None
return {
"bytes": int(m.group("bytes").replace(",", "")),
"pct": int(m.group("pct")),
"speed": m.group("speed"),
"eta": m.group("eta"),
"xfr": int(m.group("xfr")) if m.group("xfr") else 0,
"remaining": int(m.group("remaining")) if m.group("remaining") else 0,
"total": int(m.group("total")) if m.group("total") else 0,
}
def load_config(path: Path) -> dict:
"""Load and validate the YAML configuration."""
if not path.is_file():
raise FileNotFoundError(f"Config file not found: {path}")
with path.open("r") as f:
cfg = yaml.safe_load(f)
if not isinstance(cfg, dict) or "groups" not in cfg:
raise ValueError("YAML must contain a top‑level 'groups' key.")
if not isinstance(cfg["groups"], list):
raise ValueError("'groups' must be a list of group definitions.")
if "global_options" in cfg and not isinstance(cfg["global_options"], list):
raise ValueError("'global_options' must be a list of rsync option strings.")
return cfg
def ensure_dest_dir(dest: str, logger: logging.Logger, name: str) -> None:
"""Create the local destination directory if it doesn't exist."""
if ":" in dest:
logger.debug(f"[{name}] Remote destination, skipping local mkdir")
return
dest_path = Path(dest)
if not dest_path.exists():
logger.info(f"[{name}] Creating destination directory: {dest_path}")
dest_path.mkdir(parents=True, exist_ok=True)
def build_rsync_cmd(group: dict, global_options: list[str] | None = None) -> list[str]:
"""Construct the rsync command list for a given group."""
src = group.get("src")
dest = group.get("dest")
group_options = group.get("options", [])
if not src or not dest:
raise ValueError(f"Group '{group.get('name', '<unnamed>')}' missing src or dest.")
if not src.endswith("/"):
src = src + "/"
merged_options = list(global_options or []) + list(group_options)
cmd = ["rsync"] + merged_options + [src, dest]
return cmd
def extract_host(dest: str) -> str:
"""Extract the hostname from an rsync destination string."""
if "::" in dest:
host_part = dest.split("::", 1)[0]
return host_part.split("@", 1)[1] if "@" in host_part else host_part
if ":" in dest:
colon_idx = dest.index(":")
if colon_idx == 1 and dest[0].isalpha():
return "local"
host_part = dest.split(":", 1)[0]
return host_part.split("@", 1)[1] if "@" in host_part else host_part
return "local"
def setup_logging(log_level: str, log_file: str | None = None) -> logging.Logger:
"""Configure a logger.
If *log_file* is provided, log messages are written to that file only —
nothing is printed to the console (the rich progress display owns the
terminal). When no log file is given, a NullHandler is attached so
that logging calls are silently discarded; the progress bars and
summary table remain the sole user-facing output.
"""
logger = logging.getLogger("parallel_rsync")
level = getattr(logging, log_level.upper(), logging.INFO)
logger.setLevel(level)
if logger.handlers:
return logger
if log_file:
formatter = logging.Formatter("%(asctime)s %(levelname)s %(message)s")
fh = logging.FileHandler(log_file, encoding="utf-8")
fh.setLevel(level)
fh.setFormatter(formatter)
logger.addHandler(fh)
else:
logger.addHandler(logging.NullHandler())
return logger
_STATUS = {
"waiting": "[dim]waiting[/dim]",
"running": "[bold cyan]syncing[/bold cyan]",
"done": "[bold green]✔ done[/bold green]",
"failed": "[bold red]✖ failed[/bold red]",
"timeout": "[bold yellow]⏱ timeout[/bold yellow]",
}
def _inject_progress2(cmd: list[str]) -> list[str]:
"""Ensure --info=progress2 is present so we can parse progress."""
has_progress2 = any("--info=progress2" in arg for arg in cmd)
if not has_progress2:
# Insert right after 'rsync'
return [cmd[0], "--info=progress2"] + cmd[1:]
return list(cmd)
def run_rsync_live(
group: dict,
global_options: list[str],
semaphores: dict[str, threading.Semaphore],
logger: logging.Logger,
progress: "Progress | None",
task_id: "TaskID | None",
timeout: int | None = None,
) -> dict:
"""Execute rsync for one group, streaming progress to the rich bar."""
name = group.get("name", "unnamed")
src = group.get("src", "")
dest = group.get("dest", "")
host = extract_host(src)
if host == "local":
host = extract_host(dest)
semaphore = semaphores[host]
def _log(msg: str, level: str = "info") -> None:
"""Log to file only (if configured). The progress bars handle console output."""
getattr(logger, level)(msg)
# -- waiting --
if progress and task_id is not None:
progress.update(task_id, description=f"{_STATUS['waiting']} [bold]{name}[/bold]")
_log(f"[{name}] Waiting for slot on host '{host}'")
with semaphore:
try:
if group.get("mkdir_dest", False):
ensure_dest_dir(dest, logger, name)
cmd = build_rsync_cmd(group, global_options)
cmd = _inject_progress2(cmd)
cmd_str = shlex.join(cmd)
# -- running --
if progress and task_id is not None:
progress.update(
task_id,
description=f"{_STATUS['running']} [bold]{name}[/bold]",
)
_log(f"[{name}] Starting rsync on host '{host}': {cmd_str}")
proc = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
stdout_lines: list[str] = []
stderr_lines: list[str] = []
# Read stdout in a thread so we can enforce timeout
def _read_stdout():
assert proc.stdout is not None
for raw_line in proc.stdout:
line = raw_line.rstrip("\n\r")
stdout_lines.append(line)
parsed = _parse_progress_line(line)
if parsed and progress and task_id is not None:
progress.update(
task_id,
completed=parsed["pct"],
speed=parsed["speed"],
eta=parsed["eta"],
)
def _read_stderr():
assert proc.stderr is not None
for raw_line in proc.stderr:
stderr_lines.append(raw_line.rstrip("\n\r"))
t_out = threading.Thread(target=_read_stdout, daemon=True)
t_err = threading.Thread(target=_read_stderr, daemon=True)
t_out.start()
t_err.start()
try:
proc.wait(timeout=timeout)
except subprocess.TimeoutExpired:
proc.kill()
proc.wait()
t_out.join(timeout=2)
t_err.join(timeout=2)
_log(f"[{name}] rsync timed out after {timeout}s on host '{host}'", "error")
if progress and task_id is not None:
progress.update(
task_id,
description=f"{_STATUS['timeout']} [bold]{name}[/bold]",
)
return {
"name": name,
"host": host,
"cmd": cmd_str,
"returncode": -2,
"stdout": "\n".join(stdout_lines),
"stderr": f"Timed out after {timeout}s",
}
t_out.join(timeout=5)
t_err.join(timeout=5)
rc = proc.returncode
stdout_text = "\n".join(stdout_lines)
stderr_text = "\n".join(stderr_lines)
_log(f"[{name}] rsync completed with exit code {rc}")
if stderr_text:
_log(f"[{name}] STDERR:\n{stderr_text}", "warning")
# -- done / failed --
if progress and task_id is not None:
if rc == 0:
progress.update(
task_id,
completed=100,
description=f"{_STATUS['done']} [bold]{name}[/bold]",
)
else:
progress.update(
task_id,
description=f"{_STATUS['failed']} [bold]{name}[/bold]",
)
return {
"name": name,
"host": host,
"cmd": cmd_str,
"returncode": rc,
"stdout": stdout_text,
"stderr": stderr_text,
}
except Exception as e:
_log(f"[{name}] Exception while running rsync: {e}", "error")
if progress and task_id is not None:
progress.update(
task_id,
description=f"{_STATUS['failed']} [bold]{name}[/bold]",
)
return {
"name": name,
"host": host,
"cmd": shlex.join(build_rsync_cmd(group, global_options)),
"returncode": -1,
"stdout": "",
"stderr": str(e),
}
# ---------------------------------------------------------------------------
# Summary table
# ---------------------------------------------------------------------------
def _print_summary(results: list[dict]) -> None:
"""Print a pretty summary table using rich (falls back to plain text)."""
failures = [r for r in results if r["returncode"] != 0]
successes = len(results) - len(failures)
if _RICH_AVAILABLE:
console = Console()
console.print()
table = Table(
title="[bold]Summary[/bold]",
show_lines=True,
title_style="bold cyan",
border_style="dim",
)
table.add_column("Group", style="bold")
table.add_column("Host", style="dim")
table.add_column("Exit Code", justify="center")
table.add_column("Status", justify="center")
for r in sorted(results, key=lambda x: x["name"]):
rc = r["returncode"]
if rc == 0:
status = Text("✔ Success", style="bold green")
rc_text = Text(str(rc), style="green")
elif rc == -2:
status = Text("⏱ Timeout", style="bold yellow")
rc_text = Text("timeout", style="yellow")
else:
status = Text("✖ Failed", style="bold red")
rc_text = Text(str(rc), style="red")
table.add_row(r["name"], r["host"], rc_text, status)
console.print(table)
if failures:
console.print(
f"\n[bold red]✖ {len(failures)} job(s) failed[/bold red] "
f"[dim]|[/dim] [bold green]✔ {successes} succeeded[/bold green]"
)
else:
console.print(
f"\n[bold green]✔ All {successes} job(s) completed successfully![/bold green]"
)
console.print()
else:
print(f"\n{'=' * 50}")
print(f" Summary: {successes} succeeded, {len(failures)} failed")
print(f"{'=' * 50}")
for r in sorted(results, key=lambda x: x["name"]):
rc = r["returncode"]
tag = "OK" if rc == 0 else "FAIL"
print(f" [{tag}] {r['name']} (host={r['host']}, exit={rc})")
print()
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
def main() -> None:
parser = argparse.ArgumentParser(description="Launch multiple rsync jobs in parallel.")
parser.add_argument(
"-c",
"--config",
type=Path,
required=True,
help="Path to the YAML configuration file.",
)
parser.add_argument(
"--workers",
type=int,
default=4,
help="Maximum number of parallel rsync processes overall (default: 4).",
)
parser.add_argument(
"--max-per-host",
type=int,
default=2,
help="Maximum concurrent rsync jobs per host (default: 2).",
)
parser.add_argument(
"--log-file",
type=str,
default=None,
help="Optional file path for logging output. If omitted, no log file is written.",
)
parser.add_argument(
"--log-level",
type=str,
default="INFO",
choices=["DEBUG", "INFO", "WARNING", "ERROR"],
help="Logging verbosity (default: INFO).",
)
parser.add_argument(
"--timeout",
type=int,
default=None,
help="Timeout in seconds for each rsync process (default: no timeout).",
)
parser.add_argument(
"--dry-run",
action="store_true",
help="Add '--dry-run' to every rsync command for testing.",
)
parser.add_argument(
"--no-progress",
action="store_true",
help="Disable the fancy progress bars (plain log output only).",
)
args = parser.parse_args()
# ------------------------------------------------------------------
logger = setup_logging(args.log_level, args.log_file)
logger.info("=== Parallel rsync started ===")
logger.info(f"Config file: {args.config}")
logger.info(f"Overall workers: {args.workers}")
logger.info(f"Per-host concurrency limit: {args.max_per_host}")
logger.info(f"Timeout: {args.timeout or 'none'}")
logger.info(f"Dry-run mode: {'ON' if args.dry_run else 'OFF'}")
# ------------------------------------------------------------------
try:
cfg = load_config(args.config)
except Exception as exc:
logger.error(f"Failed to load config: {exc}")
sys.exit(1)
groups = cfg["groups"]
global_options: list[str] = cfg.get("global_options", [])
if global_options:
logger.info(f"Global rsync options: {shlex.join(global_options)}")
# Inject dry-run
if args.dry_run:
if "--dry-run" not in global_options:
global_options = list(global_options) + ["--dry-run"]
patched = []
for g in groups:
g = dict(g)
opts = list(g.get("options", []))
if "--dry-run" not in opts:
opts.append("--dry-run")
g["options"] = opts
patched.append(g)
groups = patched
# ------------------------------------------------------------------
# Per-host semaphores
# ------------------------------------------------------------------
def _effective_host(g: dict) -> str:
host = extract_host(g.get("src", ""))
if host == "local":
host = extract_host(g.get("dest", ""))
return host
hosts = {_effective_host(g) for g in groups}
host_semaphores = {host: threading.Semaphore(args.max_per_host) for host in hosts}
logger.info(f"Detected hosts: {', '.join(sorted(hosts))}")
# ------------------------------------------------------------------
# Build progress display
# ------------------------------------------------------------------
use_progress = _RICH_AVAILABLE and not args.no_progress
progress = None
task_ids: dict[str, "TaskID"] = {}
if use_progress:
progress = Progress(
SpinnerColumn("dots"),
TextColumn("{task.description}", markup=True),
BarColumn(bar_width=30, complete_style="green", finished_style="bright_green"),
TaskProgressColumn(),
TextColumn("•", style="dim"),
TextColumn("[cyan]{task.fields[speed]}[/cyan]", markup=True),
TextColumn("•", style="dim"),
TextColumn("[dim]ETA {task.fields[eta]}[/dim]", markup=True),
TimeElapsedColumn(),
expand=False,
transient=False,
)
# Pre-create a task/bar for every group
for g in groups:
gname = g.get("name", "unnamed")
tid = progress.add_task(
description=f"{_STATUS['waiting']} [bold]{gname}[/bold]",
total=100,
completed=0,
speed="--",
eta="--:--",
)
task_ids[gname] = tid
# ------------------------------------------------------------------
# Execute
# ------------------------------------------------------------------
results: list[dict] = []
def _run(g):
gname = g.get("name", "unnamed")
tid = task_ids.get(gname)
return run_rsync_live(
g, global_options, host_semaphores, logger, progress, tid, args.timeout
)
if use_progress:
assert progress is not None
with progress:
with ThreadPoolExecutor(max_workers=args.workers) as executor:
future_to_name = {
executor.submit(_run, g): g.get("name", "unnamed") for g in groups
}
for future in as_completed(future_to_name):
results.append(future.result())
else:
# Fallback: no progress bars
with ThreadPoolExecutor(max_workers=args.workers) as executor:
future_to_name = {executor.submit(_run, g): g.get("name", "unnamed") for g in groups}
for future in as_completed(future_to_name):
result = future.result()
results.append(result)
name = result["name"]
rc = result["returncode"]
if rc != 0:
logger.error(f"[{name}] rsync exited with errors (code {rc})")
else:
logger.info(f"[{name}] rsync completed successfully")
_print_summary(results)
failures = [r for r in results if r["returncode"] != 0]
logger.info(
f"=== All rsync jobs finished: "
f"{len(results) - len(failures)} succeeded, {len(failures)} failed ==="
)
if failures:
for f in failures:
logger.error(f" FAILED: {f['name']} (code {f['returncode']})")
sys.exit(1)
if __name__ == "__main__":
main()