add NGINXConf runs

This commit is contained in:
prance 2022-01-28 00:27:12 +01:00
parent 948444bdb9
commit 2a3c514e4c

View File

@ -1,10 +1,16 @@
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 .NGINXConfigurator import NGINXConfigurator
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
def get_sessions_data():
return tuple((session.profile.volume, session.port, session.sessionid)
for session in Session.objects.all())
@receiver(post_save, sender=Profile) @receiver(post_save, sender=Profile)
def profile_saved(sender, instance, **kwargs): def profile_saved(sender, instance, **kwargs):
# Check if volume already exists and if not -> creating new volume # Check if volume already exists and if not -> creating new volume
@ -19,8 +25,8 @@ def profile_saved(sender, instance, **kwargs):
@receiver(user_logged_in) @receiver(user_logged_in)
def profile_start_up(sender, user, request, **kwargs): def profile_start_up(sender, user, request, **kwargs):
try: try:
user.session user.session = request.session.session_key
user.session.save()
except Profile.session.RelatedObjectDoesNotExist: except Profile.session.RelatedObjectDoesNotExist:
# Create session # Create session
session = Session() session = Session()
@ -30,12 +36,12 @@ def profile_start_up(sender, user, request, **kwargs):
# Create container # Create container
docker_manager = DockerManager() docker_manager = DockerManager()
# TODO metoda wybierania portów
port = 6969 port = 6969
container_id = -1 container_id = -1
while True: while True:
try: try:
container_id = docker_manager.turn_on_container(user.volume, port) container_id = docker_manager.turn_on_container(user.volume,
port)
break break
# TODO dodać konkret bledy # TODO dodać konkret bledy
except: except:
@ -43,14 +49,15 @@ def profile_start_up(sender, user, request, **kwargs):
session.port = port session.port = port
session.container_id = container_id session.container_id = container_id
session.save() session.save()
finally:
NGINXConfigurator.refresh_config(get_sessions_data())
@receiver(user_logged_out) @receiver(user_logged_out)
def profile_logged_out(sender, user, request, **kwargs): def profile_logged_out(sender, user, request, **kwargs):
# Obtaining container_id # Obtaining container_id
session = Session.objects.get(id=user.user_id) session = Session.objects.get(user=user)
container_id = session.container_id container_id = session.container_id
# Turn off and delete container # Turn off and delete container
@ -59,3 +66,4 @@ def profile_logged_out(sender, user, request, **kwargs):
# Delete session # Delete session
session.delete() session.delete()
NGINXConfigurator.refresh_config(get_sessions_data())