Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate worker secret file #356

Merged
merged 3 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 31 additions & 28 deletions buildbot_nix/buildbot_nix/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1761,18 +1761,15 @@ def configure(self, config: dict[str, Any]) -> None:
for backend in backends.values():
projects += backend.load_projects()

worker_config = json.loads(self.config.nix_workers_secret)
worker_names = []

config.setdefault("projects", [])
config.setdefault("secretsProviders", [])
config.setdefault("www", {})

for item in worker_config:
cores = item.get("cores", 0)
for i in range(cores):
worker_name = f"{item['name']}-{i:03}"
config["workers"].append(worker.Worker(worker_name, item["pass"]))
worker_names = []
for w in self.config.nix_worker_secrets().workers:
for i in range(w.cores):
worker_name = f"{w.name}-{i:03}"
config["workers"].append(worker.Worker(worker_name, w.password))
worker_names.append(worker_name)

if worker_names == []:
Expand Down Expand Up @@ -1801,25 +1798,31 @@ def configure(self, config: dict[str, Any]) -> None:
# Hacky but we have no other hooks just now to run code on shutdown.
atexit.register(lambda: DB.close() if DB is not None else None)

succeeded_projects = []
for project in projects:
config_for_project(
config=config,
project=project,
worker_names=worker_names,
nix_supported_systems=self.config.build_systems,
nix_eval_worker_count=self.config.eval_worker_count
or multiprocessing.cpu_count(),
nix_eval_max_memory_size=self.config.eval_max_memory_size,
eval_lock=eval_lock,
post_build_steps=[
x.to_buildstep() for x in self.config.post_build_steps
],
job_report_limit=self.config.job_report_limit,
per_repo_effects_secrets=self.config.effects_per_repo_secrets,
failed_builds_db=DB,
branch_config_dict=self.config.branches,
outputs_path=self.config.outputs_path,
)
try:
config_for_project(
config=config,
project=project,
worker_names=worker_names,
nix_supported_systems=self.config.build_systems,
nix_eval_worker_count=self.config.eval_worker_count
or multiprocessing.cpu_count(),
nix_eval_max_memory_size=self.config.eval_max_memory_size,
eval_lock=eval_lock,
post_build_steps=[
x.to_buildstep() for x in self.config.post_build_steps
],
job_report_limit=self.config.job_report_limit,
per_repo_effects_secrets=self.config.effects_per_repo_secrets,
failed_builds_db=DB,
branch_config_dict=self.config.branches,
outputs_path=self.config.outputs_path,
)
except Exception: # noqa: BLE001
log.failure(f"Failed to configure project {project.name}")
else:
succeeded_projects.append(project)

config["workers"].extend(worker.LocalWorker(w) for w in SKIPPED_BUILDER_NAMES)

Expand Down Expand Up @@ -1862,7 +1865,7 @@ def configure(self, config: dict[str, Any]) -> None:
)

config.setdefault("change_source", [])
for project in projects:
for project in succeeded_projects:
change_source = project.create_change_source()
if change_source is not None:
config["change_source"].append(change_source)
Expand All @@ -1882,5 +1885,5 @@ def configure(self, config: dict[str, Any]) -> None:
config["www"]["authz"] = setup_authz(
admins=self.config.admins,
backends=list(backends.values()),
projects=projects,
projects=succeeded_projects,
)
22 changes: 19 additions & 3 deletions buildbot_nix/buildbot_nix/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import re
from collections.abc import Callable, Mapping
from enum import Enum
Expand All @@ -8,6 +9,7 @@
from pydantic import BaseModel, ConfigDict, Field, GetCoreSchemaHandler, TypeAdapter
from pydantic_core import CoreSchema, core_schema

from .errors import BuildbotNixError
from .secrets import read_secret_file


Expand Down Expand Up @@ -270,6 +272,16 @@ def do_update_outputs(self, default_branch: str, branch: str) -> bool:
return self.check_lookup(default_branch, branch, lambda bc: bc.update_outputs)


class Worker(BaseModel):
name: str
cores: int
password: str = Field(alias="pass")


class WorkerConfig(BaseModel):
workers: list[Worker]


class BuildbotNixConfig(BaseModel):
db_url: str
auth_backend: AuthBackendConfig
Expand All @@ -294,9 +306,13 @@ class BuildbotNixConfig(BaseModel):
effects_per_repo_secrets: dict[str, str]
branches: BranchConfigDict

@property
def nix_workers_secret(self) -> str:
return read_secret_file(self.nix_workers_secret_file)
def nix_worker_secrets(self) -> WorkerConfig:
try:
data = json.loads(read_secret_file(self.nix_workers_secret_file))
except json.JSONDecodeError as e:
msg = f"Failed to decode JSON from {self.nix_workers_secret_file}"
raise BuildbotNixError(msg) from e
return WorkerConfig(workers=data)

@property
def http_basic_auth_password(self) -> str:
Expand Down
2 changes: 1 addition & 1 deletion nix/checks/effects.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
nodes = {
# `self` here is set by using specialArgs in `lib.nix`
node1 =
{ self, pkgs, ... }:
{ pkgs, ... }:
{
environment.systemPackages = [
(pkgs.python3.pkgs.callPackage ../../nix/buildbot-effects.nix { })
Expand Down
1 change: 0 additions & 1 deletion nix/checks/worker.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
node1 =
{
self,
config,
pkgs,
...
}:
Expand Down
8 changes: 0 additions & 8 deletions nix/master.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
config,
pkgs,
lib,
options,
...
}:
let
Expand All @@ -20,13 +19,6 @@ let
check = x: x ? "_type" && x._type == "interpolate" && x ? "value";
};

interpolateToString =
value:
if lib.isAttrs value && value ? "_type" && value._type == "interpolate" then
"util.Interpolate(${builtins.toJSON value.value})"
else
builtins.toJSON value;

cleanUpRepoName =
name:
builtins.replaceStrings
Expand Down
2 changes: 1 addition & 1 deletion nix/nix-eval-jobs.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
curl,
nlohmann_json,
}:
nix-eval-jobs.overrideAttrs (oldAttrs: {
nix-eval-jobs.overrideAttrs (_oldAttrs: {
src = fetchFromGitHub {
owner = "nix-community";
repo = "nix-eval-jobs";
Expand Down