Merge pull request #159 from gyptazy/feature/json-output

fix: Add JSON output again
This commit is contained in:
Florian
2025-03-25 09:34:10 +01:00
committed by GitHub
3 changed files with 25 additions and 1 deletions

View File

@@ -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)

View File

@@ -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")

View File

@@ -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.")