|
| 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 | +from xcp_storage.utils.process import run_command |
| 18 | +from xcp_storage.utils.service import ( |
| 19 | + is_service_active, |
| 20 | + restart_service, |
| 21 | + start_service, |
| 22 | + stop_service, |
| 23 | +) |
| 24 | + |
| 25 | +from .satellite import LINSTOR_SATELLITE_PORT_PLAIN, LINSTOR_SATELLITE_PORT_SSL |
| 26 | + |
| 27 | +from xcp_storage.typing import List |
| 28 | + |
| 29 | +# ============================================================================== |
| 30 | + |
| 31 | +LINSTOR_CONTROLLER_PORT_PLAIN = 3370 |
| 32 | +LINSTOR_CONTROLLER_PORT_SSL = 3371 |
| 33 | + |
| 34 | +# ------------------------------------------------------------------------------ |
| 35 | + |
| 36 | +_SERVICE_LINSTOR_CONTROLLER = "linstor-controller" |
| 37 | + |
| 38 | +# ------------------------------------------------------------------------------ |
| 39 | + |
| 40 | +def get_controller_addresses() -> List[str]: |
| 41 | + stdout = run_command([ |
| 42 | + "/usr/sbin/ss", "-tnpH", "state", "established", |
| 43 | + f"( sport = :{LINSTOR_SATELLITE_PORT_PLAIN} or sport = :{LINSTOR_SATELLITE_PORT_SSL} )" |
| 44 | + ], expected_ret_code=0) |
| 45 | + return [ |
| 46 | + line.split()[3].rsplit(":", 1)[0] |
| 47 | + for line in stdout.splitlines() |
| 48 | + ] |
| 49 | + |
| 50 | +def get_controller_uri() -> str: |
| 51 | + # TODO: On caller side, check that an IP address from the current pool is returned. |
| 52 | + addresses = get_controller_addresses() |
| 53 | + return "linstor://" + addresses[0] if addresses else "" |
| 54 | + |
| 55 | +# ------------------------------------------------------------------------------ |
| 56 | + |
| 57 | +def is_controller_running() -> bool: |
| 58 | + return is_service_active(_SERVICE_LINSTOR_CONTROLLER) |
| 59 | + |
| 60 | +def start_controller() -> None: |
| 61 | + start_service(_SERVICE_LINSTOR_CONTROLLER) |
| 62 | + |
| 63 | +def stop_controller() -> None: |
| 64 | + stop_service(_SERVICE_LINSTOR_CONTROLLER) |
| 65 | + |
| 66 | +def restart_controller() -> None: |
| 67 | + restart_service(_SERVICE_LINSTOR_CONTROLLER) |
0 commit comments