From d4e787a547d4a72b4f599f19facc18caf1213e2d Mon Sep 17 00:00:00 2001 From: Sunpy Date: Mon, 25 Feb 2019 11:37:53 +0100 Subject: [PATCH] Auto-pull and restart for VPS --- main.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index 034a3c5..b5095d7 100644 --- a/main.py +++ b/main.py @@ -1,10 +1,32 @@ -from flask import Flask, url_for +import json +from flask import Flask, url_for, request from objects import glob # Global sharing of python objects in a manageable way glob.app = Flask(__name__) import routes # All flask app routes +if glob.config["git"]["auto_pull_and_restart"]: # Only used on the VPS (Do not enable in config) + @glob.app.route(glob.config["git"]["webhook_endpoint"], methods = ["POST"]) + def git_fetch(): + if request.method != "POST": + return "No" + + data = json.loads( request.data.decode() ) + if data["secret"] != glob.config["git"]["secret"]: + return "No" + + import subprocess, os, sys + process = subprocess.Popen(["git", "pull"], stdout=subprocess.PIPE) + output = process.communicate()[0] + + if output.decode().split("\n")[-2].find("fail") != -1: + return "Failed to pull changes" + + # If needed; every open file or connection HAVE to be closed at this point + + os.execv(sys.executable, ["python"] + sys.argv) # Restart service + if glob.config["disable-static-cache"]: import os