2025-01-20 00:32:25 +01:00
|
|
|
from django.db import models
|
2025-02-01 01:36:22 +01:00
|
|
|
import podman
|
2025-01-20 00:32:25 +01:00
|
|
|
|
|
|
|
class Game(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
genre = models.CharField(max_length=50, blank=True, null=True)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
class Server(models.Model):
|
|
|
|
STATUS_CHOICES = [
|
|
|
|
('online', 'Online'),
|
|
|
|
('offline', 'Offline'),
|
|
|
|
]
|
|
|
|
|
|
|
|
game = models.ForeignKey(Game, on_delete=models.CASCADE)
|
2025-01-20 18:12:01 +01:00
|
|
|
name = models.CharField(max_length=100)
|
|
|
|
ip_address = models.GenericIPAddressField(null=True)
|
|
|
|
port = models.PositiveIntegerField(null=True)
|
2025-01-20 00:32:25 +01:00
|
|
|
docker_image = models.CharField(max_length=200, null=True)
|
2025-01-20 18:12:01 +01:00
|
|
|
docker_run_command = models.CharField(max_length=500, blank=True, null=True)
|
|
|
|
command_args = models.TextField(blank=True, null=True)
|
2025-01-20 00:32:25 +01:00
|
|
|
status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='offline')
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return f"{self.game.name} Server at {self.ip_address}:{self.port}"
|
|
|
|
|
|
|
|
def sync_status(self):
|
|
|
|
"""Check the real-time status of the Docker container and update the field."""
|
2025-02-01 01:36:22 +01:00
|
|
|
client = podman.PodmanClient(base_url="unix:///run/podman/podman.sock")
|
2025-01-20 00:32:25 +01:00
|
|
|
container_name = f"{self.game.name}_{self.ip_address}_{self.port}"
|
|
|
|
try:
|
|
|
|
container = client.containers.get(container_name)
|
|
|
|
if container.status == "running":
|
|
|
|
self.status = "online"
|
|
|
|
else:
|
|
|
|
self.status = "offline"
|
2025-02-01 01:36:22 +01:00
|
|
|
except podman.errors.NotFound:
|
2025-01-20 00:32:25 +01:00
|
|
|
self.status = "offline"
|
|
|
|
except Exception as e:
|
|
|
|
self.status = "offline" # Fallback in case of unexpected errors
|
|
|
|
self.save()
|
2025-01-20 18:12:01 +01:00
|
|
|
|
|
|
|
def get_docker_run_command(self):
|
2025-02-01 01:36:22 +01:00
|
|
|
"""Returns the Podman run command, falling back to default image if not set."""
|
|
|
|
if self.docker_run_command:
|
|
|
|
# Return command as a string to be split later
|
|
|
|
return self.docker_run_command
|
|
|
|
else:
|
|
|
|
# Default command with image and arguments
|
|
|
|
base_command = f"{self.image}"
|
|
|
|
if self.command_args:
|
|
|
|
base_command += " " + self.command_args
|
|
|
|
return base_command
|