premier commit
This commit is contained in:
commit
0ac55a792e
63
server.py
Normal file
63
server.py
Normal file
@ -0,0 +1,63 @@
|
||||
from wsgiref.simple_server import make_server
|
||||
from pyramid.config import Configurator
|
||||
from pyramid.view import view_config, view_defaults
|
||||
from pyramid.httpexceptions import HTTPNotFound
|
||||
from subprocess import check_call
|
||||
import urllib.request
|
||||
import tarfile
|
||||
from tempfile import TemporaryDirectory
|
||||
from multiprocessing import Pool
|
||||
|
||||
whitelist = ['nyanloutre/site-musique']
|
||||
secret = 'xxxxxxxxxxxxxxxxxxxxxx'
|
||||
out_dir = './run'
|
||||
|
||||
def build(payload):
|
||||
print("push from " + payload['user_name'])
|
||||
print("repo: " + payload['project']['path_with_namespace'])
|
||||
print("commit: " + payload['checkout_sha'])
|
||||
temp_dir = TemporaryDirectory()
|
||||
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])
|
||||
check_call(['nix-build', '-o', out_dir + '/' + payload['project']['path_with_namespace'], repo_dir])
|
||||
|
||||
def build_success(payload):
|
||||
print("build complete")
|
||||
|
||||
def build_failed(payload):
|
||||
print("build failed")
|
||||
|
||||
@view_defaults(
|
||||
route_name="gitlab_payload", renderer="json", request_method="POST"
|
||||
)
|
||||
class PayloadView(object):
|
||||
|
||||
def __init__(self, request):
|
||||
self.request = request
|
||||
self.payload = self.request.json
|
||||
|
||||
@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:
|
||||
pool.apply_async(build, (self.payload,), callback=build_success, error_callback=build_failed)
|
||||
return "build started"
|
||||
else:
|
||||
raise HTTPNotFound
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pool = Pool(1)
|
||||
|
||||
config = Configurator()
|
||||
|
||||
config.add_route("gitlab_payload", "/gitlab_payload")
|
||||
config.scan()
|
||||
|
||||
app = config.make_wsgi_app()
|
||||
server = make_server("127.0.0.1", 52350, app)
|
||||
server.serve_forever()
|
Loading…
Reference in New Issue
Block a user