networking

This commit is contained in:
Egor Matveev 2022-03-28 12:28:13 +03:00
parent c6383ffb31
commit f26b530ff8
6 changed files with 36 additions and 61 deletions

View File

@ -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})

View File

@ -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:

View File

@ -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())

View File

@ -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

View File

@ -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():

View File

@ -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