Merge pull request 'misc-fixes' (#27) from misc-fixes into master
Reviewed-on: GibCasa/GameServerSupervisor#27
This commit is contained in:
commit
66954eb634
Before Width: | Height: | Size: 32 KiB After Width: | Height: | Size: 32 KiB |
Binary file not shown.
Before Width: | Height: | Size: 112 KiB |
Binary file not shown.
Before Width: | Height: | Size: 118 KiB |
Binary file not shown.
Before Width: | Height: | Size: 87 KiB |
Binary file not shown.
Before Width: | Height: | Size: 19 KiB |
20
webpanel/migrations/0010_alter_game_thumbnail.py
Normal file
20
webpanel/migrations/0010_alter_game_thumbnail.py
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
# 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/"
|
||||||
|
),
|
||||||
|
),
|
||||||
|
]
|
@ -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='media/game_thumbnails/', null=True, blank=True)
|
thumbnail = models.ImageField(upload_to='game_thumbnails/', null=True, blank=True)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self) -> str:
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
class Server(models.Model):
|
class Server(models.Model):
|
||||||
@ -28,15 +28,25 @@ 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):
|
def __str__(self)-> str:
|
||||||
return f"{self.game.name} Server at {self.ip_address}:{self.port}"
|
return f"{self.game.name} Server at {self.ip_address}:{self.port}"
|
||||||
|
|
||||||
def sync_status(self):
|
@property
|
||||||
|
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 = client.containers.get(safe_name)
|
container = self._get_podman_client().containers.get(self.safe_name)
|
||||||
if container.status == "running":
|
if container.status == "running":
|
||||||
self.status = "online"
|
self.status = "online"
|
||||||
else:
|
else:
|
||||||
@ -47,12 +57,10 @@ class Server(models.Model):
|
|||||||
self.status = "offline"
|
self.status = "offline"
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
def launch_pod_container(self):
|
def launch_pod_container(self) -> str:
|
||||||
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 = client.containers.create(
|
container = self._get_podman_client().containers.create(
|
||||||
name=safe_name,
|
name=self.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),
|
||||||
@ -64,7 +72,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}"
|
||||||
@ -77,11 +85,9 @@ class Server(models.Model):
|
|||||||
self.save()
|
self.save()
|
||||||
return f"Error: {e}"
|
return f"Error: {e}"
|
||||||
|
|
||||||
def stop_pod_container(self):
|
def stop_pod_container(self) -> str:
|
||||||
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 = client.containers.get(safe_name)
|
container = self._get_podman_client().containers.get(self.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}"
|
||||||
@ -89,32 +95,29 @@ 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 '{safe_name}' not found"
|
self.last_log = f"Container '{self.safe_name}' not found"
|
||||||
self.save()
|
self.save()
|
||||||
return f"Error: Container '{safe_name}' not found"
|
return f"Error: Container '{self.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):
|
def remove_pod_container(self) -> str:
|
||||||
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 = client.containers.get(safe_name)
|
container = self._get_podman_client().containers.get(self.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 {safe_name}"
|
self.last_log = f"Removed container {self.safe_name}"
|
||||||
self.sync_status()
|
self.sync_status()
|
||||||
self.save()
|
self.save()
|
||||||
return f"Container removed successfully: {safe_name}"
|
return f"Container removed successfully: {self.safe_name}"
|
||||||
except podman.errors.NotFound:
|
except podman.errors.NotFound:
|
||||||
self.last_log = f"Container '{safe_name}' not found"
|
self.last_log = f"Container '{self.safe_name}' not found"
|
||||||
self.save()
|
self.save()
|
||||||
return f"Error: Container '{safe_name}' not found"
|
return f"Error: Container '{self.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}"
|
||||||
|
|
@ -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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user