From a27b819cd5d75018d2798b54793530c87d7a3914 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Fri, 19 Jul 2019 21:01:21 +0300 Subject: Change: make controller's /trigger call VCSWorker's /updaterepo --- api.py | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/api.py b/api.py index 8ea2819..2e0026d 100755 --- a/api.py +++ b/api.py @@ -23,6 +23,7 @@ # which forwards requests to localhost. +import json import logging import os import shutil @@ -372,9 +373,14 @@ class Controller(API): '''A dummy controller API''' - def __init__(self): + VCSWORKER = 'https://wmf2-vcsworker.vm.liw.fi' + DEPLOYER = 'https://wmf2-deployer.vm.liw.fi' + MAX_TRIGGER_TIME = 60 + + def __init__(self, token): super().__init__() - self.builds = [] + self._token = token + self._builds = [] def get_routes(self): return [ @@ -394,11 +400,32 @@ class Controller(API): def _status(self): return { - 'builds': list(self.builds), + 'builds': list(self._builds), } def _trigger(self): - return 'trigger?' + spec = bottle.request.json + if spec is None: + logging.error('Request has null as spec') + logging.debug('Request: %r', dict(bottle.request.headers)) + logging.debug('Request: %r', bottle.request.body.read()) + raise bottle.HTTPError(400) + + logging.info('Triggering build of %r', spec) + self._builds.append(spec) + + url = '{}/updaterepo'.format(self.VCSWORKER) + argv = [ + 'curl', + '-HAuthorization: Bearer {}'.format(self._token), + '-HContent-Type: application/json', + '--data-binary', json.dumps(spec), + url, + ] + ok = runcmd('.', argv, self.MAX_TRIGGER_TIME) + if not ok: + raise bottle.HTTPError(500, 'Error triggering build') + return 'Triggered build' def runcmd(cwd, argv, timeout): @@ -450,7 +477,8 @@ def main(): setup_logging() if cmd == 'controller': - api = Controller() + token = get_token_from_file(args.pop(0)) + api = Controller(token) elif cmd == 'vcsworker': gitlab_token = get_token_from_file(args.pop(0)) artifact_token = get_token_from_file(args.pop(0)) -- cgit v1.2.1