From 85817848f29f1e2c3680da216a8dfc54a2ad3932 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isma=C3=ABl=20Bouya?= Date: Tue, 26 Mar 2019 01:31:24 +0100 Subject: Add deploy webhook --- nixops/modules/buildbot/default.nix | 8 ++++- .../modules/buildbot/projects/caldance/__init__.py | 38 ++++++++++++++++++++++ nixops/modules/buildbot/projects/test/__init__.py | 38 ++++++++++++++++++++++ 3 files changed, 83 insertions(+), 1 deletion(-) (limited to 'nixops/modules') diff --git a/nixops/modules/buildbot/default.nix b/nixops/modules/buildbot/default.nix index 3768bde..21b784b 100644 --- a/nixops/modules/buildbot/default.nix +++ b/nixops/modules/buildbot/default.nix @@ -83,7 +83,13 @@ in ProxyPreserveHost On - Require local + + Require local + Require ldap-group cn=users,ou=${project.name},cn=buildbot,ou=services,dc=immae,dc=eu + ${if lib.attrsets.hasAttr "webhookTokens" project then '' + Require expr "req('Access-Key') in { ${builtins.concatStringsSep ", " (map (x: "'${x}'") project.webhookTokens)} }" + '' else ""} + '') myconfig.env.buildbot.projects; diff --git a/nixops/modules/buildbot/projects/caldance/__init__.py b/nixops/modules/buildbot/projects/caldance/__init__.py index e28ef72..1069c35 100644 --- a/nixops/modules/buildbot/projects/caldance/__init__.py +++ b/nixops/modules/buildbot/projects/caldance/__init__.py @@ -1,6 +1,8 @@ from buildbot.plugins import * from buildbot_common.build_helpers import * import os +from buildbot.util import bytes2unicode +import json __all__ = [ "configure", "E" ] @@ -32,16 +34,52 @@ class E(): TITLE_URL = "https://caldance.immae.eu" TITLE = "Caldance" +class CustomBase(webhooks.base): + def getChanges(self, request): + try: + content = request.content.read() + args = json.loads(bytes2unicode(content)) + except Exception as e: + raise ValueError("Error loading JSON: " + str(e)) + + args.setdefault("comments", "") + args.setdefault("repository", "") + args.setdefault("author", args.get("who", "unknown")) + + if args["category"] == "deploy_webhook": + args = { + "category": "deploy_webhook", + "comments": "", + "repository": "", + "author": "webhook", + "project": "Caldance", + "properties": { + "environment": args.get("environment", "integration"), + "build": "caldance_{}.tar.gz".format(args.get("build", "master")) + } + } + + return ([args], None) + +def deploy_hook_scheduler(project, timer=1): + return schedulers.AnyBranchScheduler( + change_filter=util.ChangeFilter(category="deploy_webhook", project=project), + name="{}_deploy".format(project), treeStableTimer=timer, builderNames=["{}_deploy".format(project)]) + def configure(c): c["buildbotURL"] = E.BUILDBOT_URL c["www"]["port"] = E.SOCKET + c["www"]["change_hook_dialects"]["base"] = { + "custom_class": CustomBase + } c['workers'].append(worker.LocalWorker("generic-worker")) c['workers'].append(worker.LocalWorker("deploy-worker")) c['schedulers'].append(hook_scheduler("Caldance", timer=1)) c['schedulers'].append(force_scheduler("force_caldance", ["Caldance_build"])) c['schedulers'].append(deploy_scheduler("deploy_caldance", ["Caldance_deploy"])) + c['schedulers'].append(deploy_hook_scheduler("Caldance", timer=1)) c['builders'].append(factory("caldance")) diff --git a/nixops/modules/buildbot/projects/test/__init__.py b/nixops/modules/buildbot/projects/test/__init__.py index adda289..0644920 100644 --- a/nixops/modules/buildbot/projects/test/__init__.py +++ b/nixops/modules/buildbot/projects/test/__init__.py @@ -1,6 +1,8 @@ from buildbot.plugins import * from buildbot_common.build_helpers import * import os +from buildbot.util import bytes2unicode +import json __all__ = [ "configure", "E" ] @@ -28,16 +30,52 @@ class E(): TITLE_URL = "https://git.immae.eu/?p=perso/Immae/TestProject.git;a=summary" TITLE = "Test project" +class CustomBase(webhooks.base): + def getChanges(self, request): + try: + content = request.content.read() + args = json.loads(bytes2unicode(content)) + except Exception as e: + raise ValueError("Error loading JSON: " + str(e)) + + args.setdefault("comments", "") + args.setdefault("repository", "") + args.setdefault("author", args.get("who", "unknown")) + + if args["category"] == "deploy_webhook": + args = { + "category": "deploy_webhook", + "comments": "", + "repository": "", + "author": "unknown", + "project": "TestProject", + "properties": { + "environment": args.get("environment", "integration"), + "build": "test_{}.tar.gz".format(args.get("branch", "master")) + } + } + + return ([args], None) + +def deploy_hook_scheduler(project, timer=1): + return schedulers.AnyBranchScheduler( + change_filter=util.ChangeFilter(category="deploy_webhook", project=project), + name="{}_deploy".format(project), treeStableTimer=timer, builderNames=["{}_deploy".format(project)]) + def configure(c): c["buildbotURL"] = E.BUILDBOT_URL c["www"]["port"] = E.SOCKET + c["www"]["change_hook_dialects"]["base"] = { + "custom_class": CustomBase + } c['workers'].append(worker.LocalWorker("generic-worker-test")) c['workers'].append(worker.LocalWorker("deploy-worker-test")) c['schedulers'].append(hook_scheduler("TestProject", timer=1)) c['schedulers'].append(force_scheduler("force_test", ["TestProject_build"])) c['schedulers'].append(deploy_scheduler("deploy_test", ["TestProject_deploy"])) + c['schedulers'].append(deploy_hook_scheduler("TestProject", timer=1)) c['builders'].append(factory()) c['builders'].append(deploy_factory()) -- cgit v1.2.3