Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions backend/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ async def startup_event():
init_db()

if DISTRIBOX_MODE == "slave":
print(f"✓ Starting in SLAVE mode")
logger.info("Starting in SLAVE mode")
asyncio.create_task(_slave_heartbeat_loop())
return

Expand All @@ -99,7 +99,7 @@ async def startup_event():
)
session.add(admin)
session.commit()
print(f"✓ Created default admin user: {admin_username}")
logger.info("Created default admin user: %s", admin_username)
else:
should_save_admin = False
if DISTRIBOX_ADMIN_POLICY not in admin.policies:
Expand All @@ -115,7 +115,7 @@ async def startup_event():
if should_save_admin:
session.add(admin)
session.commit()
print(f"✓ Admin user already exists: {admin_username}")
logger.info("Admin user already exists: %s", admin_username)

users = session.exec(
select(UserORM).where(UserORM.password.is_not(None))
Expand All @@ -128,14 +128,14 @@ async def startup_event():
migrated_usernames.append(user.username)
if migrated_usernames:
session.commit()
print(
"Encrypted plaintext passwords for users: " +
", ".join(migrated_usernames)
logger.info(
"Encrypted plaintext passwords for users: %s",
", ".join(migrated_usernames),
)

asyncio.create_task(_enforce_event_deadlines())
asyncio.create_task(_check_stale_slaves())
print(f"✓ Starting in MASTER mode")
logger.info("Starting in MASTER mode")


async def _enforce_event_deadlines():
Expand Down
6 changes: 3 additions & 3 deletions backend/app/services/event_service.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import re
import uuid
from datetime import datetime
Expand All @@ -19,6 +20,8 @@
from app.services.slave_service import SlaveService
from app.services.slave_client import slave_get_host_info

logger = logging.getLogger(__name__)


def _sanitize_name(name: str) -> str:
sanitized = re.sub(r"[^a-z0-9-]", "-", name.lower().strip())
Expand Down Expand Up @@ -166,9 +169,6 @@ def _pick_node_for_vm(required_mem: int, required_vcpus: int, required_disk: int

Returns None for master, or slave UUID for a slave node.
"""
import logging
logger = logging.getLogger(__name__)

# Check master first
try:
master = HostService.get_host_info()
Expand Down
54 changes: 0 additions & 54 deletions backend/app/services/host_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,6 @@
from app.services.vm_service import VmService
from app.core.config import system_monitor

# cpu_total_usage = 0
# usage_per_cpu = []

# def get_cpu_usage_percent(cpu_idle_time_t2, cpu_idle_time_t1, cpu_total_time_t2, cpu_total_time_t1):
# return (1 - (cpu_idle_time_t2 - cpu_idle_time_t1)/
# (sum(cpu_total_time_t2) - sum(cpu_total_time_t1))) * 100

# def get_cpu_counters():
# per_cpus = psutil.cpu_times(percpu=True)
# total = Counter()
# for cpu in per_cpus:
# total.update(cpu._asdict())
# cpu_total = psutil.cpu_times()
# return {
# "per_cpus": per_cpus,
# "cpu_total": cpu_total
# }

# def get_cpu_usage():
# global cpu_total_usage, usage_per_cpu
# while True:
# cpu_usage_t1 = get_cpu_counters()
# sleep(3)
# cpu_usage_t2 = get_cpu_counters()
# cpu_total_usage = get_cpu_usage_percent(cpu_usage_t2["cpu_total"].idle, cpu_usage_t1["cpu_total"].idle, cpu_usage_t2["cpu_total"], cpu_usage_t1["cpu_total"])
# usage_per_cpu = []
# for i in range(len(cpu_usage_t1["per_cpus"])):
# usage_per_cpu.append(round(get_cpu_usage_percent(cpu_usage_t2["per_cpus"][i].idle, cpu_usage_t1["per_cpus"][i].idle, cpu_usage_t2["per_cpus"][i], cpu_usage_t1["per_cpus"][i]), 2))


# Thread(target=get_cpu_usage, daemon=True).start()

# cpu_overall_time_t1 = psutil.cpu_times(percpu=True)
# cpu_total_time_t1 = psutil.cpu_times()
# print(sum(cpu_total_time_t1))
# cpu_idle_time_t1 = cpu_total_time_t1.idle


# sleep(3)
# cpu_total_time_t2 = psutil.cpu_times()
# cpu_idle_time_t2 = cpu_total_time_t2.idle

# percent_used = (1 - (cpu_idle_time_t2 - cpu_idle_time_t1)/ (sum(cpu_total_time_t2) - sum(cpu_total_time_t1))) * 100
# print(percent_used)
# print(psutil.cpu_percent(interval=1))
# print(sum(psutil.cpu_times(percpu=True)))


# print(psutil.cpu_times_percent(interval=2))


# for dom_stats in stats:
# data = dom_stats[1]
# print(data.get('cpu.system'))

class HostService:

Expand Down
13 changes: 8 additions & 5 deletions backend/app/services/image_service.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import logging
import subprocess
import json
import yaml
from app.models.image import ImageRead
from pathlib import Path
from app.core.config import s3, distribox_bucket_registry

logger = logging.getLogger(__name__)


class ImageService():

Expand All @@ -17,10 +20,10 @@ def get_distribox_image(image_name):
data = yaml.safe_load(content)
return ImageRead(**data)
except s3.exceptions.NoSuchKey:
print(f"No image found")
logger.warning("No image found: %s", image_name)
return None
except Exception as e:
print(f"Error")
except Exception:
logger.exception("Error fetching image: %s", image_name)
return None

@staticmethod
Expand All @@ -39,6 +42,6 @@ def get_distribox_image_list():
try:
image = ImageRead(**data)
images.append(image)
except Exception as e:
print(f"Error on {key}: {e}")
except Exception:
logger.warning("Failed to parse image %s", key)
return images
Loading