Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziosalmi committed Feb 1, 2025
1 parent 7f3d868 commit 7de03bc
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 37 deletions.
74 changes: 51 additions & 23 deletions lxc_autoscale/config.py
Original file line number Diff line number Diff line change
@@ -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."""
Expand Down Expand Up @@ -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 = [
Expand Down
18 changes: 6 additions & 12 deletions lxc_autoscale/logging_setup.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,33 @@
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.
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
)

# 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()
Expand All @@ -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()
4 changes: 2 additions & 2 deletions lxc_autoscale/lxc_autoscale.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 7de03bc

Please sign in to comment.