From ff160cbbefffde3f4cae6dc044d11b0b7e896e22 Mon Sep 17 00:00:00 2001 From: virusdefender Date: Thu, 27 Oct 2016 18:38:04 +0800 Subject: [PATCH] fix project structure --- .gitignore | 1 - Dockerfile => build/Dockerfile | 6 +++--- {deploy => build}/java_policy | 0 {deploy => build}/sources.list | 0 docker-compose.example.yml | 2 +- __init__.py => server/__init__.py | 0 compiler.py => server/compiler.py | 1 - config.py => server/config.py | 0 exception.py => server/exception.py | 0 judge_client.py => server/judge_client.py | 0 server/run.sh | 4 ++++ server.py => server/server.py | 16 +++------------ service.py => server/service.py | 4 ++-- utils.py => server/utils.py | 25 +++++++++++++++-------- 14 files changed, 30 insertions(+), 29 deletions(-) rename Dockerfile => build/Dockerfile (76%) rename {deploy => build}/java_policy (100%) rename {deploy => build}/sources.list (100%) rename __init__.py => server/__init__.py (100%) rename compiler.py => server/compiler.py (99%) rename config.py => server/config.py (100%) rename exception.py => server/exception.py (100%) rename judge_client.py => server/judge_client.py (100%) create mode 100644 server/run.sh rename server.py => server/server.py (93%) rename service.py => server/service.py (92%) rename utils.py => server/utils.py (82%) diff --git a/.gitignore b/.gitignore index 89215f5..942a17c 100644 --- a/.gitignore +++ b/.gitignore @@ -7,7 +7,6 @@ __pycache__/ .Python env/ bin/ -build/ develop-eggs/ dist/ eggs/ diff --git a/Dockerfile b/build/Dockerfile similarity index 76% rename from Dockerfile rename to build/Dockerfile index 0bed906..1e25353 100644 --- a/Dockerfile +++ b/build/Dockerfile @@ -1,16 +1,16 @@ FROM ubuntu:14.04 ENV DEBIAN_FRONTEND noninteractive RUN rm /etc/apt/sources.list -COPY deploy/sources.list /etc/apt/ +COPY sources.list /etc/apt/ RUN apt-get update RUN apt-get -y install software-properties-common python-software-properties python python-dev gcc g++ git libtool python-pip libseccomp-dev cmake openjdk-7-jdk RUN cd /tmp && git clone https://github.com/QingdaoU/Judger && cd Judger && git checkout newnew && mkdir build && cd build && cmake .. && make && make install && cd ../bindings/Python && python setup.py install RUN pip install psutil gunicorn web.py requests RUN mkdir -p /judger_run /test_case /log /code -COPY deploy/java_policy /etc +COPY java_policy /etc RUN pip install futures psutil gunicorn web.py RUN useradd -r compiler HEALTHCHECK --interval=5s --retries=3 CMD python /code/service.py WORKDIR /code EXPOSE 8080 -CMD chown compiler:compiler /spj; echo 0 > /tmp/counter; gunicorn --workers 4 --threads 4 --error-logfile /log/gunicorn.log --bind 0.0.0.0:8080 server:wsgiapp +CMD /bin/bash /code/run.sh diff --git a/deploy/java_policy b/build/java_policy similarity index 100% rename from deploy/java_policy rename to build/java_policy diff --git a/deploy/sources.list b/build/sources.list similarity index 100% rename from deploy/sources.list rename to build/sources.list diff --git a/docker-compose.example.yml b/docker-compose.example.yml index 32449cb..d6b5e57 100644 --- a/docker-compose.example.yml +++ b/docker-compose.example.yml @@ -18,7 +18,7 @@ services: volumes: - /data/JudgeServer/tests/test_case:/test_case:ro - /data/log:/log - - /data/JudgeServer:/code:ro + - /data/JudgeServer/server:/code:ro environment: - judger_token=token - service_discovery_url=https://virusdefender.net/service.php diff --git a/__init__.py b/server/__init__.py similarity index 100% rename from __init__.py rename to server/__init__.py diff --git a/compiler.py b/server/compiler.py similarity index 99% rename from compiler.py rename to server/compiler.py index a2501d9..035873a 100644 --- a/compiler.py +++ b/server/compiler.py @@ -3,7 +3,6 @@ from __future__ import unicode_literals import json import os -import time import _judger diff --git a/config.py b/server/config.py similarity index 100% rename from config.py rename to server/config.py diff --git a/exception.py b/server/exception.py similarity index 100% rename from exception.py rename to server/exception.py diff --git a/judge_client.py b/server/judge_client.py similarity index 100% rename from judge_client.py rename to server/judge_client.py diff --git a/server/run.sh b/server/run.sh new file mode 100644 index 0000000..f13b057 --- /dev/null +++ b/server/run.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash +chown compiler:compiler /spj +echo 0 > /tmp/counter +gunicorn --workers 4 --threads 4 --error-logfile /log/gunicorn.log --bind 0.0.0.0:8080 server:wsgiapp diff --git a/server.py b/server/server.py similarity index 93% rename from server.py rename to server/server.py index e0e4587..1861246 100644 --- a/server.py +++ b/server/server.py @@ -13,7 +13,7 @@ from compiler import Compiler from config import JUDGER_WORKSPACE_BASE, SPJ_SRC_DIR, SPJ_EXE_DIR, COUNTER_FILE_PATH from exception import TokenVerificationFailed, CompileError, SPJCompileError,JudgeClientError from judge_client import JudgeClient -from utils import server_info, get_token, logger, TaskCounter +from utils import server_info, logger, TaskCounter, token DEBUG = os.environ.get("judger_debug") == "1" @@ -49,13 +49,6 @@ class JudgeServer(object): data["action"] = "pong" return data - @property - def _token(self): - t = get_token() - if not t: - raise TokenVerificationFailed("token not set") - return hashlib.sha256(t).hexdigest() - def judge(self, language_config, src, max_cpu_time, max_memory, test_case_id, spj_version=None, spj_config=None, spj_compile_config=None, spj_src=None, output=False): # init @@ -118,9 +111,9 @@ class JudgeServer(object): return "success" def POST(self): - token = web.ctx.env.get("HTTP_X_JUDGE_SERVER_TOKEN", None) + _token = web.ctx.env.get("HTTP_X_JUDGE_SERVER_TOKEN", None) try: - if token != self._token: + if _token != token: raise TokenVerificationFailed("invalid token") if web.data(): try: @@ -164,9 +157,6 @@ urls = ( if DEBUG: logger.info("DEBUG=ON") -# check token -JudgeServer()._token - app = web.application(urls, globals()) wsgiapp = app.wsgifunc() diff --git a/service.py b/server/service.py similarity index 92% rename from service.py rename to server/service.py index 26b55ac..3b1e12f 100644 --- a/service.py +++ b/server/service.py @@ -6,7 +6,7 @@ import requests import hashlib from exception import JudgeServiceError -from utils import server_info, get_token, logger +from utils import server_info, logger, token class JudgeService(object): @@ -29,7 +29,7 @@ class JudgeService(object): def _request(self, data): try: r = requests.post(self.service_discovery_url, data=json.dumps(data), - headers={"X-JUDGE-SERVER-TOKEN": hashlib.sha256(get_token()).hexdigest(), + headers={"X-JUDGE-SERVER-TOKEN": token, "Content-Type": "application/json"}, timeout=5).json() except Exception as e: logger.exception(e) diff --git a/utils.py b/server/utils.py similarity index 82% rename from utils.py rename to server/utils.py index 631a43f..1b3cb8a 100644 --- a/utils.py +++ b/server/utils.py @@ -3,11 +3,21 @@ from __future__ import unicode_literals import _judger import psutil import socket -import os import logging import fcntl +import hashlib +import os from config import COUNTER_FILE_PATH +from exception import JudgeClientError + + +logger = logging.getLogger(__name__) +handler = logging.FileHandler("/log/judge_server.log") +formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') +handler.setFormatter(formatter) +logger.addHandler(handler) +logger.setLevel(logging.WARNING) class TaskCounter(object): @@ -48,12 +58,11 @@ def server_info(): def get_token(): - return os.environ.get("OJ_WEB_SERVER_ENV_judger_token") or os.environ.get("judger_token") + token = os.environ.get("OJ_WEB_SERVER_ENV_judger_token") or os.environ.get("judger_token") + if not token: + raise JudgeClientError("judger_token not set") + return token -logger = logging.getLogger(__name__) -handler = logging.FileHandler("/log/judge_server.log") -formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') -handler.setFormatter(formatter) -logger.addHandler(handler) -logger.setLevel(logging.WARNING) \ No newline at end of file +token = hashlib.sha256(get_token()).hexdigest() +