From f26b530ff808743727f4c8a8bd77685be16cf16c Mon Sep 17 00:00:00 2001 From: Egor Matveev Date: Mon, 28 Mar 2022 12:28:13 +0300 Subject: [PATCH] networking --- FileStorage/views/delete_file.py | 2 - FileStorage/views/get_file.py | 2 - FileStorage/views/upload_file.py | 2 - Sprint/settings.py | 6 +-- SprintLib/utils.py | 7 ++- docker-compose-deploy.yaml | 78 ++++++++++++-------------------- 6 files changed, 36 insertions(+), 61 deletions(-) diff --git a/FileStorage/views/delete_file.py b/FileStorage/views/delete_file.py index 239b6f9..dbd9c9c 100644 --- a/FileStorage/views/delete_file.py +++ b/FileStorage/views/delete_file.py @@ -5,7 +5,5 @@ from aiohttp import web async def delete_file(request): - if 'token' not in request.headers or request.headers['token'] != os.getenv('FS_TOKEN'): - return web.json_response({"success": False}, status=403) remove("data/" + request.rel_url.query['id']) return web.json_response({"success": True}) diff --git a/FileStorage/views/get_file.py b/FileStorage/views/get_file.py index d40267e..7af1f11 100644 --- a/FileStorage/views/get_file.py +++ b/FileStorage/views/get_file.py @@ -5,8 +5,6 @@ from aiohttp import web async def get_file(request): - if 'token' not in request.headers or request.headers['token'] != os.getenv('FS_TOKEN'): - return web.json_response({"success": False}, status=403) response = web.StreamResponse() await response.prepare(request) async with aiofiles.open("data/" + request.rel_url.query['id'], "rb") as fs: diff --git a/FileStorage/views/upload_file.py b/FileStorage/views/upload_file.py index b0392ea..2795c2e 100644 --- a/FileStorage/views/upload_file.py +++ b/FileStorage/views/upload_file.py @@ -7,8 +7,6 @@ import aiofiles async def upload_file(request): - if 'token' not in request.headers or request.headers['token'] != os.getenv('FS_TOKEN'): - return web.json_response({"success": False}, status=403) file_id = await write_meta(request) async with aiofiles.open("data/" + str(file_id), "wb") as fs: await fs.write(await request.content.read()) diff --git a/Sprint/settings.py b/Sprint/settings.py index 87f487f..40d29dd 100644 --- a/Sprint/settings.py +++ b/Sprint/settings.py @@ -88,7 +88,7 @@ DATABASES = { "NAME": "sprint", "USER": "postgres", "PASSWORD": os.getenv("DB_PASSWORD"), - "HOST": os.getenv("DB_HOST"), + "HOST": os.getenv("DB_HOST", "postgres"), "PORT": 5432, } } @@ -144,10 +144,10 @@ for root in DATA_ROOT, EXTRA_FILES_ROOT: SOLUTIONS_ROOT = os.path.join(DATA_ROOT, "solutions") -RABBIT_HOST = os.getenv("RABBIT_HOST") +RABBIT_HOST = os.getenv("RABBIT_HOST", "rabbitmq") RABBIT_PORT = 5672 -FS_HOST = "http://" + os.getenv("FS_HOST") +FS_HOST = "http://" + os.getenv("FS_HOST", "storage") FS_PORT = 5555 # Authentication backends diff --git a/SprintLib/utils.py b/SprintLib/utils.py index b3d5f20..0ca8c5b 100644 --- a/SprintLib/utils.py +++ b/SprintLib/utils.py @@ -1,5 +1,4 @@ import datetime -import os from random import choice from requests import get, post @@ -10,19 +9,19 @@ from Sprint import settings def write_bytes(data: bytes): url = settings.FS_HOST + ":" + str(settings.FS_PORT) + "/upload_file" print(url) - return post(url, data=data, headers={'token': os.getenv('FS_TOKEN')}).json()["id"] + return post(url, data=data).json()["id"] def get_bytes(num: int) -> bytes: url = settings.FS_HOST + ":" + str(settings.FS_PORT) + "/get_file?id=" + str(num) print(url) - return get(url, headers={'token': os.getenv('FS_TOKEN')}).content + return get(url).content def delete_file(num: int): url = settings.FS_HOST + ":" + str(settings.FS_PORT) + "/delete_file?id=" + str(num) print(url) - post(url, headers={'token': os.getenv('FS_TOKEN')}) + post(url) def generate_token(): diff --git a/docker-compose-deploy.yaml b/docker-compose-deploy.yaml index de2b0ff..5ea6a46 100644 --- a/docker-compose-deploy.yaml +++ b/docker-compose-deploy.yaml @@ -12,8 +12,8 @@ services: volumes: - /sprint-data/postgres-data:/var/lib/postgresql/data # - ./pg_hba.conf:/var/lib/postgresql/data/pg_hba.conf - ports: - - "5432:5432" + networks: + - net deploy: mode: replicated restart_policy: @@ -25,15 +25,13 @@ services: migrations: image: mathwave/sprint-repo:sprint command: ./manage.py migrate + networks: + - net environment: SOLUTIONS_ROOT_EXTERNAL: "/sprint-data/data/solutions" - DB_HOST: $DB_HOST DB_PASSWORD: $DB_PASSWORD - RABBIT_HOST: $RABBIT_HOST - FS_HOST: $FS_HOST DEBUG: $DEBUG TELEGRAM_TOKEN: $TELEGRAM_TOKEN - FS_TOKEN: $FS_TOKEN deploy: mode: replicated restart_policy: @@ -41,15 +39,13 @@ services: collect_static: image: mathwave/sprint-repo:sprint + networks: + - net environment: SOLUTIONS_ROOT_EXTERNAL: "/sprint-data/data/solutions" - DB_HOST: $DB_HOST DB_PASSWORD: $DB_PASSWORD - RABBIT_HOST: $RABBIT_HOST - FS_HOST: $FS_HOST DEBUG: $DEBUG TELEGRAM_TOKEN: $TELEGRAM_TOKEN - FS_TOKEN: $FS_TOKEN volumes: - /sprint-data/static:/usr/src/app/static command: ./manage.py collectstatic --noinput @@ -60,15 +56,13 @@ services: web: image: mathwave/sprint-repo:sprint + networks: + - net environment: SOLUTIONS_ROOT_EXTERNAL: "/sprint-data/data/solutions" - DB_HOST: $DB_HOST DB_PASSWORD: $DB_PASSWORD - RABBIT_HOST: $RABBIT_HOST - FS_HOST: $FS_HOST DEBUG: $DEBUG TELEGRAM_TOKEN: $TELEGRAM_TOKEN - FS_TOKEN: $FS_TOKEN volumes: - /sprint-data/static:/usr/src/app/static command: ./manage.py runserver 0.0.0.0:80 --noreload --insecure @@ -84,18 +78,14 @@ services: storage: image: mathwave/sprint-repo:sprint + networks: + - net command: ./manage.py storage environment: SOLUTIONS_ROOT_EXTERNAL: "/sprint-data/data/solutions" - DB_HOST: $DB_HOST DB_PASSWORD: $DB_PASSWORD - RABBIT_HOST: $RABBIT_HOST - FS_HOST: $FS_HOST DEBUG: $DEBUG TELEGRAM_TOKEN: $TELEGRAM_TOKEN - FS_TOKEN: $FS_TOKEN - ports: - - "5555:5555" volumes: - /sprint-data/data:/usr/src/app/data deploy: @@ -108,16 +98,14 @@ services: health_check: image: mathwave/sprint-repo:sprint + networks: + - net command: ./manage.py health_check environment: SOLUTIONS_ROOT_EXTERNAL: "/sprint-data/data/solutions" - DB_HOST: $DB_HOST DB_PASSWORD: $DB_PASSWORD - RABBIT_HOST: $RABBIT_HOST - FS_HOST: $FS_HOST DEBUG: $DEBUG TELEGRAM_TOKEN: $TELEGRAM_TOKEN - FS_TOKEN: $FS_TOKEN deploy: mode: replicated restart_policy: @@ -128,15 +116,13 @@ services: bot: image: mathwave/sprint-repo:sprint + networks: + - net environment: SOLUTIONS_ROOT_EXTERNAL: "/sprint-data/data/solutions" - DB_HOST: $DB_HOST DB_PASSWORD: $DB_PASSWORD - RABBIT_HOST: $RABBIT_HOST - FS_HOST: $FS_HOST DEBUG: $DEBUG TELEGRAM_TOKEN: $TELEGRAM_TOKEN - FS_TOKEN: $FS_TOKEN command: ./manage.py bot deploy: mode: replicated @@ -148,15 +134,13 @@ services: loop: image: mathwave/sprint-repo:sprint + networks: + - net environment: SOLUTIONS_ROOT_EXTERNAL: "/sprint-data/data/solutions" - DB_HOST: $DB_HOST DB_PASSWORD: $DB_PASSWORD - RABBIT_HOST: $RABBIT_HOST - FS_HOST: $FS_HOST DEBUG: $DEBUG TELEGRAM_TOKEN: $TELEGRAM_TOKEN - FS_TOKEN: $FS_TOKEN command: ./manage.py loop deploy: mode: replicated @@ -171,6 +155,8 @@ services: ports: - "15672:15672" - "5672:5672" + networks: + - net deploy: mode: replicated restart_policy: @@ -181,16 +167,14 @@ services: worker: image: mathwave/sprint-repo:sprint + networks: + - net command: ./manage.py receive environment: SOLUTIONS_ROOT_EXTERNAL: "/sprint-data/data/solutions" - DB_HOST: $DB_HOST DB_PASSWORD: $DB_PASSWORD - RABBIT_HOST: $RABBIT_HOST - FS_HOST: $FS_HOST DEBUG: $DEBUG TELEGRAM_TOKEN: $TELEGRAM_TOKEN - FS_TOKEN: $FS_TOKEN volumes: - /sprint-data/solutions:/usr/src/app/solutions - /var/run/docker.sock:/var/run/docker.sock @@ -205,16 +189,14 @@ services: file_generator: image: mathwave/sprint-repo:sprint + networks: + - net command: ./manage.py file_generator environment: SOLUTIONS_ROOT_EXTERNAL: "/sprint-data/data/solutions" - DB_HOST: $DB_HOST DB_PASSWORD: $DB_PASSWORD - RABBIT_HOST: $RABBIT_HOST - FS_HOST: $FS_HOST DEBUG: $DEBUG TELEGRAM_TOKEN: $TELEGRAM_TOKEN - FS_TOKEN: $FS_TOKEN deploy: mode: replicated replicas: 1 @@ -226,16 +208,14 @@ services: notification_manager: image: mathwave/sprint-repo:sprint + networks: + - net command: ./manage.py notification_manager environment: SOLUTIONS_ROOT_EXTERNAL: "/sprint-data/data/solutions" - DB_HOST: $DB_HOST DB_PASSWORD: $DB_PASSWORD - RABBIT_HOST: $RABBIT_HOST - FS_HOST: $FS_HOST DEBUG: $DEBUG TELEGRAM_TOKEN: $TELEGRAM_TOKEN - FS_TOKEN: $FS_TOKEN deploy: mode: replicated replicas: 1 @@ -247,17 +227,19 @@ services: apply-languages: image: mathwave/sprint-repo:sprint + networks: + - net command: ./manage.py apply_languages environment: SOLUTIONS_ROOT_EXTERNAL: "/sprint-data/data/solutions" - DB_HOST: $DB_HOST DB_PASSWORD: $DB_PASSWORD - RABBIT_HOST: $RABBIT_HOST - FS_HOST: $FS_HOST DEBUG: $DEBUG TELEGRAM_TOKEN: $TELEGRAM_TOKEN - FS_TOKEN: $FS_TOKEN deploy: mode: replicated restart_policy: condition: on-failure + +networks: + net: + driver: overlay