|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Copyright (C) 2026 Vates SAS |
| 4 | +# |
| 5 | +# This program is free software: you can redistribute it and/or modify |
| 6 | +# it under the terms of the GNU General Public License as published by |
| 7 | +# the Free Software Foundation, either version 3 of the License, or |
| 8 | +# (at your option) any later version. |
| 9 | +# This program is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +# GNU General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU General Public License |
| 15 | +# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +import contextlib |
| 18 | +import json |
| 19 | + |
| 20 | +import xcp_storage.log as log |
| 21 | +from xcp_storage.utils.process import run_command |
| 22 | + |
| 23 | +from xcp_storage.typing import ( |
| 24 | + Any, |
| 25 | + Dict, |
| 26 | + Iterator, |
| 27 | +) |
| 28 | + |
| 29 | +# ============================================================================== |
| 30 | + |
| 31 | +@contextlib.contextmanager |
| 32 | +def _handle_drbd_json_error() -> Iterator[None]: |
| 33 | + try: |
| 34 | + yield |
| 35 | + except KeyError as e: |
| 36 | + log.error( |
| 37 | + f"The key `{e}` could not be found in the DRBD configuration. The JSON format may have changed.", |
| 38 | + exc_info=True |
| 39 | + ) |
| 40 | + except Exception as e: |
| 41 | + log.error(f"Failed to parse DRBD configuration: `{e}`. The JSON format may have changed.", exc_info=True) |
| 42 | + |
| 43 | +def _get_drbd_status(resource_name: str) -> Dict[str, Any]: |
| 44 | + stdout, _stderr, ret_code = run_command(["drbdsetup", "status", resource_name, "--json"], simple=False) |
| 45 | + if ret_code != 0: |
| 46 | + return {} |
| 47 | + |
| 48 | + try: |
| 49 | + status = json.loads(stdout) |
| 50 | + except Exception as e: |
| 51 | + log.error(f"Failed to read DRBD status as JSON: `{e}`.") |
| 52 | + return {} |
| 53 | + |
| 54 | + with _handle_drbd_json_error(): |
| 55 | + return status[0] |
| 56 | + return {} |
| 57 | + |
| 58 | +# ------------------------------------------------------------------------------ |
| 59 | + |
| 60 | +def get_drbd_connection_address(resource_name: str, node_name: str) -> str: |
| 61 | + status = _get_drbd_status(resource_name) |
| 62 | + if not status: |
| 63 | + return "" |
| 64 | + |
| 65 | + with _handle_drbd_json_error(): |
| 66 | + for connection in status["connections"]: |
| 67 | + if connection["name"] == node_name: |
| 68 | + return connection["paths"][0]["remote_host"]["address"] |
| 69 | + return "" |
| 70 | + |
| 71 | +def get_drbd_primary_address(resource_name: str) -> str: |
| 72 | + status = _get_drbd_status(resource_name) |
| 73 | + if not status: |
| 74 | + return "" |
| 75 | + |
| 76 | + with _handle_drbd_json_error(): |
| 77 | + if status["role"] == "Primary": |
| 78 | + return status["connections"][0]["paths"][0]["this_host"]["address"] |
| 79 | + |
| 80 | + for connection in status["connections"]: |
| 81 | + if connection["peer-role"] == "Primary": |
| 82 | + return connection["paths"][0]["remote_host"]["address"] |
| 83 | + |
| 84 | + return "" |
0 commit comments