From 1515dcd15089e5e10edb094abc065877fbb7e7a4 Mon Sep 17 00:00:00 2001 From: Ethanell Date: Sun, 12 Jan 2020 21:54:20 +0100 Subject: [PATCH] Init commit --- .gitignore | 2 ++ Dockerfile | 10 ++++++++++ app.py | 47 ++++++++++++++++++++++++++++++++++++++++++++++ docker-compose.yml | 11 +++++++++++ 4 files changed, 70 insertions(+) create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 app.py create mode 100644 docker-compose.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fcaef19 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +webhook_secret.env +TelegramEDT/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a3515be --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM alpine +RUN apk add --no-cache bash git python3 zbar jpeg-dev zlib-dev python3-dev +RUN pip3 install flask alembic +RUN mkdir /TelegramEDT +RUN git clone https://github.com/flifloo/TelegramEDT.git /TelegramEDT +RUN apk add --no-cache --virtual .build-deps build-base linux-headers +RUN pip3 install -r /TelegramEDT/requirements.txt +RUN apk del .build-deps +COPY ./app.py /root/app.py +CMD python3 /root/app.py diff --git a/app.py b/app.py new file mode 100644 index 0000000..bf63331 --- /dev/null +++ b/app.py @@ -0,0 +1,47 @@ +from flask import Flask, request, abort, request +from subprocess import run, Popen +import hmac +import hashlib +from os import environ +from os.path import isdir, isfile +from os import mkdir +from sqlite3 import connect + +if not isdir("/TelegramEDT/.git"): + run(["git", "clone", "https://github.com/flifloo/TelegramEDT.git", "/TelegramEDT"]) +if (not isdir("/TelegramEDT/alembic")) and isfile("/TelegramEDT/edt.db"): + c = connect("/TelegramEDT/edt.db") + c.execute("delete from alembic_version;") + c.commit() + +app = Flask(__name__) +webhook_secret = environ.get("webhook_secret") +bot = Popen(["python3", "main.py"], cwd="/TelegramEDT/") + +@app.route("/git", methods=["POST"]) +def git(): + if not "X-Hub-Signature" in request.headers: + abort(400) + + request.get_data() + signature = request.headers['X-Hub-Signature'] + payload = request.data + + secret = webhook_secret.encode() + hmac_gen = hmac.new(secret, payload, hashlib.sha1) + digest = "sha1=" + hmac_gen.hexdigest() + + + if signature != digest: + abort(400) + global bot + bot.kill() + + run(["git", "pull"], cwd="/TelegramEDT/") + run(["bash", "./update.sh"], cwd="/TelegramEDT/") + bot = Popen(["python3", "main.py"], cwd="/TelegramEDT/") + return "Ok" + +if __name__ == "__main__": + app.run(host="0.0.0.0") + diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..b9164cd --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,11 @@ +version: '3' +services: + bot: + build: . + ports: + - "5002:5000" + volumes: + - /home/docker/TelegramEDT/TelegramEDT:/TelegramEDT + env_file: + - webhook_secret.env +