2023-09-10 08:11:56 +00:00
|
|
|
import json
|
|
|
|
import multiprocessing
|
|
|
|
import os
|
2024-03-07 21:49:35 +00:00
|
|
|
import re
|
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
|
2023-09-10 08:11:56 +00:00
|
|
|
from pathlib import Path
|
2024-04-27 15:06:17 +00:00
|
|
|
from typing import TYPE_CHECKING, Any
|
2023-12-26 18:49:57 +00:00
|
|
|
|
2024-04-27 15:06:17 +00:00
|
|
|
from buildbot.config.builder import BuilderConfig
|
2023-09-17 20:14:56 +00:00
|
|
|
from buildbot.configurators import ConfiguratorBase
|
2024-04-06 13:08:21 +00:00
|
|
|
from buildbot.interfaces import WorkerSetupError
|
2024-04-27 15:06:17 +00:00
|
|
|
from buildbot.locks import MasterLock
|
|
|
|
from buildbot.plugins import schedulers, steps, util, worker
|
2023-09-10 08:11:56 +00:00
|
|
|
from buildbot.process import buildstep, logobserver, remotecommand
|
2023-09-17 20:14:56 +00:00
|
|
|
from buildbot.process.project import Project
|
2024-04-27 15:06:17 +00:00
|
|
|
from buildbot.process.properties import Properties
|
2023-09-10 08:11:56 +00:00
|
|
|
from buildbot.process.results import ALL_RESULTS, statusToString
|
2024-04-27 15:06:17 +00:00
|
|
|
from buildbot.secrets.providers.file import SecretInAFile
|
2023-09-10 08:11:56 +00:00
|
|
|
from buildbot.steps.trigger import Trigger
|
2024-04-22 13:58:38 +00:00
|
|
|
from buildbot.www.authz import Authz
|
2024-04-27 15:06:17 +00:00
|
|
|
from buildbot.www.authz.endpointmatchers import EndpointMatcherBase, Match
|
2023-10-27 08:49:40 +00:00
|
|
|
|
2023-12-26 20:56:36 +00:00
|
|
|
if TYPE_CHECKING:
|
2024-04-27 15:06:17 +00:00
|
|
|
from buildbot.process.log import StreamLog
|
|
|
|
from buildbot.www.auth import AuthBase
|
2023-12-26 20:56:36 +00:00
|
|
|
|
2024-04-27 15:06:17 +00:00
|
|
|
from twisted.internet import defer
|
2023-12-26 20:56:36 +00:00
|
|
|
from twisted.logger import Logger
|
|
|
|
|
2024-04-27 15:06:17 +00:00
|
|
|
from .common import (
|
|
|
|
slugify_project_name,
|
2024-04-22 13:58:38 +00:00
|
|
|
)
|
2024-07-19 20:20:42 +00:00
|
|
|
from .gitea_projects import GiteaBackend
|
2023-12-03 10:48:01 +00:00
|
|
|
from .github_projects import (
|
2024-04-22 13:58:38 +00:00
|
|
|
GithubBackend,
|
|
|
|
)
|
2024-07-19 20:20:42 +00:00
|
|
|
from .models import BuildbotNixConfig
|
2024-04-27 15:06:17 +00:00
|
|
|
from .projects import GitBackend, GitProject
|
2024-04-22 13:58:38 +00:00
|
|
|
|
2023-11-18 07:18:46 +00:00
|
|
|
SKIPPED_BUILDER_NAME = "skipped-builds"
|
|
|
|
|
2023-12-04 18:44:13 +00:00
|
|
|
log = Logger()
|
|
|
|
|
2023-09-10 08:11:56 +00:00
|
|
|
|
2023-12-26 20:56:36 +00:00
|
|
|
class BuildbotNixError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2023-09-10 08:11:56 +00:00
|
|
|
class BuildTrigger(Trigger):
|
2023-12-26 20:56:36 +00:00
|
|
|
"""Dynamic trigger that creates a build for every attribute."""
|
2023-09-10 08:11:56 +00:00
|
|
|
|
2024-04-22 13:58:38 +00:00
|
|
|
project: GitProject
|
|
|
|
|
2023-09-10 08:11:56 +00:00
|
|
|
def __init__(
|
2023-11-18 07:18:46 +00:00
|
|
|
self,
|
2024-04-22 13:58:38 +00:00
|
|
|
project: GitProject,
|
2023-11-18 07:18:46 +00:00
|
|
|
builds_scheduler: str,
|
|
|
|
skipped_builds_scheduler: str,
|
|
|
|
jobs: list[dict[str, Any]],
|
2024-07-11 18:56:15 +00:00
|
|
|
report_status: bool,
|
2023-11-18 07:18:46 +00:00
|
|
|
**kwargs: Any,
|
2023-09-10 08:11:56 +00:00
|
|
|
) -> None:
|
|
|
|
if "name" not in kwargs:
|
|
|
|
kwargs["name"] = "trigger"
|
2024-04-22 13:58:38 +00:00
|
|
|
self.project = project
|
2023-09-10 08:11:56 +00:00
|
|
|
self.jobs = jobs
|
2024-07-11 18:56:15 +00:00
|
|
|
self.report_status = report_status
|
2023-09-10 08:11:56 +00:00
|
|
|
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
|
2024-04-27 15:06:17 +00:00
|
|
|
build_props = self.build.getProperties()
|
2024-04-22 13:58:38 +00:00
|
|
|
repo_name = self.project.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)
|
2024-07-11 18:56:15 +00:00
|
|
|
props.setProperty("report_status", self.report_status, source)
|
2023-11-18 07:18:46 +00:00
|
|
|
|
2024-07-16 13:06:13 +00:00
|
|
|
drv_path = job.get("drvPath")
|
|
|
|
system = job.get("system")
|
|
|
|
out_path = job.get("outputs", {}).get("out")
|
|
|
|
|
|
|
|
props.setProperty("attr", attr, source)
|
|
|
|
props.setProperty("system", system, source)
|
|
|
|
props.setProperty("drv_path", drv_path, source)
|
|
|
|
props.setProperty("out_path", out_path, source)
|
|
|
|
props.setProperty("default_branch", self.project.default_branch, source)
|
|
|
|
# we use this to identify builds when running a retry
|
|
|
|
props.setProperty("build_uuid", str(uuid.uuid4()), source)
|
|
|
|
|
2023-11-18 07:18:46 +00:00
|
|
|
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
|
|
|
|
|
2023-09-10 08:11:56 +00:00
|
|
|
build_props.setProperty(f"{attr}-out_path", out_path, source)
|
|
|
|
build_props.setProperty(f"{attr}-drv_path", drv_path, source)
|
|
|
|
|
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-12-26 20:56:36 +00:00
|
|
|
"""The original build trigger will the generic builder name `nix-build` in this case, which is not helpful"""
|
2023-09-10 08:11:56 +00:00
|
|
|
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(
|
2023-12-26 20:56:36 +00:00
|
|
|
f"{self._result_list.count(status)} {statusToString(status, count)}",
|
2023-09-10 08:11:56 +00:00
|
|
|
)
|
|
|
|
return {"step": f"({', '.join(summary)})"}
|
|
|
|
|
|
|
|
|
2024-07-11 18:56:15 +00:00
|
|
|
class NixBuildCombined(steps.BuildStep):
|
|
|
|
"""Shows the error message of a failed evaluation."""
|
|
|
|
|
|
|
|
name = "nix-build-combined"
|
|
|
|
|
|
|
|
def run(self) -> Generator[Any, object, Any]:
|
|
|
|
return self.build.results
|
|
|
|
|
|
|
|
|
2023-09-10 08:11:56 +00:00
|
|
|
class NixEvalCommand(buildstep.ShellMixin, steps.BuildStep):
|
2023-12-26 20:56:36 +00:00
|
|
|
"""Parses the output of `nix-eval-jobs` and triggers a `nix-build` build for
|
2023-09-10 08:11:56 +00:00
|
|
|
every attribute.
|
|
|
|
"""
|
|
|
|
|
2024-04-22 13:58:38 +00:00
|
|
|
project: GitProject
|
|
|
|
|
2024-04-27 15:06:17 +00:00
|
|
|
def __init__(
|
2024-07-12 14:44:02 +00:00
|
|
|
self,
|
|
|
|
project: GitProject,
|
|
|
|
supported_systems: list[str],
|
|
|
|
job_report_limit: int | None,
|
|
|
|
**kwargs: Any,
|
2024-04-27 15:06:17 +00:00
|
|
|
) -> None:
|
2023-09-10 08:11:56 +00:00
|
|
|
kwargs = self.setupShellMixin(kwargs)
|
|
|
|
super().__init__(**kwargs)
|
2024-04-22 13:58:38 +00:00
|
|
|
self.project = project
|
2023-09-10 08:11:56 +00:00
|
|
|
self.observer = logobserver.BufferLogObserver()
|
|
|
|
self.addLogObserver("stdio", self.observer)
|
|
|
|
self.supported_systems = supported_systems
|
2024-07-12 14:44:02 +00:00
|
|
|
self.job_report_limit = job_report_limit
|
2023-09-10 08:11:56 +00:00
|
|
|
|
|
|
|
@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
|
2024-04-27 15:06:17 +00:00
|
|
|
cmd: remotecommand.RemoteCommand = yield self.makeRemoteShellCommand()
|
2023-09-10 08:11:56 +00:00
|
|
|
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:
|
2023-12-26 20:56:36 +00:00
|
|
|
msg = f"Failed to parse line: {line}"
|
|
|
|
raise BuildbotNixError(msg) from e
|
2023-09-10 08:11:56 +00:00
|
|
|
jobs.append(job)
|
2024-04-22 13:58:38 +00:00
|
|
|
repo_name = self.project.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")
|
2023-12-26 20:56:36 +00:00
|
|
|
if not system or system in self.supported_systems: # report eval errors
|
2023-09-10 08:11:56 +00:00
|
|
|
filtered_jobs.append(job)
|
|
|
|
|
2024-07-11 18:56:15 +00:00
|
|
|
self.number_of_jobs = len(filtered_jobs)
|
|
|
|
|
2023-09-10 08:11:56 +00:00
|
|
|
self.build.addStepsAfterCurrentStep(
|
2024-07-12 14:44:02 +00:00
|
|
|
[
|
2023-10-27 09:39:11 +00:00
|
|
|
BuildTrigger(
|
2024-04-22 13:58:38 +00:00
|
|
|
self.project,
|
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,
|
2024-07-12 14:44:02 +00:00
|
|
|
report_status=(
|
|
|
|
self.job_report_limit is None
|
|
|
|
or self.number_of_jobs <= self.job_report_limit
|
|
|
|
),
|
2023-12-26 20:56:36 +00:00
|
|
|
),
|
2024-07-11 18:56:15 +00:00
|
|
|
]
|
2024-07-12 14:44:02 +00:00
|
|
|
+ (
|
|
|
|
[
|
|
|
|
Trigger(
|
|
|
|
waitForFinish=True,
|
|
|
|
schedulerNames=[f"{project_id}-nix-build-combined"],
|
|
|
|
haltOnFailure=True,
|
|
|
|
flunkOnFailure=True,
|
|
|
|
sourceStamps=[],
|
|
|
|
alwaysUseLatest=False,
|
|
|
|
updateSourceStamp=False,
|
|
|
|
),
|
|
|
|
]
|
|
|
|
if self.job_report_limit is not None
|
|
|
|
and self.number_of_jobs > self.job_report_limit
|
|
|
|
else []
|
|
|
|
),
|
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)
|
|
|
|
|
2023-12-26 20:56:36 +00:00
|
|
|
def retry_build(self, build_id: uuid.UUID) -> int:
|
|
|
|
retries = self.builds[build_id]
|
2023-09-10 08:11:56 +00:00
|
|
|
if retries > 1:
|
2023-12-26 20:56:36 +00:00
|
|
|
self.builds[build_id] = retries - 1
|
2023-09-10 08:11:56 +00:00
|
|
|
return retries
|
2023-12-26 20:56:36 +00:00
|
|
|
return 0
|
2023-09-10 08:11:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
# 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):
|
2023-12-26 20:56:36 +00:00
|
|
|
"""Shows the error message of a failed evaluation."""
|
2023-11-18 07:18:46 +00:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def run(self) -> Generator[Any, object, Any]:
|
|
|
|
error = self.getProperty("error")
|
|
|
|
attr = self.getProperty("attr")
|
|
|
|
# show eval error
|
2024-04-27 15:06:17 +00:00
|
|
|
error_log: StreamLog = yield self.addLog("nix_error")
|
2023-11-18 07:18:46 +00:00
|
|
|
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-12-26 20:56:36 +00:00
|
|
|
"""Builds a nix derivation."""
|
2023-09-10 08:11:56 +00:00
|
|
|
|
2024-07-09 09:16:40 +00:00
|
|
|
def __init__(self, retries: int, **kwargs: Any) -> None:
|
2023-09-10 08:11:56 +00:00
|
|
|
kwargs = self.setupShellMixin(kwargs)
|
2024-07-09 09:16:40 +00:00
|
|
|
self.retries = retries
|
2023-09-10 08:11:56 +00:00
|
|
|
super().__init__(**kwargs)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def run(self) -> Generator[Any, object, Any]:
|
|
|
|
# run `nix build`
|
2024-04-27 15:06:17 +00:00
|
|
|
cmd: remotecommand.RemoteCommand = yield self.makeRemoteShellCommand()
|
2023-09-10 08:11:56 +00:00
|
|
|
yield self.runCommand(cmd)
|
|
|
|
|
|
|
|
res = cmd.results()
|
2024-07-12 05:28:28 +00:00
|
|
|
if res == util.FAILURE and self.retries > 0:
|
2023-09-10 08:11:56 +00:00
|
|
|
retries = RETRY_COUNTER.retry_build(self.getProperty("build_uuid"))
|
2024-07-09 09:16:40 +00:00
|
|
|
if retries > self.retries - 1:
|
2023-09-10 08:11:56 +00:00
|
|
|
return util.RETRY
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
|
|
class UpdateBuildOutput(steps.BuildStep):
|
2023-12-26 20:56:36 +00:00
|
|
|
"""Updates store paths in a public www directory.
|
2023-09-10 08:11:56 +00:00
|
|
|
This is useful to prefetch updates without having to evaluate
|
|
|
|
on the target machine.
|
|
|
|
"""
|
|
|
|
|
2024-04-22 13:58:38 +00:00
|
|
|
project: GitProject
|
|
|
|
|
|
|
|
def __init__(self, project: GitProject, path: Path, **kwargs: Any) -> None:
|
2023-09-10 08:11:56 +00:00
|
|
|
super().__init__(**kwargs)
|
2024-04-22 13:58:38 +00:00
|
|
|
self.project = project
|
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()
|
2024-04-22 13:58:38 +00:00
|
|
|
if props.getProperty("branch") != self.project.default_branch:
|
2023-09-10 08:11:56 +00:00
|
|
|
return util.SKIPPED
|
2023-11-17 15:27:55 +00:00
|
|
|
|
2023-12-26 20:56:36 +00:00
|
|
|
attr = Path(props.getProperty("attr")).name
|
2023-09-10 08:11:56 +00:00
|
|
|
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
|
|
|
|
|
2024-04-27 15:06:17 +00:00
|
|
|
|
2024-04-06 13:08:21 +00:00
|
|
|
# GitHub somtimes fires the PR webhook before it has computed the merge commit
|
|
|
|
# This is a workaround to fetch the merge commit and checkout the PR branch in CI
|
|
|
|
class GitLocalPrMerge(steps.Git):
|
2023-12-03 10:48:01 +00:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def run_vc(
|
2023-12-26 20:56:36 +00:00
|
|
|
self,
|
|
|
|
branch: str,
|
|
|
|
revision: str,
|
|
|
|
patch: str,
|
2023-12-03 10:48:01 +00:00
|
|
|
) -> Generator[Any, object, Any]:
|
2024-04-06 13:08:21 +00:00
|
|
|
build_props = self.build.getProperties()
|
2024-05-03 10:16:18 +00:00
|
|
|
# TODO: abstract this into an interface as well
|
|
|
|
merge_base = build_props.getProperty(
|
|
|
|
"github.base.sha"
|
|
|
|
) or build_props.getProperty("base_sha")
|
|
|
|
pr_head = build_props.getProperty("github.head.sha") or build_props.getProperty(
|
|
|
|
"head_sha"
|
|
|
|
)
|
2024-04-06 13:08:21 +00:00
|
|
|
|
|
|
|
# Not a PR, fallback to default behavior
|
|
|
|
if merge_base is None or pr_head is None:
|
|
|
|
res = yield super().run_vc(branch, revision, patch)
|
|
|
|
return res
|
|
|
|
|
|
|
|
# The code below is a modified version of Git.run_vc
|
2024-04-27 15:06:17 +00:00
|
|
|
self.stdio_log: StreamLog = yield self.addLogForRemoteCommands("stdio")
|
2024-04-06 13:08:21 +00:00
|
|
|
self.stdio_log.addStdout(f"Merging {merge_base} into {pr_head}\n")
|
|
|
|
|
|
|
|
git_installed = yield self.checkFeatureSupport()
|
|
|
|
|
|
|
|
if not git_installed:
|
|
|
|
msg = "git is not installed on worker"
|
|
|
|
raise WorkerSetupError(msg)
|
|
|
|
|
2024-04-09 05:59:38 +00:00
|
|
|
has_git = yield self.pathExists(
|
|
|
|
self.build.path_module.join(self.workdir, ".git")
|
|
|
|
)
|
|
|
|
|
|
|
|
if not has_git:
|
|
|
|
yield self._dovccmd(["clone", "--recurse-submodules", self.repourl, "."])
|
|
|
|
|
2024-04-06 13:08:21 +00:00
|
|
|
patched = yield self.sourcedirIsPatched()
|
|
|
|
|
|
|
|
if patched:
|
|
|
|
yield self._dovccmd(["clean", "-f", "-f", "-d", "-x"])
|
|
|
|
|
|
|
|
yield self._dovccmd(["fetch", "-f", "-t", self.repourl, merge_base, pr_head])
|
|
|
|
|
|
|
|
yield self._dovccmd(["checkout", "--detach", "-f", pr_head])
|
|
|
|
|
|
|
|
yield self._dovccmd(
|
|
|
|
[
|
|
|
|
"-c",
|
|
|
|
"user.email=buildbot@example.com",
|
|
|
|
"-c",
|
|
|
|
"user.name=buildbot",
|
|
|
|
"merge",
|
|
|
|
"--no-ff",
|
|
|
|
"-m",
|
|
|
|
f"Merge {merge_base} into {pr_head}",
|
|
|
|
merge_base,
|
|
|
|
]
|
|
|
|
)
|
|
|
|
self.updateSourceProperty("got_revision", pr_head)
|
|
|
|
res = yield self.parseCommitDescription()
|
|
|
|
return res
|
2023-12-03 10:48:01 +00:00
|
|
|
|
2023-09-10 08:11:56 +00:00
|
|
|
|
|
|
|
def nix_eval_config(
|
2024-04-22 13:58:38 +00:00
|
|
|
project: GitProject,
|
2023-09-10 08:11:56 +00:00
|
|
|
worker_names: list[str],
|
2024-04-22 13:58:38 +00:00
|
|
|
git_url: str,
|
2023-09-10 08:11:56 +00:00
|
|
|
supported_systems: list[str],
|
2024-04-22 13:58:38 +00:00
|
|
|
eval_lock: MasterLock,
|
2023-11-12 06:07:30 +00:00
|
|
|
worker_count: int,
|
|
|
|
max_memory_size: int,
|
2024-07-12 14:44:02 +00:00
|
|
|
job_report_limit: int | None,
|
2024-04-22 13:58:38 +00:00
|
|
|
) -> BuilderConfig:
|
2023-12-26 20:56:36 +00:00
|
|
|
"""Uses nix-eval-jobs to evaluate hydraJobs from flake.nix in parallel.
|
2023-09-10 08:11:56 +00:00
|
|
|
For each evaluated attribute a new build pipeline is started.
|
|
|
|
"""
|
|
|
|
factory = util.BuildFactory()
|
|
|
|
# check out the source
|
2024-04-22 13:58:38 +00:00
|
|
|
url_with_secret = util.Interpolate(git_url)
|
2023-09-10 08:11:56 +00:00
|
|
|
factory.addStep(
|
2024-04-06 13:08:21 +00:00
|
|
|
GitLocalPrMerge(
|
2023-09-10 08:11:56 +00:00
|
|
|
repourl=url_with_secret,
|
|
|
|
method="clean",
|
|
|
|
submodules=True,
|
|
|
|
haltOnFailure=True,
|
2023-12-26 20:56:36 +00:00
|
|
|
),
|
2023-09-10 08:11:56 +00:00
|
|
|
)
|
2024-01-15 12:41:48 +00:00
|
|
|
drv_gcroots_dir = util.Interpolate(
|
|
|
|
"/nix/var/nix/gcroots/per-user/buildbot-worker/%(prop:project)s/drvs/",
|
|
|
|
)
|
2023-09-10 08:11:56 +00:00
|
|
|
|
|
|
|
factory.addStep(
|
|
|
|
NixEvalCommand(
|
2024-04-22 13:58:38 +00:00
|
|
|
project=project,
|
2023-09-10 08:11:56 +00:00
|
|
|
env={},
|
|
|
|
name="evaluate flake",
|
|
|
|
supported_systems=supported_systems,
|
2024-07-12 14:44:02 +00:00
|
|
|
job_report_limit=job_report_limit,
|
2023-09-10 08:11:56 +00:00
|
|
|
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",
|
2024-01-15 12:41:48 +00:00
|
|
|
drv_gcroots_dir,
|
2023-09-10 08:11:56 +00:00
|
|
|
"--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-12-26 20:56:36 +00:00
|
|
|
),
|
2023-09-10 08:11:56 +00:00
|
|
|
)
|
|
|
|
|
2024-01-15 12:41:48 +00:00
|
|
|
factory.addStep(
|
|
|
|
steps.ShellCommand(
|
|
|
|
name="Cleanup drv paths",
|
|
|
|
command=[
|
|
|
|
"rm",
|
|
|
|
"-rf",
|
|
|
|
drv_gcroots_dir,
|
|
|
|
],
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
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"),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-07-16 13:06:13 +00:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def do_register_gcroot_if(s: steps.BuildStep) -> Generator[Any, object, Any]:
|
|
|
|
gc_root = yield util.Interpolate(
|
|
|
|
"/nix/var/nix/gcroots/per-user/buildbot-worker/%(prop:project)s/%(prop:attr)s"
|
|
|
|
).getRenderingFor(s.getProperties())
|
|
|
|
out_path = yield util.Property("out_path").getRenderingFor(s.getProperties())
|
|
|
|
default_branch = yield util.Property("default_branch").getRenderingFor(
|
|
|
|
s.getProperties()
|
|
|
|
)
|
|
|
|
|
|
|
|
return s.getProperty("branch") == str(default_branch) and not (
|
|
|
|
Path(str(gc_root)).exists() and Path(str(gc_root)).readlink() == str(out_path)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-09-10 08:11:56 +00:00
|
|
|
def nix_build_config(
|
2024-04-22 13:58:38 +00:00
|
|
|
project: GitProject,
|
2023-09-10 08:11:56 +00:00
|
|
|
worker_names: list[str],
|
2024-07-18 15:39:36 +00:00
|
|
|
post_build_steps: list[steps.BuildStep],
|
2023-11-04 08:50:23 +00:00
|
|
|
outputs_path: Path | None = None,
|
2024-07-09 09:16:40 +00:00
|
|
|
retries: int = 1,
|
2024-04-22 13:58:38 +00:00
|
|
|
) -> BuilderConfig:
|
2023-12-26 20:56:36 +00:00
|
|
|
"""Builds one nix flake attribute."""
|
2023-09-10 08:11:56 +00:00
|
|
|
factory = util.BuildFactory()
|
|
|
|
factory.addStep(
|
|
|
|
NixBuildCommand(
|
|
|
|
env={},
|
|
|
|
name="Build flake attr",
|
|
|
|
command=[
|
|
|
|
"nix",
|
|
|
|
"build",
|
|
|
|
"-L",
|
|
|
|
"--option",
|
|
|
|
"keep-going",
|
|
|
|
"true",
|
2023-12-22 13:37:31 +00:00
|
|
|
# stop stuck builds after 20 minutes
|
|
|
|
"--max-silent-time",
|
|
|
|
str(60 * 20),
|
2023-09-10 08:11:56 +00:00
|
|
|
"--accept-flake-config",
|
|
|
|
"--out-link",
|
|
|
|
util.Interpolate("result-%(prop:attr)s"),
|
|
|
|
util.Interpolate("%(prop:drv_path)s^*"),
|
|
|
|
],
|
2023-12-22 13:37:31 +00:00
|
|
|
# 3 hours, defaults to 20 minutes
|
|
|
|
# We increase this over the default since the build output might end up in a different `nix build`.
|
|
|
|
timeout=60 * 60 * 3,
|
2024-07-09 09:16:40 +00:00
|
|
|
retries=retries,
|
2023-09-10 08:11:56 +00:00
|
|
|
haltOnFailure=True,
|
2023-12-26 20:56:36 +00:00
|
|
|
),
|
2023-09-10 08:11:56 +00:00
|
|
|
)
|
2024-07-18 15:39:36 +00:00
|
|
|
factory.addSteps(post_build_steps)
|
2023-10-09 15:13:46 +00:00
|
|
|
|
2023-09-10 08:11:56 +00:00
|
|
|
factory.addStep(
|
2024-07-16 13:06:13 +00:00
|
|
|
Trigger(
|
2023-09-10 08:11:56 +00:00
|
|
|
name="Register gcroot",
|
2024-07-16 13:06:13 +00:00
|
|
|
waitForFinish=True,
|
|
|
|
schedulerNames=[
|
|
|
|
f"{slugify_project_name(project.name)}-nix-register-gcroot"
|
2023-09-10 08:11:56 +00:00
|
|
|
],
|
2024-07-16 13:06:13 +00:00
|
|
|
haltOnFailure=True,
|
|
|
|
flunkOnFailure=True,
|
|
|
|
sourceStamps=[],
|
|
|
|
alwaysUseLatest=False,
|
|
|
|
updateSourceStamp=False,
|
|
|
|
doStepIf=do_register_gcroot_if,
|
|
|
|
copy_properties=["out_path", "attr"],
|
2024-07-24 19:22:46 +00:00
|
|
|
set_properties={"report_status": False},
|
2023-12-26 20:56:36 +00:00
|
|
|
),
|
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-12-26 20:56:36 +00:00
|
|
|
),
|
2023-09-10 08:11:56 +00:00
|
|
|
)
|
2023-11-04 08:50:23 +00:00
|
|
|
if outputs_path is not None:
|
|
|
|
factory.addStep(
|
|
|
|
UpdateBuildOutput(
|
2024-04-22 13:58:38 +00:00
|
|
|
project=project,
|
2023-11-04 08:50:23 +00:00
|
|
|
name="Update build output",
|
|
|
|
path=outputs_path,
|
2023-12-26 20:56:36 +00:00
|
|
|
),
|
2023-11-04 08:50:23 +00:00
|
|
|
)
|
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(
|
2024-04-22 13:58:38 +00:00
|
|
|
project: GitProject,
|
2023-12-26 20:56:36 +00:00
|
|
|
worker_names: list[str],
|
2024-04-22 13:58:38 +00:00
|
|
|
) -> BuilderConfig:
|
2023-12-26 20:56:36 +00:00
|
|
|
"""Dummy builder that is triggered when a build is skipped."""
|
2023-11-18 07:18:46 +00:00
|
|
|
factory = util.BuildFactory()
|
|
|
|
factory.addStep(
|
|
|
|
EvalErrorStep(
|
|
|
|
name="Nix evaluation",
|
|
|
|
doStepIf=lambda s: s.getProperty("error"),
|
|
|
|
hideStepIf=lambda _, s: not s.getProperty("error"),
|
2023-12-26 20:56:36 +00:00
|
|
|
),
|
2023-11-18 07:18:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# 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"),
|
2023-12-26 20:56:36 +00:00
|
|
|
),
|
2023-11-18 07:18:46 +00:00
|
|
|
)
|
2024-07-16 13:06:13 +00:00
|
|
|
|
|
|
|
# if the change got pulled in from a PR, the roots haven't been created yet
|
|
|
|
factory.addStep(
|
|
|
|
Trigger(
|
|
|
|
name="Register gcroot",
|
|
|
|
waitForFinish=True,
|
|
|
|
schedulerNames=[
|
|
|
|
f"{slugify_project_name(project.name)}-nix-register-gcroot"
|
|
|
|
],
|
|
|
|
haltOnFailure=True,
|
|
|
|
flunkOnFailure=True,
|
|
|
|
sourceStamps=[],
|
|
|
|
alwaysUseLatest=False,
|
|
|
|
updateSourceStamp=False,
|
|
|
|
doStepIf=do_register_gcroot_if,
|
|
|
|
copy_properties=["out_path", "attr"],
|
2024-07-24 19:22:46 +00:00
|
|
|
set_properties={"report_status": False},
|
2024-07-16 13:06:13 +00:00
|
|
|
),
|
|
|
|
)
|
2023-11-18 07:18:46 +00:00
|
|
|
return util.BuilderConfig(
|
|
|
|
name=f"{project.name}/nix-skipped-build",
|
|
|
|
project=project.name,
|
|
|
|
workernames=worker_names,
|
|
|
|
collapseRequests=False,
|
|
|
|
env={},
|
|
|
|
factory=factory,
|
|
|
|
)
|
|
|
|
|
2024-04-27 15:06:17 +00:00
|
|
|
|
2024-07-16 13:06:13 +00:00
|
|
|
def nix_register_gcroot_config(
|
|
|
|
project: GitProject,
|
|
|
|
worker_names: list[str],
|
|
|
|
) -> BuilderConfig:
|
|
|
|
factory = util.BuildFactory()
|
|
|
|
|
|
|
|
# if the change got pulled in from a PR, the roots haven't been created yet
|
|
|
|
factory.addStep(
|
|
|
|
steps.ShellCommand(
|
|
|
|
name="Register gcroot",
|
|
|
|
command=[
|
|
|
|
"nix-store",
|
|
|
|
"--add-root",
|
|
|
|
# FIXME: cleanup old build attributes
|
|
|
|
util.Interpolate(
|
|
|
|
"/nix/var/nix/gcroots/per-user/buildbot-worker/%(prop:project)s/%(prop:attr)s",
|
|
|
|
),
|
|
|
|
"-r",
|
|
|
|
util.Property("out_path"),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
return util.BuilderConfig(
|
|
|
|
name=f"{project.name}/nix-register-gcroot",
|
|
|
|
project=project.name,
|
|
|
|
workernames=worker_names,
|
|
|
|
collapseRequests=False,
|
|
|
|
env={},
|
|
|
|
factory=factory,
|
|
|
|
)
|
|
|
|
|
2024-07-24 19:22:46 +00:00
|
|
|
|
2024-07-11 18:56:15 +00:00
|
|
|
def nix_build_combined_config(
|
|
|
|
project: GitProject,
|
|
|
|
worker_names: list[str],
|
|
|
|
) -> BuilderConfig:
|
|
|
|
factory = util.BuildFactory()
|
|
|
|
factory.addStep(NixBuildCombined())
|
|
|
|
|
|
|
|
return util.BuilderConfig(
|
|
|
|
name=f"{project.name}/nix-build-combined",
|
|
|
|
project=project.name,
|
|
|
|
workernames=worker_names,
|
|
|
|
collapseRequests=False,
|
|
|
|
env={},
|
|
|
|
factory=factory,
|
|
|
|
properties=dict(status_name="nix-build-combined"),
|
|
|
|
)
|
|
|
|
|
2024-07-16 13:06:13 +00:00
|
|
|
|
2023-09-17 20:14:56 +00:00
|
|
|
def config_for_project(
|
|
|
|
config: dict[str, Any],
|
2024-04-22 13:58:38 +00:00
|
|
|
project: GitProject,
|
2023-09-17 20:14:56 +00:00
|
|
|
worker_names: list[str],
|
|
|
|
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,
|
2024-04-22 13:58:38 +00:00
|
|
|
eval_lock: MasterLock,
|
2024-07-18 15:39:36 +00:00
|
|
|
post_build_steps: list[steps.BuildStep],
|
2024-07-12 14:44:02 +00:00
|
|
|
job_report_limit: int | None,
|
2023-11-04 08:50:23 +00:00
|
|
|
outputs_path: Path | None = None,
|
2024-07-09 09:16:40 +00:00
|
|
|
build_retries: int = 1,
|
2024-04-22 13:58:38 +00:00
|
|
|
) -> None:
|
2023-09-17 20:14:56 +00:00
|
|
|
config["projects"].append(Project(project.name))
|
|
|
|
config["schedulers"].extend(
|
|
|
|
[
|
|
|
|
schedulers.SingleBranchScheduler(
|
2023-12-26 20:56:36 +00:00
|
|
|
name=f"{project.project_id}-default-branch",
|
2023-09-17 20:14:56 +00:00
|
|
|
change_filter=util.ChangeFilter(
|
|
|
|
repository=project.url,
|
2024-04-22 13:58:38 +00:00
|
|
|
filter_fn=lambda c: c.branch == project.default_branch,
|
2023-09-17 20:14:56 +00:00
|
|
|
),
|
|
|
|
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-12-26 20:56:36 +00:00
|
|
|
name=f"{project.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-12-26 20:56:36 +00:00
|
|
|
name=f"{project.project_id}-prs",
|
2023-09-17 20:14:56 +00:00
|
|
|
change_filter=util.ChangeFilter(
|
2023-12-26 20:56:36 +00:00
|
|
|
repository=project.url,
|
|
|
|
category="pull",
|
2023-09-17 20:14:56 +00:00
|
|
|
),
|
|
|
|
builderNames=[f"{project.name}/nix-eval"],
|
|
|
|
),
|
|
|
|
# this is triggered from `nix-eval`
|
|
|
|
schedulers.Triggerable(
|
2023-12-26 20:56:36 +00:00
|
|
|
name=f"{project.project_id}-nix-build",
|
2023-09-17 20:14:56 +00:00
|
|
|
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(
|
2023-12-26 20:56:36 +00:00
|
|
|
name=f"{project.project_id}-nix-skipped-build",
|
2023-11-18 07:18:46 +00:00
|
|
|
builderNames=[f"{project.name}/nix-skipped-build"],
|
|
|
|
),
|
2024-07-11 18:56:15 +00:00
|
|
|
# this is triggered from `nix-eval` when the build contains too many outputs
|
|
|
|
schedulers.Triggerable(
|
|
|
|
name=f"{project.project_id}-nix-build-combined",
|
|
|
|
builderNames=[f"{project.name}/nix-build-combined"],
|
|
|
|
),
|
2024-07-16 13:06:13 +00:00
|
|
|
schedulers.Triggerable(
|
|
|
|
name=f"{project.project_id}-nix-register-gcroot",
|
|
|
|
builderNames=[f"{project.name}/nix-register-gcroot"],
|
|
|
|
),
|
2023-09-17 20:14:56 +00:00
|
|
|
# allow to manually trigger a nix-build
|
|
|
|
schedulers.ForceScheduler(
|
2023-12-26 20:56:36 +00:00
|
|
|
name=f"{project.project_id}-force",
|
2023-10-27 08:49:40 +00:00
|
|
|
builderNames=[f"{project.name}/nix-eval"],
|
2023-10-27 08:35:26 +00:00
|
|
|
properties=[
|
|
|
|
util.StringParameter(
|
|
|
|
name="project",
|
2024-04-22 13:58:38 +00:00
|
|
|
label=f"Name of the {project.pretty_type} repository.",
|
2023-10-27 08:49:40 +00:00
|
|
|
default=project.name,
|
2023-12-26 20:56:36 +00:00
|
|
|
),
|
2023-10-27 08:49:40 +00:00
|
|
|
],
|
2023-09-17 20:14:56 +00:00
|
|
|
),
|
2023-12-26 20:56:36 +00:00
|
|
|
],
|
2023-09-17 20:14:56 +00:00
|
|
|
)
|
|
|
|
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,
|
2024-01-05 07:41:32 +00:00
|
|
|
worker_names,
|
2024-04-22 13:58:38 +00:00
|
|
|
git_url=project.get_project_url(),
|
2023-09-17 20:14:56 +00:00
|
|
|
supported_systems=nix_supported_systems,
|
2024-07-12 14:44:02 +00:00
|
|
|
job_report_limit=job_report_limit,
|
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,
|
2023-11-04 08:50:23 +00:00
|
|
|
outputs_path=outputs_path,
|
2024-07-09 09:16:40 +00:00
|
|
|
retries=build_retries,
|
2024-07-18 15:39:36 +00:00
|
|
|
post_build_steps=post_build_steps,
|
2023-09-17 20:14:56 +00:00
|
|
|
),
|
2023-11-18 07:18:46 +00:00
|
|
|
nix_skipped_build_config(project, [SKIPPED_BUILDER_NAME]),
|
2024-07-16 13:06:13 +00:00
|
|
|
nix_register_gcroot_config(project, worker_names),
|
2024-07-11 18:56:15 +00:00
|
|
|
nix_build_combined_config(project, worker_names),
|
2023-12-26 20:56:36 +00:00
|
|
|
],
|
2023-09-17 20:14:56 +00:00
|
|
|
)
|
|
|
|
|
2024-04-27 15:06:17 +00:00
|
|
|
|
2024-03-07 21:49:35 +00:00
|
|
|
def normalize_virtual_builder_name(name: str) -> str:
|
2024-04-22 13:58:38 +00:00
|
|
|
if re.match(r"^[^:]+:", name) is not None:
|
2024-03-07 21:49:35 +00:00
|
|
|
# rewrites github:nix-community/srvos#checks.aarch64-linux.nixos-stable-example-hardware-hetzner-online-intel -> nix-community/srvos/nix-build
|
2024-07-05 11:26:50 +00:00
|
|
|
match = re.match(r"[^:]+:(?P<owner>[^/]+)/(?P<repo>[^#]+)#.+", name)
|
2024-03-07 21:49:35 +00:00
|
|
|
if match:
|
|
|
|
return f"{match['owner']}/{match['repo']}/nix-build"
|
|
|
|
|
|
|
|
return name
|
|
|
|
|
|
|
|
|
2023-12-04 18:44:13 +00:00
|
|
|
class AnyProjectEndpointMatcher(EndpointMatcherBase):
|
2023-12-26 20:56:36 +00:00
|
|
|
def __init__(self, builders: set[str] | None = None, **kwargs: Any) -> None:
|
|
|
|
if builders is None:
|
|
|
|
builders = set()
|
2023-12-04 18:44:13 +00:00
|
|
|
self.builders = builders
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def check_builder(
|
2023-12-26 20:56:36 +00:00
|
|
|
self,
|
|
|
|
endpoint_object: Any,
|
|
|
|
endpoint_dict: dict[str, Any],
|
|
|
|
object_type: str,
|
2024-01-01 07:32:22 +00:00
|
|
|
) -> Generator[defer.Deferred[Match], Any, Any]:
|
2023-12-04 18:44:13 +00:00
|
|
|
res = yield endpoint_object.get({}, endpoint_dict)
|
|
|
|
if res is None:
|
|
|
|
return None
|
|
|
|
|
2024-07-05 10:46:20 +00:00
|
|
|
builderid = res.get("builderid")
|
|
|
|
if builderid is None:
|
|
|
|
builder_name = res["builder_names"][0]
|
|
|
|
else:
|
|
|
|
builder = yield self.master.data.get(("builders", builderid))
|
|
|
|
builder_name = builder["name"]
|
|
|
|
|
|
|
|
builder_name = normalize_virtual_builder_name(builder_name)
|
2024-03-07 21:49:35 +00:00
|
|
|
if builder_name in self.builders:
|
2024-01-01 07:00:13 +00:00
|
|
|
log.warn(
|
2023-12-04 18:44:13 +00:00
|
|
|
"Builder {builder} allowed by {role}: {builders}",
|
2024-03-07 21:49:35 +00:00
|
|
|
builder=builder_name,
|
2023-12-04 18:44:13 +00:00
|
|
|
role=self.role,
|
|
|
|
builders=self.builders,
|
|
|
|
)
|
|
|
|
return Match(self.master, **{object_type: res})
|
|
|
|
else:
|
2024-01-01 07:00:13 +00:00
|
|
|
log.warn(
|
2023-12-04 18:44:13 +00:00
|
|
|
"Builder {builder} not allowed by {role}: {builders}",
|
2024-03-07 21:49:35 +00:00
|
|
|
builder=builder_name,
|
2023-12-04 18:44:13 +00:00
|
|
|
role=self.role,
|
|
|
|
builders=self.builders,
|
|
|
|
)
|
|
|
|
|
2024-07-05 10:46:20 +00:00
|
|
|
def match_ForceSchedulerEndpoint_force( # noqa: N802
|
|
|
|
self,
|
|
|
|
epobject: Any,
|
|
|
|
epdict: dict[str, Any],
|
|
|
|
options: dict[str, Any],
|
|
|
|
) -> defer.Deferred[Match]:
|
|
|
|
return self.check_builder(epobject, epdict, "build")
|
|
|
|
|
2023-12-04 18:44:13 +00:00
|
|
|
def match_BuildEndpoint_rebuild( # noqa: N802
|
2023-12-26 20:56:36 +00:00
|
|
|
self,
|
|
|
|
epobject: Any,
|
|
|
|
epdict: dict[str, Any],
|
|
|
|
options: dict[str, Any],
|
2024-01-01 07:32:22 +00:00
|
|
|
) -> defer.Deferred[Match]:
|
2023-12-04 18:44:13 +00:00
|
|
|
return self.check_builder(epobject, epdict, "build")
|
|
|
|
|
|
|
|
def match_BuildEndpoint_stop( # noqa: N802
|
2023-12-26 20:56:36 +00:00
|
|
|
self,
|
|
|
|
epobject: Any,
|
|
|
|
epdict: dict[str, Any],
|
|
|
|
options: dict[str, Any],
|
2024-01-01 07:32:22 +00:00
|
|
|
) -> defer.Deferred[Match]:
|
2023-12-04 18:44:13 +00:00
|
|
|
return self.check_builder(epobject, epdict, "build")
|
|
|
|
|
|
|
|
def match_BuildRequestEndpoint_stop( # noqa: N802
|
2023-12-26 20:56:36 +00:00
|
|
|
self,
|
|
|
|
epobject: Any,
|
|
|
|
epdict: dict[str, Any],
|
|
|
|
options: dict[str, Any],
|
2024-01-01 07:32:22 +00:00
|
|
|
) -> defer.Deferred[Match]:
|
2023-12-04 18:44:13 +00:00
|
|
|
return self.check_builder(epobject, epdict, "buildrequest")
|
|
|
|
|
|
|
|
|
2024-04-27 15:06:17 +00:00
|
|
|
def setup_authz(
|
|
|
|
backends: list[GitBackend], projects: list[GitProject], admins: list[str]
|
|
|
|
) -> Authz:
|
2023-12-04 18:44:13 +00:00
|
|
|
allow_rules = []
|
|
|
|
allowed_builders_by_org: defaultdict[str, set[str]] = defaultdict(
|
2024-04-22 13:58:38 +00:00
|
|
|
lambda: {backend.reload_builder_name for backend in backends},
|
2023-12-04 18:44:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
for project in projects:
|
|
|
|
if project.belongs_to_org:
|
|
|
|
for builder in ["nix-build", "nix-skipped-build", "nix-eval"]:
|
|
|
|
allowed_builders_by_org[project.owner].add(f"{project.name}/{builder}")
|
|
|
|
|
|
|
|
for org, allowed_builders in allowed_builders_by_org.items():
|
|
|
|
allow_rules.append(
|
|
|
|
AnyProjectEndpointMatcher(
|
|
|
|
builders=allowed_builders,
|
|
|
|
role=org,
|
|
|
|
defaultDeny=False,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
allow_rules.append(util.AnyEndpointMatcher(role="admin", defaultDeny=False))
|
|
|
|
allow_rules.append(util.AnyControlEndpointMatcher(role="admins"))
|
|
|
|
return util.Authz(
|
|
|
|
roleMatchers=[
|
|
|
|
util.RolesFromUsername(roles=["admin"], usernames=admins),
|
|
|
|
util.RolesFromGroups(groupPrefix=""), # so we can match on ORG
|
|
|
|
],
|
|
|
|
allowRules=allow_rules,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-01-08 07:10:03 +00:00
|
|
|
class PeriodicWithStartup(schedulers.Periodic):
|
|
|
|
def __init__(self, *args: Any, run_on_startup: bool = False, **kwargs: Any) -> None:
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.run_on_startup = run_on_startup
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def activate(self) -> Generator[Any, object, Any]:
|
|
|
|
if self.run_on_startup:
|
|
|
|
yield self.setState("last_build", None)
|
|
|
|
yield super().activate()
|
|
|
|
|
|
|
|
|
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"""
|
|
|
|
|
2024-07-19 20:20:42 +00:00
|
|
|
def __init__(self, config: BuildbotNixConfig) -> None:
|
2023-09-17 20:14:56 +00:00
|
|
|
super().__init__()
|
2024-07-19 20:20:42 +00:00
|
|
|
|
|
|
|
self.config = config
|
2023-09-17 20:14:56 +00:00
|
|
|
|
|
|
|
def configure(self, config: dict[str, Any]) -> None:
|
2024-04-27 15:06:17 +00:00
|
|
|
backends: dict[str, GitBackend] = {}
|
2024-04-22 13:58:38 +00:00
|
|
|
|
2024-07-19 20:20:42 +00:00
|
|
|
if self.config.github is not None:
|
|
|
|
backends["github"] = GithubBackend(self.config.github, self.config.url)
|
2024-04-27 15:06:17 +00:00
|
|
|
|
2024-07-19 20:20:42 +00:00
|
|
|
if self.config.gitea is not None:
|
2024-07-24 15:24:47 +00:00
|
|
|
backends["gitea"] = GiteaBackend(self.config.gitea, self.config.url)
|
2024-04-27 15:06:17 +00:00
|
|
|
|
|
|
|
auth: AuthBase | None = (
|
2024-07-19 20:20:42 +00:00
|
|
|
backends[self.config.auth_backend].create_auth()
|
|
|
|
if self.config.auth_backend != "none"
|
2024-04-27 15:06:17 +00:00
|
|
|
else None
|
|
|
|
)
|
2024-04-22 13:58:38 +00:00
|
|
|
|
|
|
|
projects: list[GitProject] = []
|
|
|
|
|
2024-04-27 15:06:17 +00:00
|
|
|
for backend in backends.values():
|
2024-04-22 13:58:38 +00:00
|
|
|
projects += backend.load_projects()
|
|
|
|
|
2024-07-19 20:20:42 +00:00
|
|
|
worker_config = json.loads(self.config.nix_workers_secret)
|
2023-09-17 20:14:56 +00:00
|
|
|
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)
|
|
|
|
|
2024-01-05 07:41:32 +00:00
|
|
|
eval_lock = util.MasterLock("nix-eval")
|
2023-10-12 13:59:26 +00:00
|
|
|
|
2024-07-19 20:20:42 +00:00
|
|
|
if self.config.cachix is not None:
|
2024-07-18 15:39:36 +00:00
|
|
|
self.post_build_steps.append(
|
|
|
|
steps.ShellCommand(
|
|
|
|
name="Upload cachix",
|
|
|
|
env=self.cachix.cachix_env(),
|
|
|
|
command=[
|
|
|
|
"cachix",
|
|
|
|
"push",
|
|
|
|
self.cachix.name,
|
|
|
|
util.Interpolate("result-%(prop:attr)s"),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2023-10-12 13:59:26 +00:00
|
|
|
for project in projects:
|
2023-09-17 20:14:56 +00:00
|
|
|
config_for_project(
|
|
|
|
config,
|
|
|
|
project,
|
|
|
|
worker_names,
|
2024-07-19 20:20:42 +00:00
|
|
|
self.config.build_systems,
|
|
|
|
self.config.eval_worker_count or multiprocessing.cpu_count(),
|
|
|
|
self.config.eval_max_memory_size,
|
2023-11-10 13:30:09 +00:00
|
|
|
eval_lock,
|
2024-07-19 20:20:42 +00:00
|
|
|
[x.to_buildstep() for x in self.config.post_build_steps],
|
2024-07-12 14:44:02 +00:00
|
|
|
self.config.job_report_limit,
|
2024-07-19 20:20:42 +00:00
|
|
|
self.config.outputs_path,
|
|
|
|
self.config.build_retries,
|
2023-09-17 20:14:56 +00:00
|
|
|
)
|
2023-10-12 13:59:26 +00:00
|
|
|
|
2023-11-18 07:18:46 +00:00
|
|
|
config["workers"].append(worker.LocalWorker(SKIPPED_BUILDER_NAME))
|
2023-12-05 20:52:25 +00:00
|
|
|
|
2024-04-27 15:06:17 +00:00
|
|
|
for backend in backends.values():
|
2024-04-22 13:58:38 +00:00
|
|
|
# Reload backend projects
|
2024-04-27 15:06:17 +00:00
|
|
|
config["builders"].append(backend.create_reload_builder([worker_names[0]]))
|
2024-04-22 13:58:38 +00:00
|
|
|
config["schedulers"].extend(
|
|
|
|
[
|
|
|
|
schedulers.ForceScheduler(
|
|
|
|
name=f"reload-{backend.type}-projects",
|
|
|
|
builderNames=[backend.reload_builder_name],
|
|
|
|
buttonName="Update projects",
|
|
|
|
),
|
|
|
|
# project list twice a day and on startup
|
|
|
|
PeriodicWithStartup(
|
|
|
|
name=f"reload-{backend.type}-projects-bidaily",
|
|
|
|
builderNames=[backend.reload_builder_name],
|
|
|
|
periodicBuildTimer=12 * 60 * 60,
|
|
|
|
run_on_startup=not backend.are_projects_cached(),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
config["services"].append(backend.create_reporter())
|
2024-05-18 19:36:22 +00:00
|
|
|
config.setdefault("secretProviders", [])
|
|
|
|
config["secretsProviders"].extend(backend.create_secret_providers())
|
2024-04-22 13:58:38 +00:00
|
|
|
|
|
|
|
systemd_secrets = SecretInAFile(
|
2023-12-26 20:56:36 +00:00
|
|
|
dirname=os.environ["CREDENTIALS_DIRECTORY"],
|
2023-09-17 20:14:56 +00:00
|
|
|
)
|
|
|
|
config["secretsProviders"].append(systemd_secrets)
|
2023-12-05 20:52:25 +00:00
|
|
|
|
|
|
|
config["www"].setdefault("plugins", {})
|
|
|
|
|
|
|
|
config["www"].setdefault("change_hook_dialects", {})
|
2024-04-27 15:06:17 +00:00
|
|
|
for backend in backends.values():
|
|
|
|
config["www"]["change_hook_dialects"][backend.change_hook_name] = (
|
|
|
|
backend.create_change_hook()
|
|
|
|
)
|
2023-10-26 11:01:08 +00:00
|
|
|
|
2024-06-02 14:13:14 +00:00
|
|
|
config["www"].setdefault("avatar_methods", [])
|
|
|
|
|
|
|
|
for backend in backends.values():
|
|
|
|
avatar_method = backend.create_avatar_method()
|
|
|
|
print(avatar_method)
|
|
|
|
if avatar_method is not None:
|
|
|
|
config["www"]["avatar_methods"].append(avatar_method)
|
2024-04-22 13:58:38 +00:00
|
|
|
|
2024-06-02 14:13:14 +00:00
|
|
|
if "auth" not in config["www"]:
|
2024-04-22 13:58:38 +00:00
|
|
|
# TODO one cannot have multiple auth backends...
|
2024-04-27 15:06:17 +00:00
|
|
|
if auth is not None:
|
|
|
|
config["www"]["auth"] = auth
|
2023-12-04 18:44:13 +00:00
|
|
|
|
|
|
|
config["www"]["authz"] = setup_authz(
|
2024-07-19 20:20:42 +00:00
|
|
|
admins=self.config.admins,
|
2024-04-27 15:06:17 +00:00
|
|
|
backends=list(backends.values()),
|
2023-12-26 20:56:36 +00:00
|
|
|
projects=projects,
|
2023-10-26 11:01:08 +00:00
|
|
|
)
|