Skip to content

Commit 955c06e

Browse files
committed
fixes
1 parent 7de03bc commit 955c06e

File tree

6 files changed

+25
-63
lines changed

6 files changed

+25
-63
lines changed

lxc_autoscale/config.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import yaml
44
from socket import gethostname
55
from typing import Any, Dict, List, Set, Union
6+
import logging
67

78
CONFIG_FILE = '/etc/lxc_autoscale/lxc_autoscale.yml'
89
LOG_FILE = '/var/log/lxc_autoscale.log'
@@ -11,11 +12,19 @@
1112

1213
# Load configuration
1314
def load_config() -> Dict[str, Any]:
15+
"""Load configuration from YAML file with better error handling."""
1416
try:
15-
with open(CONFIG_FILE, 'r') as f:
17+
with open(CONFIG_FILE, 'r', encoding='utf-8') as f:
1618
return yaml.safe_load(f) or {}
17-
except Exception:
19+
except FileNotFoundError:
20+
logging.warning(f"Config file not found at {CONFIG_FILE}, using defaults")
1821
return {}
22+
except yaml.YAMLError as e:
23+
logging.error(f"Error parsing config file: {e}")
24+
sys.exit(1)
25+
except Exception as e:
26+
logging.error(f"Unexpected error loading config: {e}")
27+
sys.exit(1)
1928

2029
config = load_config()
2130

lxc_autoscale/logging_setup.py

-47
This file was deleted.

lxc_autoscale/lxc_autoscale.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@
22

33
import argparse
44
import logging
5-
import os # Added import for directory operations
65
from typing import Optional
76

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

1613

1714

lxc_autoscale/lxc_autoscale.yaml

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
# Default configuration values
22
DEFAULTS:
3-
# Log file path
43
log_file: /var/log/lxc_autoscale.log
5-
# Lock file path
64
lock_file: /var/lock/lxc_autoscale.lock
7-
# Backup directory path
85
backup_dir: /var/lib/lxc_autoscale/backups
6+
reserve_cpu_percent: 10
7+
reserve_memory_mb: 2048
98
# Percentage of CPU cores to reserve (e.g., 10%)
109
reserve_cpu_percent: 10
1110
# Amount of memory (in MB) to reserve (e.g., 2048 MB)

lxc_autoscale/lxc_utils.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
# Global variable to hold the SSH client
2222
ssh_client: Optional[paramiko.SSHClient] = None
2323

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

lxc_autoscale/resource_manager.py

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ def collect_data_for_container(ctid: str) -> Optional[Dict[str, Any]]:
3030
Returns:
3131
A dictionary with resource data for the container, or None if the container is not running.
3232
"""
33+
if ctid in IGNORE_LXC:
34+
logging.debug(f"Skipping ignored container {ctid}")
35+
return None
36+
3337
if not lxc_utils.is_container_running(ctid):
3438
logging.debug(f"Container {ctid} is not running")
3539
return None

0 commit comments

Comments
 (0)