2023-09-10 08:11:56 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import json
|
|
|
|
import multiprocessing
|
|
|
|
import os
|
2023-10-12 21:01:07 +00:00
|
|
|
import signal
|
2023-09-17 20:14:56 +00:00
|
|
|
import sys
|
2023-09-10 08:11:56 +00:00
|
|
|
import uuid
|
|
|
|
from collections import defaultdict
|
2023-09-17 20:14:56 +00:00
|
|
|
from collections.abc import Generator
|
|
|
|
from dataclasses import dataclass
|
2023-09-10 08:11:56 +00:00
|
|
|
from pathlib import Path
|
2023-09-18 13:41:34 +00:00
|
|
|
from typing import Any
|
2023-09-10 08:11:56 +00:00
|
|
|
|
2023-09-17 20:14:56 +00:00
|
|
|
from buildbot.configurators import ConfiguratorBase
|
2023-10-15 06:36:45 +00:00
|
|
|
from buildbot.plugins import reporters, schedulers, secrets, steps, util, worker
|
2023-09-10 08:11:56 +00:00
|
|
|
from buildbot.process import buildstep, logobserver, remotecommand
|
|
|
|
from buildbot.process.log import Log
|
2023-09-17 20:14:56 +00:00
|
|
|
from buildbot.process.project import Project
|
|
|
|
from buildbot.process.properties import Interpolate, Properties
|
2023-09-10 08:11:56 +00:00
|
|
|
from buildbot.process.results import ALL_RESULTS, statusToString
|
|
|
|
from buildbot.steps.trigger import Trigger
|
2023-10-27 08:49:40 +00:00
|
|
|
from twisted.internet import defer, threads
|
|
|
|
from twisted.python.failure import Failure
|
|
|
|
|
2023-10-26 07:25:50 +00:00
|
|
|
from .github_projects import ( # noqa: E402
|
2023-10-15 06:36:45 +00:00
|
|
|
GithubProject,
|
|
|
|
create_project_hook,
|
|
|
|
load_projects,
|
|
|
|
refresh_projects,
|
2023-10-27 08:49:40 +00:00
|
|
|
slugify_project_name,
|
2023-10-15 06:36:45 +00:00
|
|
|
)
|
2023-09-10 08:11:56 +00:00
|
|
|
|
2023-11-18 07:18:46 +00:00
|
|
|
SKIPPED_BUILDER_NAME = "skipped-builds"
|
|
|
|
|
2023-09-10 08:11:56 +00:00
|
|
|
|
|
|
|
class BuildTrigger(Trigger):
|
|
|
|
"""
|
|
|
|
Dynamic trigger that creates a build for every attribute.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(
|
2023-11-18 07:18:46 +00:00
|
|
|
self,
|
|
|
|
builds_scheduler: str,
|
|
|
|
skipped_builds_scheduler: str,
|
|
|
|
jobs: list[dict[str, Any]],
|
|
|
|
**kwargs: Any,
|
2023-09-10 08:11:56 +00:00
|
|
|
) -> None:
|
|
|
|
if "name" not in kwargs:
|
|
|
|
kwargs["name"] = "trigger"
|
|
|
|
self.jobs = jobs
|
|
|
|
self.config = None
|
2023-11-18 07:18:46 +00:00
|
|
|
self.builds_scheduler = builds_scheduler
|
|
|
|
self.skipped_builds_scheduler = skipped_builds_scheduler
|
2023-09-10 08:11:56 +00:00
|
|
|
Trigger.__init__(
|
|
|
|
self,
|
|
|
|
waitForFinish=True,
|
2023-11-18 07:18:46 +00:00
|
|
|
schedulerNames=[builds_scheduler, skipped_builds_scheduler],
|
2023-09-10 08:11:56 +00:00
|
|
|
haltOnFailure=True,
|
|
|
|
flunkOnFailure=True,
|
|
|
|
sourceStamps=[],
|
|
|
|
alwaysUseLatest=False,
|
|
|
|
updateSourceStamp=False,
|
|
|
|
**kwargs,
|
|
|
|
)
|
|
|
|
|
2023-10-12 21:01:07 +00:00
|
|
|
def createTriggerProperties(self, props: Any) -> Any: # noqa: N802
|
2023-09-10 08:11:56 +00:00
|
|
|
return props
|
|
|
|
|
2023-10-12 21:01:07 +00:00
|
|
|
def getSchedulersAndProperties(self) -> list[tuple[str, Properties]]: # noqa: N802
|
2023-09-10 08:11:56 +00:00
|
|
|
build_props = self.build.getProperties()
|
|
|
|
repo_name = build_props.getProperty(
|
|
|
|
"github.base.repo.full_name",
|
|
|
|
build_props.getProperty("github.repository.full_name"),
|
|
|
|
)
|
2023-10-26 12:54:01 +00:00
|
|
|
project_id = slugify_project_name(repo_name)
|
2023-09-10 08:11:56 +00:00
|
|
|
source = f"nix-eval-{project_id}"
|
|
|
|
|
|
|
|
triggered_schedulers = []
|
|
|
|
for job in self.jobs:
|
|
|
|
attr = job.get("attr", "eval-error")
|
|
|
|
name = attr
|
|
|
|
if repo_name is not None:
|
|
|
|
name = f"github:{repo_name}#checks.{name}"
|
|
|
|
else:
|
|
|
|
name = f"checks.{name}"
|
|
|
|
error = job.get("error")
|
2023-11-18 07:18:46 +00:00
|
|
|
props = Properties()
|
2023-11-20 08:07:45 +00:00
|
|
|
props.setProperty("virtual_builder_name", name, source)
|
|
|
|
props.setProperty("status_name", f"nix-build .#checks.{attr}", source)
|
2023-11-18 07:18:46 +00:00
|
|
|
props.setProperty("virtual_builder_tags", "", source)
|
|
|
|
|
|
|
|
if error is not None:
|
|
|
|
props.setProperty("error", error, source)
|
|
|
|
triggered_schedulers.append((self.skipped_builds_scheduler, props))
|
|
|
|
continue
|
|
|
|
|
|
|
|
if job.get("isCached"):
|
|
|
|
triggered_schedulers.append((self.skipped_builds_scheduler, props))
|
|
|
|
continue
|
|
|
|
|
|
|
|
drv_path = job.get("drvPath")
|
2023-09-10 08:11:56 +00:00
|
|
|
system = job.get("system")
|
|
|
|
out_path = job.get("outputs", {}).get("out")
|
|
|
|
|
|
|
|
build_props.setProperty(f"{attr}-out_path", out_path, source)
|
|
|
|
build_props.setProperty(f"{attr}-drv_path", drv_path, source)
|
|
|
|
|
|
|
|
props.setProperty("attr", attr, source)
|
|
|
|
props.setProperty("system", system, source)
|
|
|
|
props.setProperty("drv_path", drv_path, source)
|
|
|
|
props.setProperty("out_path", out_path, source)
|
|
|
|
# we use this to identify builds when running a retry
|
|
|
|
props.setProperty("build_uuid", str(uuid.uuid4()), source)
|
2023-11-17 15:27:55 +00:00
|
|
|
|
2023-11-18 07:18:46 +00:00
|
|
|
triggered_schedulers.append((self.builds_scheduler, props))
|
2023-09-10 08:11:56 +00:00
|
|
|
return triggered_schedulers
|
|
|
|
|
2023-10-12 21:01:07 +00:00
|
|
|
def getCurrentSummary(self) -> dict[str, str]: # noqa: N802
|
2023-09-10 08:11:56 +00:00
|
|
|
"""
|
|
|
|
The original build trigger will the generic builder name `nix-build` in this case, which is not helpful
|
|
|
|
"""
|
|
|
|
if not self.triggeredNames:
|
|
|
|
return {"step": "running"}
|
|
|
|
summary = []
|
|
|
|
if self._result_list:
|
|
|
|
for status in ALL_RESULTS:
|
|
|
|
count = self._result_list.count(status)
|
|
|
|
if count:
|
|
|
|
summary.append(
|
|
|
|
f"{self._result_list.count(status)} {statusToString(status, count)}"
|
|
|
|
)
|
|
|
|
return {"step": f"({', '.join(summary)})"}
|
|
|
|
|
|
|
|
|
|
|
|
class NixEvalCommand(buildstep.ShellMixin, steps.BuildStep):
|
|
|
|
"""
|
|
|
|
Parses the output of `nix-eval-jobs` and triggers a `nix-build` build for
|
|
|
|
every attribute.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, supported_systems: list[str], **kwargs: Any) -> None:
|
|
|
|
kwargs = self.setupShellMixin(kwargs)
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
self.observer = logobserver.BufferLogObserver()
|
|
|
|
self.addLogObserver("stdio", self.observer)
|
|
|
|
self.supported_systems = supported_systems
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def run(self) -> Generator[Any, object, Any]:
|
2023-11-08 13:31:14 +00:00
|
|
|
# run nix-eval-jobs --flake .#checks to generate the dict of stages
|
2023-09-10 08:11:56 +00:00
|
|
|
cmd: remotecommand.RemoteCommand = yield self.makeRemoteShellCommand()
|
|
|
|
yield self.runCommand(cmd)
|
|
|
|
|
|
|
|
# if the command passes extract the list of stages
|
|
|
|
result = cmd.results()
|
|
|
|
if result == util.SUCCESS:
|
|
|
|
# create a ShellCommand for each stage and add them to the build
|
|
|
|
jobs = []
|
|
|
|
|
|
|
|
for line in self.observer.getStdout().split("\n"):
|
|
|
|
if line != "":
|
|
|
|
try:
|
|
|
|
job = json.loads(line)
|
|
|
|
except json.JSONDecodeError as e:
|
|
|
|
raise Exception(f"Failed to parse line: {line}") from e
|
|
|
|
jobs.append(job)
|
|
|
|
build_props = self.build.getProperties()
|
|
|
|
repo_name = build_props.getProperty(
|
|
|
|
"github.base.repo.full_name",
|
|
|
|
build_props.getProperty("github.repository.full_name"),
|
|
|
|
)
|
2023-10-26 12:54:01 +00:00
|
|
|
project_id = slugify_project_name(repo_name)
|
2023-09-10 08:11:56 +00:00
|
|
|
filtered_jobs = []
|
|
|
|
for job in jobs:
|
|
|
|
system = job.get("system")
|
|
|
|
if not system: # report eval errors
|
|
|
|
filtered_jobs.append(job)
|
|
|
|
elif system in self.supported_systems:
|
|
|
|
filtered_jobs.append(job)
|
|
|
|
|
|
|
|
self.build.addStepsAfterCurrentStep(
|
2023-10-27 09:39:11 +00:00
|
|
|
[
|
|
|
|
BuildTrigger(
|
2023-11-18 07:18:46 +00:00
|
|
|
builds_scheduler=f"{project_id}-nix-build",
|
|
|
|
skipped_builds_scheduler=f"{project_id}-nix-skipped-build",
|
|
|
|
name="build flake",
|
|
|
|
jobs=filtered_jobs,
|
2023-10-27 09:39:11 +00:00
|
|
|
)
|
|
|
|
]
|
2023-09-10 08:11:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
# FIXME this leaks memory... but probably not enough that we care
|
|
|
|
class RetryCounter:
|
|
|
|
def __init__(self, retries: int) -> None:
|
|
|
|
self.builds: dict[uuid.UUID, int] = defaultdict(lambda: retries)
|
|
|
|
|
|
|
|
def retry_build(self, id: uuid.UUID) -> int:
|
|
|
|
retries = self.builds[id]
|
|
|
|
if retries > 1:
|
|
|
|
self.builds[id] = retries - 1
|
|
|
|
return retries
|
|
|
|
else:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
# For now we limit this to two. Often this allows us to make the error log
|
|
|
|
# shorter because we won't see the logs for all previous succeeded builds
|
|
|
|
RETRY_COUNTER = RetryCounter(retries=2)
|
|
|
|
|
|
|
|
|
2023-11-18 07:18:46 +00:00
|
|
|
class EvalErrorStep(steps.BuildStep):
|
|
|
|
"""
|
|
|
|
Shows the error message of a failed evaluation.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def run(self) -> Generator[Any, object, Any]:
|
|
|
|
error = self.getProperty("error")
|
|
|
|
attr = self.getProperty("attr")
|
|
|
|
# show eval error
|
|
|
|
error_log: Log = yield self.addLog("nix_error")
|
|
|
|
error_log.addStderr(f"{attr} failed to evaluate:\n{error}")
|
|
|
|
return util.FAILURE
|
|
|
|
|
|
|
|
|
2023-09-10 08:11:56 +00:00
|
|
|
class NixBuildCommand(buildstep.ShellMixin, steps.BuildStep):
|
|
|
|
"""
|
2023-11-18 07:18:46 +00:00
|
|
|
Builds a nix derivation.
|
2023-09-10 08:11:56 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, **kwargs: Any) -> None:
|
|
|
|
kwargs = self.setupShellMixin(kwargs)
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def run(self) -> Generator[Any, object, Any]:
|
|
|
|
# run `nix build`
|
|
|
|
cmd: remotecommand.RemoteCommand = yield self.makeRemoteShellCommand()
|
|
|
|
yield self.runCommand(cmd)
|
|
|
|
|
|
|
|
res = cmd.results()
|
|
|
|
if res == util.FAILURE:
|
|
|
|
retries = RETRY_COUNTER.retry_build(self.getProperty("build_uuid"))
|
|
|
|
if retries > 0:
|
|
|
|
return util.RETRY
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
|
|
class UpdateBuildOutput(steps.BuildStep):
|
|
|
|
"""
|
|
|
|
Updates store paths in a public www directory.
|
|
|
|
This is useful to prefetch updates without having to evaluate
|
|
|
|
on the target machine.
|
|
|
|
"""
|
|
|
|
|
2023-11-04 08:50:23 +00:00
|
|
|
def __init__(self, path: Path, **kwargs: Any) -> None:
|
2023-09-10 08:11:56 +00:00
|
|
|
super().__init__(**kwargs)
|
2023-11-04 08:50:23 +00:00
|
|
|
self.path = path
|
2023-09-10 08:11:56 +00:00
|
|
|
|
|
|
|
def run(self) -> Generator[Any, object, Any]:
|
|
|
|
props = self.build.getProperties()
|
|
|
|
if props.getProperty("branch") != props.getProperty(
|
|
|
|
"github.repository.default_branch"
|
|
|
|
):
|
|
|
|
return util.SKIPPED
|
2023-11-17 15:27:55 +00:00
|
|
|
|
2023-09-10 08:11:56 +00:00
|
|
|
attr = os.path.basename(props.getProperty("attr"))
|
|
|
|
out_path = props.getProperty("out_path")
|
|
|
|
# XXX don't hardcode this
|
2023-11-04 08:50:23 +00:00
|
|
|
self.path.mkdir(parents=True, exist_ok=True)
|
|
|
|
(self.path / attr).write_text(out_path)
|
2023-09-10 08:11:56 +00:00
|
|
|
return util.SUCCESS
|
|
|
|
|
|
|
|
|
2023-10-12 21:01:07 +00:00
|
|
|
class ReloadGithubProjects(steps.BuildStep):
|
|
|
|
name = "reload_github_projects"
|
|
|
|
|
|
|
|
def __init__(self, token: str, project_cache_file: Path, **kwargs: Any) -> None:
|
|
|
|
self.token = token
|
|
|
|
self.project_cache_file = project_cache_file
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
|
|
|
|
def reload_projects(self) -> None:
|
|
|
|
refresh_projects(self.token, self.project_cache_file)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def run(self) -> Generator[Any, object, Any]:
|
|
|
|
d = threads.deferToThread(self.reload_projects)
|
|
|
|
|
|
|
|
self.error_msg = ""
|
|
|
|
|
|
|
|
def error_cb(failure: Failure) -> int:
|
|
|
|
self.error_msg += failure.getTraceback()
|
|
|
|
return util.FAILURE
|
|
|
|
|
|
|
|
d.addCallbacks(lambda _: util.SUCCESS, error_cb)
|
|
|
|
res = yield d
|
|
|
|
if res == util.SUCCESS:
|
|
|
|
# reload the buildbot config
|
|
|
|
os.kill(os.getpid(), signal.SIGHUP)
|
|
|
|
return util.SUCCESS
|
|
|
|
else:
|
|
|
|
log: Log = yield self.addLog("log")
|
|
|
|
log.addStderr(f"Failed to reload project list: {self.error_msg}")
|
|
|
|
return util.FAILURE
|
|
|
|
|
|
|
|
|
|
|
|
def reload_github_projects(
|
|
|
|
worker_names: list[str],
|
|
|
|
github_token_secret: str,
|
|
|
|
project_cache_file: Path,
|
|
|
|
) -> util.BuilderConfig:
|
|
|
|
"""
|
|
|
|
Updates the flake an opens a PR for it.
|
|
|
|
"""
|
|
|
|
factory = util.BuildFactory()
|
|
|
|
factory.addStep(
|
|
|
|
ReloadGithubProjects(github_token_secret, project_cache_file=project_cache_file)
|
|
|
|
)
|
|
|
|
return util.BuilderConfig(
|
|
|
|
name="reload-github-projects",
|
|
|
|
workernames=worker_names,
|
|
|
|
factory=factory,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-09-10 08:11:56 +00:00
|
|
|
|
|
|
|
def nix_eval_config(
|
|
|
|
project: GithubProject,
|
|
|
|
worker_names: list[str],
|
|
|
|
github_token_secret: str,
|
|
|
|
supported_systems: list[str],
|
2023-11-10 13:30:09 +00:00
|
|
|
eval_lock: util.WorkerLock,
|
2023-11-12 06:07:30 +00:00
|
|
|
worker_count: int,
|
|
|
|
max_memory_size: int,
|
2023-09-10 08:11:56 +00:00
|
|
|
) -> util.BuilderConfig:
|
|
|
|
"""
|
|
|
|
Uses nix-eval-jobs to evaluate hydraJobs from flake.nix in parallel.
|
|
|
|
For each evaluated attribute a new build pipeline is started.
|
|
|
|
"""
|
|
|
|
factory = util.BuildFactory()
|
|
|
|
# check out the source
|
|
|
|
url_with_secret = util.Interpolate(
|
|
|
|
f"https://git:%(secret:{github_token_secret})s@github.com/%(prop:project)s"
|
|
|
|
)
|
|
|
|
factory.addStep(
|
|
|
|
steps.Git(
|
|
|
|
repourl=url_with_secret,
|
|
|
|
method="clean",
|
|
|
|
submodules=True,
|
|
|
|
haltOnFailure=True,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
factory.addStep(
|
|
|
|
NixEvalCommand(
|
|
|
|
env={},
|
|
|
|
name="evaluate flake",
|
|
|
|
supported_systems=supported_systems,
|
|
|
|
command=[
|
2023-10-15 09:26:09 +00:00
|
|
|
"nix-eval-jobs",
|
2023-09-10 08:11:56 +00:00
|
|
|
"--workers",
|
2023-11-12 06:07:30 +00:00
|
|
|
str(worker_count),
|
2023-09-10 08:11:56 +00:00
|
|
|
"--max-memory-size",
|
|
|
|
str(max_memory_size),
|
|
|
|
"--option",
|
|
|
|
"accept-flake-config",
|
|
|
|
"true",
|
|
|
|
"--gc-roots-dir",
|
|
|
|
# FIXME: don't hardcode this
|
|
|
|
"/var/lib/buildbot-worker/gcroot",
|
|
|
|
"--force-recurse",
|
2023-11-17 15:27:55 +00:00
|
|
|
"--check-cache-status",
|
2023-09-10 08:11:56 +00:00
|
|
|
"--flake",
|
|
|
|
".#checks",
|
|
|
|
],
|
|
|
|
haltOnFailure=True,
|
2023-11-11 07:10:58 +00:00
|
|
|
locks=[eval_lock.access("exclusive")],
|
2023-09-10 08:11:56 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
return util.BuilderConfig(
|
|
|
|
name=f"{project.name}/nix-eval",
|
|
|
|
workernames=worker_names,
|
|
|
|
project=project.name,
|
|
|
|
factory=factory,
|
|
|
|
properties=dict(status_name="nix-eval"),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def nix_build_config(
|
|
|
|
project: GithubProject,
|
|
|
|
worker_names: list[str],
|
|
|
|
has_cachix_auth_token: bool = False,
|
|
|
|
has_cachix_signing_key: bool = False,
|
2023-11-04 08:50:23 +00:00
|
|
|
outputs_path: Path | None = None,
|
2023-09-10 08:11:56 +00:00
|
|
|
) -> util.BuilderConfig:
|
|
|
|
"""
|
|
|
|
Builds one nix flake attribute.
|
|
|
|
"""
|
|
|
|
factory = util.BuildFactory()
|
|
|
|
factory.addStep(
|
|
|
|
NixBuildCommand(
|
|
|
|
env={},
|
|
|
|
name="Build flake attr",
|
|
|
|
command=[
|
|
|
|
"nix",
|
|
|
|
"build",
|
|
|
|
"-L",
|
|
|
|
"--option",
|
|
|
|
"keep-going",
|
|
|
|
"true",
|
|
|
|
"--accept-flake-config",
|
|
|
|
"--out-link",
|
|
|
|
util.Interpolate("result-%(prop:attr)s"),
|
|
|
|
util.Interpolate("%(prop:drv_path)s^*"),
|
|
|
|
],
|
|
|
|
haltOnFailure=True,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
if has_cachix_auth_token or has_cachix_signing_key:
|
|
|
|
if has_cachix_signing_key:
|
|
|
|
env = dict(CACHIX_SIGNING_KEY=util.Secret("cachix-signing-key"))
|
|
|
|
else:
|
|
|
|
env = dict(CACHIX_AUTH_TOKEN=util.Secret("cachix-auth-token"))
|
|
|
|
factory.addStep(
|
|
|
|
steps.ShellCommand(
|
|
|
|
name="Upload cachix",
|
|
|
|
env=env,
|
|
|
|
command=[
|
|
|
|
"cachix",
|
|
|
|
"push",
|
|
|
|
util.Secret("cachix-name"),
|
|
|
|
util.Interpolate("result-%(prop:attr)s"),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
)
|
2023-10-09 15:13:46 +00:00
|
|
|
|
2023-09-10 08:11:56 +00:00
|
|
|
factory.addStep(
|
|
|
|
steps.ShellCommand(
|
|
|
|
name="Register gcroot",
|
|
|
|
command=[
|
|
|
|
"nix-store",
|
|
|
|
"--add-root",
|
|
|
|
# FIXME: cleanup old build attributes
|
|
|
|
util.Interpolate(
|
2023-10-12 10:43:57 +00:00
|
|
|
"/nix/var/nix/gcroots/per-user/buildbot-worker/%(prop:project)s/%(prop:attr)s"
|
2023-09-10 08:11:56 +00:00
|
|
|
),
|
|
|
|
"-r",
|
|
|
|
util.Property("out_path"),
|
|
|
|
],
|
2023-11-18 07:18:46 +00:00
|
|
|
doStepIf=lambda s: s.getProperty("branch")
|
2023-10-12 10:43:57 +00:00
|
|
|
== s.getProperty("github.repository.default_branch"),
|
2023-09-10 08:11:56 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
factory.addStep(
|
|
|
|
steps.ShellCommand(
|
|
|
|
name="Delete temporary gcroots",
|
|
|
|
command=["rm", "-f", util.Interpolate("result-%(prop:attr)s")],
|
|
|
|
)
|
|
|
|
)
|
2023-11-04 08:50:23 +00:00
|
|
|
if outputs_path is not None:
|
|
|
|
factory.addStep(
|
|
|
|
UpdateBuildOutput(
|
|
|
|
name="Update build output",
|
|
|
|
path=outputs_path,
|
|
|
|
)
|
|
|
|
)
|
2023-09-10 08:11:56 +00:00
|
|
|
return util.BuilderConfig(
|
|
|
|
name=f"{project.name}/nix-build",
|
|
|
|
project=project.name,
|
|
|
|
workernames=worker_names,
|
|
|
|
collapseRequests=False,
|
|
|
|
env={},
|
|
|
|
factory=factory,
|
|
|
|
)
|
2023-09-17 20:14:56 +00:00
|
|
|
|
|
|
|
|
2023-11-18 07:18:46 +00:00
|
|
|
def nix_skipped_build_config(
|
|
|
|
project: GithubProject, worker_names: list[str]
|
|
|
|
) -> util.BuilderConfig:
|
|
|
|
"""
|
|
|
|
Dummy builder that is triggered when a build is skipped.
|
|
|
|
"""
|
|
|
|
factory = util.BuildFactory()
|
|
|
|
factory.addStep(
|
|
|
|
EvalErrorStep(
|
|
|
|
name="Nix evaluation",
|
|
|
|
doStepIf=lambda s: s.getProperty("error"),
|
|
|
|
hideStepIf=lambda _, s: not s.getProperty("error"),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# This is just a dummy step showing the cached build
|
|
|
|
factory.addStep(
|
|
|
|
steps.BuildStep(
|
|
|
|
name="Nix build (cached)",
|
|
|
|
doStepIf=lambda _: False,
|
|
|
|
hideStepIf=lambda _, s: s.getProperty("error"),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return util.BuilderConfig(
|
|
|
|
name=f"{project.name}/nix-skipped-build",
|
|
|
|
project=project.name,
|
|
|
|
workernames=worker_names,
|
|
|
|
collapseRequests=False,
|
|
|
|
env={},
|
|
|
|
factory=factory,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-09-17 20:14:56 +00:00
|
|
|
def read_secret_file(secret_name: str) -> str:
|
|
|
|
directory = os.environ.get("CREDENTIALS_DIRECTORY")
|
|
|
|
if directory is None:
|
|
|
|
print("directory not set", file=sys.stderr)
|
|
|
|
sys.exit(1)
|
2023-10-27 09:09:20 +00:00
|
|
|
return Path(directory).joinpath(secret_name).read_text().rstrip()
|
2023-09-17 20:14:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class GithubConfig:
|
|
|
|
oauth_id: str
|
|
|
|
admins: list[str]
|
|
|
|
buildbot_user: str
|
|
|
|
oauth_secret_name: str = "github-oauth-secret"
|
|
|
|
webhook_secret_name: str = "github-webhook-secret"
|
|
|
|
token_secret_name: str = "github-token"
|
|
|
|
project_cache_file: Path = Path("github-project-cache.json")
|
2023-10-12 13:59:26 +00:00
|
|
|
topic: str | None = "build-with-buildbot"
|
2023-09-17 20:14:56 +00:00
|
|
|
|
|
|
|
def token(self) -> str:
|
|
|
|
return read_secret_file(self.token_secret_name)
|
|
|
|
|
|
|
|
|
|
|
|
def config_for_project(
|
|
|
|
config: dict[str, Any],
|
|
|
|
project: GithubProject,
|
|
|
|
credentials: str,
|
|
|
|
worker_names: list[str],
|
|
|
|
github: GithubConfig,
|
|
|
|
nix_supported_systems: list[str],
|
2023-11-12 06:07:30 +00:00
|
|
|
nix_eval_worker_count: int,
|
2023-09-17 20:14:56 +00:00
|
|
|
nix_eval_max_memory_size: int,
|
2023-11-10 13:30:09 +00:00
|
|
|
eval_lock: util.WorkerLock,
|
2023-11-04 08:50:23 +00:00
|
|
|
outputs_path: Path | None = None,
|
2023-09-17 20:14:56 +00:00
|
|
|
) -> Project:
|
|
|
|
config["projects"].append(Project(project.name))
|
|
|
|
config["schedulers"].extend(
|
|
|
|
[
|
|
|
|
schedulers.SingleBranchScheduler(
|
2023-11-10 22:12:45 +00:00
|
|
|
name=f"{project.id}-default-branch",
|
2023-09-17 20:14:56 +00:00
|
|
|
change_filter=util.ChangeFilter(
|
|
|
|
repository=project.url,
|
|
|
|
filter_fn=lambda c: c.branch
|
|
|
|
== c.properties.getProperty("github.repository.default_branch"),
|
|
|
|
),
|
|
|
|
builderNames=[f"{project.name}/nix-eval"],
|
2023-11-04 11:00:59 +00:00
|
|
|
treeStableTimer=5,
|
2023-09-17 20:14:56 +00:00
|
|
|
),
|
|
|
|
# this is compatible with bors or github's merge queue
|
|
|
|
schedulers.SingleBranchScheduler(
|
2023-11-10 22:12:45 +00:00
|
|
|
name=f"{project.id}-merge-queue",
|
2023-09-17 20:14:56 +00:00
|
|
|
change_filter=util.ChangeFilter(
|
|
|
|
repository=project.url,
|
|
|
|
branch_re="(gh-readonly-queue/.*|staging|trying)",
|
|
|
|
),
|
|
|
|
builderNames=[f"{project.name}/nix-eval"],
|
|
|
|
),
|
|
|
|
# build all pull requests
|
|
|
|
schedulers.SingleBranchScheduler(
|
2023-11-10 22:12:45 +00:00
|
|
|
name=f"{project.id}-prs",
|
2023-09-17 20:14:56 +00:00
|
|
|
change_filter=util.ChangeFilter(
|
|
|
|
repository=project.url, category="pull"
|
|
|
|
),
|
|
|
|
builderNames=[f"{project.name}/nix-eval"],
|
|
|
|
),
|
|
|
|
# this is triggered from `nix-eval`
|
|
|
|
schedulers.Triggerable(
|
|
|
|
name=f"{project.id}-nix-build",
|
|
|
|
builderNames=[f"{project.name}/nix-build"],
|
|
|
|
),
|
2023-11-18 07:18:46 +00:00
|
|
|
# this is triggered from `nix-eval` when the build is skipped
|
|
|
|
schedulers.Triggerable(
|
|
|
|
name=f"{project.id}-nix-skipped-build",
|
|
|
|
builderNames=[f"{project.name}/nix-skipped-build"],
|
|
|
|
),
|
2023-09-17 20:14:56 +00:00
|
|
|
# allow to manually trigger a nix-build
|
|
|
|
schedulers.ForceScheduler(
|
2023-10-27 08:49:40 +00:00
|
|
|
name=f"{project.id}-force",
|
|
|
|
builderNames=[f"{project.name}/nix-eval"],
|
2023-10-27 08:35:26 +00:00
|
|
|
properties=[
|
|
|
|
util.StringParameter(
|
|
|
|
name="project",
|
|
|
|
label="Name of the GitHub repository.",
|
2023-10-27 08:49:40 +00:00
|
|
|
default=project.name,
|
|
|
|
)
|
|
|
|
],
|
2023-09-17 20:14:56 +00:00
|
|
|
),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
has_cachix_auth_token = os.path.isfile(
|
|
|
|
os.path.join(credentials, "cachix-auth-token")
|
|
|
|
)
|
|
|
|
has_cachix_signing_key = os.path.isfile(
|
|
|
|
os.path.join(credentials, "cachix-signing-key")
|
|
|
|
)
|
|
|
|
config["builders"].extend(
|
|
|
|
[
|
|
|
|
# Since all workers run on the same machine, we only assign one of them to do the evaluation.
|
|
|
|
# This should prevent exessive memory usage.
|
|
|
|
nix_eval_config(
|
|
|
|
project,
|
|
|
|
[worker_names[0]],
|
|
|
|
github_token_secret=github.token_secret_name,
|
|
|
|
supported_systems=nix_supported_systems,
|
2023-11-12 06:07:30 +00:00
|
|
|
worker_count=nix_eval_worker_count,
|
2023-09-17 20:14:56 +00:00
|
|
|
max_memory_size=nix_eval_max_memory_size,
|
2023-11-10 13:30:09 +00:00
|
|
|
eval_lock=eval_lock,
|
2023-09-17 20:14:56 +00:00
|
|
|
),
|
|
|
|
nix_build_config(
|
|
|
|
project,
|
|
|
|
worker_names,
|
|
|
|
has_cachix_auth_token,
|
|
|
|
has_cachix_signing_key,
|
2023-11-04 08:50:23 +00:00
|
|
|
outputs_path=outputs_path,
|
2023-09-17 20:14:56 +00:00
|
|
|
),
|
2023-11-18 07:18:46 +00:00
|
|
|
nix_skipped_build_config(project, [SKIPPED_BUILDER_NAME]),
|
2023-09-17 20:14:56 +00:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class NixConfigurator(ConfiguratorBase):
|
|
|
|
"""Janitor is a configurator which create a Janitor Builder with all needed Janitor steps"""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
# Shape of this file:
|
|
|
|
# [ { "name": "<worker-name>", "pass": "<worker-password>", "cores": "<cpu-cores>" } ]
|
|
|
|
github: GithubConfig,
|
2023-10-12 13:59:26 +00:00
|
|
|
url: str,
|
2023-09-17 20:14:56 +00:00
|
|
|
nix_supported_systems: list[str],
|
2023-11-12 06:07:30 +00:00
|
|
|
nix_eval_worker_count: int | None,
|
|
|
|
nix_eval_max_memory_size: int,
|
2023-09-17 20:14:56 +00:00
|
|
|
nix_workers_secret_name: str = "buildbot-nix-workers",
|
2023-11-04 08:50:23 +00:00
|
|
|
outputs_path: str | None = None,
|
2023-09-17 20:14:56 +00:00
|
|
|
) -> None:
|
|
|
|
super().__init__()
|
|
|
|
self.nix_workers_secret_name = nix_workers_secret_name
|
|
|
|
self.nix_eval_max_memory_size = nix_eval_max_memory_size
|
2023-11-12 06:07:30 +00:00
|
|
|
self.nix_eval_worker_count = nix_eval_worker_count
|
2023-09-17 20:14:56 +00:00
|
|
|
self.nix_supported_systems = nix_supported_systems
|
|
|
|
self.github = github
|
2023-10-12 13:59:26 +00:00
|
|
|
self.url = url
|
2023-09-17 20:14:56 +00:00
|
|
|
self.systemd_credentials_dir = os.environ["CREDENTIALS_DIRECTORY"]
|
2023-11-04 08:50:23 +00:00
|
|
|
if outputs_path is None:
|
|
|
|
self.outputs_path = None
|
|
|
|
else:
|
|
|
|
self.outputs_path = Path(outputs_path)
|
2023-09-17 20:14:56 +00:00
|
|
|
|
|
|
|
def configure(self, config: dict[str, Any]) -> None:
|
|
|
|
projects = load_projects(self.github.token(), self.github.project_cache_file)
|
2023-10-12 13:59:26 +00:00
|
|
|
if self.github.topic is not None:
|
|
|
|
projects = [p for p in projects if self.github.topic in p.topics]
|
2023-09-17 20:14:56 +00:00
|
|
|
worker_config = json.loads(read_secret_file(self.nix_workers_secret_name))
|
|
|
|
worker_names = []
|
2023-11-06 06:46:07 +00:00
|
|
|
|
|
|
|
config.setdefault("projects", [])
|
|
|
|
config.setdefault("secretsProviders", [])
|
|
|
|
config.setdefault("www", {})
|
|
|
|
|
2023-09-17 20:14:56 +00:00
|
|
|
for item in worker_config:
|
|
|
|
cores = item.get("cores", 0)
|
|
|
|
for i in range(cores):
|
2023-11-09 07:18:03 +00:00
|
|
|
worker_name = f"{item['name']}-{i:03}"
|
2023-09-17 20:14:56 +00:00
|
|
|
config["workers"].append(worker.Worker(worker_name, item["pass"]))
|
|
|
|
worker_names.append(worker_name)
|
|
|
|
|
2023-10-12 13:59:26 +00:00
|
|
|
webhook_secret = read_secret_file(self.github.webhook_secret_name)
|
2023-11-11 07:10:58 +00:00
|
|
|
eval_lock = util.WorkerLock("nix-eval")
|
2023-10-12 13:59:26 +00:00
|
|
|
|
2023-10-12 13:59:26 +00:00
|
|
|
for project in projects:
|
|
|
|
create_project_hook(
|
|
|
|
project.owner,
|
|
|
|
project.repo,
|
|
|
|
self.github.token(),
|
2023-11-04 03:47:55 +00:00
|
|
|
self.url + "change_hook/github",
|
2023-10-12 13:59:26 +00:00
|
|
|
webhook_secret,
|
2023-10-12 13:59:26 +00:00
|
|
|
)
|
|
|
|
|
2023-09-17 20:14:56 +00:00
|
|
|
for project in projects:
|
|
|
|
config_for_project(
|
|
|
|
config,
|
|
|
|
project,
|
|
|
|
self.systemd_credentials_dir,
|
|
|
|
worker_names,
|
|
|
|
self.github,
|
|
|
|
self.nix_supported_systems,
|
2023-11-12 06:07:30 +00:00
|
|
|
self.nix_eval_worker_count or multiprocessing.cpu_count(),
|
2023-09-17 20:14:56 +00:00
|
|
|
self.nix_eval_max_memory_size,
|
2023-11-10 13:30:09 +00:00
|
|
|
eval_lock,
|
2023-11-04 08:50:23 +00:00
|
|
|
self.outputs_path,
|
2023-09-17 20:14:56 +00:00
|
|
|
)
|
2023-10-12 13:59:26 +00:00
|
|
|
|
2023-10-12 21:01:07 +00:00
|
|
|
# Reload github projects
|
|
|
|
config["builders"].append(
|
|
|
|
reload_github_projects(
|
|
|
|
[worker_names[0]],
|
|
|
|
self.github.token(),
|
|
|
|
self.github.project_cache_file,
|
|
|
|
)
|
|
|
|
)
|
2023-11-18 07:18:46 +00:00
|
|
|
config["workers"].append(worker.LocalWorker(SKIPPED_BUILDER_NAME))
|
2023-10-15 06:36:45 +00:00
|
|
|
config["schedulers"].extend(
|
|
|
|
[
|
|
|
|
schedulers.ForceScheduler(
|
|
|
|
name="reload-github-projects",
|
|
|
|
builderNames=["reload-github-projects"],
|
|
|
|
buttonName="Update projects",
|
|
|
|
),
|
|
|
|
# project list twice a day
|
|
|
|
schedulers.Periodic(
|
|
|
|
name="reload-github-projects-bidaily",
|
|
|
|
builderNames=["reload-github-projects"],
|
|
|
|
periodicBuildTimer=12 * 60 * 60,
|
|
|
|
),
|
|
|
|
]
|
2023-10-12 21:01:07 +00:00
|
|
|
)
|
2023-09-17 20:14:56 +00:00
|
|
|
config["services"].append(
|
|
|
|
reporters.GitHubStatusPush(
|
|
|
|
token=self.github.token(),
|
|
|
|
# Since we dynamically create build steps,
|
|
|
|
# we use `virtual_builder_name` in the webinterface
|
|
|
|
# so that we distinguish what has beeing build
|
|
|
|
context=Interpolate("buildbot/%(prop:status_name)s"),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
systemd_secrets = secrets.SecretInAFile(
|
|
|
|
dirname=os.environ["CREDENTIALS_DIRECTORY"]
|
|
|
|
)
|
|
|
|
config["secretsProviders"].append(systemd_secrets)
|
|
|
|
config["www"]["change_hook_dialects"] = config["www"].get(
|
|
|
|
"change_hook_dialects", {}
|
|
|
|
)
|
|
|
|
config["www"]["change_hook_dialects"]["github"] = {
|
2023-10-12 13:59:26 +00:00
|
|
|
"secret": webhook_secret,
|
2023-09-17 20:14:56 +00:00
|
|
|
"strict": True,
|
|
|
|
"token": self.github.token(),
|
|
|
|
"github_property_whitelist": "*",
|
|
|
|
}
|
2023-10-26 11:01:08 +00:00
|
|
|
|
|
|
|
if not config["www"].get("auth"):
|
|
|
|
config["www"]["avatar_methods"] = config["www"].get("avatar_methods", [])
|
2023-11-06 06:46:24 +00:00
|
|
|
config["www"]["avatar_methods"].append(
|
2023-11-12 05:47:13 +00:00
|
|
|
util.AvatarGitHub(token=self.github.token())
|
2023-11-06 06:46:24 +00:00
|
|
|
)
|
2023-10-26 11:01:08 +00:00
|
|
|
config["www"]["auth"] = util.GitHubAuth(
|
|
|
|
self.github.oauth_id, read_secret_file(self.github.oauth_secret_name)
|
|
|
|
)
|
|
|
|
config["www"]["authz"] = util.Authz(
|
|
|
|
roleMatchers=[
|
2023-10-27 08:49:40 +00:00
|
|
|
util.RolesFromUsername(
|
|
|
|
roles=["admin"], usernames=self.github.admins
|
|
|
|
)
|
2023-10-26 11:01:08 +00:00
|
|
|
],
|
|
|
|
allowRules=[
|
|
|
|
util.AnyEndpointMatcher(role="admin", defaultDeny=False),
|
|
|
|
util.AnyControlEndpointMatcher(role="admins"),
|
|
|
|
],
|
|
|
|
)
|