docker - kinda works

This commit is contained in:
makram89 2022-01-28 00:00:44 +01:00
parent e8b9824873
commit e1441af9a3
2 changed files with 12 additions and 10 deletions

View File

@ -1,5 +1,4 @@
import docker import docker
from models import Profile
import hashlib import hashlib

View File

@ -1,7 +1,7 @@
from django.db.models.signals import post_save from django.db.models.signals import post_save
from django.dispatch import receiver from django.dispatch import receiver
from DockerManager import DockerManager from .DockerManager import DockerManager
from models import Profile, Session from .models import Profile, Session
from django.contrib.auth.signals import user_logged_in, user_logged_out from django.contrib.auth.signals import user_logged_in, user_logged_out
@ -17,27 +17,30 @@ def profile_saved(sender, instance, **kwargs):
@receiver(user_logged_in) @receiver(user_logged_in)
def profile_start_up(sender, instance, request, **kwargs): def profile_start_up(sender, user, request, **kwargs):
if instance.session is None: try:
user.session
except Profile.session.RelatedObjectDoesNotExist:
# Create session # Create session
session = Session() session = Session()
session.user = instance session.user = user
session.sessionid = request.COOKIE.get('sessionid') session.sessionid = request.COOKIES.get('sessionid')
# TODO metoda wybierania portów # TODO metoda wybierania portów
port = 6969 port = 6969
session.port = port session.port = port
# Create container # Create container
docker_manager = DockerManager() docker_manager = DockerManager()
container_id = docker_manager.turn_on_container(instance.volume, port) container_id = docker_manager.turn_on_container(user.volume, port)
session.container_id = container_id session.container_id = container_id
session.save() session.save()
@receiver(user_logged_out) @receiver(user_logged_out)
def profile_logged_out(sender, instance, request, **kwargs): def profile_logged_out(sender, user, request, **kwargs):
# Obtaining container_id # Obtaining container_id
session = Session.objects.get(id=instance.user_id) session = Session.objects.get(id=user.user_id)
container_id = session.container_id container_id = session.container_id
# Turn off and delete container # Turn off and delete container