nixos/buildkite-agents: support multiple buildkite agents

This commit is contained in:
Yorick van Pelt 2020-01-23 17:19:53 +01:00
parent d7e67bf088
commit f003810989
No known key found for this signature in database
GPG Key ID: A36E70F9DC014A15
4 changed files with 54 additions and 48 deletions

View File

@ -253,7 +253,7 @@
./services/computing/slurm/slurm.nix ./services/computing/slurm/slurm.nix
./services/continuous-integration/buildbot/master.nix ./services/continuous-integration/buildbot/master.nix
./services/continuous-integration/buildbot/worker.nix ./services/continuous-integration/buildbot/worker.nix
./services/continuous-integration/buildkite-agent.nix ./services/continuous-integration/buildkite-agents.nix
./services/continuous-integration/hail.nix ./services/continuous-integration/hail.nix
./services/continuous-integration/hydra/default.nix ./services/continuous-integration/hydra/default.nix
./services/continuous-integration/gitlab-runner.nix ./services/continuous-integration/gitlab-runner.nix

View File

@ -3,7 +3,7 @@
with lib; with lib;
let let
cfg = config.services.buildkite-agent; cfg = config.services.buildkite-agents;
mkHookOption = { name, description, example ? null }: { mkHookOption = { name, description, example ? null }: {
inherit name; inherit name;
@ -15,7 +15,7 @@ let
}; };
mkHookOptions = hooks: listToAttrs (map mkHookOption hooks); mkHookOptions = hooks: listToAttrs (map mkHookOption hooks);
hooksDir = let hooksDir = cfg: let
mkHookEntry = name: value: '' mkHookEntry = name: value: ''
cat > $out/${name} <<'EOF' cat > $out/${name} <<'EOF'
#! ${pkgs.runtimeShell} #! ${pkgs.runtimeShell}
@ -29,12 +29,13 @@ let
${concatStringsSep "\n" (mapAttrsToList mkHookEntry (filterAttrs (n: v: v != null) cfg.hooks))} ${concatStringsSep "\n" (mapAttrsToList mkHookEntry (filterAttrs (n: v: v != null) cfg.hooks))}
''; '';
in buildkiteOptions = { name ? "", config, ... }: {
{
options = { options = {
services.buildkite-agent = { enable = mkOption {
enable = mkEnableOption "buildkite-agent"; default = true;
type = types.bool;
description = "Whether to enable this buildkite agent";
};
package = mkOption { package = mkOption {
default = pkgs.buildkite-agent; default = pkgs.buildkite-agent;
@ -44,7 +45,7 @@ in
}; };
dataDir = mkOption { dataDir = mkOption {
default = "/var/lib/buildkite-agent"; default = "/var/lib/buildkite-agent-${name}";
description = "The workdir for the agent"; description = "The workdir for the agent";
type = types.str; type = types.str;
}; };
@ -68,9 +69,9 @@ in
name = mkOption { name = mkOption {
type = types.str; type = types.str;
default = "%hostname-%n"; default = "%hostname-${name}-%n";
description = '' description = ''
The name of the agent. The name of the agent as seen in the buildkite dashboard.
''; '';
}; };
@ -166,11 +167,11 @@ in
hooksPath = mkOption { hooksPath = mkOption {
type = types.path; type = types.path;
default = hooksDir; default = hooksDir config;
defaultText = "generated from services.buildkite-agent.hooks"; defaultText = "generated from services.buildkite-agents.<name>.hooks";
description = '' description = ''
Path to the directory storing the hooks. Path to the directory storing the hooks.
Consider using <option>services.buildkite-agent.hooks.&lt;name&gt;</option> Consider using <option>services.buildkite-agents.&lt;name&gt;.hooks.&lt;name&gt;</option>
instead. instead.
''; '';
}; };
@ -184,24 +185,38 @@ in
}; };
}; };
}; };
enabledAgents = lib.filterAttrs (n: v: v.enable) cfg;
mapAgents = function: lib.mkMerge (lib.mapAttrsToList function enabledAgents);
in
{
options.services.buildkite-agents = mkOption {
type = types.attrsOf (types.submodule buildkiteOptions);
default = {};
description = ''
Attribute set of buildkite agents.
The attribute key is combined with the hostname and a unique integer to
create the final agent name. This can be overridden by setting the `name`
attribute.
'';
};
config = mkIf config.services.buildkite-agent.enable { config.users.users = mapAgents (name: cfg: {
users.users.buildkite-agent = { "buildkite-agent-${name}" = {
name = "buildkite-agent"; name = "buildkite-agent-${name}";
home = cfg.dataDir; home = cfg.dataDir;
createHome = true; createHome = true;
description = "Buildkite agent user"; description = "Buildkite agent user";
extraGroups = [ "keys" ]; extraGroups = [ "keys" ];
isSystemUser = true; isSystemUser = true;
}; };
});
environment.systemPackages = [ cfg.package ]; config.systemd.services = mapAgents (name: cfg: {
"buildkite-agent-${name}" =
systemd.services.buildkite-agent =
{ description = "Buildkite Agent"; { description = "Buildkite Agent";
wantedBy = [ "multi-user.target" ]; wantedBy = [ "multi-user.target" ];
after = [ "network.target" ]; after = [ "network.target" ];
path = cfg.runtimePackages ++ [ pkgs.coreutils ]; path = cfg.runtimePackages ++ [ cfg.package pkgs.coreutils ];
environment = config.networking.proxy.envVars // { environment = config.networking.proxy.envVars // {
HOME = cfg.dataDir; HOME = cfg.dataDir;
NIX_REMOTE = "daemon"; NIX_REMOTE = "daemon";
@ -230,8 +245,8 @@ in
''; '';
serviceConfig = serviceConfig =
{ ExecStart = "${cfg.package}/bin/buildkite-agent start --config /var/lib/buildkite-agent/buildkite-agent.cfg"; { ExecStart = "${cfg.package}/bin/buildkite-agent start --config ${cfg.dataDir}/buildkite-agent.cfg";
User = "buildkite-agent"; User = "buildkite-agent-${name}";
RestartSec = 5; RestartSec = 5;
Restart = "on-failure"; Restart = "on-failure";
TimeoutSec = 10; TimeoutSec = 10;
@ -240,22 +255,18 @@ in
KillMode = "mixed"; KillMode = "mixed";
}; };
}; };
});
assertions = [ config.assertions = mapAgents (name: cfg: [
{ assertion = cfg.hooksPath == hooksDir || all (v: v == null) (attrValues cfg.hooks); { assertion = cfg.hooksPath == hooksDir || all (v: v == null) (attrValues cfg.hooks);
message = '' message = ''
Options `services.buildkite-agent.hooksPath' and Options `services.buildkite-agents.${name}.hooksPath' and
`services.buildkite-agent.hooks.<name>' are mutually exclusive. `services.buildkite-agents.${name}.hooks.<name>' are mutually exclusive.
''; '';
} }
]; ]);
};
imports = [ imports = [
(mkRenamedOptionModule [ "services" "buildkite-agent" "token" ] [ "services" "buildkite-agent" "tokenPath" ]) (mkRemovedOptionModule [ "services" "buildkite-agent"] "services.buildkite-agent has been moved to an attribute set at services.buildkite-agents")
(mkRenamedOptionModule [ "services" "buildkite-agent" "openssh" "privateKey" ] [ "services" "buildkite-agent" "privateSshKeyPath" ])
(mkRenamedOptionModule [ "services" "buildkite-agent" "openssh" "privateKeyPath" ] [ "services" "buildkite-agent" "privateSshKeyPath" ])
(mkRemovedOptionModule [ "services" "buildkite-agent" "openssh" "publicKey" ] "SSH public keys aren't necessary to clone private repos.")
(mkRemovedOptionModule [ "services" "buildkite-agent" "openssh" "publicKeyPath" ] "SSH public keys aren't necessary to clone private repos.")
(mkRenamedOptionModule [ "services" "buildkite-agent" "meta-data"] [ "services" "buildkite-agent" "tags" ])
]; ];
} }

View File

@ -32,7 +32,7 @@ in
bees = handleTest ./bees.nix {}; bees = handleTest ./bees.nix {};
bind = handleTest ./bind.nix {}; bind = handleTest ./bind.nix {};
bittorrent = handleTest ./bittorrent.nix {}; bittorrent = handleTest ./bittorrent.nix {};
buildkite-agent = handleTest ./buildkite-agent.nix {}; buildkite-agents = handleTest ./buildkite-agents.nix {};
boot = handleTestOn ["x86_64-linux"] ./boot.nix {}; # syslinux is unsupported on aarch64 boot = handleTestOn ["x86_64-linux"] ./boot.nix {}; # syslinux is unsupported on aarch64
boot-stage1 = handleTest ./boot-stage1.nix {}; boot-stage1 = handleTest ./boot-stage1.nix {};
borgbackup = handleTest ./borgbackup.nix {}; borgbackup = handleTest ./borgbackup.nix {};

View File

@ -6,18 +6,13 @@ import ./make-test-python.nix ({ pkgs, ... }:
maintainers = [ flokli ]; maintainers = [ flokli ];
}; };
nodes = { machine = { pkgs, ... }: {
node1 = { pkgs, ... }: { services.buildkite-agents = {
services.buildkite-agent = { one = {
enable = true;
privateSshKeyPath = (import ./ssh-keys.nix pkgs).snakeOilPrivateKey; privateSshKeyPath = (import ./ssh-keys.nix pkgs).snakeOilPrivateKey;
tokenPath = (pkgs.writeText "my-token" "5678"); tokenPath = (pkgs.writeText "my-token" "5678");
}; };
}; two = {
# don't configure ssh key, run as a separate user
node2 = { pkgs, ...}: {
services.buildkite-agent = {
enable = true;
tokenPath = (pkgs.writeText "my-token" "1234"); tokenPath = (pkgs.writeText "my-token" "1234");
}; };
}; };
@ -28,9 +23,9 @@ import ./make-test-python.nix ({ pkgs, ... }:
# we can't wait on the unit to start up, as we obviously can't connect to buildkite, # we can't wait on the unit to start up, as we obviously can't connect to buildkite,
# but we can look whether files are set up correctly # but we can look whether files are set up correctly
node1.wait_for_file("/var/lib/buildkite-agent/buildkite-agent.cfg") machine.wait_for_file("/var/lib/buildkite-agent-one/buildkite-agent.cfg")
node1.wait_for_file("/var/lib/buildkite-agent/.ssh/id_rsa") machine.wait_for_file("/var/lib/buildkite-agent-one/.ssh/id_rsa")
node2.wait_for_file("/var/lib/buildkite-agent/buildkite-agent.cfg") machine.wait_for_file("/var/lib/buildkite-agent-two/buildkite-agent.cfg")
''; '';
}) })