From 7de03bc562e2931e1027680d7b45d313d7cb78af Mon Sep 17 00:00:00 2001 From: fabriziosalmi Date: Sat, 1 Feb 2025 11:46:35 +0100 Subject: [PATCH] fixes --- lxc_autoscale/config.py | 74 +++++++++++++++++++++++----------- lxc_autoscale/logging_setup.py | 18 +++------ lxc_autoscale/lxc_autoscale.py | 4 +- 3 files changed, 59 insertions(+), 37 deletions(-) diff --git a/lxc_autoscale/config.py b/lxc_autoscale/config.py index 04db8ba..60ec544 100644 --- a/lxc_autoscale/config.py +++ b/lxc_autoscale/config.py @@ -1,24 +1,59 @@ -import logging import os import sys +import yaml from socket import gethostname from typing import Any, Dict, List, Set, Union -import yaml - -CONFIG_FILE = "/etc/lxc_autoscale/lxc_autoscale.yaml" - -if not os.path.exists(CONFIG_FILE): - sys.exit(f"Configuration file {CONFIG_FILE} does not exist. Exiting...") - -with open(CONFIG_FILE, 'r', encoding='utf-8') as file: - config: Dict[str, Any] = yaml.safe_load(file) - -if not isinstance(config, dict): - sys.exit("Invalid configuration format. Expected a dictionary.") - -# --- Default Configuration (moved to top) --- -DEFAULTS: Dict[str, Any] = config.get('DEFAULTS', {}) +CONFIG_FILE = '/etc/lxc_autoscale/lxc_autoscale.yml' +LOG_FILE = '/var/log/lxc_autoscale.log' +BACKUP_DIR = '/var/lib/lxc_autoscale/backups' +PROXMOX_HOSTNAME = os.uname().nodename + +# Load configuration +def load_config() -> Dict[str, Any]: + try: + with open(CONFIG_FILE, 'r') as f: + return yaml.safe_load(f) or {} + except Exception: + return {} + +config = load_config() + +# Default settings +DEFAULTS = { + 'poll_interval': 300, + 'energy_mode': False, + 'behaviour': 'normal', + 'reserve_cpu_percent': 10, + 'reserve_memory_mb': 2048, + 'off_peak_start': 22, + 'off_peak_end': 6, + 'cpu_upper_threshold': 80, + 'cpu_lower_threshold': 20, + 'memory_upper_threshold': 80, + 'memory_lower_threshold': 20, + 'min_cores': 1, + 'max_cores': 4, + 'min_memory': 512, + 'core_min_increment': 1, + 'core_max_increment': 2, + 'memory_min_increment': 256, + 'min_decrease_chunk': 128, +} + +# Update defaults with values from config file +DEFAULTS.update(config.get('DEFAULT', {})) + +def get_config_value(section: str, key: str, default: Any = None) -> Any: + """Get configuration value with fallback to default.""" + return config.get(section, {}).get(key, DEFAULTS.get(key, default)) + +IGNORE_LXC = set(config.get('DEFAULT', {}).get('ignore_lxc', [])) +HORIZONTAL_SCALING_GROUPS = config.get('horizontal_scaling_groups', {}) +LXC_TIER_ASSOCIATIONS = config.get('lxc_tier_associations', {}) +CPU_SCALE_DIVISOR = config.get('cpu_scale_divisor', 10) +MEMORY_SCALE_FACTOR = config.get('memory_scale_factor', 1.5) +TIMEOUT_EXTENDED = 300 def load_tier_configurations() -> Dict[str, Dict[str, Any]]: """Load and validate tier configurations.""" @@ -49,13 +84,6 @@ def load_tier_configurations() -> Dict[str, Dict[str, Any]]: return tier_configs -def get_config_value(section: str, key: str, default: Any) -> Any: - """Retrieve a configuration value with a fallback to a default.""" - # Map "DEFAULT" to "DEFAULTS" to match YAML - if section == "DEFAULT": - section = "DEFAULTS" - return config.get(section, {}).get(key, default) - def validate_config() -> None: """Validate essential configuration values.""" required_defaults = [ diff --git a/lxc_autoscale/logging_setup.py b/lxc_autoscale/logging_setup.py index 033ad70..12ade55 100644 --- a/lxc_autoscale/logging_setup.py +++ b/lxc_autoscale/logging_setup.py @@ -1,9 +1,7 @@ import logging # Import the logging module to handle logging throughout the application import os # Import os module to handle directory operations -# Removed: from config import get_config_value and global LOG_FILE assignment - -def setup_logging(): +def setup_logging(log_file: str = '/var/log/lxc_autoscale.log') -> None: """ Set up the logging configuration for the application. This function configures logging to write to both a log file and the console. @@ -11,18 +9,14 @@ def setup_logging(): Log messages will include timestamps and the severity level of the message. """ - # Lazy import to break circular dependency - from config import get_config_value - LOG_FILE = get_config_value('DEFAULT', 'log_file', '/var/log/lxc_autoscale.log') - # Ensure the directory for the log file exists - log_dir = os.path.dirname(LOG_FILE) + log_dir = os.path.dirname(log_file) if not os.path.exists(log_dir): os.makedirs(log_dir) # Configure the logging to write to a file with the specified format and date format logging.basicConfig( - filename=LOG_FILE, # Log file path + filename=log_file, # Log file path level=logging.INFO, # Log level: INFO (this can be adjusted to DEBUG, WARNING, etc.) format='%(asctime)s - %(levelname)s - %(message)s', # Format of log messages datefmt='%Y-%m-%d %H:%M:%S' # Date format for timestamps @@ -30,10 +24,10 @@ def setup_logging(): # Debug: Verify that the log file is writable try: - with open(LOG_FILE, 'a') as f: + with open(log_file, 'a') as f: f.write("# Log file initialized successfully.\n") except Exception as err: - print(f"Error writing to log file ({LOG_FILE}): {err}") + print(f"Error writing to log file ({log_file}): {err}") # Create a console handler to output log messages to the console console = logging.StreamHandler() @@ -47,7 +41,7 @@ def setup_logging(): logging.getLogger().addHandler(console) # Ensure that log messages are flushed immediately to the file. - logging.info("Logging is set up. Log file: %s", LOG_FILE) + logging.info("Logging is set up. Log file: %s", log_file) for handler in logging.getLogger().handlers: if hasattr(handler, 'flush'): handler.flush() diff --git a/lxc_autoscale/lxc_autoscale.py b/lxc_autoscale/lxc_autoscale.py index 757476f..9d5f65f 100644 --- a/lxc_autoscale/lxc_autoscale.py +++ b/lxc_autoscale/lxc_autoscale.py @@ -54,8 +54,8 @@ def parse_arguments() -> argparse.Namespace: # Entry point of the script if __name__ == "__main__": - # Setup logging based on the configuration - setup_logging() + # Setup logging with the configured log file + setup_logging(LOG_FILE) # Parse command-line arguments args: argparse.Namespace = parse_arguments()