Skip to content

Commit

Permalink
Update resource_manager.py
Browse files Browse the repository at this point in the history
Hopefully should fix [this issue](#9)
  • Loading branch information
fabriziosalmi authored Nov 29, 2024
1 parent 77ce49a commit 5e90000
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions lxc_autoscale/resource_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
# Debug print statement to ensure paramiko is imported
# print(f"Paramiko version: {paramiko.__version__}")



def collect_data_for_container(ctid: str) -> dict:
"""
Collect resource usage data for a single LXC container.
Expand All @@ -32,8 +30,36 @@ def collect_data_for_container(ctid: str) -> dict:
try:
# Retrieve the current configuration of the container using Python string operations
config_output = lxc_utils.run_command(f"pct config {ctid}")
cores = int([line.split()[1] for line in config_output.splitlines() if 'cores' in line][0])
memory = int([line.split()[1] for line in config_output.splitlines() if 'memory' in line][0])

# Initialize values for cores and memory
cores = None
memory = None

# Parse the config_output for cores and memory
for line in config_output.splitlines():
# Check if 'cores' and 'memory' exist and extract their values safely
if 'cores' in line:
try:
cores_value = line.split()[1]
if cores_value.isdigit(): # Ensure it's a valid integer string
cores = int(cores_value)
else:
logging.warning(f"Invalid value for cores: {cores_value}")
except IndexError:
logging.warning(f"Unable to extract cores value from line: {line}")
elif 'memory' in line:
try:
memory_value = line.split()[1]
if memory_value.isdigit(): # Ensure it's a valid integer string
memory = int(memory_value)
else:
logging.warning(f"Invalid value for memory: {memory_value}")
except IndexError:
logging.warning(f"Unable to extract memory value from line: {line}")

if cores is None or memory is None:
raise ValueError(f"Failed to extract valid cores or memory values for container {ctid}")

settings = {"cores": cores, "memory": memory}

# Backup the current settings
Expand All @@ -55,6 +81,7 @@ def collect_data_for_container(ctid: str) -> dict:
logging.error(f"Error retrieving or parsing configuration for container {ctid}: {e}")
return None


def collect_container_data() -> dict:
"""
Collect resource usage data for all LXC containers.
Expand Down

0 comments on commit 5e90000

Please sign in to comment.