Skip to content

Commit

Permalink
Update resource_manager.py
Browse files Browse the repository at this point in the history
updated to work on docker
  • Loading branch information
fabriziosalmi authored Aug 31, 2024
1 parent 946d6f3 commit 0a88c99
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions lxc_autoscale/resource_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,41 @@
import lxc_utils
import scaling_manager
import notification
import paramiko
from concurrent.futures import ThreadPoolExecutor, as_completed

import paramiko

# 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.
Args:
ctid (str): The container ID.
Returns:
dict: The data collected for the container, or None if the container is not running.
"""
if not lxc_utils.is_container_running(ctid):
return None

logging.debug(f"Collecting data for container {ctid}...")

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])
settings = {"cores": cores, "memory": memory}

# Backup the current settings
lxc_utils.backup_container_settings(ctid, settings)

# Collect CPU and memory usage data
return {
ctid: {
Expand All @@ -50,7 +58,7 @@ def collect_data_for_container(ctid: str) -> dict:
def collect_container_data() -> dict:
"""
Collect resource usage data for all LXC containers.
Returns:
dict: A dictionary where the keys are container IDs and the values are their respective data.
"""
Expand All @@ -69,7 +77,7 @@ def collect_container_data() -> dict:
def main_loop(poll_interval: int, energy_mode: bool):
"""
Main loop that handles the resource allocation and scaling process.
Args:
poll_interval (int): The interval in seconds between each resource allocation process.
energy_mode (bool): A flag to indicate if energy efficiency mode should be enabled during off-peak hours.
Expand Down

0 comments on commit 0a88c99

Please sign in to comment.