diff --git a/infrastructure/checker/.env.template b/infrastructure/checker/.env.template index 7c6f9d3..696fced 100644 --- a/infrastructure/checker/.env.template +++ b/infrastructure/checker/.env.template @@ -1,2 +1,2 @@ REGISTRY_LOGIN=devitq -REGISTRY_PASSWORD=14zQrbzDTM0WXK@CogMQikAvP74Rj4 +REGISTRY_PASSWORD=prod-zAoUDyHwkgRfQPyVvskH diff --git a/services/checker/.env.template b/services/checker/.env.template new file mode 100644 index 0000000..8bea4d3 --- /dev/null +++ b/services/checker/.env.template @@ -0,0 +1,10 @@ +# Change all vars before going to production and remove all comments (!) +# Below all environment variables and default values + +REGISTRY_USERNAME= + +REGISTRY_PASSWORD= + +REGISTRY_URL=gitlab.prodcontest.ru:5050 + +DOCKER_IMAGE=gitlab.prodcontest.ru:5050/team-15/project/custom-python diff --git a/services/checker/config.py b/services/checker/config.py index b297cc8..7c49c2f 100644 --- a/services/checker/config.py +++ b/services/checker/config.py @@ -14,5 +14,5 @@ REGISTRY_PASSWORD = os.getenv("REGISTRY_USERNAME", None) REGISTRY_URL = os.getenv("REGISTRY_URL", "gitlab.prodcontest.ru:5050") DOCKER_IMAGE = os.getenv( - "IMAGE", default="gitlab.prodcontest.ru:5050/team-15/project/custom-python" + "DOCKER_IMAGE", default="gitlab.prodcontest.ru:5050/team-15/project/custom-python" ) diff --git a/services/checker/main.py b/services/checker/main.py index bd10819..c352bbc 100644 --- a/services/checker/main.py +++ b/services/checker/main.py @@ -1,5 +1,6 @@ +import docker.errors from fastapi import FastAPI, HTTPException, status -from pydantic import BaseModel, Field, HttpUrl, constr +from pydantic import BaseModel, Field, HttpUrl import aiohttp import asyncio import docker @@ -20,13 +21,24 @@ ALLOWED_FILENAME_CHARS = r"[^a-zA-Z0-9_\-.]" app = FastAPI() docker_client = docker.from_env() -logger = logging.getLogger(__name__) -logging.basicConfig(level=logging.INFO) docker_client.login( username=config.REGISTRY_LOGIN, password=config.REGISTRY_PASSWORD, registry=config.REGISTRY_URL, ) +logger = logging.getLogger(__name__) +logging.basicConfig(level=logging.INFO) + + +@app.on_event("startup") +async def pull_docker_image(): + client = docker.from_env() + image_name = config.DOCKER_IMAGE + try: + client.images.pull(image_name) + print(f"Successfully pulled {image_name}") + except docker.errors.DockerException as e: + print(f"Error pulling {image_name}: {e}") class FileDetails(BaseModel): @@ -93,9 +105,7 @@ async def download_file( session: aiohttp.ClientSession, url: str, dest_path: str ) -> None: try: - async with session.get( - url, timeout=aiohttp.ClientTimeout(total=30) - ) as resp: + async with session.get(url, timeout=aiohttp.ClientTimeout(total=30)) as resp: if resp.status != 200: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, @@ -276,9 +286,7 @@ async def execute_code(request: ExecutionRequest) -> ExecutionResponse: return ExecutionResponse( success=success, hash_match=( - result_hash == request.expected_hash - if request.expected_hash - else None + result_hash == request.expected_hash if request.expected_hash else None ), output=output[:5000], result_hash=result_hash,