summaryrefslogtreecommitdiffstats
path: root/krebs/3modules/buildbot/slave.nix
blob: d877b9911f55045895f24e0e1b6a1b0044b573bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
{ config, pkgs, lib, ... }:

with import <stockholm/lib>;
let
  buildbot-slave-init = pkgs.writeText "buildbot-slave.tac" ''
    import os

    from buildslave.bot import BuildSlave
    from twisted.application import service

    basedir = '${cfg.workDir}'
    rotateLength = 10000000
    maxRotatedFiles = 10

    application = service.Application('buildslave')

    from twisted.python.logfile import LogFile
    from twisted.python.log import ILogObserver, FileLogObserver
    logfile = LogFile.fromFullPath(os.path.join(basedir, "twistd.log"), rotateLength=rotateLength,
                                  maxRotatedFiles=maxRotatedFiles)
    application.setComponent(ILogObserver, FileLogObserver(logfile).emit)

    buildmaster_host = '${cfg.masterhost}'
    # TODO: masterport?
    port = 9989
    slavename = '${cfg.username}'
    passwd = '${cfg.password}'
    keepalive = 600
    usepty = 0
    umask = None
    maxdelay = 300
    allow_shutdown = None

    ${cfg.extraConfig}

    s = BuildSlave(buildmaster_host, port, slavename, passwd, basedir,
                  keepalive, usepty, umask=umask, maxdelay=maxdelay,
                  allow_shutdown=allow_shutdown)
    s.setServiceParent(application)
    '';
  default-packages = [ pkgs.git pkgs.bash ];
  cfg = config.krebs.buildbot.slave;

  api = {
    enable = mkEnableOption "Buildbot Slave";

    workDir = mkOption {
      default = "/var/lib/buildbot/slave";
      type = types.str;
      description = ''
        Path to build bot slave directory.
        Will be created on startup.
      '';
    };

    masterhost = mkOption {
      default = "localhost";
      type = types.str;
      description = ''
        Hostname/IP of the buildbot master
      '';
    };

    username = mkOption {
      type = types.str;
      description = ''
        slavename used to authenticate with master
      '';
    };

    password = mkOption {
      type = types.str;
      description = ''
        slave password used to authenticate with master
      '';
    };

    contact = mkOption {
      default = "nix slave <buildslave@${config.networking.hostName}>";
      type = types.str;
      description = ''
        contact to be announced by buildslave
      '';
    };

    description = mkOption {
      default = "Nix Generated BuildSlave";
      type = types.str;
      description = ''
        description for hostto be announced by buildslave
      '';
    };

    packages = mkOption {
      default = [ pkgs.git ];
      type = with types; listOf package;
      description = ''
        packages which should be in path for buildslave
      '';
    };

    extraEnviron = mkOption {
      default = {};
      example = {
        NIX_PATH = "nixpkgs=/path/to/my/nixpkgs";
      };
      type = types.attrsOf types.str;
      description = ''
        extra environment variables to be provided to the buildslave service
        if you need nixpkgs, e.g. for running nix-shell you can set NIX_PATH here.
      '';
    };

    extraConfig = mkOption {
      default = "";
      type = types.lines;
      example = ''
        port = 443
        keepalive = 600
      '';
      description = ''
        extra config evaluated before calling BuildSlave init in .tac file
      '';
    };
  };

  imp = {

    users.extraUsers.buildbotSlave = {
      uid = genid "buildbotSlave";
      description = "Buildbot Slave";
      home = cfg.workDir;
      createHome = false;
      isSystemUser = true;
    };

    users.extraGroups.buildbotSlave = {
      gid = 1408105834;
    };

    systemd.services."buildbotSlave-${cfg.username}-${cfg.masterhost}" = {
      description = "Buildbot Slave for ${cfg.username}@${cfg.masterhost}";
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];
      path = default-packages ++ cfg.packages;

      environment = {
          SSL_CERT_FILE = "${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt";
          NIX_REMOTE="daemon";
      } // cfg.extraEnviron;

      serviceConfig = let
        workdir = shell.escape cfg.workDir;
        contact = shell.escape cfg.contact;
        description = shell.escape cfg.description;
        # TODO:make this
      in {
        PermissionsStartOnly = true;
        Type = "forking";
        PIDFile = "${workdir}/twistd.pid";
        # TODO: maybe also prepare buildbot.tac?
        ExecStartPre = pkgs.writeDash "buildbot-master-init" ''
          set -efux
          mkdir -p ${workdir}/info
          cp ${buildbot-slave-init} ${workdir}/buildbot.tac
          echo ${contact} > ${workdir}/info/admin
          echo ${description} > ${workdir}/info/host

          chown buildbotSlave:buildbotSlave -R ${workdir}
          chmod 700 ${workdir}
        '';
        ExecStart = "${pkgs.buildbot-classic-slave}/bin/buildslave start ${workdir}";
        ExecStop = "${pkgs.buildbot-classic-slave}/bin/buildslave stop ${workdir}";
        PrivateTmp = "true";
        User = "buildbotSlave";
        Restart = "always";
        RestartSec = "10";
      };
    };
  };
in
{
  options.krebs.buildbot.slave = api;
  config = lib.mkIf cfg.enable imp;
}