Base Functionality added. Adding a Server with the image port ip and other params specified will spawn a container and one can manage the lifecycle in the admin panel and the game server detail view updates based on container status. Signed-off-by: Pratyush Desai <pratyush.desai@liberta.casa>
41 lines
1.4 KiB
Python
41 lines
1.4 KiB
Python
from django.db import models
|
|
import docker
|
|
|
|
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)
|
|
ip_address = models.GenericIPAddressField()
|
|
port = models.PositiveIntegerField()
|
|
docker_image = models.CharField(max_length=200, null=True)
|
|
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."""
|
|
client = docker.from_env()
|
|
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"
|
|
except docker.errors.NotFound:
|
|
self.status = "offline"
|
|
except Exception as e:
|
|
self.status = "offline" # Fallback in case of unexpected errors
|
|
self.save()
|