From 7e118c37f5248caf021039ced38ec9371653690f Mon Sep 17 00:00:00 2001 From: Yannik Schmidt Date: Thu, 5 Jan 2023 19:35:09 +0100 Subject: [PATCH] feat: docker build & config dir --- .github/workflows/main.yaml | 37 +++++++++++++++++++++++++++++++++++++ Dockerfile | 19 +++++++++++++++++++ req.txt | 3 +++ server.py | 32 +++++++++++++++++++++++++++----- 4 files changed, 86 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/main.yaml create mode 100644 Dockerfile create mode 100644 req.txt diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml new file mode 100644 index 0000000..71d1d4e --- /dev/null +++ b/.github/workflows/main.yaml @@ -0,0 +1,37 @@ +name: ci + +on: + push: + branches: + - "master" + +jobs: + docker: + runs-on: ubuntu-latest + environment: + name: prod + steps: + - + name: Checkout + uses: actions/checkout@v3 + - + name: Set up QEMU + uses: docker/setup-qemu-action@v2 + - + name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + - + name: Login to Docker Registry + uses: docker/login-action@v2 + with: + registry: ${{ secrets.REGISTRY }} + username: ${{ secrets.REGISTRY_USER }} + password: ${{ secrets.REGISTRY_PASS }} + - + name: Build and push async-icinga image + uses: docker/build-push-action@v3 + with: + context: . + platforms: linux/amd64 + push: true + tags: "${{ secrets.REGISTRY }}/athq/async-icinga:latest" diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..4e1af8c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +FROM python:3.9-slim-buster + +RUN apt update +RUN apt install python3-pip git curl -y +RUN python3 -m pip install waitress +RUN python3 -m pip install --upgrade pip + +WORKDIR /app +COPY ./ . + +RUN python3 -m pip install --no-cache-dir -r req.txt + +#HEALTHCHECK --interval=5m --timeout=5s CMD /usr/bin/curl http://localhost:5000/ || exit 1 +EXPOSE 5000/tcp + +RUN apt remove git -y +RUN apt autoremove -y + +CMD waitress-serve --host 0.0.0.0 --port 5000 --call 'app:createApp' diff --git a/req.txt b/req.txt new file mode 100644 index 0000000..bd5e1e5 --- /dev/null +++ b/req.txt @@ -0,0 +1,3 @@ +pytimeparse +flask +flask-sqlalchemy diff --git a/server.py b/server.py index 5bcd3bf..b7dc962 100755 --- a/server.py +++ b/server.py @@ -6,6 +6,7 @@ import argparse import os import datetime import pytimeparse.timeparse as timeparse +import sys from sqlalchemy import Column, Integer, String, Boolean, or_, and_ from sqlalchemy.orm import sessionmaker @@ -18,6 +19,7 @@ from sqlalchemy.sql.expression import func app = flask.Flask("Icinga Report In Gateway") app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.sqlite' app.config['JSON_CONFIG_FILE'] = 'services.json' +app.config['JSON_CONFIG_DIR'] = 'config' db = SQLAlchemy(app) class Service(db.Model): @@ -67,6 +69,11 @@ def alive(): # simple location for icinga alive checks via HTTP # return ("", 204) +@app.route('/reload-configuration') +def reload(): + init() + return ("", 204) + @app.route('/', methods=["GET", "POST"]) def default(): if flask.request.method == "GET": @@ -152,13 +159,28 @@ def default(): @app.before_first_request def init(): + db.create_all() - with open(app.config["JSON_CONFIG_FILE"]) as f: - config = json.load(f) - for key in config: - timeout = timeparse.timeparse(config[key]["timeout"]) - db.session.merge(Service(service=key, token=config[key]["token"], timeout=timeout)) + config = {} + + if os.path.isfile(app.config["JSON_CONFIG_FILE"]): + with open(app.config["JSON_CONFIG_FILE"]) as f: + config |= json.load(f) + elif os.path.isdir(app.config["JSON_CONFIG_DIR"]): + for fname in os.listdir(app.config["JSON_CONFIG_DIR"]): + fullpath = os.path.join(app.config["JSON_CONFIG_DIR"], fname) + if fname.endswith(".json"): + with open(fullpath) as f: + config |= json.load(f) + + if not config: + raise ValueError("No valid configuration found - need at least one service") + + for key in config: + timeout = timeparse.timeparse(config[key]["timeout"]) + db.session.merge(Service(service=key, token=config[key]["token"], timeout=timeout)) db.session.commit() + if __name__ == "__main__":