From c9f07df262444154731ea35f80be5630e45cb32d Mon Sep 17 00:00:00 2001 From: Gardient Date: Sun, 19 Dec 2021 20:27:13 +0200 Subject: [PATCH] fix things for docker compose --- Dockerfile | 12 +++++------ api/__init__.py | 1 + api/registration/views.py | 6 +++--- api/target/views.py | 4 ++-- app.ini | 2 +- docker-compose.yml | 45 +++++++++++++++++++++++++++++++++++++++ rmq_helper/__init__.py | 4 ++-- scripts/entrypoint.sh | 7 ++++++ 8 files changed, 67 insertions(+), 14 deletions(-) create mode 100644 docker-compose.yml create mode 100644 scripts/entrypoint.sh diff --git a/Dockerfile b/Dockerfile index 72a1681..5796c26 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,8 @@ # Use the Python3.9.7 image -FROM python:3.9.7-bullseye +FROM python:3.10.1-bullseye + +ADD https://github.com/ufoscout/docker-compose-wait/releases/download/2.9.0/wait /wait +RUN chmod +x /wait # Set the working directory to /app WORKDIR /app @@ -10,10 +13,7 @@ ADD . /app # Install the dependencies RUN pip install -r requirements/prod.txt -#make sure we have everything we need -RUN flask db upgrade -RUN flask seed -RUN flask setup-rabmq +RUN chmod +x scripts/entrypoint.sh # run the command to start uWSGI -CMD ["uwsgi", "app.ini"] +CMD ["scripts/entrypoint.sh"] diff --git a/api/__init__.py b/api/__init__.py index 14fbc56..5f19f98 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -91,6 +91,7 @@ def register_shellcontext(app: Flask): 'TargetExchange': target_exchange.models.TargetExchange, 'Target': target.models.Target, 'Registration': registration.models.Registration, + 'config': app.config, } app.shell_context_processor(shell_context) diff --git a/api/registration/views.py b/api/registration/views.py index b8be25b..c268f97 100644 --- a/api/registration/views.py +++ b/api/registration/views.py @@ -35,11 +35,11 @@ def get_list(exchange=None, target=None): @use_kwargs(registration_schema) @marshal_with(registration_schema) def create(name, routing_key, targets): - target_ids = [t.id for t in targets] + target_ids = [t["id"] for t in targets] db_targets = Target.query.filter(Target.id.in_(target_ids)) if len(db_targets) != len(targets): - xchange_ids = [t.id for t in db_targets] - not_found = ','.join([f'{t.name}({t.id})' for t in targets if t.id not in xchange_ids]) + xchange_ids = [t["id"] for t in db_targets] + not_found = ','.join([f'{t["name"]}({t["id"]})' for t in targets if t["id"] not in xchange_ids]) raise BadRequestException(f"the target {not_found} could not be found") registration = Registration(name=name, token=routing_key, targets=db_targets) registration.save() diff --git a/api/target/views.py b/api/target/views.py index 4f1772a..b4e1f07 100644 --- a/api/target/views.py +++ b/api/target/views.py @@ -32,9 +32,9 @@ def get_list(exchange=None): @use_kwargs(target_schema) @marshal_with(target_schema) def create(name, routing_key, exchange): - xchange = TargetExchange.get_by_id(exchange.id) + xchange = TargetExchange.get_by_id(exchange["id"]) if xchange is None: - raise BadRequestException(f"the exchange {exchange.name}({exchange.id}) could not be found") + raise BadRequestException(f"the exchange {exchange['name']}({exchange['id']}) could not be found") target = Target(name=name, routing_key=routing_key, target_exchange_id=xchange.id) target.save() return target diff --git a/app.ini b/app.ini index 4e98651..7a9c2f2 100644 --- a/app.ini +++ b/app.ini @@ -1,7 +1,7 @@ [uwsgi] wsgi-file = run.py callable = app -socket = :8080 +http = 0.0.0.0:8080 processes = 4 threads = 2 master = true diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..25b2e2b --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,45 @@ +version: "3.9" +services: + rabbit: + image: rabbitmq:management + container_name: rabbit + volumes: + - rabbit_data:/var/lib/rabbitmq + expose: + - 5672 + - 15672 + ports: + - 15672:15762 + networks: + net: + aliases: + - rabbit + api: + build: . + restart: always + environment: + FLASK_APP: run.py + SECRET_KEY: $API_SECRET + API_PASS: $API_PASS + RABBITMQ_HOST: rabbit + DB_PATH: /app/db/prod.db + WAIT_HOSTS: rabbit:15672 + expose: + - 8080 + ports: + - 8080:8080 + networks: + net: + aliases: + - api + volumes: + - api_db:/app/db + depends_on: + - rabbit + +volumes: + rabbit_data: + api_db: + +networks: + net: diff --git a/rmq_helper/__init__.py b/rmq_helper/__init__.py index 397721e..97be1d2 100644 --- a/rmq_helper/__init__.py +++ b/rmq_helper/__init__.py @@ -6,8 +6,8 @@ from pika.adapters.blocking_connection import BlockingChannel class RabbitMQ: host: str - connection: pika.BlockingConnection - channel: BlockingChannel + connection: pika.BlockingConnection = None + channel: BlockingChannel = None def __init__(self, app: Flask = None): if app is not None: diff --git a/scripts/entrypoint.sh b/scripts/entrypoint.sh new file mode 100644 index 0000000..14d812a --- /dev/null +++ b/scripts/entrypoint.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +/wait +flask db upgrade +flask seed +flask setup-rabmq +uwsgi app.ini