From 31b7171656efceb4e354c7a838ce6ceb567fb14c Mon Sep 17 00:00:00 2001 From: nyanloutre Date: Sun, 4 Nov 2018 18:22:16 +0100 Subject: [PATCH] Arguments: fichiers de mot de passe --- server.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/server.py b/server.py index c7393a5..84d66ee 100755 --- a/server.py +++ b/server.py @@ -10,14 +10,12 @@ import tarfile from tempfile import TemporaryDirectory from multiprocessing import Pool from gitlab import Gitlab +import argparse whitelist = ['nyanloutre/site-musique'] -secret = open('secret', 'r').readline().splitlines()[0] -gitlab_token = open('gitlab_token', 'r').readline().splitlines()[0] -out_dir = './run' def build(payload, gl): - gl.projects.get(payload['project']['path_with_namespace']).commits.get(payload['checkout_sha']).statuses.create({'state': 'running'}) + gl.projects.get(payload['project']['path_with_namespace']).commits.get(payload['checkout_sha']).statuses.create({'state': 'running', 'name': 'Python CI'}) print("push from " + payload['user_name']) print("repo: " + payload['project']['path_with_namespace']) print("commit: " + payload['checkout_sha']) @@ -30,7 +28,7 @@ def build(payload, gl): gitlab_repo_files.extractall(path=temp_dir.name) check_call(['ls', '-lha', repo_dir]) try: - check_call(['nix-build', '-o', out_dir + '/' + payload['project']['path_with_namespace'], repo_dir]) + check_call(['nix-build', '-o', args.output + '/' + payload['project']['path_with_namespace'], repo_dir]) except CalledProcessError: return {'payload': payload, 'gitlab': gl, 'status': 'failed'} @@ -39,24 +37,26 @@ def build(payload, gl): def build_success(result): gl = result['gitlab'] payload = result['payload'] - gl.projects.get(payload['project']['path_with_namespace']).commits.get(payload['checkout_sha']).statuses.create({'state': result['status']}) + gl.projects.get(payload['project']['path_with_namespace']).commits.get(payload['checkout_sha']).statuses.create({'state': result['status'], 'name': 'Python CI'}) print("build: " + result['status']) @view_defaults( route_name="gitlab_payload", renderer="json", request_method="POST" ) -class PayloadView(object): +class GitlabBuild(object): def __init__(self, request): self.request = request self.payload = self.request.json - self.gl = Gitlab('https://gitlab.com', private_token=gitlab_token) + self.secret = open('secret', 'r').readline().splitlines()[0] + self.gitlab_token = open('gitlab_token', 'r').readline().splitlines()[0] + self.gl = Gitlab('https://gitlab.com', private_token=self.gitlab_token) @view_config(header="X-Gitlab-Event:Push Hook") def payload_push(self): - if self.payload['project']['path_with_namespace'] in whitelist and self.request.headers['X-Gitlab-Token'] == secret: - self.gl.projects.get(self.payload['project']['path_with_namespace']).commits.get(self.payload['checkout_sha']).statuses.create({'state': 'pending'}) + if self.payload['project']['path_with_namespace'] in whitelist and self.request.headers['X-Gitlab-Token'] == self.secret: + self.gl.projects.get(self.payload['project']['path_with_namespace']).commits.get(self.payload['checkout_sha']).statuses.create({'state': 'pending', 'name': 'Python CI'}) pool.apply_async(build, (self.payload, self.gl), callback=build_success) return "build started" else: @@ -64,6 +64,12 @@ class PayloadView(object): if __name__ == "__main__": + parser = argparse.ArgumentParser(description='CI server') + parser.add_argument('--output', help='output directory') + parser.add_argument('--secret', help='repo secret file') + parser.add_argument('--gitlab-token', help='gitlab token file') + args = parser.parse_args() + pool = Pool(1) config = Configurator()