2022-01-27 22:46:31 +01:00
|
|
|
from django.db.models.signals import post_save
|
|
|
|
from django.dispatch import receiver
|
2022-01-28 00:00:44 +01:00
|
|
|
from .DockerManager import DockerManager
|
2022-01-28 00:27:12 +01:00
|
|
|
from .NGINXConfigurator import NGINXConfigurator
|
2022-01-28 00:00:44 +01:00
|
|
|
from .models import Profile, Session
|
2022-01-27 23:30:11 +01:00
|
|
|
from django.contrib.auth.signals import user_logged_in, user_logged_out
|
2022-01-27 22:46:31 +01:00
|
|
|
|
|
|
|
|
2022-01-28 00:27:12 +01:00
|
|
|
def get_sessions_data():
|
|
|
|
return tuple((session.profile.volume, session.port, session.sessionid)
|
|
|
|
for session in Session.objects.all())
|
|
|
|
|
|
|
|
|
2022-01-27 22:46:31 +01:00
|
|
|
@receiver(post_save, sender=Profile)
|
|
|
|
def profile_saved(sender, instance, **kwargs):
|
|
|
|
# Check if volume already exists and if not -> creating new volume
|
|
|
|
if instance.volume is None:
|
|
|
|
docker_manager = DockerManager()
|
|
|
|
volume = docker_manager.create_new_volume(instance.username)
|
|
|
|
instance.volume = volume
|
|
|
|
|
|
|
|
instance.save()
|
|
|
|
|
|
|
|
|
2022-01-27 23:18:02 +01:00
|
|
|
@receiver(user_logged_in)
|
2022-01-28 00:00:44 +01:00
|
|
|
def profile_start_up(sender, user, request, **kwargs):
|
|
|
|
try:
|
2022-01-28 00:27:12 +01:00
|
|
|
user.session = request.session.session_key
|
|
|
|
user.session.save()
|
2022-01-28 00:00:44 +01:00
|
|
|
except Profile.session.RelatedObjectDoesNotExist:
|
2022-01-27 23:18:02 +01:00
|
|
|
# Create session
|
|
|
|
session = Session()
|
2022-01-28 00:00:44 +01:00
|
|
|
session.user = user
|
2022-01-28 00:14:14 +01:00
|
|
|
session.sessionid = request.session.session_key
|
2022-01-27 23:18:02 +01:00
|
|
|
|
|
|
|
# Create container
|
|
|
|
docker_manager = DockerManager()
|
2022-01-28 00:13:08 +01:00
|
|
|
|
|
|
|
port = 6969
|
|
|
|
container_id = -1
|
|
|
|
while True:
|
|
|
|
try:
|
2022-01-28 00:27:12 +01:00
|
|
|
container_id = docker_manager.turn_on_container(user.volume,
|
|
|
|
port)
|
2022-01-28 00:13:08 +01:00
|
|
|
break
|
|
|
|
# TODO dodać konkret bledy
|
|
|
|
except:
|
|
|
|
port += 1
|
|
|
|
|
|
|
|
session.port = port
|
2022-01-27 23:18:02 +01:00
|
|
|
session.container_id = container_id
|
|
|
|
session.save()
|
2022-01-28 00:27:12 +01:00
|
|
|
finally:
|
|
|
|
NGINXConfigurator.refresh_config(get_sessions_data())
|
2022-01-27 23:18:02 +01:00
|
|
|
|
2022-01-27 22:46:31 +01:00
|
|
|
|
2022-01-27 23:30:11 +01:00
|
|
|
@receiver(user_logged_out)
|
2022-01-28 00:00:44 +01:00
|
|
|
def profile_logged_out(sender, user, request, **kwargs):
|
2022-01-27 23:30:11 +01:00
|
|
|
# Obtaining container_id
|
2022-01-28 00:27:12 +01:00
|
|
|
session = Session.objects.get(user=user)
|
2022-01-27 23:30:11 +01:00
|
|
|
container_id = session.container_id
|
|
|
|
|
|
|
|
# Turn off and delete container
|
|
|
|
docker_manager = DockerManager()
|
|
|
|
docker_manager.turn_off_container(container_id)
|
|
|
|
|
|
|
|
# Delete session
|
|
|
|
session.delete()
|
2022-01-28 00:27:12 +01:00
|
|
|
NGINXConfigurator.refresh_config(get_sessions_data())
|