diff --git a/orchestrator.py b/orchestrator.py index 2674f68..842466e 100644 --- a/orchestrator.py +++ b/orchestrator.py @@ -67,9 +67,12 @@ def lock_exclusive(fp): def resolve_repo_host_dir(host_name=None): - host = host_name or socket.gethostname().split('.')[0] - candidate = Path(__file__).resolve().parent / 'hosts' / host - return candidate if candidate.is_dir() else None + try: + host = host_name or socket.gethostname().split('.')[0] + candidate = Path(__file__).resolve().parent / 'hosts' / host + return candidate if candidate.is_dir() else None + except (OSError, PermissionError): + return None def load_host_config(path=DEFAULT_HOST_CONFIG, host_name=None): @@ -95,12 +98,15 @@ def load_host_config(path=DEFAULT_HOST_CONFIG, host_name=None): if host_dir: candidates.append(host_dir / 'host.json') for candidate in candidates: - if candidate.is_file(): - with candidate.open(encoding='utf-8') as fp: - data = json.load(fp) - if isinstance(data, dict): - default.update(data) - break + try: + if candidate.is_file(): + with candidate.open(encoding='utf-8') as fp: + data = json.load(fp) + if isinstance(data, dict): + default.update(data) + break + except (OSError, PermissionError): + continue return default @@ -117,9 +123,12 @@ def load_targets_config(path=DEFAULT_TARGETS_CONFIG, host_name=None): candidates.append(host_dir / 'targets.json') candidates.append(Path(__file__).resolve().parent / 'targets.json') for candidate in candidates: - if candidate.is_file(): - with candidate.open(encoding='utf-8') as fp: - return json.load(fp) + try: + if candidate.is_file(): + with candidate.open(encoding='utf-8') as fp: + return json.load(fp) + except (OSError, PermissionError): + continue return {'schemaVersion': 1, 'hosts': {}, 'targets': {}} diff --git a/tests/test_orchestrator.py b/tests/test_orchestrator.py index 417606a..58fcdce 100644 --- a/tests/test_orchestrator.py +++ b/tests/test_orchestrator.py @@ -20,6 +20,7 @@ class OrchestratorTests(unittest.TestCase): self.root = Path(self.temp.name) self.patches = [patch.object(app, 'legacy_schedules', return_value=[]), patch.object(app, 'nas_mount_ready', return_value=True), + patch.object(app, 'load_host_config', return_value={'hostname':'test-host', 'nas_mount':'/backup', 'nas_source':'192.168.2.63:/volume1/Backup_Paperless'}), patch.dict(os.environ, {'BACKUP_ORCHESTRATOR_CUTOVER_APPROVED':'YES'})] for p in self.patches: p.start()