Compare commits

..

No commits in common. "66954eb63449160bd39c747d4d1ab39661305ed1" and "b120c6904ec8633f1aaaf40184296223075785d6" have entirely different histories.

8 changed files with 31 additions and 54 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 112 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 118 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 87 KiB

View File

Before

Width:  |  Height:  |  Size: 32 KiB

After

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

View File

@ -1,20 +0,0 @@
# Generated by Django 5.1.5 on 2025-04-20 18:59
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("webpanel", "0009_server_container_id_server_last_log"),
]
operations = [
migrations.AlterField(
model_name="game",
name="thumbnail",
field=models.ImageField(
blank=True, null=True, upload_to="game_thumbnails/"
),
),
]

View File

@ -2,14 +2,14 @@ from django.db import models
import podman import podman
import shlex import shlex
import re import re
from typing import Optional,List
class Game(models.Model): class Game(models.Model):
name = models.CharField(max_length=100) name = models.CharField(max_length=100)
genre = models.CharField(max_length=50, blank=True, null=True) genre = models.CharField(max_length=50, blank=True, null=True)
thumbnail = models.ImageField(upload_to='game_thumbnails/', null=True, blank=True) thumbnail = models.ImageField(upload_to='media/game_thumbnails/', null=True, blank=True)
def __str__(self) -> str: def __str__(self):
return self.name return self.name
class Server(models.Model): class Server(models.Model):
@ -28,25 +28,15 @@ class Server(models.Model):
container_id = models.CharField(max_length=64, blank=True, null=True) container_id = models.CharField(max_length=64, blank=True, null=True)
status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='offline') status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='offline')
def __str__(self)-> str: def __str__(self):
return f"{self.game.name} Server at {self.ip_address}:{self.port}" return f"{self.game.name} Server at {self.ip_address}:{self.port}"
@property def sync_status(self):
def safe_name(self) -> str:
"""Return a container-safe version of the server name"""
return re.sub(r'[^a-zA-Z0-9_.-]', '_', self.name)
def _log_error(self, msg):
self.last_log = msg
self.save(update_fields=["status", "last_log"])
def _get_podman_client(self) -> podman.PodmanClient:
"""Get a configured Podman client instance."""
return podman.PodmanClient(base_url="unix:///run/user/1000/podman/podman.sock")
def sync_status(self) -> None:
"""Check the real-time status of the container and update the field.""" """Check the real-time status of the container and update the field."""
safe_name = re.sub(r'[^a-zA-Z0-9_.-]', '_', self.name)
client = podman.PodmanClient(base_url="unix:///run/user/1000/podman/podman.sock")
try: try:
container = self._get_podman_client().containers.get(self.safe_name) container = client.containers.get(safe_name)
if container.status == "running": if container.status == "running":
self.status = "online" self.status = "online"
else: else:
@ -57,10 +47,12 @@ class Server(models.Model):
self.status = "offline" self.status = "offline"
self.save() self.save()
def launch_pod_container(self) -> str: def launch_pod_container(self):
safe_name = re.sub(r'[^a-zA-Z0-9_.-]', '_', self.name) # sanitize name
client = podman.PodmanClient(base_url="unix:///run/user/1000/podman/podman.sock")
try: try:
container = self._get_podman_client().containers.create( container = client.containers.create(
name=self.safe_name, name=safe_name,
image=self.image, image=self.image,
ports={ ports={
f'{self.port}/udp': ('0.0.0.0', self.port), f'{self.port}/udp': ('0.0.0.0', self.port),
@ -72,7 +64,7 @@ class Server(models.Model):
container.start() container.start()
self.container_id = container.id self.container_id = container.id
self.last_log = f"Launched container {container.id}" self.last_log = f"Launched container {container.id}"
# self.is_running = True self.is_running = True
self.sync_status() self.sync_status()
self.save() self.save()
return f"Container launched successfully: {container.id}" return f"Container launched successfully: {container.id}"
@ -85,9 +77,11 @@ class Server(models.Model):
self.save() self.save()
return f"Error: {e}" return f"Error: {e}"
def stop_pod_container(self) -> str: def stop_pod_container(self):
safe_name = re.sub(r'[^a-zA-Z0-9_.-]', '_', self.name)
client = podman.PodmanClient(base_url="unix:///run/user/1000/podman/podman.sock")
try: try:
container = self._get_podman_client().containers.get(self.safe_name) container = client.containers.get(safe_name)
container.stop() container.stop()
self.status = "offline" self.status = "offline"
self.last_log = f"Stopped container {container.id}" self.last_log = f"Stopped container {container.id}"
@ -95,29 +89,32 @@ class Server(models.Model):
self.save() self.save()
return f"Container stopped successfully: {container.id}" return f"Container stopped successfully: {container.id}"
except podman.errors.NotFound: except podman.errors.NotFound:
self.last_log = f"Container '{self.safe_name}' not found" self.last_log = f"Container '{safe_name}' not found"
self.save() self.save()
return f"Error: Container '{self.safe_name}' not found" return f"Error: Container '{safe_name}' not found"
except Exception as e: except Exception as e:
self.last_log = f"Error stopping container: {str(e)}" self.last_log = f"Error stopping container: {str(e)}"
self.save() self.save()
return f"Error: {e}" return f"Error: {e}"
def remove_pod_container(self) -> str: def remove_pod_container(self):
safe_name = re.sub(r'[^a-zA-Z0-9_.-]', '_', self.name)
client = podman.PodmanClient(base_url="unix:///run/user/1000/podman/podman.sock")
try: try:
container = self._get_podman_client().containers.get(self.safe_name) container = client.containers.get(safe_name)
container.remove(force=True) container.remove(force=True)
self.status = "offline" self.status = "offline"
self.container_id = None self.container_id = None
self.last_log = f"Removed container {self.safe_name}" self.last_log = f"Removed container {safe_name}"
self.sync_status() self.sync_status()
self.save() self.save()
return f"Container removed successfully: {self.safe_name}" return f"Container removed successfully: {safe_name}"
except podman.errors.NotFound: except podman.errors.NotFound:
self.last_log = f"Container '{self.safe_name}' not found" self.last_log = f"Container '{safe_name}' not found"
self.save() self.save()
return f"Error: Container '{self.safe_name}' not found" return f"Error: Container '{safe_name}' not found"
except Exception as e: except Exception as e:
self.last_log = f"Error removing container: {str(e)}" self.last_log = f"Error removing container: {str(e)}"
self.save() self.save()
return f"Error: {e}" return f"Error: {e}"

View File

@ -7,4 +7,4 @@ urlpatterns = [
path('', views.home, name='home'), path('', views.home, name='home'),
path('games/', views.games, name='games'), path('games/', views.games, name='games'),
path('games/<str:game_name>/', views.game_detail, name='game_detail'), path('games/<str:game_name>/', views.game_detail, name='game_detail'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) ]