From f7f40738d04f18269b5317807f0e7038f7ae3946 Mon Sep 17 00:00:00 2001 From: norohind <60548839+norohind@users.noreply.github.com> Date: Tue, 28 Nov 2023 19:00:10 +0300 Subject: [PATCH] Dockerized --- .dockerignore | 3 +++ .gitignore | 5 +++++ Dockerfile | 27 +++++++++++++++++++++++++ capi/__init__.py | 2 +- capi/model.py | 4 ++-- config.py | 2 ++ entrypoint.sh | 7 +++++++ generate_uswgi_config.py | 30 ++++++++++++++++++++++++++++ requerements.txt => requirements.txt | 0 9 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 .dockerignore create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100755 entrypoint.sh create mode 100644 generate_uswgi_config.py rename requerements.txt => requirements.txt (100%) diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..051ed09 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,3 @@ +.git +.gitignore +.dockerignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a3bc581 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +__pycache__ +*.sqlite +venv +start_web_uwsgi.bash +uwsgi.ini diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..033254f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,27 @@ +FROM python:3.11-slim as builder +WORKDIR /app + +ENV PYTHONDONTWRITEBYTECODE 1 +ENV PYTHONUNBUFFERED 1 + +COPY requirements.txt . +RUN apt update && apt install -y gcc +RUN pip wheel --no-cache-dir --no-deps --wheel-dir /app/wheels -r requirements.txt + +FROM python:3.11-slim + +ENV PYTHONDONTWRITEBYTECODE 1 +ENV PYTHONUNBUFFERED 1 + +RUN useradd --no-create-home --system user +WORKDIR /app + +COPY --from=builder /app/wheels /wheels +COPY --from=builder /app/requirements.txt . + +RUN pip install --no-cache /wheels/* +USER user +COPY . . + +ENTRYPOINT ["/app/entrypoint.sh"] + diff --git a/capi/__init__.py b/capi/__init__.py index a16f2ad..b240b23 100644 --- a/capi/__init__.py +++ b/capi/__init__.py @@ -207,4 +207,4 @@ class CAPIAuthorizer: return self.model.list_all_records() -capi_authorizer = CAPIAuthorizer(model.Model()) +capi_authorizer = CAPIAuthorizer(model.Model(config.db_location)) diff --git a/capi/model.py b/capi/model.py index c811b3e..ba17b9a 100644 --- a/capi/model.py +++ b/capi/model.py @@ -8,8 +8,8 @@ logger = get_main_logger() class Model: - def __init__(self): - self.db: sqlite3.Connection = sqlite3.connect('companion-api.sqlite', check_same_thread=False) + def __init__(self, db_location: str): + self.db: sqlite3.Connection = sqlite3.connect(db_location, check_same_thread=False) self.db.row_factory = lambda c, r: dict(zip([col[0] for col in c.description], r)) with self.db: self.db.execute(sqlite_requests.schema) diff --git a/config.py b/config.py index 9f37401..3a2c86c 100644 --- a/config.py +++ b/config.py @@ -10,6 +10,8 @@ log_level = os.getenv('LOG_LEVEL', 'DEBUG').upper() access_key = os.getenv('access_key') +db_location = os.getenv('db_location', 'companion-api.sqlite') + REDIRECT_URL = requests.utils.quote(os.getenv('REDIRECT_URL', '')) AUTH_URL = 'https://auth.frontierstore.net/auth' TOKEN_URL = 'https://auth.frontierstore.net/token' diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100755 index 0000000..dc8f851 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +set -eu + +python3 generate_uswgi_config.py +exec uwsgi -c /tmp/uwsgi.ini + diff --git a/generate_uswgi_config.py b/generate_uswgi_config.py new file mode 100644 index 0000000..514562d --- /dev/null +++ b/generate_uswgi_config.py @@ -0,0 +1,30 @@ +import os + +template = """ +[uwsgi] +master = 1 +vacuum = true +socket = 0.0.0.0:8080 +enable-threads = true +die-on-term = true +thunder-lock = true +threads = {threads} +processes = {processes} +wsgi-file = {wsgi_file} +need-app = true +check-static = static +chdir = {project_dir}"""[1:] + +project_dir = os.path.dirname(os.path.abspath(__file__)) # current dir +wsgi_file = os.path.join(project_dir, 'web.py') + +cpu_count = os.cpu_count() +process_count = cpu_count + +config = template.format(threads=cpu_count, + processes=process_count, + wsgi_file=wsgi_file, + project_dir=project_dir) + +with open('/tmp/uwsgi.ini', 'w') as file: + file.write(config) diff --git a/requerements.txt b/requirements.txt similarity index 100% rename from requerements.txt rename to requirements.txt