From 3c1589bd0c675b91250a35e9e5569ce1f8c9a292 Mon Sep 17 00:00:00 2001 From: Lars Date: Thu, 29 Oct 2026 14:59:58 +0100 Subject: [PATCH] refactor(core): read mount, conflict, migration and adapter settings from host/target config --- orchestrator.py | 102 ++++++++++++++++++++++++++++++------------------ 1 file changed, 63 insertions(+), 39 deletions(-) diff --git a/orchestrator.py b/orchestrator.py index ee51420..2674f68 100644 --- a/orchestrator.py +++ b/orchestrator.py @@ -218,10 +218,16 @@ def notification_counts(config): return len(config['webhooks']), int(bool(kuma['overall_push_url'])), sum(bool(v) for v in kuma['job_push_urls'].values()) -def nas_mount_ready(mountpoint=None, source=None, fstype=None): +def nas_mount_ready(mountpoint=None, source=None, fstype=None, host_cfg=None): + if host_cfg: + mountpoint = mountpoint or host_cfg.get('nas_mount') + source = source or host_cfg.get('nas_source') + fstype = fstype or host_cfg.get('nas_fstype') mountpoint = mountpoint or NAS_MOUNT source = source or NAS_SOURCE fstype = fstype or 'nfs4' + if not mountpoint or not source: + return True try: result = subprocess.run( ['findmnt', '--mountpoint', mountpoint, '--types', fstype, '--source', source], @@ -232,31 +238,39 @@ def nas_mount_ready(mountpoint=None, source=None, fstype=None): return False -def legacy_schedules(): - """Stateful detection of active legacy schedules and conflicting running services.""" +def legacy_schedules(host_cfg=None): + """Stateful detection of active legacy schedules and conflicting running services configured in host.json.""" + cfg = host_cfg or load_host_config() + cron_users = cfg.get('cron_users', ['root', 'lars']) + cron_pattern = cfg.get('conflict_cron_pattern', r'/home/lars/(?:db-[\w-]+\.sh|backupPaperlessData\.sh)') + conflict_services = cfg.get('conflict_services', ['raspiBackup.service']) + conflict_timers = cfg.get('conflict_timers', ['paperless-data-backup.timer', 'borgmatic.timer']) + found = [] - for user in ('root', 'lars'): - try: - p = subprocess.run(['crontab', '-u', user, '-l'], capture_output=True, text=True, timeout=8) - except (OSError, subprocess.TimeoutExpired): - continue - for line in p.stdout.splitlines(): - stripped = line.strip() - if stripped.startswith('#') or not stripped: + if cron_pattern: + for user in cron_users: + try: + p = subprocess.run(['crontab', '-u', user, '-l'], capture_output=True, text=True, timeout=8) + except (OSError, subprocess.TimeoutExpired): continue - if re.search(r'/home/lars/(?:db-[\w-]+\.sh|backupPaperlessData\.sh)', stripped): - found.append(f'{user} crontab: {stripped}') - # Stateful coordination with raspiBackup: only block if raspiBackup.service is currently active/running. - try: - rb_active = subprocess.run( - ['systemctl', 'is-active', 'raspiBackup.service'], - capture_output=True, text=True, timeout=8, - ).stdout.strip() - if rb_active in ('active', 'activating', 'reloading'): - found.append(f'raspiBackup.service: active={rb_active}') - except (OSError, subprocess.TimeoutExpired): - pass - for timer in ('paperless-data-backup.timer', 'borgmatic.timer'): + for line in p.stdout.splitlines(): + stripped = line.strip() + if stripped.startswith('#') or not stripped: + continue + if re.search(cron_pattern, stripped): + found.append(f'{user} crontab: {stripped}') + # Stateful coordination with conflicting services (e.g. raspiBackup.service): only block if currently active/running. + for service in conflict_services: + try: + svc_active = subprocess.run( + ['systemctl', 'is-active', service], + capture_output=True, text=True, timeout=8, + ).stdout.strip() + if svc_active in ('active', 'activating', 'reloading'): + found.append(f'{service}: active={svc_active}') + except (OSError, subprocess.TimeoutExpired): + pass + for timer in conflict_timers: try: enabled = subprocess.run(['systemctl', 'is-enabled', timer], capture_output=True, text=True, timeout=8).stdout.strip() active = subprocess.run(['systemctl', 'is-active', timer], capture_output=True, text=True, timeout=8).stdout.strip() @@ -283,20 +297,23 @@ def script_issues(job): return issues -def check(jobs, max_parallel, notify): - print(f'Backup-Orchestrator {VERSION}: INSPECTION ONLY (no backups, no schedule changes)') +def check(jobs, max_parallel, notify, host_cfg=None): + cfg = host_cfg or load_host_config() + mountpoint = cfg.get('nas_mount', NAS_MOUNT) + source = cfg.get('nas_source', NAS_SOURCE) + print(f'Backup-Orchestrator {VERSION} (host={cfg.get("hostname", socket.gethostname())}): INSPECTION ONLY (no backups, no schedule changes)') print(f'Parallel limit per phase: {max_parallel}') for job in jobs: issues = script_issues(job) state = 'enabled' if job['enabled'] else 'disabled' print(f"phase {job['phase']:>2} {job['id']:<24} {state:<8} {job['timeout_seconds']:>5}s {job['script']} [{', '.join(issues) if issues else 'OK'}]") - conflicts = legacy_schedules() + conflicts = legacy_schedules(cfg) print('\nLegacy schedules:') for line in conflicts: print(' ACTIVE: ' + line) if not conflicts: print(' None detected (best effort)') - print(f'\nNAS mount {NAS_MOUNT}: {"OK" if nas_mount_ready() else "NOT AVAILABLE"} (expected {NAS_SOURCE})') + print(f'\nNAS mount {mountpoint}: {"OK" if nas_mount_ready(host_cfg=cfg) else "NOT AVAILABLE"} (expected {source})') a, b, c = notification_counts(notify) print(f'Notifications configured: webhooks={a}, Kuma overall={b}, Kuma jobs={c} (tokens hidden)') print('Same phase: parallel; next phase starts after all members complete.') @@ -551,17 +568,20 @@ def execute_one(job, folder, hub): return result -def live_run(jobs, args): +def live_run(jobs, args, host_cfg=None): if not jobs: print('Nothing enabled; no backups executed.') return 0 if os.environ.get(CUTOVER_ENV) != 'YES': print(f'REFUSED: Set {CUTOVER_ENV}=YES only after testing scripts and coordinating existing schedules.', file=sys.stderr) return 2 - if not nas_mount_ready(): - print(f'REFUSED: {NAS_MOUNT} is not mounted from {NAS_SOURCE}', file=sys.stderr) + cfg = host_cfg or load_host_config(host_name=getattr(args, 'host', None)) + mountpoint = cfg.get('nas_mount', NAS_MOUNT) + source = cfg.get('nas_source', NAS_SOURCE) + if not nas_mount_ready(host_cfg=cfg): + print(f'REFUSED: {mountpoint} is not mounted from {source}', file=sys.stderr) return 2 - conflicts = legacy_schedules() + conflicts = legacy_schedules() if host_cfg is None else legacy_schedules(cfg) if conflicts: print('REFUSED: existing scheduled backups could overlap this run:', file=sys.stderr) for issue in conflicts: @@ -1071,9 +1091,10 @@ def check_compatibility(verification, target_name, targets_cfg, current_host=Non dump_migrations = extract_migrations_from_sql_dump(verification['dump_path']) if code_migrations is None: - app_dir = Path(tcfg.get('app_dir', '')) - mig_dir = app_dir / 'backend' / 'migrations' - if mig_dir.is_dir(): + app_dir = Path(tcfg.get('app_dir', tcfg.get('compose_dir', ''))) + mig_rel = tcfg.get('migrations_dir', 'backend/migrations') + mig_dir = app_dir / mig_rel if app_dir and str(app_dir) != '.' else Path('') + if mig_dir and mig_dir.is_dir(): code_migrations = discover_code_migrations(mig_dir) schema_relation = 'UNKNOWN' @@ -1324,13 +1345,15 @@ def execute_recovery_command(mode, args, targets_cfg): result_file = folder / 'adapter-result.json' log_file = folder / 'recovery.log' - adapter_dir = Path(tcfg.get('adapter_dir', '/etc/backup-orchestrator/scripts/shinkan')) + app_id = tcfg.get('application', 'app') + adapter_dir = Path(tcfg.get('adapter_dir', f'/etc/backup-orchestrator/scripts/{app_id}')) if not adapter_dir.is_dir(): - adapter_dir = Path(__file__).resolve().parent / 'scripts' / 'shinkan' + adapter_dir = Path(__file__).resolve().parent / 'scripts' / app_id dump_path = plan_doc['recovery_point']['dump_path'] if mode == 'restore-test': - script = adapter_dir / 'shinkan-restore-test' + custom_script = tcfg.get('restore_test_script') + script = Path(custom_script) if custom_script and Path(custom_script).is_file() else (adapter_dir / f'{app_id}-restore-test') extra = ['--dump', dump_path, '--result', str(result_file)] if getattr(args, 'playwright', False): extra.append('--playwright') @@ -1338,7 +1361,8 @@ def execute_recovery_command(mode, args, targets_cfg): extra.append('--keep') cmd = _build_script_cmd(script, extra) else: - script = adapter_dir / 'shinkan-restore' + custom_script = tcfg.get('restore_script') + script = Path(custom_script) if custom_script and Path(custom_script).is_file() else (adapter_dir / f'{app_id}-restore') extra = [ '--target', target_name, '--dump', dump_path, '--result', str(result_file), '--sessions', args.sessions,