diff --git a/server.py b/server.py index 84d66ee..099739f 100755 --- a/server.py +++ b/server.py @@ -12,10 +12,13 @@ from multiprocessing import Pool from gitlab import Gitlab import argparse -whitelist = ['nyanloutre/site-musique'] -def build(payload, gl): - gl.projects.get(payload['project']['path_with_namespace']).commits.get(payload['checkout_sha']).statuses.create({'state': 'running', 'name': 'Python CI'}) +def gitlab_build(payload, gl): + payload = payload + gl = gl + commit = gl.projects.get(payload['project']['path_with_namespace']).commits.get(payload['checkout_sha']) + + commit.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']) @@ -23,41 +26,41 @@ def build(payload, gl): repo_dir = temp_dir.name + '/' + payload['project']['name'] + '-' + payload['checkout_sha'] archive_url = payload['project']['web_url'] + '/-/archive/' + payload['checkout_sha'] + \ '/' + payload['project']['name'] + '-' + payload['checkout_sha'] + '.tar.gz' + with urllib.request.urlopen(archive_url) as gitlab_archive: with tarfile.open(fileobj=gitlab_archive, mode='r|gz') as gitlab_repo_files: gitlab_repo_files.extractall(path=temp_dir.name) + check_call(['ls', '-lha', repo_dir]) + try: check_call(['nix-build', '-o', args.output + '/' + payload['project']['path_with_namespace'], repo_dir]) except CalledProcessError: - return {'payload': payload, 'gitlab': gl, 'status': 'failed'} + commit.statuses.create({'state': 'failed', 'name': 'Python CI'}) + print("erreur build") - return {'payload': payload, 'gitlab': gl, 'status': 'success'} - -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'], 'name': 'Python CI'}) - print("build: " + result['status']) + commit.statuses.create({'state': 'success', 'name': 'Python CI'}) + print("build terminé") @view_defaults( route_name="gitlab_payload", renderer="json", request_method="POST" ) -class GitlabBuild(object): +class GitlabHook(object): def __init__(self, request): self.request = request self.payload = self.request.json - self.secret = open('secret', 'r').readline().splitlines()[0] - self.gitlab_token = open('gitlab_token', 'r').readline().splitlines()[0] + self.whitelist = ['nyanloutre/site-musique'] + self.secret = open(args.secret, 'r').readline().splitlines()[0] + self.gitlab_token = open(args.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'] == self.secret: + def push_hook(self): + if self.payload['project']['path_with_namespace'] in self.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) + pool.apply_async(gitlab_build, (self.payload, self.gl)) return "build started" else: raise HTTPNotFound @@ -65,6 +68,8 @@ class GitlabBuild(object): if __name__ == "__main__": parser = argparse.ArgumentParser(description='CI server') + parser.add_argument('--address', help='listening address', default='127.0.0.1') + parser.add_argument('--port', type=int, help='listening port') parser.add_argument('--output', help='output directory') parser.add_argument('--secret', help='repo secret file') parser.add_argument('--gitlab-token', help='gitlab token file') @@ -78,6 +83,6 @@ if __name__ == "__main__": config.scan() app = config.make_wsgi_app() - server = make_server("127.0.0.1", 52350, app) + server = make_server(args.address, args.port, app) print('listening ...') server.serve_forever()