2023-09-10 08:11:56 +00:00
|
|
|
|
import json
|
|
|
|
|
import multiprocessing
|
|
|
|
|
import os
|
2023-09-17 20:14:56 +00:00
|
|
|
|
import sys
|
2024-03-10 21:27:24 +00:00
|
|
|
|
import graphlib
|
2024-07-19 17:24:33 +00:00
|
|
|
|
import base64
|
2023-09-17 20:14:56 +00:00
|
|
|
|
from collections.abc import Generator
|
2024-07-19 17:24:33 +00:00
|
|
|
|
from dataclasses import dataclass, field
|
2023-09-10 08:11:56 +00:00
|
|
|
|
from pathlib import Path
|
2023-12-26 20:56:36 +00:00
|
|
|
|
from typing import TYPE_CHECKING, Any
|
2023-12-26 18:49:57 +00:00
|
|
|
|
|
2024-06-09 15:01:32 +00:00
|
|
|
|
import buildbot
|
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
|
2023-09-17 20:14:56 +00:00
|
|
|
|
from buildbot.process.project import Project
|
2024-05-06 17:39:35 +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-05-06 17:52:55 +00:00
|
|
|
|
from buildbot.www.auth import AuthBase
|
2024-03-02 14:59:44 +00:00
|
|
|
|
from buildbot.www.oauth2 import OAuth2Auth
|
|
|
|
|
from buildbot.changes.gerritchangesource import GerritChangeSource
|
2024-03-10 21:27:24 +00:00
|
|
|
|
from buildbot.reporters.utils import getURLForBuildrequest
|
2024-07-19 17:24:33 +00:00
|
|
|
|
from buildbot.reporters.generators.build import BuildStatusGenerator
|
|
|
|
|
from buildbot.reporters.message import MessageFormatterFunction
|
2024-03-10 21:27:24 +00:00
|
|
|
|
from buildbot.process.buildstep import EXCEPTION
|
|
|
|
|
from buildbot.process.buildstep import SUCCESS
|
2024-03-10 22:55:38 +00:00
|
|
|
|
from buildbot.process.results import worst_status
|
2024-06-09 15:01:32 +00:00
|
|
|
|
import requests
|
2023-10-27 08:49:40 +00:00
|
|
|
|
|
2023-12-26 20:56:36 +00:00
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from buildbot.process.log import Log
|
|
|
|
|
|
2024-05-06 17:39:35 +00:00
|
|
|
|
from twisted.internet import defer
|
2023-12-26 20:56:36 +00:00
|
|
|
|
from twisted.logger import Logger
|
|
|
|
|
|
2024-03-11 21:32:06 +00:00
|
|
|
|
from .binary_cache import S3BinaryCacheConfig
|
|
|
|
|
|
2023-12-04 18:44:13 +00:00
|
|
|
|
log = Logger()
|
|
|
|
|
|
2024-07-19 17:24:33 +00:00
|
|
|
|
FLAKE_TARGET_ATTRIBUTE_FOR_JOBS = "buildbotJobs"
|
2024-05-06 18:11:12 +00:00
|
|
|
|
|
2024-07-19 17:24:33 +00:00
|
|
|
|
@dataclass
|
|
|
|
|
class NixBuilder:
|
|
|
|
|
protocol: str
|
|
|
|
|
hostName: str
|
|
|
|
|
maxJobs: int
|
|
|
|
|
speedFactor: int = 1
|
|
|
|
|
# without base64
|
|
|
|
|
publicHostKey: str | None = None
|
|
|
|
|
sshUser: str | None = None
|
|
|
|
|
sshKey: str | None = None
|
2024-07-19 17:24:33 +00:00
|
|
|
|
systems: list[str] = field(default_factory=lambda: ["-"])
|
|
|
|
|
supportedFeatures: list[str] = field(default_factory=lambda: ["-"])
|
|
|
|
|
mandatoryFeatures: list[str] = field(default_factory=lambda: ["-"])
|
2024-07-19 17:24:33 +00:00
|
|
|
|
|
|
|
|
|
def to_nix_line(self):
|
|
|
|
|
encoded_public_key = base64.b64encode(self.publicHostKey.encode('ascii')).decode('ascii') if self.publicHostKey is not None else "-"
|
|
|
|
|
fullConnection = f"{self.protocol}://{self.sshUser}@{self.hostName}" if self.sshUser is not None else self.hostName
|
|
|
|
|
return f"{fullConnection} {",".join(self.systems)} {self.sshKey or "-"} {self.maxJobs} {self.speedFactor} {",".join(self.supportedFeatures)} {",".join(self.mandatoryFeatures)} {encoded_public_key}"
|
|
|
|
|
|
|
|
|
|
|
2024-05-06 17:52:55 +00:00
|
|
|
|
@dataclass
|
|
|
|
|
class OAuth2Config:
|
|
|
|
|
name: str
|
|
|
|
|
faIcon: str
|
|
|
|
|
resourceEndpoint: str
|
|
|
|
|
authUri: str
|
|
|
|
|
tokenUri: str
|
|
|
|
|
userinfoUri: str
|
|
|
|
|
sslVerify: bool = True
|
|
|
|
|
debug: bool = False
|
|
|
|
|
|
|
|
|
|
class KeycloakOAuth2Auth(OAuth2Auth):
|
|
|
|
|
def __init__(self, userinfoUri: str, *args, debug=False, **kwargs):
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
self.userinfoUri = userinfoUri
|
|
|
|
|
self.debug = debug
|
2023-09-10 08:11:56 +00:00
|
|
|
|
|
2024-06-09 15:01:32 +00:00
|
|
|
|
def createSessionFromToken(self, token):
|
|
|
|
|
s = requests.Session()
|
|
|
|
|
s.headers = {
|
|
|
|
|
'Authorization': 'Bearer ' + token['access_token'],
|
|
|
|
|
'User-Agent': f'buildbot/{buildbot.version}',
|
|
|
|
|
}
|
|
|
|
|
if self.debug:
|
|
|
|
|
log.info("Token obtained: {}".format(token))
|
|
|
|
|
s.verify = self.sslVerify
|
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
|
def getUserInfoFromOAuthClient(self, c):
|
2024-05-06 17:52:55 +00:00
|
|
|
|
userinfo_resp = c.get(self.userinfoUri)
|
|
|
|
|
log.info("Userinfo request to OAuth2: {}".format(userinfo_resp.status_code))
|
2024-06-09 15:01:32 +00:00
|
|
|
|
if userinfo_resp.status_code != 200:
|
2024-05-06 17:52:55 +00:00
|
|
|
|
log.error("Userinfo failure: {}".format(userinfo_resp.headers["www-authenticate"]))
|
|
|
|
|
userinfo_resp.raise_for_status()
|
2024-06-09 15:01:32 +00:00
|
|
|
|
userinfo_data = userinfo_resp.json()
|
|
|
|
|
return {
|
|
|
|
|
'groups': userinfo_data['buildbot_roles']
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-06 17:52:55 +00:00
|
|
|
|
|
|
|
|
|
def make_oauth2_method(oauth2_config: OAuth2Config):
|
|
|
|
|
"""
|
|
|
|
|
This constructs dynamically a class inheriting
|
|
|
|
|
an OAuth2 base configured using a dataclass.
|
|
|
|
|
"""
|
|
|
|
|
return type(f'{oauth2_config.name}DynamicOAuth2',
|
|
|
|
|
(KeycloakOAuth2Auth,),
|
|
|
|
|
oauth2_config.__dict__)
|
|
|
|
|
|
2023-12-26 20:56:36 +00:00
|
|
|
|
class BuildbotNixError(Exception):
|
|
|
|
|
pass
|
|
|
|
|
|
2024-03-02 14:59:44 +00:00
|
|
|
|
@dataclass
|
|
|
|
|
class GerritProject:
|
2024-03-02 16:34:52 +00:00
|
|
|
|
# `project` field.
|
2024-03-02 14:59:44 +00:00
|
|
|
|
name: str
|
2024-05-06 17:39:16 +00:00
|
|
|
|
# Private SSH key path to access Gerrit API
|
|
|
|
|
private_sshkey_path: str
|
2023-12-26 20:56:36 +00:00
|
|
|
|
|
2024-05-06 18:10:32 +00:00
|
|
|
|
@dataclass
|
|
|
|
|
class GerritConfig:
|
|
|
|
|
# Gerrit server domain
|
|
|
|
|
domain: str
|
|
|
|
|
port: int
|
|
|
|
|
username: str
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def repourl_template(self) -> str:
|
|
|
|
|
"""
|
|
|
|
|
Returns the prefix to build a repourl using that gerrit configuration.
|
|
|
|
|
"""
|
2024-07-19 21:04:20 +00:00
|
|
|
|
return f'ssh://{self.username}@{self.domain}:{self.port}/'
|
2024-05-06 18:10:32 +00:00
|
|
|
|
|
2024-03-10 21:27:24 +00:00
|
|
|
|
class BuildTrigger(steps.BuildStep):
|
2023-09-10 08:11:56 +00:00
|
|
|
|
def __init__(
|
2023-11-18 07:18:46 +00:00
|
|
|
|
self,
|
2024-03-15 13:47:49 +00:00
|
|
|
|
builds_scheduler_group: str,
|
2023-11-18 07:18:46 +00:00
|
|
|
|
jobs: list[dict[str, Any]],
|
2024-03-11 12:07:35 +00:00
|
|
|
|
all_deps: dict[str, Any],
|
2023-11-18 07:18:46 +00:00
|
|
|
|
**kwargs: Any,
|
2023-09-10 08:11:56 +00:00
|
|
|
|
) -> None:
|
|
|
|
|
self.jobs = jobs
|
2024-03-11 12:07:35 +00:00
|
|
|
|
self.all_deps = all_deps
|
2023-09-10 08:11:56 +00:00
|
|
|
|
self.config = None
|
2024-03-15 13:47:49 +00:00
|
|
|
|
self.builds_scheduler_group = builds_scheduler_group
|
2024-03-10 22:55:38 +00:00
|
|
|
|
self._result_list = []
|
|
|
|
|
self.ended = False
|
|
|
|
|
self.waitForFinishDeferred = None
|
2024-03-11 12:05:12 +00:00
|
|
|
|
self.brids = []
|
2024-07-20 15:07:30 +00:00
|
|
|
|
self.description = f"building {len(jobs)} jobs"
|
2024-03-10 22:55:38 +00:00
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
|
|
|
|
|
|
def interrupt(self, reason):
|
|
|
|
|
# We cancel the buildrequests, as the data api handles
|
|
|
|
|
# both cases:
|
|
|
|
|
# - build started: stop is sent,
|
|
|
|
|
# - build not created yet: related buildrequests are set to CANCELLED.
|
|
|
|
|
# Note that there is an identified race condition though (more details
|
|
|
|
|
# are available at buildbot.data.buildrequests).
|
|
|
|
|
for brid in self.brids:
|
|
|
|
|
self.master.data.control(
|
|
|
|
|
"cancel", {'reason': 'parent build was interrupted'}, ("buildrequests", brid)
|
|
|
|
|
)
|
|
|
|
|
if self.running and not self.ended:
|
|
|
|
|
self.ended = True
|
|
|
|
|
# if we are interrupted because of a connection lost, we interrupt synchronously
|
|
|
|
|
if self.build.conn is None and self.waitForFinishDeferred is not None:
|
|
|
|
|
self.waitForFinishDeferred.cancel()
|
2024-03-10 21:27:24 +00:00
|
|
|
|
|
|
|
|
|
def getSchedulerByName(self, name):
|
|
|
|
|
schedulers = self.master.scheduler_manager.namedServices
|
|
|
|
|
if name not in schedulers:
|
|
|
|
|
raise ValueError(f"unknown triggered scheduler: {repr(name)}")
|
|
|
|
|
sch = schedulers[name]
|
|
|
|
|
# todo: check ITriggerableScheduler
|
|
|
|
|
return sch
|
|
|
|
|
|
2024-05-06 19:03:58 +00:00
|
|
|
|
def schedule_one(self, build_props: Properties, job):
|
2024-07-22 20:42:35 +00:00
|
|
|
|
project_name = build_props.getProperty("event.refUpdate.project") or build_props.getProperty("event.change.project")
|
2024-07-20 16:01:27 +00:00
|
|
|
|
source = f"{project_name}-eval"
|
2024-03-10 21:27:24 +00:00
|
|
|
|
attr = job.get("attr", "eval-error")
|
2024-07-20 15:07:30 +00:00
|
|
|
|
name = f"buildbotJobs.{attr}"
|
2024-03-10 21:27:24 +00:00
|
|
|
|
error = job.get("error")
|
|
|
|
|
props = Properties()
|
|
|
|
|
props.setProperty("virtual_builder_name", name, source)
|
2024-07-20 15:07:30 +00:00
|
|
|
|
props.setProperty("status_name", f"building buildbotJobs.{attr}", source)
|
2024-03-10 21:27:24 +00:00
|
|
|
|
props.setProperty("virtual_builder_tags", "", source)
|
|
|
|
|
|
|
|
|
|
if error is not None:
|
|
|
|
|
props.setProperty("error", error, source)
|
2024-03-15 13:47:49 +00:00
|
|
|
|
return (f"{self.builds_scheduler_group}-other", props)
|
2024-03-10 21:27:24 +00:00
|
|
|
|
|
|
|
|
|
drv_path = job.get("drvPath")
|
|
|
|
|
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)
|
|
|
|
|
props.setProperty("isCached", job.get("isCached"), source)
|
|
|
|
|
|
2024-03-15 13:47:49 +00:00
|
|
|
|
return (f"{self.builds_scheduler_group}-{system}", props)
|
2024-03-10 21:27:24 +00:00
|
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2024-03-10 22:55:38 +00:00
|
|
|
|
def _add_results(self, brid):
|
2024-03-10 21:27:24 +00:00
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
|
def _is_buildrequest_complete(brid):
|
|
|
|
|
buildrequest = yield self.master.db.buildrequests.getBuildRequest(brid)
|
|
|
|
|
return buildrequest['complete']
|
|
|
|
|
|
|
|
|
|
event = ('buildrequests', str(brid), 'complete')
|
|
|
|
|
yield self.master.mq.waitUntilEvent(event, lambda: _is_buildrequest_complete(brid))
|
|
|
|
|
builds = yield self.master.db.builds.getBuilds(buildrequestid=brid)
|
|
|
|
|
for build in builds:
|
|
|
|
|
self._result_list.append(build["results"])
|
|
|
|
|
self.updateSummary()
|
|
|
|
|
|
2024-03-10 22:55:38 +00:00
|
|
|
|
def prepareSourcestampListForTrigger(self):
|
|
|
|
|
ss_for_trigger = {}
|
|
|
|
|
objs_from_build = self.build.getAllSourceStamps()
|
|
|
|
|
for ss in objs_from_build:
|
|
|
|
|
ss_for_trigger[ss.codebase] = ss.asDict()
|
|
|
|
|
|
|
|
|
|
trigger_values = [ss_for_trigger[k] for k in sorted(ss_for_trigger.keys())]
|
|
|
|
|
return trigger_values
|
|
|
|
|
|
2024-03-10 21:27:24 +00:00
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
|
def run(self):
|
2024-03-10 23:09:27 +00:00
|
|
|
|
self.running = True
|
2023-09-10 08:11:56 +00:00
|
|
|
|
build_props = self.build.getProperties()
|
2024-03-11 15:05:15 +00:00
|
|
|
|
logs: Log = yield self.addLog("build info")
|
2023-09-10 08:11:56 +00:00
|
|
|
|
|
2024-03-10 21:27:24 +00:00
|
|
|
|
builds_to_schedule = list(self.jobs)
|
|
|
|
|
build_schedule_order = []
|
2024-03-11 12:07:35 +00:00
|
|
|
|
sorter = graphlib.TopologicalSorter(self.all_deps)
|
2024-03-10 21:27:24 +00:00
|
|
|
|
for item in sorter.static_order():
|
|
|
|
|
i = 0
|
2024-03-10 22:55:38 +00:00
|
|
|
|
while i < len(builds_to_schedule):
|
2024-03-10 21:27:24 +00:00
|
|
|
|
if item == builds_to_schedule[i].get("drvPath"):
|
|
|
|
|
build_schedule_order.append(builds_to_schedule[i])
|
|
|
|
|
del builds_to_schedule[i]
|
|
|
|
|
else:
|
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
|
|
done = []
|
|
|
|
|
scheduled = []
|
2024-03-11 15:05:15 +00:00
|
|
|
|
failed = []
|
2024-03-10 22:55:38 +00:00
|
|
|
|
all_results = SUCCESS
|
|
|
|
|
ss_for_trigger = self.prepareSourcestampListForTrigger()
|
2024-03-10 23:09:27 +00:00
|
|
|
|
while not self.ended and (len(build_schedule_order) > 0 or len(scheduled) > 0):
|
2024-03-10 21:27:24 +00:00
|
|
|
|
schedule_now = []
|
|
|
|
|
for build in list(build_schedule_order):
|
2024-03-11 12:07:35 +00:00
|
|
|
|
if self.all_deps.get(build.get("drvPath"), []) == []:
|
2024-03-10 21:27:24 +00:00
|
|
|
|
build_schedule_order.remove(build)
|
|
|
|
|
schedule_now.append(build)
|
|
|
|
|
for job in schedule_now:
|
2024-03-11 15:05:15 +00:00
|
|
|
|
if job.get('isCached'):
|
|
|
|
|
logs.addStdout(f"Cached {job.get('attr')} ({job.get('drvPath')}) - skipping\n")
|
|
|
|
|
for dep in self.all_deps:
|
|
|
|
|
if job.get("drvPath") in self.all_deps[dep]:
|
|
|
|
|
self.all_deps[dep].remove(job.get("drvPath"))
|
|
|
|
|
continue
|
|
|
|
|
logs.addStdout(f"Scheduling {job.get('attr')} ({job.get('drvPath')})\n")
|
2024-03-10 21:27:24 +00:00
|
|
|
|
(scheduler, props) = self.schedule_one(build_props, job)
|
|
|
|
|
scheduler = self.getSchedulerByName(scheduler)
|
|
|
|
|
|
|
|
|
|
idsDeferred, resultsDeferred = scheduler.trigger(
|
|
|
|
|
waited_for = True,
|
|
|
|
|
sourcestamps = ss_for_trigger,
|
|
|
|
|
set_props = props,
|
|
|
|
|
parent_buildid = self.build.buildid,
|
|
|
|
|
parent_relationship = "Triggered from",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
brids = {}
|
|
|
|
|
try:
|
|
|
|
|
_, brids = yield idsDeferred
|
|
|
|
|
except Exception as e:
|
|
|
|
|
yield self.addLogWithException(e)
|
|
|
|
|
results = EXCEPTION
|
|
|
|
|
scheduled.append((job, brids, resultsDeferred))
|
|
|
|
|
|
|
|
|
|
for brid in brids.values():
|
|
|
|
|
url = getURLForBuildrequest(self.master, brid)
|
2024-03-10 22:55:38 +00:00
|
|
|
|
yield self.addURL(f"{scheduler.name} #{brid}", url)
|
2024-03-10 21:27:24 +00:00
|
|
|
|
self._add_results(brid)
|
2024-03-11 12:05:12 +00:00
|
|
|
|
self.brids.append(brid)
|
2024-03-11 15:05:15 +00:00
|
|
|
|
if len(scheduled) == 0:
|
|
|
|
|
if len(build_schedule_order) == 0:
|
|
|
|
|
logs.addStderr('Ran out of builds\n')
|
|
|
|
|
break
|
|
|
|
|
continue
|
2024-03-10 21:27:24 +00:00
|
|
|
|
wait_for_next = defer.DeferredList([results for _, _, results in scheduled], fireOnOneCallback = True, fireOnOneErrback=True)
|
2024-03-10 22:55:38 +00:00
|
|
|
|
self.waitForFinishDeferred = wait_for_next
|
2024-03-10 21:27:24 +00:00
|
|
|
|
results, index = yield wait_for_next
|
|
|
|
|
job, brids, _ = scheduled[index]
|
|
|
|
|
done.append((job, brids, results))
|
|
|
|
|
del scheduled[index]
|
2024-03-10 22:55:38 +00:00
|
|
|
|
result = results[0]
|
2024-03-11 15:05:15 +00:00
|
|
|
|
logs.addStdout(f'Build {job.get("attr")} ({job.get("drvPath")}) finished, result {util.Results[result].upper()}\n')
|
2024-03-10 22:55:38 +00:00
|
|
|
|
if result != SUCCESS:
|
|
|
|
|
failed_checks = []
|
2024-03-10 23:09:27 +00:00
|
|
|
|
failed_paths = [job.get('drvPath')]
|
2024-03-10 22:55:38 +00:00
|
|
|
|
removed = []
|
2024-03-11 15:05:15 +00:00
|
|
|
|
failed.append((
|
|
|
|
|
job.get("attr"),
|
|
|
|
|
"failed",
|
|
|
|
|
[ getURLForBuildrequest(self.master, brid) for brid in brids.values() ]
|
|
|
|
|
))
|
2024-03-10 22:55:38 +00:00
|
|
|
|
while True:
|
|
|
|
|
old_paths = list(failed_paths)
|
|
|
|
|
for build in list(build_schedule_order):
|
2024-03-11 12:07:35 +00:00
|
|
|
|
deps = self.all_deps.get(build.get("drvPath"), [])
|
2024-03-10 22:55:38 +00:00
|
|
|
|
for path in old_paths:
|
|
|
|
|
if path in deps:
|
|
|
|
|
failed_checks.append(build)
|
|
|
|
|
failed_paths.append(build.get("drvPath"))
|
|
|
|
|
build_schedule_order.remove(build)
|
|
|
|
|
removed.append(build.get("attr"))
|
2024-03-11 15:05:15 +00:00
|
|
|
|
failed.append((build.get("attr"), f"dependency {job.get('attr')} failed", []))
|
2024-03-10 22:55:38 +00:00
|
|
|
|
|
|
|
|
|
break
|
|
|
|
|
if old_paths == failed_paths:
|
|
|
|
|
break
|
2024-03-11 15:05:15 +00:00
|
|
|
|
if len(removed) > 3:
|
|
|
|
|
yield logs.addStdout(' Skipping jobs: ' + ', '.join(removed[:3]) + f', ... ({len(removed) - 3} more)\n')
|
|
|
|
|
else:
|
|
|
|
|
yield logs.addStdout(' Skipping jobs: ' + ', '.join(removed) + '\n')
|
2024-03-10 22:55:38 +00:00
|
|
|
|
all_results = worst_status(result, all_results)
|
2024-03-11 12:07:35 +00:00
|
|
|
|
for dep in self.all_deps:
|
|
|
|
|
if job.get("drvPath") in self.all_deps[dep]:
|
|
|
|
|
self.all_deps[dep].remove(job.get("drvPath"))
|
2024-03-11 15:05:15 +00:00
|
|
|
|
yield logs.addHeader('Done!\n')
|
|
|
|
|
yield logs.finish()
|
2024-05-06 19:03:58 +00:00
|
|
|
|
build_props.setProperty("failed_builds", failed, "nix-eval")
|
2024-03-10 23:09:27 +00:00
|
|
|
|
if self.ended:
|
|
|
|
|
return util.CANCELLED
|
2024-03-10 22:55:38 +00:00
|
|
|
|
return all_results
|
2023-09-10 08:11:56 +00:00
|
|
|
|
|
2023-10-12 21:01:07 +00:00
|
|
|
|
def getCurrentSummary(self) -> dict[str, str]: # noqa: N802
|
2023-09-10 08:11:56 +00:00
|
|
|
|
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)})"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
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]:
|
2024-05-06 18:11:12 +00:00
|
|
|
|
# run nix-eval-jobs --flake .#$FLAKE_TARGET_ATTRIBUTE_FOR_JOBS to generate the dict of stages
|
2023-09-10 08:11:56 +00:00
|
|
|
|
cmd: remotecommand.RemoteCommand = yield self.makeRemoteShellCommand()
|
2024-05-06 18:26:28 +00:00
|
|
|
|
build_props = self.build.getProperties()
|
2024-07-22 20:42:35 +00:00
|
|
|
|
project_name = build_props.getProperty("event.refUpdate.project") or build_props.getProperty("event.change.project")
|
|
|
|
|
assert project_name is not None, "`event.refUpdate.project` or `event.change.project` is not available on the build properties, unexpected build type!"
|
2024-05-06 18:26:28 +00:00
|
|
|
|
|
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)
|
|
|
|
|
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-20 15:07:30 +00:00
|
|
|
|
# Filter out failed evaluations
|
|
|
|
|
succeeded_jobs = [job for job in filtered_jobs if job.get('error') is None]
|
|
|
|
|
|
2024-03-10 20:27:45 +00:00
|
|
|
|
drv_show_log: Log = yield self.getLog("stdio")
|
2024-03-11 12:07:35 +00:00
|
|
|
|
all_deps = dict()
|
2024-05-06 17:39:35 +00:00
|
|
|
|
|
2024-03-11 12:07:35 +00:00
|
|
|
|
def closure_of(key, deps):
|
|
|
|
|
r, size = set([key]), 0
|
|
|
|
|
while len(r) != size:
|
|
|
|
|
size = len(r)
|
|
|
|
|
r.update(*[ deps[k] for k in r ])
|
|
|
|
|
return r.difference([key])
|
2024-05-06 17:39:35 +00:00
|
|
|
|
|
2024-07-20 15:07:30 +00:00
|
|
|
|
if succeeded_jobs:
|
|
|
|
|
drv_show_log.addStdout(f"getting derivation infos for valid derivations\n")
|
|
|
|
|
cmd = yield self.makeRemoteShellCommand(
|
|
|
|
|
stdioLogName=None,
|
|
|
|
|
collectStdout=True,
|
|
|
|
|
command=(
|
|
|
|
|
["nix", "derivation", "show", "--recursive"]
|
|
|
|
|
+ [ drv for drv in (job.get("drvPath") for job in succeeded_jobs) if drv ]
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
yield self.runCommand(cmd)
|
|
|
|
|
drv_show_log.addStdout(f"done\n")
|
|
|
|
|
try:
|
|
|
|
|
drv_info = json.loads(cmd.stdout)
|
|
|
|
|
except json.JSONDecodeError as e:
|
|
|
|
|
msg = f"Failed to parse `nix derivation show` output for {cmd.command}"
|
|
|
|
|
raise BuildbotNixError(msg) from e
|
|
|
|
|
for drv, info in drv_info.items():
|
|
|
|
|
all_deps[drv] = set(info.get("inputDrvs").keys())
|
|
|
|
|
|
|
|
|
|
job_set = set(( drv for drv in ( job.get("drvPath") for job in filtered_jobs ) if drv ))
|
|
|
|
|
all_deps = { k: list(closure_of(k, all_deps).intersection(job_set)) for k in job_set }
|
2024-03-10 20:27:45 +00:00
|
|
|
|
|
2023-09-10 08:11:56 +00:00
|
|
|
|
self.build.addStepsAfterCurrentStep(
|
2023-10-27 09:39:11 +00:00
|
|
|
|
[
|
|
|
|
|
BuildTrigger(
|
2024-05-06 18:26:28 +00:00
|
|
|
|
builds_scheduler_group=f"{project_name}-nix-build",
|
2023-11-18 07:18:46 +00:00
|
|
|
|
name="build flake",
|
|
|
|
|
jobs=filtered_jobs,
|
2024-03-11 12:07:35 +00:00
|
|
|
|
all_deps=all_deps,
|
2023-12-26 20:56:36 +00:00
|
|
|
|
),
|
|
|
|
|
],
|
2023-09-10 08:11:56 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
def __init__(self, **kwargs: Any) -> None:
|
|
|
|
|
kwargs = self.setupShellMixin(kwargs)
|
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
|
def run(self) -> Generator[Any, object, Any]:
|
2024-03-11 05:24:51 +00:00
|
|
|
|
if 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
|
|
|
|
|
|
|
|
|
|
if self.getProperty("isCached"):
|
|
|
|
|
yield self.addCompleteLog(
|
|
|
|
|
"cached outpath from previous builds",
|
|
|
|
|
# buildbot apparently hides the first line in the ui?
|
|
|
|
|
f'\n{self.getProperty("out_path")}\n')
|
|
|
|
|
return util.SKIPPED
|
|
|
|
|
|
2023-09-10 08:11:56 +00:00
|
|
|
|
# run `nix build`
|
|
|
|
|
cmd: remotecommand.RemoteCommand = yield self.makeRemoteShellCommand()
|
|
|
|
|
yield self.runCommand(cmd)
|
|
|
|
|
|
2024-03-10 16:34:22 +00:00
|
|
|
|
return cmd.results()
|
2023-09-10 08:11:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def nix_eval_config(
|
2024-05-06 18:10:32 +00:00
|
|
|
|
gerrit_config: GerritConfig,
|
2024-03-02 14:59:44 +00:00
|
|
|
|
project: GerritProject,
|
2023-09-10 08:11:56 +00:00
|
|
|
|
worker_names: list[str],
|
|
|
|
|
supported_systems: list[str],
|
2024-01-05 07:41:32 +00:00
|
|
|
|
eval_lock: util.MasterLock,
|
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:
|
2024-05-06 18:11:12 +00:00
|
|
|
|
"""Uses nix-eval-jobs to evaluate $FLAKE_TARGET_ATTRIBUTE_FOR_JOBS (`.#hydraJobs` by default) 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()
|
2024-07-19 21:38:51 +00:00
|
|
|
|
|
|
|
|
|
gerrit_private_key = None
|
|
|
|
|
with open(project.private_sshkey_path, 'r') as f:
|
|
|
|
|
gerrit_private_key = f.read()
|
|
|
|
|
|
|
|
|
|
if gerrit_private_key is None:
|
|
|
|
|
raise RuntimeError('No gerrit private key to fetch the repositories')
|
|
|
|
|
|
2023-09-10 08:11:56 +00:00
|
|
|
|
# check out the source
|
|
|
|
|
factory.addStep(
|
2024-03-02 14:59:44 +00:00
|
|
|
|
steps.Gerrit(
|
2024-05-06 18:10:32 +00:00
|
|
|
|
repourl=f'{gerrit_config.repourl_template}/{project.name}',
|
2024-03-02 14:59:44 +00:00
|
|
|
|
mode="full",
|
|
|
|
|
retry=[60, 60],
|
2024-03-02 20:35:59 +00:00
|
|
|
|
timeout=3600,
|
2024-07-19 21:38:51 +00:00
|
|
|
|
sshPrivateKey=gerrit_private_key
|
2023-12-26 20:56:36 +00:00
|
|
|
|
),
|
2023-09-10 08:11:56 +00:00
|
|
|
|
)
|
2024-07-19 21:38:51 +00:00
|
|
|
|
|
2024-03-11 05:40:14 +00:00
|
|
|
|
# use one gcroots directory per worker. this should be scoped to the largest unique resource
|
|
|
|
|
# in charge of builds (ie, buildnumber is too narrow) to not litter the system with permanent
|
|
|
|
|
# gcroots in case of worker restarts.
|
|
|
|
|
# TODO perhaps we should clean the entire /drvs/ directory up too during startup.
|
2024-01-15 12:41:48 +00:00
|
|
|
|
drv_gcroots_dir = util.Interpolate(
|
2024-03-11 05:40:14 +00:00
|
|
|
|
"/nix/var/nix/gcroots/per-user/buildbot-worker/%(prop:project)s/drvs/%(prop:workername)s/",
|
2024-01-15 12:41:48 +00:00
|
|
|
|
)
|
2023-09-10 08:11:56 +00:00
|
|
|
|
|
|
|
|
|
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),
|
|
|
|
|
"--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",
|
2024-05-06 18:11:12 +00:00
|
|
|
|
f".#{FLAKE_TARGET_ATTRIBUTE_FOR_JOBS}"
|
2023-09-10 08:11:56 +00:00
|
|
|
|
],
|
|
|
|
|
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,
|
|
|
|
|
],
|
2024-03-11 05:40:14 +00:00
|
|
|
|
alwaysRun=True,
|
2024-01-15 12:41:48 +00:00
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
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(
|
2024-03-02 14:59:44 +00:00
|
|
|
|
project: GerritProject,
|
2024-03-15 13:47:49 +00:00
|
|
|
|
worker_arch: str,
|
2023-09-10 08:11:56 +00:00
|
|
|
|
worker_names: list[str],
|
2024-07-19 17:24:33 +00:00
|
|
|
|
builders_spec: str,
|
2024-03-11 21:32:06 +00:00
|
|
|
|
signing_keyfile: str | None = None,
|
|
|
|
|
binary_cache_config: S3BinaryCacheConfig | None = None
|
2023-09-10 08:11:56 +00:00
|
|
|
|
) -> util.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",
|
2024-04-05 12:12:15 +00:00
|
|
|
|
# do not build directly on the coordinator
|
|
|
|
|
"--max-jobs", "0",
|
2023-12-22 13:37:31 +00:00
|
|
|
|
# stop stuck builds after 20 minutes
|
2024-07-19 17:24:33 +00:00
|
|
|
|
"--max-silent-time",
|
|
|
|
|
str(60 * 20),
|
2024-05-26 13:59:51 +00:00
|
|
|
|
# kill builds after two hours regardless of activity
|
2024-07-19 17:24:33 +00:00
|
|
|
|
"--timeout",
|
|
|
|
|
"7200",
|
|
|
|
|
"--builders",
|
|
|
|
|
builders_spec,
|
2023-09-10 08:11:56 +00:00
|
|
|
|
"--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,
|
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
|
|
|
|
)
|
2023-10-09 15:13:46 +00:00
|
|
|
|
|
2024-03-11 21:32:06 +00:00
|
|
|
|
if signing_keyfile is not None:
|
|
|
|
|
factory.addStep(
|
|
|
|
|
steps.ShellCommand(
|
|
|
|
|
name="Sign the store path",
|
|
|
|
|
command=[
|
|
|
|
|
"nix",
|
|
|
|
|
"store",
|
|
|
|
|
"sign",
|
2024-03-11 22:20:58 +00:00
|
|
|
|
"--key-file",
|
2024-03-11 21:32:06 +00:00
|
|
|
|
signing_keyfile,
|
|
|
|
|
util.Interpolate(
|
|
|
|
|
"%(prop:drv_path)s^*"
|
|
|
|
|
)
|
|
|
|
|
]
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if binary_cache_config is not None:
|
|
|
|
|
factory.addStep(
|
|
|
|
|
steps.ShellCommand(
|
|
|
|
|
name="Upload the store path to the cache",
|
|
|
|
|
command=[
|
|
|
|
|
"nix",
|
|
|
|
|
"copy",
|
|
|
|
|
"--to",
|
|
|
|
|
f"s3://{binary_cache_config.bucket}?profile={binary_cache_config.profile}®ion={binary_cache_config.region}&endpoint={binary_cache_config.endpoint}",
|
|
|
|
|
util.Property(
|
|
|
|
|
"out_path"
|
|
|
|
|
)
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
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-12-26 20:56:36 +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-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
|
|
|
|
)
|
2024-05-06 17:37:46 +00:00
|
|
|
|
|
2023-09-10 08:11:56 +00:00
|
|
|
|
return util.BuilderConfig(
|
2024-03-15 13:47:49 +00:00
|
|
|
|
name=f"{project.name}/nix-build/{worker_arch}",
|
2023-09-10 08:11:56 +00:00
|
|
|
|
project=project.name,
|
|
|
|
|
workernames=worker_names,
|
|
|
|
|
collapseRequests=False,
|
|
|
|
|
env={},
|
|
|
|
|
factory=factory,
|
|
|
|
|
)
|
2023-09-17 20:14:56 +00:00
|
|
|
|
|
2024-07-19 17:24:33 +00:00
|
|
|
|
def assemble_secret_file_path(secret_name: str) -> Path:
|
2023-09-17 20:14:56 +00:00
|
|
|
|
directory = os.environ.get("CREDENTIALS_DIRECTORY")
|
|
|
|
|
if directory is None:
|
|
|
|
|
print("directory not set", file=sys.stderr)
|
|
|
|
|
sys.exit(1)
|
2024-07-19 17:24:33 +00:00
|
|
|
|
return Path(directory).joinpath(secret_name)
|
2023-09-17 20:14:56 +00:00
|
|
|
|
|
2024-07-19 17:24:33 +00:00
|
|
|
|
def read_secret_file(secret_name: str) -> str:
|
|
|
|
|
return assemble_secret_file_path(secret_name).read_text().rstrip()
|
2023-09-17 20:14:56 +00:00
|
|
|
|
|
|
|
|
|
def config_for_project(
|
|
|
|
|
config: dict[str, Any],
|
2024-05-06 18:10:32 +00:00
|
|
|
|
gerrit_config: GerritConfig,
|
2024-03-02 14:59:44 +00:00
|
|
|
|
project: GerritProject,
|
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-01-05 07:41:32 +00:00
|
|
|
|
eval_lock: util.MasterLock,
|
2024-07-19 17:24:33 +00:00
|
|
|
|
builders_spec: str,
|
2024-03-11 21:32:06 +00:00
|
|
|
|
signing_keyfile: str | None = None,
|
|
|
|
|
binary_cache_config: S3BinaryCacheConfig | None = None
|
2023-09-17 20:14:56 +00:00
|
|
|
|
) -> Project:
|
|
|
|
|
config["projects"].append(Project(project.name))
|
|
|
|
|
config["schedulers"].extend(
|
|
|
|
|
[
|
2024-03-02 16:34:52 +00:00
|
|
|
|
# build everything pertaining to a project
|
|
|
|
|
# TODO(raito): will this catch also post-merge? we don't really care about that… do we?
|
2023-09-17 20:14:56 +00:00
|
|
|
|
schedulers.SingleBranchScheduler(
|
2024-03-02 16:34:52 +00:00
|
|
|
|
name=f"{project.name}-changes",
|
2023-09-17 20:14:56 +00:00
|
|
|
|
change_filter=util.ChangeFilter(
|
2024-03-02 16:34:52 +00:00
|
|
|
|
project=project.name,
|
2023-09-17 20:14:56 +00:00
|
|
|
|
),
|
|
|
|
|
builderNames=[f"{project.name}/nix-eval"],
|
|
|
|
|
),
|
|
|
|
|
# this is triggered from `nix-eval`
|
2024-03-15 13:47:49 +00:00
|
|
|
|
*(
|
|
|
|
|
schedulers.Triggerable(
|
|
|
|
|
name=f"{project.name}-nix-build-{arch}",
|
|
|
|
|
builderNames=[f"{project.name}/nix-build/{arch}"],
|
|
|
|
|
)
|
|
|
|
|
for arch in nix_supported_systems + [ "other" ]
|
2023-09-17 20:14:56 +00:00
|
|
|
|
),
|
|
|
|
|
# allow to manually trigger a nix-build
|
|
|
|
|
schedulers.ForceScheduler(
|
2024-03-02 16:34:52 +00:00
|
|
|
|
name=f"{project.name}-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-03-02 21:48:44 +00:00
|
|
|
|
label="Name of the Gerrit 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
|
|
|
|
)
|
2024-03-02 20:35:59 +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(
|
2024-05-06 18:10:32 +00:00
|
|
|
|
gerrit_config,
|
2023-09-17 20:14:56 +00:00
|
|
|
|
project,
|
2024-03-15 13:47:49 +00:00
|
|
|
|
[ f"{w}-other" for w in worker_names ],
|
2023-09-17 20:14:56 +00:00
|
|
|
|
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
|
|
|
|
),
|
2024-03-15 13:47:49 +00:00
|
|
|
|
*(
|
|
|
|
|
nix_build_config(
|
|
|
|
|
project,
|
|
|
|
|
arch,
|
|
|
|
|
[ f"{w}-{arch}" for w in worker_names ],
|
2024-07-19 17:24:33 +00:00
|
|
|
|
builders_spec,
|
2024-03-15 13:47:49 +00:00
|
|
|
|
signing_keyfile=signing_keyfile,
|
|
|
|
|
binary_cache_config=binary_cache_config
|
|
|
|
|
)
|
|
|
|
|
for arch in nix_supported_systems + [ "other" ]
|
2023-09-17 20:14:56 +00:00
|
|
|
|
),
|
2023-12-26 20:56:36 +00:00
|
|
|
|
],
|
2023-09-17 20:14:56 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2024-03-02 14:59:44 +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
|
2023-12-04 18:44:13 +00:00
|
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2024-03-02 14:59:44 +00:00
|
|
|
|
def activate(self) -> Generator[Any, object, Any]:
|
|
|
|
|
if self.run_on_startup:
|
|
|
|
|
yield self.setState("last_build", None)
|
|
|
|
|
yield super().activate()
|
2023-12-04 18:44:13 +00:00
|
|
|
|
|
2024-07-19 17:24:33 +00:00
|
|
|
|
def gerritReviewFmt(url, data):
|
|
|
|
|
if 'build' not in data:
|
|
|
|
|
raise ValueError('`build` is supposed to be present to format a build')
|
|
|
|
|
|
|
|
|
|
build = data['build']
|
|
|
|
|
if 'builder' not in build and 'name' not in build['builder']:
|
|
|
|
|
raise ValueError('either `builder` or `builder.name` is not present in the build dictionary, unexpected format request')
|
|
|
|
|
|
|
|
|
|
builderName = build['builder']['name']
|
|
|
|
|
|
|
|
|
|
if len(build['results']) != 1:
|
|
|
|
|
raise ValueError('this review request contains more than one build results, unexpected format request')
|
|
|
|
|
|
|
|
|
|
result = build['results'][0]
|
2024-03-02 14:59:44 +00:00
|
|
|
|
if result == util.RETRY:
|
|
|
|
|
return dict()
|
2023-12-04 18:44:13 +00:00
|
|
|
|
|
2024-05-06 18:26:42 +00:00
|
|
|
|
if builderName != f'{build["properties"].get("event.project")}/nix-eval':
|
2024-03-04 19:50:12 +00:00
|
|
|
|
return dict()
|
|
|
|
|
|
2024-03-11 15:05:15 +00:00
|
|
|
|
failed = build['properties'].get('failed_builds', [[]])[0]
|
2024-03-07 04:40:58 +00:00
|
|
|
|
|
2024-03-11 13:44:09 +00:00
|
|
|
|
labels = {
|
2024-03-18 00:07:34 +00:00
|
|
|
|
'Verified': -1 if result != util.SUCCESS else 1,
|
2024-03-11 13:44:09 +00:00
|
|
|
|
}
|
2024-03-07 04:40:58 +00:00
|
|
|
|
|
|
|
|
|
message = "Buildbot finished compiling your patchset!\n"
|
2024-03-02 14:59:44 +00:00
|
|
|
|
message += "The result is: %s\n" % util.Results[result].upper()
|
2024-03-07 04:40:58 +00:00
|
|
|
|
if result != util.SUCCESS:
|
2024-03-11 13:44:09 +00:00
|
|
|
|
message += "\nFailed checks:\n"
|
2024-03-11 15:05:15 +00:00
|
|
|
|
for check, how, urls in failed:
|
|
|
|
|
if not urls:
|
|
|
|
|
message += " "
|
2024-03-11 13:44:09 +00:00
|
|
|
|
message += f" - {check}: {how}"
|
|
|
|
|
if urls:
|
|
|
|
|
message += f" (see {', '.join(urls)})"
|
|
|
|
|
message += "\n"
|
2023-12-04 18:44:13 +00:00
|
|
|
|
|
2024-07-19 17:24:33 +00:00
|
|
|
|
if url:
|
2024-03-02 14:59:44 +00:00
|
|
|
|
message += "\nFor more details visit:\n"
|
|
|
|
|
message += build['url'] + "\n"
|
2023-12-04 18:44:13 +00:00
|
|
|
|
|
2024-03-07 04:40:58 +00:00
|
|
|
|
return dict(message=message, labels=labels)
|
2023-12-04 18:44:13 +00:00
|
|
|
|
|
2024-03-02 14:59:44 +00:00
|
|
|
|
class GerritNixConfigurator(ConfiguratorBase):
|
2023-09-17 20:14:56 +00:00
|
|
|
|
"""Janitor is a configurator which create a Janitor Builder with all needed Janitor steps"""
|
|
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
|
self,
|
2023-12-26 20:56:36 +00:00
|
|
|
|
# Shape of this file: [ { "name": "<worker-name>", "pass": "<worker-password>", "cores": "<cpu-cores>" } ]
|
2024-03-02 14:59:44 +00:00
|
|
|
|
gerrit_server: str,
|
|
|
|
|
gerrit_user: str,
|
|
|
|
|
gerrit_port: int,
|
2024-03-02 20:35:59 +00:00
|
|
|
|
gerrit_sshkey_path: str,
|
2024-05-06 17:39:16 +00:00
|
|
|
|
projects: list[str],
|
2023-10-12 13:59:26 +00:00
|
|
|
|
url: str,
|
2024-07-19 17:24:33 +00:00
|
|
|
|
allowed_origins: list[str],
|
2024-07-19 17:24:33 +00:00
|
|
|
|
nix_builders: list[dict[str, Any]],
|
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-12-26 20:56:36 +00:00
|
|
|
|
nix_workers_secret_name: str = "buildbot-nix-workers", # noqa: S107
|
2024-03-11 21:32:06 +00:00
|
|
|
|
signing_keyfile: str | None = None,
|
2024-05-06 12:26:32 +00:00
|
|
|
|
prometheus_config: dict[str, int | str] | None = None,
|
2024-03-11 21:32:06 +00:00
|
|
|
|
binary_cache_config: dict[str, str] | None = None,
|
2024-05-06 17:52:55 +00:00
|
|
|
|
auth_method: AuthBase | None = None,
|
2024-07-22 12:36:23 +00:00
|
|
|
|
manhole: Any = None,
|
2023-09-17 20:14:56 +00:00
|
|
|
|
) -> None:
|
|
|
|
|
super().__init__()
|
2024-07-22 12:36:23 +00:00
|
|
|
|
self.manhole = manhole
|
2024-07-19 17:24:33 +00:00
|
|
|
|
self.allowed_origins = allowed_origins
|
2024-03-02 14:59:44 +00:00
|
|
|
|
self.gerrit_server = gerrit_server
|
|
|
|
|
self.gerrit_user = gerrit_user
|
|
|
|
|
self.gerrit_port = gerrit_port
|
2024-07-19 21:01:17 +00:00
|
|
|
|
self.gerrit_sshkey_path = str(gerrit_sshkey_path)
|
2024-05-06 18:10:32 +00:00
|
|
|
|
self.gerrit_config = GerritConfig(domain=self.gerrit_server,
|
|
|
|
|
username=self.gerrit_user,
|
|
|
|
|
port=self.gerrit_port)
|
2024-05-06 17:39:16 +00:00
|
|
|
|
self.projects = projects
|
|
|
|
|
|
2023-09-17 20:14:56 +00:00
|
|
|
|
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
|
2024-07-19 17:24:33 +00:00
|
|
|
|
self.nix_builders: list[NixBuilder] = [NixBuilder(**builder_cfg) for builder_cfg in nix_builders]
|
2024-05-06 17:39:16 +00:00
|
|
|
|
|
2024-03-02 20:35:59 +00:00
|
|
|
|
self.gerrit_change_source = GerritChangeSource(gerrit_server, gerrit_user, gerritport=gerrit_port, identity_file=gerrit_sshkey_path)
|
2024-05-06 17:39:16 +00:00
|
|
|
|
|
2023-10-12 13:59:26 +00:00
|
|
|
|
self.url = url
|
2024-05-06 12:26:32 +00:00
|
|
|
|
self.prometheus_config = prometheus_config
|
2024-05-06 17:39:16 +00:00
|
|
|
|
|
2024-03-11 21:32:06 +00:00
|
|
|
|
if binary_cache_config is not None:
|
|
|
|
|
self.binary_cache_config = S3BinaryCacheConfig(**binary_cache_config)
|
|
|
|
|
else:
|
|
|
|
|
self.binary_cache_config = None
|
2024-05-06 17:39:16 +00:00
|
|
|
|
|
2024-03-11 21:32:06 +00:00
|
|
|
|
self.signing_keyfile = signing_keyfile
|
2023-09-17 20:14:56 +00:00
|
|
|
|
|
2024-05-06 17:52:55 +00:00
|
|
|
|
self.auth_method = auth_method
|
|
|
|
|
|
2023-09-17 20:14:56 +00:00
|
|
|
|
def configure(self, config: dict[str, Any]) -> None:
|
|
|
|
|
worker_config = json.loads(read_secret_file(self.nix_workers_secret_name))
|
|
|
|
|
worker_names = []
|
2023-11-06 06:46:07 +00:00
|
|
|
|
|
2024-07-22 12:36:23 +00:00
|
|
|
|
if self.manhole is not None:
|
|
|
|
|
config["manhole"] = self.manhole
|
|
|
|
|
|
2023-11-06 06:46:07 +00:00
|
|
|
|
config.setdefault("projects", [])
|
|
|
|
|
config.setdefault("secretsProviders", [])
|
2024-07-19 17:24:33 +00:00
|
|
|
|
config.setdefault("www", {
|
|
|
|
|
'allowed_origins': self.allowed_origins
|
|
|
|
|
})
|
2023-11-06 06:46:07 +00:00
|
|
|
|
|
2023-09-17 20:14:56 +00:00
|
|
|
|
for item in worker_config:
|
|
|
|
|
cores = item.get("cores", 0)
|
|
|
|
|
for i in range(cores):
|
2024-03-15 13:47:49 +00:00
|
|
|
|
for arch in self.nix_supported_systems + ["other"]:
|
|
|
|
|
worker_name = f"{item['name']}-{i:03}"
|
|
|
|
|
config["workers"].append(worker.Worker(f"{worker_name}-{arch}", item["pass"]))
|
|
|
|
|
worker_names.append(worker_name)
|
2023-09-17 20:14:56 +00:00
|
|
|
|
|
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 17:24:33 +00:00
|
|
|
|
builders_spec = " ; ".join(builder.to_nix_line() for builder in self.nix_builders)
|
2024-05-06 17:39:16 +00:00
|
|
|
|
for project in self.projects:
|
|
|
|
|
config_for_project(
|
|
|
|
|
config,
|
2024-05-06 18:10:32 +00:00
|
|
|
|
self.gerrit_config,
|
2024-05-06 17:39:16 +00:00
|
|
|
|
GerritProject(name=project, private_sshkey_path=self.gerrit_sshkey_path),
|
|
|
|
|
worker_names,
|
|
|
|
|
self.nix_supported_systems,
|
|
|
|
|
self.nix_eval_worker_count or multiprocessing.cpu_count(),
|
|
|
|
|
self.nix_eval_max_memory_size,
|
|
|
|
|
eval_lock,
|
2024-07-19 17:24:33 +00:00
|
|
|
|
builders_spec,
|
2024-05-06 17:39:16 +00:00
|
|
|
|
signing_keyfile=self.signing_keyfile,
|
|
|
|
|
binary_cache_config=self.binary_cache_config
|
|
|
|
|
)
|
2023-10-12 13:59:26 +00:00
|
|
|
|
|
2024-03-02 15:23:03 +00:00
|
|
|
|
config["change_source"] = self.gerrit_change_source
|
2024-03-03 01:32:05 +00:00
|
|
|
|
config["services"].append(
|
|
|
|
|
reporters.GerritStatusPush(self.gerrit_server, self.gerrit_user,
|
2024-05-06 17:39:16 +00:00
|
|
|
|
port=self.gerrit_port,
|
|
|
|
|
identity_file=self.gerrit_sshkey_path,
|
2024-07-19 17:24:33 +00:00
|
|
|
|
generators=[
|
|
|
|
|
# gerritReviewCB / self.url
|
|
|
|
|
BuildStatusGenerator(
|
|
|
|
|
message_formatter=MessageFormatterFunction(
|
|
|
|
|
lambda data: gerritReviewFmt(self.url, data),
|
|
|
|
|
"plain",
|
|
|
|
|
want_properties=True,
|
|
|
|
|
want_steps=True
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
])
|
2024-05-06 16:27:38 +00:00
|
|
|
|
# startCB, summaryCB are too noisy, we won't use them.
|
2024-03-03 01:32:05 +00:00
|
|
|
|
)
|
2023-12-05 20:52:25 +00:00
|
|
|
|
|
2024-05-06 12:26:32 +00:00
|
|
|
|
if self.prometheus_config is not None:
|
|
|
|
|
config['services'].append(reporters.Prometheus(port=self.prometheus_config.get('port', 9100), interface=self.prometheus_config.get('address', '')))
|
|
|
|
|
|
2024-05-06 17:57:12 +00:00
|
|
|
|
# Upstream defaults pretend they already do something similar
|
|
|
|
|
# but they didn't work, hence the custom function.
|
2024-03-28 02:52:13 +00:00
|
|
|
|
def gerritBranchKey(b):
|
|
|
|
|
ref = b['branch']
|
|
|
|
|
if not ref.startswith('refs/changes/'):
|
|
|
|
|
return ref
|
|
|
|
|
return ref.rsplit('/', 1)[0]
|
2024-05-06 17:39:16 +00:00
|
|
|
|
|
2024-03-28 02:52:13 +00:00
|
|
|
|
config["services"].append(
|
|
|
|
|
util.OldBuildCanceller(
|
2024-05-06 17:57:12 +00:00
|
|
|
|
"build_canceller",
|
2024-03-28 02:52:13 +00:00
|
|
|
|
filters=[
|
|
|
|
|
(
|
|
|
|
|
[
|
2024-05-06 17:57:12 +00:00
|
|
|
|
f"{project}/nix-{kind}"
|
2024-03-28 02:52:13 +00:00
|
|
|
|
for kind in [ "eval" ] + [
|
|
|
|
|
f"build/{arch}"
|
|
|
|
|
for arch in self.nix_supported_systems + [ "other" ]
|
|
|
|
|
]
|
|
|
|
|
],
|
2024-05-06 17:57:12 +00:00
|
|
|
|
util.SourceStampFilter(project_eq=[project])
|
2024-03-28 02:52:13 +00:00
|
|
|
|
)
|
2024-05-06 17:57:12 +00:00
|
|
|
|
for project in self.projects
|
2024-03-28 02:52:13 +00:00
|
|
|
|
],
|
|
|
|
|
branch_key=gerritBranchKey
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
2023-09-17 20:14:56 +00:00
|
|
|
|
systemd_secrets = 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", {})
|
|
|
|
|
|
2024-06-09 15:01:32 +00:00
|
|
|
|
if "authz" not in config["www"]:
|
|
|
|
|
config["www"]["authz"] = util.Authz(
|
|
|
|
|
allowRules=[
|
|
|
|
|
util.AnyEndpointMatcher(role="admin", defaultDeny=False),
|
|
|
|
|
util.StopBuildEndpointMatcher(role="owner"),
|
|
|
|
|
util.AnyControlEndpointMatcher(role="admin"),
|
|
|
|
|
],
|
|
|
|
|
roleMatchers=[
|
|
|
|
|
# A user must have buildbot-<something> to have the role <something>
|
|
|
|
|
# e.g. buildbot-admin to be admin.
|
|
|
|
|
util.RolesFromGroups(groupPrefix="buildbot-"),
|
|
|
|
|
util.RolesFromOwner(role="owner")
|
|
|
|
|
],
|
|
|
|
|
)
|
2024-05-06 17:52:55 +00:00
|
|
|
|
|
|
|
|
|
if "auth" not in config["www"] and self.auth_method is not None:
|
|
|
|
|
config["www"]["auth"] = self.auth_method
|