Initialer Commit

master
Clemens Klug 2018-02-01 16:59:35 +01:00
commit b2441c50b4
4 changed files with 80 additions and 0 deletions

BIN
.server.py.swp Normal file

Binary file not shown.

2
requirements.txt Normal file
View File

@ -0,0 +1,2 @@
Flask==0.12.2
toml==0.9.4

65
server.py Normal file
View File

@ -0,0 +1,65 @@
import json
import os
import shlex
from subprocess import Popen
import toml
from flask import Flask, request, abort
app = Flask(__name__)
SETTINGS_FILE = "settings.toml"
config = {}
if os.path.exists(SETTINGS_FILE):
config = toml.loads(open(SETTINGS_FILE, "r").read())
def json_path(obj, path):
if not type(obj) in [dict, list]:
return None
if "." not in path:
return None if path not in obj else obj[path]
key = path.split(".")
if key[0] not in obj:
return None
return json_path(obj[key[0]], ".".join(key[1:]))
def process(target):
app.logger.error("PROCESSING: "+str(target))
Popen(shlex.split(target["exec"]), cwd=target["path"])
app.logger.error("POST-processing :P")
return "True", 200
@app.route("/", methods=['GET','POST'])
def index():
content = request.data.decode("utf8")
try:
data = json.loads(content)
#app.logger.error(json.dumps(data, indent=1))
except Exception as e:
app.logger.exception(e)
return "False", 400
#app.logger.error(request.headers)
for source in config:
source = config[source]
if not "targets" in source:
continue
for target in source["targets"]:
target_name = target
target = source["targets"][target]
requested_name = json_path(data, source["name_path"])
if not requested_name:
continue
app.logger.error(str(target_name) + str(requested_name))
if requested_name == target_name:
if "token" in target:
if not source["tokenHeader"] in request.headers:
return "False", 403
if not request.headers[source["tokenHeader"]] == target["token"]:
return "False", 403
return process(target)
return "False", 404
app.run("0.0.0.0",8000)

13
settings.toml Normal file
View File

@ -0,0 +1,13 @@
[gitlab]
name_path = "project.path_with_namespace"
tokenHeader = "X-Gitlab-Token"
[gogs]
name_path = "repository.name"
tokenHeader = ""
[gitlab.targets."cklug/test"]
path = "/tmp/cd-target"
exec = "bash update.sh"
token = "fooBar"