diff --git a/proxlb/main.py b/proxlb/main.py index f1df87f..ef5e2a4 100644 --- a/proxlb/main.py +++ b/proxlb/main.py @@ -74,6 +74,9 @@ def main(): if not cli_args.dry_run: Balancing(proxmox_api, proxlb_data) + # Validate if the JSON output should be + # printed to stdout + Helper.print_json(proxlb_data, cli_args.json) # Validate daemon mode Helper.get_daemon_mode(proxlb_config) diff --git a/proxlb/models/calculations.py b/proxlb/models/calculations.py index 8b4d9c4..8b625ae 100644 --- a/proxlb/models/calculations.py +++ b/proxlb/models/calculations.py @@ -207,7 +207,7 @@ class Calculations: None """ logger.debug("Starting: relocate_guests.") - if proxlb_data["meta"]["balancing"]["balance"] or proxlb_data["meta"]["balancing"]["enforce_affinity"]: + if proxlb_data["meta"]["balancing"]["balance"] or proxlb_data["meta"]["balancing"].get("enforce_affinity", False): if proxlb_data["meta"]["balancing"].get("balance", False): logger.debug("Balancing of guests will be performt. Reason: balanciness") diff --git a/proxlb/utils/helper.py b/proxlb/utils/helper.py index e3bbe0f..679f8b2 100644 --- a/proxlb/utils/helper.py +++ b/proxlb/utils/helper.py @@ -8,6 +8,7 @@ __copyright__ = "Copyright (C) 2025 Florian Paul Azim Hoberg (@gyptazy)" __license__ = "GPL-3.0" +import json import uuid import sys import time @@ -124,3 +125,23 @@ class Helper: sys.exit(0) logger.debug("Finished: get_daemon_mode.") + + @staticmethod + def print_json(proxlb_config: Dict[str, Any], print_json: bool = False) -> None: + """ + Prints the calculated balancing matrix as a JSON output to stdout. + + Parameters: + proxlb_config (Dict[str, Any]): A dictionary containing the ProxLB configuration. + + Returns: + None + """ + logger.debug("Starting: print_json.") + if print_json: + # Create a filtered list by stripping the 'meta' key from the proxlb_config dictionary + # to make sure that no credentials are leaked. + filtered_data = {k: v for k, v in proxlb_config.items() if k != "meta"} + print(json.dumps(filtered_data, indent=4)) + + logger.debug("Finished: print_json.")