summary refs log tree commit diff
path: root/nixos/modules/services/continuous-integration/buildbot/worker.nix
blob: 7613692f0a343ec53daa6846b97d7c9442be551b (plain) (blame)
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
186
187
# NixOS module for Buildbot Worker.

{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.buildbot-worker;

  python = cfg.package.pythonModule;

  tacFile = pkgs.writeText "aur-buildbot-worker.tac" ''
    import os
    from io import open

    from buildbot_worker.bot import Worker
    from twisted.application import service

    basedir = '${cfg.buildbotDir}'

    # note: this line is matched against to check that this is a worker
    # directory; do not edit it.
    application = service.Application('buildbot-worker')

    master_url_split = '${cfg.masterUrl}'.split(':')
    buildmaster_host = master_url_split[0]
    port = int(master_url_split[1])
    workername = '${cfg.workerUser}'

    with open('${cfg.workerPassFile}', 'r', encoding='utf-8') as passwd_file:
        passwd = passwd_file.read().strip('\r\n')
    keepalive = 600
    umask = None
    maxdelay = 300
    numcpus = None
    allow_shutdown = None

    s = Worker(buildmaster_host, port, workername, passwd, basedir,
               keepalive, umask=umask, maxdelay=maxdelay,
               numcpus=numcpus, allow_shutdown=allow_shutdown)
    s.setServiceParent(application)
  '';

in {
  options = {
    services.buildbot-worker = {

      enable = mkOption {
        type = types.bool;
        default = false;
        description = "Whether to enable the Buildbot Worker.";
      };

      user = mkOption {
        default = "bbworker";
        type = types.str;
        description = "User the buildbot Worker should execute under.";
      };

      group = mkOption {
        default = "bbworker";
        type = types.str;
        description = "Primary group of buildbot Worker user.";
      };

      extraGroups = mkOption {
        type = types.listOf types.str;
        default = [];
        description = "List of extra groups that the Buildbot Worker user should be a part of.";
      };

      home = mkOption {
        default = "/home/bbworker";
        type = types.path;
        description = "Buildbot home directory.";
      };

      buildbotDir = mkOption {
        default = "${cfg.home}/worker";
        type = types.path;
        description = "Specifies the Buildbot directory.";
      };

      workerUser = mkOption {
        default = "example-worker";
        type = types.str;
        description = "Specifies the Buildbot Worker user.";
      };

      workerPass = mkOption {
        default = "pass";
        type = types.str;
        description = "Specifies the Buildbot Worker password.";
      };

      workerPassFile = mkOption {
        type = types.path;
        description = "File used to store the Buildbot Worker password";
      };

      hostMessage = mkOption {
        default = null;
        type = types.nullOr types.str;
        description = "Description of this worker";
      };

      adminMessage = mkOption {
        default = null;
        type = types.nullOr types.str;
        description = "Name of the administrator of this worker";
      };

      masterUrl = mkOption {
        default = "localhost:9989";
        type = types.str;
        description = "Specifies the Buildbot Worker connection string.";
      };

      package = mkOption {
        type = types.package;
        default = pkgs.python3Packages.buildbot-worker;
        defaultText = "pkgs.python3Packages.buildbot-worker";
        description = "Package to use for buildbot worker.";
        example = literalExample "pkgs.python2Packages.buildbot-worker";
      };

      packages = mkOption {
        default = with pkgs; [ git ];
        example = literalExample "[ pkgs.git ]";
        type = types.listOf types.package;
        description = "Packages to add to PATH for the buildbot process.";
      };
    };
  };

  config = mkIf cfg.enable {
    services.buildbot-worker.workerPassFile = mkDefault (pkgs.writeText "buildbot-worker-password" cfg.workerPass);

    users.groups = optional (cfg.group == "bbworker") {
      bbworker = { };
    };

    users.users = optionalAttrs (cfg.user == "bbworker") {
      bbworker = {
        description = "Buildbot Worker User.";
        isNormalUser = true;
        createHome = true;
        home = cfg.home;
        group = cfg.group;
        extraGroups = cfg.extraGroups;
        useDefaultShell = true;
      };
    };

    systemd.services.buildbot-worker = {
      description = "Buildbot Worker.";
      after = [ "network.target" "buildbot-master.service" ];
      wantedBy = [ "multi-user.target" ];
      path = cfg.packages;
      environment.PYTHONPATH = "${python.withPackages (p: [ cfg.package ])}/${python.sitePackages}";

      preStart = ''
        mkdir -vp "${cfg.buildbotDir}/info"
        ${optionalString (cfg.hostMessage != null) ''
          ln -sf "${pkgs.writeText "buildbot-worker-host" cfg.hostMessage}" "${cfg.buildbotDir}/info/host"
        ''}
        ${optionalString (cfg.adminMessage != null) ''
          ln -sf "${pkgs.writeText "buildbot-worker-admin" cfg.adminMessage}" "${cfg.buildbotDir}/info/admin"
        ''}
      '';

      serviceConfig = {
        Type = "simple";
        User = cfg.user;
        Group = cfg.group;
        WorkingDirectory = cfg.home;

        # NOTE: call twistd directly with stdout logging for systemd
        ExecStart = "${python.pkgs.twisted}/bin/twistd --nodaemon --pidfile= --logfile - --python ${tacFile}";
      };

    };
  };

  meta.maintainers = with lib.maintainers; [ nand0p ];

}