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 7de03bc commit 955c06e
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 63 deletions.
13 changes: 11 additions & 2 deletions lxc_autoscale/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import yaml
from socket import gethostname
from typing import Any, Dict, List, Set, Union
import logging

CONFIG_FILE = '/etc/lxc_autoscale/lxc_autoscale.yml'
LOG_FILE = '/var/log/lxc_autoscale.log'
Expand All @@ -11,11 +12,19 @@

# Load configuration
def load_config() -> Dict[str, Any]:
"""Load configuration from YAML file with better error handling."""
try:
with open(CONFIG_FILE, 'r') as f:
with open(CONFIG_FILE, 'r', encoding='utf-8') as f:
return yaml.safe_load(f) or {}
except Exception:
except FileNotFoundError:
logging.warning(f"Config file not found at {CONFIG_FILE}, using defaults")
return {}
except yaml.YAMLError as e:
logging.error(f"Error parsing config file: {e}")
sys.exit(1)
except Exception as e:
logging.error(f"Unexpected error loading config: {e}")
sys.exit(1)

config = load_config()

Expand Down
47 changes: 0 additions & 47 deletions lxc_autoscale/logging_setup.py

This file was deleted.

13 changes: 5 additions & 8 deletions lxc_autoscale/lxc_autoscale.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@

import argparse
import logging
import os # Added import for directory operations
from typing import Optional

import paramiko # Import the paramiko library

from config import DEFAULTS, get_config_value, IGNORE_LXC, LOG_FILE, PROXMOX_HOSTNAME, BACKUP_DIR # Import configuration constants and utility functions
from logging_setup import setup_logging # Import the logging setup function
from lock_manager import acquire_lock # Function to acquire a lock, ensuring only one instance of the script runs
from lxc_utils import get_containers, rollback_container_settings # Utility functions for managing LXC containers
from resource_manager import main_loop # Main loop function that handles the resource allocation and scaling process
from config import DEFAULTS, LOG_FILE
from logging_setup import setup_logging
from lock_manager import acquire_lock
from lxc_utils import get_containers, rollback_container_settings
from resource_manager import main_loop



Expand Down
5 changes: 2 additions & 3 deletions lxc_autoscale/lxc_autoscale.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# Default configuration values
DEFAULTS:
# Log file path
log_file: /var/log/lxc_autoscale.log
# Lock file path
lock_file: /var/lock/lxc_autoscale.lock
# Backup directory path
backup_dir: /var/lib/lxc_autoscale/backups
reserve_cpu_percent: 10
reserve_memory_mb: 2048
# Percentage of CPU cores to reserve (e.g., 10%)
reserve_cpu_percent: 10
# Amount of memory (in MB) to reserve (e.g., 2048 MB)
Expand Down
6 changes: 3 additions & 3 deletions lxc_autoscale/lxc_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
# Global variable to hold the SSH client
ssh_client: Optional[paramiko.SSHClient] = None

def get_ssh_client() -> Optional[paramiko.SSHClient]:
"""Get or create a new SSH client connection."""
def get_ssh_client() -> Optional['paramiko.SSHClient']:
"""Get or create SSH client with better error handling."""
global ssh_client
if ssh_client is None:
logging.debug("Creating a new SSH connection...")
Expand All @@ -43,7 +43,7 @@ def get_ssh_client() -> Optional[paramiko.SSHClient]:
logging.error("SSH connection failed: %s", str(e))
return None
except Exception as e:
logging.error("Unexpected error establishing SSH connection: %s", str(e))
logging.error(f"Failed to create SSH client: {e}")
return None
return ssh_client

Expand Down
4 changes: 4 additions & 0 deletions lxc_autoscale/resource_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ def collect_data_for_container(ctid: str) -> Optional[Dict[str, Any]]:
Returns:
A dictionary with resource data for the container, or None if the container is not running.
"""
if ctid in IGNORE_LXC:
logging.debug(f"Skipping ignored container {ctid}")
return None

if not lxc_utils.is_container_running(ctid):
logging.debug(f"Container {ctid} is not running")
return None
Expand Down

0 comments on commit 955c06e

Please sign in to comment.