]> git.immae.eu Git - perso/Immae/Config/Nix.git/blobdiff - modules/private/buildbot/common/build_helpers.py
Fix ttrss not synchronizing
[perso/Immae/Config/Nix.git] / modules / private / buildbot / common / build_helpers.py
index 384b1ac34f895eed6342318f0fbc0be63af749af..ebd49ae9d4a53310b9aaf7ecb605bb07769a00ac 100644 (file)
@@ -1,10 +1,13 @@
 from buildbot.plugins import util, steps, schedulers
 from buildbot_buildslist import BuildsList
+from shutil import which
 
 __all__ = [
-        "force_scheduler", "deploy_scheduler", "hook_scheduler",
+        "force_scheduler", "deploy_scheduler", "git_hook_scheduler",
         "clean_branch", "package_and_upload", "SlackStatusPush",
-        "XMPPStatusPush"
+        "XMPPStatusPush", "NixShellCommand",
+        "all_builder_names", "compute_build_infos", "deploy_ssh_command",
+        "configure_slack_push", "configure_xmpp_push", "deploy_hook_scheduler",
         ]
 
 # Small helpers"
@@ -18,27 +21,60 @@ def clean_branch(props):
 def package_and_upload(package, package_dest, package_url):
     return [
             steps.ShellCommand(name="build package",
-                logEnviron=False, haltOnFailure=True, workdir="source",
+                logEnviron=False, haltOnFailure=True,
                 command=["git", "archive", "HEAD", "-o", package]),
 
             steps.FileUpload(name="upload package", workersrc=package,
-                workdir="source", masterdest=package_dest,
+                masterdest=package_dest,
                 url=package_url, mode=0o644),
 
             steps.ShellCommand(name="cleanup package", logEnviron=False,
-                haltOnFailure=True, workdir="source", alwaysRun=True,
+                haltOnFailure=True, alwaysRun=True,
                 command=["rm", "-f", package]),
             ]
 
+# Steps
+class NixShellCommand(steps.ShellCommand):
+    def __init__(self, command=None, nixPackages=[], pure=True, nixFile=None, nixIncludes={}, nixArgs={}, **kwargs):
+        oldpath = kwargs.get("env", {}).get("PATH", None)
+        if which("nix-shell", path=oldpath) is None:
+            kwargs["env"] = kwargs.get("env", {})
+            if isinstance(oldpath, str):
+                kwargs["env"]["PATH"] = "/run/current-system/sw/bin:" + oldpath
+            elif isinstance(oldpath, list):
+                kwargs["env"]["PATH"] = ["/run/current-system/sw/bin"] + oldpath
+        nixcommand = ["nix-shell"]
+        for k, v in nixArgs.items():
+            nixcommand.append("--arg")
+            nixcommand.append(k)
+            nixcommand.append(v)
+        if pure:
+            nixcommand.append("--pure")
+        for k, v in nixIncludes.items():
+            nixcommand.append("-I")
+            nixcommand.append("{}={}".format(k, v))
+        nixcommand.append("--run")
+        nixcommand.append(command)
+        if len(nixPackages) > 0:
+            nixcommand.append("-p")
+            nixcommand += nixPackages
+        elif nixFile is not None:
+            nixcommand.append(nixFile)
+        super().__init__(command=nixcommand, **kwargs)
+
 # Schedulers
-def force_scheduler(name, builders):
+def force_scheduler(name, builders, nobranch=False):
+    if nobranch:
+        branch = util.FixedParameter(name="branch", default="")
+    else:
+        branch=util.StringParameter(name="branch", label="Git reference (tag, branch)", required=True)
+
     return schedulers.ForceScheduler(name=name,
         label="Force build", buttonName="Force build",
         reason=util.StringParameter(name="reason", label="Reason", default="Force build"),
         codebases=[
             util.CodebaseParameter("",
-                branch=util.StringParameter(
-                    name="branch", label="Git reference (tag, branch)", required=True),
+                branch=branch,
                 revision=util.FixedParameter(name="revision", default=""),
                 repository=util.FixedParameter(name="repository", default=""),
                 project=util.FixedParameter(name="project", default=""),
@@ -67,10 +103,21 @@ def deploy_scheduler(name, builders):
             ]
         )
 
-def hook_scheduler(project, timer=10):
+def git_hook_scheduler(project, builders=[], timer=1):
+    if len(builders) == 0:
+        builders = ["{}_build".format(project)]
     return schedulers.AnyBranchScheduler(
-            change_filter=util.ChangeFilter(category="hooks", project=project),
-            name=project, treeStableTimer=timer, builderNames=["{}_build".format(project)])
+            change_filter=util.ChangeFilter(category="gitolite-hooks", project=project),
+            name="{}_git_hook".format(project), treeStableTimer=timer, builderNames=builders)
+
+def deploy_hook_scheduler(project, builders, timer=1):
+    return schedulers.AnyBranchScheduler(
+            change_filter=util.ChangeFilter(category="deploy_webhook", project=project),
+            name="{}_deploy".format(project), treeStableTimer=timer, builderNames=builders)
+
+# Builders
+def all_builder_names(c):
+    return [builder.name for builder in c['builders']]
 
 # Slack/XMPP status push
 from buildbot.reporters.http import HttpStatusPushBase
@@ -115,7 +162,7 @@ class SlackStatusPush(HttpStatusPushBase):
             msg = "{} environment".format(build["properties"]["environment"][0])
             if "build" in build["properties"]:
                 msg = "of archive {} in ".format(build["properties"]["build"][0]) + msg
-        elif len(build["buildset"]["sourcestamps"][0]["branch"]) > 0:
+        elif len(build["buildset"]["sourcestamps"][0]["branch"] or []) > 0:
             msg = "revision {}".format(build["buildset"]["sourcestamps"][0]["branch"])
         else:
             msg = "build"
@@ -192,6 +239,11 @@ class SlackStatusPush(HttpStatusPushBase):
                 "attachments": attachments,
                 }
 
+def configure_slack_push(c, secrets_file, builders):
+    c['services'].append(SlackStatusPush(
+        name="slack_status", builders=builders,
+        serverUrl=open(secrets_file + "/slack_webhook", "r").read().rstrip()))
+
 class XMPPStatusPush(HttpStatusPushBase):
     name = "XMPPStatusPush"
 
@@ -223,7 +275,7 @@ class XMPPStatusPush(HttpStatusPushBase):
             msg = "{} environment".format(build["properties"]["environment"][0])
             if "build" in build["properties"]:
                 msg = "of archive {} in ".format(build["properties"]["build"][0]) + msg
-        elif len(build["buildset"]["sourcestamps"][0]["branch"]) > 0:
+        elif len(build["buildset"]["sourcestamps"][0]["branch"] or []) > 0:
             msg = "revision {}".format(build["buildset"]["sourcestamps"][0]["branch"])
         else:
             msg = "build"
@@ -254,3 +306,38 @@ class XMPPStatusPush(HttpStatusPushBase):
                     )
 
         return text
+
+def configure_xmpp_push(c, secrets_file, builders, recipients):
+    c['services'].append(XMPPStatusPush(
+        name="xmpp_status", builders=builders, recipients=recipients,
+        password=open(secrets_file + "/notify_xmpp_password", "r").read().rstrip()))
+
+# LDAP edit
+from buildbot.process.buildstep import FAILURE
+from buildbot.process.buildstep import SUCCESS
+from buildbot.process.buildstep import BuildStep
+
+def compute_build_infos(prefix, release_path):
+    @util.renderer
+    def compute(props):
+        import re, hashlib
+        build_file = props.getProperty("build")
+        package_dest = "{}/{}".format(release_path, build_file)
+        version = re.match(r"{0}_(.*).tar.gz".format(prefix), build_file).group(1)
+        with open(package_dest, "rb") as f:
+            sha = hashlib.sha256(f.read()).hexdigest()
+        return {
+                "build_version": version,
+                "build_hash": sha,
+                }
+    return compute
+
+def deploy_ssh_command(ssh_key_path, deploy_hosts):
+    @util.renderer
+    def compute(props):
+        environment = props["environment"] if props.hasProperty("environment") else "integration"
+        ssh_command = [
+                "ssh", "-o", "UserKnownHostsFile=/dev/null", "-o", "StrictHostKeyChecking=no", "-o", "CheckHostIP=no",
+                "-i", ssh_key_path ]
+        return ssh_command + deploy_hosts.get(environment, ["host.invalid"])
+    return compute