From f040151b6874c0958c3b7043a976f4b2164d8cbc Mon Sep 17 00:00:00 2001 From: Egor Matveev Date: Sat, 26 Mar 2022 16:58:28 +0300 Subject: [PATCH] docker --- Main/templatetags/filters.py | 5 +++++ Main/views/SendCodeView.py | 1 + SprintLib/testers/BaseTester.py | 12 +++++++++++- templates/task_settings.html | 4 +++- 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/Main/templatetags/filters.py b/Main/templatetags/filters.py index d64d6de..19de855 100644 --- a/Main/templatetags/filters.py +++ b/Main/templatetags/filters.py @@ -18,3 +18,8 @@ def solved(user, task): @register.filter('settask') def settask(set, task): return SetTask.objects.get(set=set, task=task) + + +@register.filter('startswith') +def startswith(s, prefix): + return s.startswith(prefix) diff --git a/Main/views/SendCodeView.py b/Main/views/SendCodeView.py index a8a1d87..a7b74e2 100644 --- a/Main/views/SendCodeView.py +++ b/Main/views/SendCodeView.py @@ -19,6 +19,7 @@ class SendCodeView(BaseView): "message": "Пользователя с таким именем не существует", } code = randrange(10000, 100000) + print(code) user.userinfo.code = code user.userinfo.save() notify(user, "any", "Код для входа в сервис: " + str(code)) diff --git a/SprintLib/testers/BaseTester.py b/SprintLib/testers/BaseTester.py index 7a28898..d4589a5 100644 --- a/SprintLib/testers/BaseTester.py +++ b/SprintLib/testers/BaseTester.py @@ -67,6 +67,9 @@ class BaseTester: def build_command(self): return "" + def call(self, command): + return call(f'cd {self.path} && {command}', shell=True) + @property def path(self): return join("solutions", str(self.solution.id)) @@ -94,9 +97,16 @@ class BaseTester: join(self.path, file.path), "wb" ) as fs: fs.write(get_bytes(file.fs_id).replace(b"\r\n", b"\n")) + dockerfiles = sorted(list(ExtraFile.objects.filter(filename__startswith="Dockerfile", readable=True, task=self.solution.task)), key=lambda x: x.filename) + self.call(f"docker network create solution_network_{self.solution.id}") + for file in dockerfiles: + with open(join(self.path, 'Dockerfile'), 'w') as fs: + fs.write(file.text) + self.call(f"docker build -t solution_image_{self.solution.id}_{file.filename} .".lower()) + self.call(f"docker run --network solution_network_{self.solution.id} --name solution_container_{self.solution.id}_{file.filename} -t -d solution_image_{self.solution.id}_{file.filename}".lower()) self.solution.result = CONSTS["testing_status"] self.solution.save() - docker_command = f"docker run --name solution_{self.solution.id} --volume=/sprint-data/solutions/{self.solution.id}:/{self.working_directory} -t -d {self.solution.language.image}" + docker_command = f"docker run --network solution_network_{self.solution.id} --name solution_{self.solution.id} --volume=/sprint-data/solutions/{self.solution.id}:/{self.working_directory} -t -d {self.solution.language.image}" print(docker_command) call(docker_command, shell=True) checker = ExtraFile.objects.filter(task=self.solution.task, filename='checker.py').first() diff --git a/templates/task_settings.html b/templates/task_settings.html index 3157398..72a407e 100644 --- a/templates/task_settings.html +++ b/templates/task_settings.html @@ -2,6 +2,8 @@ {% block title %}{{ task.name }}{% endblock %} +{% load filters %} + {% block scripts %} function deleteFile(file_id) { $.ajax({ @@ -255,7 +257,7 @@ {% for test in task.files %}
-
+
{% if test.readable %}
{% csrf_token %}