From 330a259be1005890d85270cad8d240bbd80d0ef2 Mon Sep 17 00:00:00 2001 From: Benedikt Ziemons Date: Thu, 28 Oct 2021 22:58:37 +0200 Subject: [PATCH] Refactor to wmn package/module --- Dockerfile | 27 ++++++++++++++------------- wmn/__init__.py | 1 + common.py => wmn/common.py | 16 +++++++++++++--- notify.py => wmn/notify.py | 12 ++++++++---- wmn.py => wmn/wmn.py | 4 ++-- 5 files changed, 38 insertions(+), 22 deletions(-) create mode 100644 wmn/__init__.py rename common.py => wmn/common.py (92%) rename notify.py => wmn/notify.py (94%) rename wmn.py => wmn/wmn.py (99%) diff --git a/Dockerfile b/Dockerfile index 928369f..4272b0c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,21 +3,21 @@ MAINTAINER Benedikt Ziemons RUN apk add --no-cache uwsgi-python3 python3 py3-yaml py3-flask py3-matrix-nio py3-dateutil -# partly from https://hub.docker.com/_/python?tab=description#create-a-dockerfile-in-your-python-app-project -WORKDIR /usr/src/wmn +# copy required source files +COPY wmn/ /usr/local/lib/wmn/wmn -# copy required source file -COPY wmn.py ./ +ARG WMN_UID=1000 +ARG WMN_GID=1000 -WORKDIR /run/wmn +RUN mkdir -p /etc/wmn && \ + chmod 0700 /etc/wmn && \ + chown "${WMN_UID}" /etc/wmn && \ + addgroup -g "${WMN_GID}" wmn && \ + adduser -s /bin/sh -u "${WMN_UID}" -G wmn -D wmn -ARG WMN_UID=999 - -# requires config.yml to be present at build -COPY config.yml ./ -RUN chown -R $WMN_UID /run/wmn && chmod 0600 /run/wmn/config.yml - -USER $WMN_UID +USER wmn +VOLUME /etc/wmn/config.yml +ENV WMN_CONFIG_PATH=/etc/wmn/config.yml ARG PORT=3031 EXPOSE $PORT @@ -26,7 +26,8 @@ ENV UWSGI_SOCKET=:$PORT # opens a uwsgi socket at the given port, which is to be used by a reverse proxy CMD [ "uwsgi", "--die-on-term", \ "--need-plugin", "python3", \ - "--wsgi-file", "/usr/src/wmn/wmn.py", \ + "--module", "wmn.wmn", \ + "--pythonpath", "/usr/local/lib/wmn", \ "--master", \ "--processes", "1", \ "--threads", "2" ] diff --git a/wmn/__init__.py b/wmn/__init__.py new file mode 100644 index 0000000..5ffa7e9 --- /dev/null +++ b/wmn/__init__.py @@ -0,0 +1 @@ +__all__ = ["common", "notify", "wmn"] diff --git a/common.py b/wmn/common.py similarity index 92% rename from common.py rename to wmn/common.py index 822efc8..7ec3017 100644 --- a/common.py +++ b/wmn/common.py @@ -16,7 +16,8 @@ # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - +import os +import pathlib import sys from typing import Optional, Dict, Any, Tuple @@ -49,13 +50,22 @@ class MatrixException(Exception): return format_response(self.response) +def config_path() -> pathlib.Path: + path = os.environ.get("WMN_CONFIG_PATH", "./config.yml") + path = pathlib.Path(path) + path = path.absolute().resolve() + if not path.exists(): + raise RuntimeError("Cannot find config: " + path) + return path + + def load_configuration() -> Cfg: - with open("config.yml", "r") as ymlfile: + with open(str(config_path()), "r") as ymlfile: return yaml.safe_load(ymlfile) def save_configuration(configuration: Cfg): - with open("config.yml", "w") as ymlfile: + with open(str(config_path()), "w") as ymlfile: yaml.safe_dump(configuration, ymlfile) diff --git a/notify.py b/wmn/notify.py similarity index 94% rename from notify.py rename to wmn/notify.py index 5042d8e..21c7d14 100755 --- a/notify.py +++ b/wmn/notify.py @@ -24,9 +24,14 @@ import re import sys import nio -import yaml -from common import client_login, send_message, resolve_room, MatrixException +from .common import ( + client_login, + send_message, + resolve_room, + MatrixException, + load_configuration, +) # Not going to care for specifics like the underscore. # Generally match !anything:example.com with unicode support. @@ -42,8 +47,7 @@ async def main(): username: ... password: "..." """ - with open("config.yml", "r") as ymlfile: - cfg = yaml.safe_load(ymlfile) + cfg = load_configuration() parser = argparse.ArgumentParser(description="Notify a matrix channel.") parser.add_argument( diff --git a/wmn.py b/wmn/wmn.py similarity index 99% rename from wmn.py rename to wmn/wmn.py index 992603d..0f55862 100644 --- a/wmn.py +++ b/wmn/wmn.py @@ -24,12 +24,12 @@ import traceback from datetime import datetime from typing import Tuple, Optional, Dict, Any +import dateutil.parser import nio from flask import Flask, request, abort from werkzeug.datastructures import MultiDict -import dateutil.parser -from common import ( +from .common import ( client_login, send_message, Cfg,