add NGINXConf runs
This commit is contained in:
parent
948444bdb9
commit
2a3c514e4c
@ -1,10 +1,16 @@
|
||||
from django.db.models.signals import post_save
|
||||
from django.dispatch import receiver
|
||||
from .DockerManager import DockerManager
|
||||
from .NGINXConfigurator import NGINXConfigurator
|
||||
from .models import Profile, Session
|
||||
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)
|
||||
def profile_saved(sender, instance, **kwargs):
|
||||
# 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)
|
||||
def profile_start_up(sender, user, request, **kwargs):
|
||||
try:
|
||||
user.session
|
||||
|
||||
user.session = request.session.session_key
|
||||
user.session.save()
|
||||
except Profile.session.RelatedObjectDoesNotExist:
|
||||
# Create session
|
||||
session = Session()
|
||||
@ -30,12 +36,12 @@ def profile_start_up(sender, user, request, **kwargs):
|
||||
# Create container
|
||||
docker_manager = DockerManager()
|
||||
|
||||
# TODO metoda wybierania portów
|
||||
port = 6969
|
||||
container_id = -1
|
||||
while True:
|
||||
try:
|
||||
container_id = docker_manager.turn_on_container(user.volume, port)
|
||||
container_id = docker_manager.turn_on_container(user.volume,
|
||||
port)
|
||||
break
|
||||
# TODO dodać konkret bledy
|
||||
except:
|
||||
@ -43,14 +49,15 @@ def profile_start_up(sender, user, request, **kwargs):
|
||||
|
||||
session.port = port
|
||||
session.container_id = container_id
|
||||
|
||||
session.save()
|
||||
finally:
|
||||
NGINXConfigurator.refresh_config(get_sessions_data())
|
||||
|
||||
|
||||
@receiver(user_logged_out)
|
||||
def profile_logged_out(sender, user, request, **kwargs):
|
||||
# Obtaining container_id
|
||||
session = Session.objects.get(id=user.user_id)
|
||||
session = Session.objects.get(user=user)
|
||||
container_id = session.container_id
|
||||
|
||||
# Turn off and delete container
|
||||
@ -59,3 +66,4 @@ def profile_logged_out(sender, user, request, **kwargs):
|
||||
|
||||
# Delete session
|
||||
session.delete()
|
||||
NGINXConfigurator.refresh_config(get_sessions_data())
|
||||
|
Loading…
Reference in New Issue
Block a user