From 940792c9b79e9ef078633ca6c8d9aa1aaa32ff13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torma=20Krist=C3=B3f?= Date: Sat, 18 Jul 2020 12:34:52 +0200 Subject: [PATCH] project structure done --- .dockerignore | 140 ++++++++++++++++++ .drone.yml | 77 ++++++++++ .idea/.gitignore | 8 + .idea/guard-service.iml | 15 ++ .../inspectionProfiles/profiles_settings.xml | 6 + .idea/misc.xml | 7 + .idea/modules.xml | 8 + .idea/vcs.xml | 6 + Dockerfile | 17 +++ docker-compose.yml | 37 +++++ k8s/configmap.yaml | 19 +++ k8s/deployment.yaml | 27 ++++ requirements.txt | 4 + src/app.py | 25 ++++ src/config.py | 28 ++++ 15 files changed, 424 insertions(+) create mode 100644 .dockerignore create mode 100644 .drone.yml create mode 100644 .idea/.gitignore create mode 100644 .idea/guard-service.iml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 k8s/configmap.yaml create mode 100644 k8s/deployment.yaml create mode 100644 requirements.txt create mode 100644 src/app.py create mode 100644 src/config.py diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..51f8a0c --- /dev/null +++ b/.dockerignore @@ -0,0 +1,140 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +pip-wheel-metadata/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +#Pycharm +.idea/ + +*.md +.gitignore +.git/ +*.yml +contrib/* +postman/* +*.wav \ No newline at end of file diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..4e11b5a --- /dev/null +++ b/.drone.yml @@ -0,0 +1,77 @@ +kind: pipeline +type: docker +name: default + +steps: + - name: restore-cache-with-filesystem + image: meltwater/drone-cache + settings: + backend: "filesystem" + restore: true + cache_key: "{{ .Repo.Name }}" + archive_format: "gzip" + filesystem_cache_root: "/tmp/cache" + mount: + - '.pipcache' + volumes: + - name: cache + path: /tmp/cache + + - name: static_analysis + image: "python:3.8" + commands: + - pip3 install --cache-dir='./.pipcache' pylint bandit mccabe + - pip3 install --cache-dir='./.pipcache' -r requirements.txt + - find . -name "*.py" -exec python3 -m py_compile '{}' \; + - find . -name "*.py" -exec pylint '{}' + || if [ $? -eq 1 ]; then echo "you fail"; fi + - find . -name "*.py" -exec python3 -m mccabe --min 3 '{}' + || if [ $? -eq 1 ]; then echo "you fail"; fi + - bandit -r . + || if [ $? -eq 1 ]; then echo "you fail"; fi + + - name: code-analysis + image: aosapps/drone-sonar-plugin + settings: + sonar_host: + from_secret: SONAR_HOST + sonar_token: + from_secret: SONAR_CODE + + - name: rebuild-cache-with-filesystem + image: meltwater/drone-cache:dev + pull: true + settings: + backend: "filesystem" + rebuild: true + cache_key: "{{ .Repo.Name }}" + archive_format: "gzip" + filesystem_cache_root: "/tmp/cache" + mount: + - '.pipcache' + volumes: + - name: cache + path: /tmp/cache + + - name: kaniko + image: banzaicloud/drone-kaniko + settings: + registry: registry.kmlabz.com + repo: birbnetes/${DRONE_REPO_NAME} + username: + from_secret: DOCKER_USERNAME + password: + from_secret: DOCKER_PASSWORD + tags: + - latest + - ${DRONE_BUILD_NUMBER} + + - name: ms-teams + image: kuperiu/drone-teams + settings: + webhook: + from_secret: TEAMS_WEBHOOK + when: + status: [ failure ] + +volumes: + - name: cache + host: + path: "/tmp/cache" \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/guard-service.iml b/.idea/guard-service.iml new file mode 100644 index 0000000..9c74c25 --- /dev/null +++ b/.idea/guard-service.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..d3edc0d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..31595a4 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..3380952 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +FROM python:3.8-slim + +ENV TZ Europe/Budapest +RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone + +WORKDIR /app + +ARG RELEASE_ID +ENV RELEASE_ID ${RELEASE_ID:-""} + +COPY requirements.txt ./ + +RUN pip install --no-cache-dir -r requirements.txt + +COPY ./src . + +ENTRYPOINT ["python3", "app.py"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..958d860 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,37 @@ +networks: + guard: + external: false + +services: + rabbitmq: + image: "rabbitmq:3" + hostname: "test-rabbitmq" + environment: + RABBITMQ_ERLANG_COOKIE: "akjahsfvbkueasnvfjkhsga" + RABBITMQ_DEFAULT_USER: "rabbitmq" + RABBITMQ_DEFAULT_PASS: "rabbitmq" + RABBITMQ_DEFAULT_VHOST: "/" + networks: + - guard + ports: + - "127.0.0.1:15672:15672" + - "127.0.0.1:5672:5672" + + activemq: + image: registry.kmlabz.com/birbnetes/activemq-artemis + restart: always + networks: + - guard + volumes: + - ./artemis-volume:/var/lib/artemis-instance + + guard-service: + image: registry.kmlabz.com/birbnetes/guard-service + restart: always + depends_on: + - activemq + - postgres + networks: + - guard + ports: + - "127.0.0.1:8080:8080" \ No newline at end of file diff --git a/k8s/configmap.yaml b/k8s/configmap.yaml new file mode 100644 index 0000000..b55881d --- /dev/null +++ b/k8s/configmap.yaml @@ -0,0 +1,19 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: guard-service + labels: + app: guard-service + namespace: birbnetes +data: + SENTRY_DSN: "https://80d27db8c74f4556a19a1bf0180b373f@sentry.kmlabz.com/23" + RELEASE_ID: birb-k8s + GUARD_SERVICE_RELEASEMODE: release + GUARD_RABBITMQ_HOSTNAME: birb-rabbitmq + GUARD_RABBITMQ_EXCHANGE: "sample" + GUARD_RABBITMQ_USERNAME: user + GUARD_RABBITMQ_PASSWORD: 1wZVQnP5vy + GUARD_MQTT_HOSTNAME: guard-postgres + GUARD_MQTT_USERNAME: guard-service + GUARD_MQTT_PASSWORD: guard-service-supersecret + GUARD_MQTT_TOPIC: guard-service \ No newline at end of file diff --git a/k8s/deployment.yaml b/k8s/deployment.yaml new file mode 100644 index 0000000..03acee4 --- /dev/null +++ b/k8s/deployment.yaml @@ -0,0 +1,27 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: guard-service + namespace: birbnetes + labels: + app: guard-service +spec: + replicas: 1 + selector: + matchLabels: + app: guard-service + strategy: + type: Recreate + template: + metadata: + labels: + app: guard-service + spec: + containers: + - name: guard-service + image: registry.kmlabz.com/birbnetesgit/guard-service + envFrom: + - configMapRef: + name: guard-service + imagePullSecrets: + - name: regcred \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..adc26f8 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +sentry_sdk +pika +marshmallow +paho-mqtt \ No newline at end of file diff --git a/src/app.py b/src/app.py new file mode 100644 index 0000000..5e98948 --- /dev/null +++ b/src/app.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 +import logging +import sentry_sdk + +from config import * + +""" +Main entry point +""" + +__author__ = "@tormakris" +__copyright__ = "Copyright 2020, Birbnetes Team" +__module_name__ = "app" +__version__text__ = "1" + +if SENTRY_DSN: + sentry_sdk.init( + dsn=SENTRY_DSN, + send_default_pii=True, + release=RELEASE_ID, + environment=RELEASEMODE + ) + +if __name__ == "__main__": + pass diff --git a/src/config.py b/src/config.py new file mode 100644 index 0000000..19b0723 --- /dev/null +++ b/src/config.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 +import os + + +""" +Configuration variables +""" + + +__author__ = "@tormakris" +__copyright__ = "Copyright 2020, Birbnetes Team" +__module_name__ = "config" +__version__text__ = "1" + + +SENTRY_DSN = os.environ.get("SENTRY_DSN") +RELEASE_ID = os.environ.get("RELEASE_ID", "test") +RELEASEMODE = os.environ.get("GUARD_SERVICE_RELEASEMODE", "dev") + +RABBIT_HOSTNAME = os.getenv("GUARD_RABBITMQ_HOSTNAME", "localhost") +RABBIT_USERNAME = os.getenv("GUARD_RABBITMQ_USERNAME", "guard-service") +RABBIT_PASSWORD = os.getenv("GUARD_RABBITMQ_PASSWORD", "guard-service") +RABBIT_EXCHANGE = os.getenv("GUARD_RABBITMQ_EXCHANGE", "guard-service") + +MQTT_HOSTNAME = os.getenv("GUARD_MQTT_HOSTNAME", "localhost") +MQTT_USERNAME = os.getenv("GUARD_MQTT_USERNAME", "guard-service") +MQTT_PASSWORD = os.getenv("GUARD_MQTT_PASSWORD", "guard-service") +MQTT_EXCHANGE = os.getenv("GUARD_MQTT_EXCHANGE", "guard-service")