aboutsummaryrefslogtreecommitdiff
path: root/nixops/modules/buildbot/default.nix
blob: 9b661f199628bb349bedf3163ce067ab205670a8 (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
{ lib, pkgs, pkgsNext, config, myconfig, mylibs, ... }:
let
  varDir = "/var/lib/buildbot";
  buildslist_src = mylibs.fetchedGitPrivate ./buildslist.json;
  buildslist_yarn = pkgsNext.yarn2nix.mkYarnModules {
    name = "buildslist-yarn-modules";
    packageJSON = "${buildslist_src.src}/package.json";
    yarnLock = "${buildslist_src.src}/yarn.lock";
  };
  buildslist_bower = pkgsNext.buildBowerComponents {
    name = "buildslist";
    generated = ./bower.nix;
    src = "${buildslist_src.src}/guanlecoja/";
  };

  buildslist = pkgsNext.python3Packages.buildPythonPackage rec {
    pname = "buildbot-buildslist";
    inherit (pkgsNext.buildbot-pkg) version;

    preConfigure = ''
      export HOME=$PWD
      cp -a ${buildslist_yarn}/node_modules .
      chmod -R u+w node_modules
      cp -a ${buildslist_bower}/bower_components ./libs
      chmod -R u+w libs
      '';
    propagatedBuildInputs = with pkgsNext.python3Packages; [
      (klein.overridePythonAttrs(old: { checkPhase = ""; }))
      buildbot-pkg
    ];
    nativeBuildInputs = with pkgsNext; [ yarn nodejs ];
    buildInputs = [ buildslist_yarn buildslist_bower ];

    doCheck = false;
    src = buildslist_src.src;
  };
  buildbot_common = pkgsNext.python3Packages.buildPythonPackage rec {
    name = "buildbot_common";
    src = ./common;
    format = "other";
    installPhase = ''
      mkdir -p $out/${pkgsNext.python3.pythonForBuild.sitePackages}
      cp -a $src $out/${pkgsNext.python3.pythonForBuild.sitePackages}/buildbot_common
      '';
  };
  buildbot = pkgsNext.python3Packages.buildbot-full.withPlugins ([ buildslist ]);
in
{
  options = {
    services.buildbot.enable = lib.mkOption {
      type = lib.types.bool;
      default = false;
      description = ''
        Whether to enable buildbot.
      '';
    };
  };

  config = lib.mkIf config.services.buildbot.enable {
    ids.uids.buildbot = myconfig.env.buildbot.user.uid;
    ids.gids.buildbot = myconfig.env.buildbot.user.gid;

    users.groups.buildbot.gid = config.ids.gids.buildbot;
    users.users.buildbot = {
      name = "buildbot";
      uid = config.ids.uids.buildbot;
      group = "buildbot";
      description = "Buildbot user";
      home = varDir;
    };

    services.myWebsites.tools.vhostConfs.git.extraConfig = lib.attrsets.mapAttrsToList (k: project: ''
        RedirectMatch permanent "^/buildbot/${project.name}$" "/buildbot/${project.name}/"
        RewriteEngine On
        RewriteRule ^/buildbot/${project.name}/ws(.*)$   unix:///run/buildbot/${project.name}.sock|ws://git.immae.eu/ws$1 [P,NE,QSA,L]
        ProxyPass /buildbot/${project.name}/             unix:///run/buildbot/${project.name}.sock|http://${project.name}-git.immae.eu/
        ProxyPassReverse /buildbot/${project.name}/      unix:///run/buildbot/${project.name}.sock|http://${project.name}-git.immae.eu/
        <Location /buildbot/${project.name}/>
          Use LDAPConnect
          Require ldap-group cn=users,ou=${project.name},cn=buildbot,ou=services,dc=immae,dc=eu

          SetEnvIf X-Url-Scheme https HTTPS=1
          ProxyPreserveHost On
        </Location>
        <Location /buildbot/${project.name}/change_hook/base>
          Require local
        </Location>
        '') myconfig.env.buildbot.projects;

    system.activationScripts = lib.attrsets.mapAttrs' (k: project: lib.attrsets.nameValuePair "buildbot-${project.name}" {
      deps = [ "users" "wrappers" ];
      text = let
        master-cfg = "${buildbot_common}/${pkgsNext.python3.pythonForBuild.sitePackages}/buildbot_common/master.cfg";
        buildbot_key = pkgs.writeText "buildbot_key" (builtins.readFile "${myconfig.privateFiles}/buildbot_ssh_key");
        tac_file = pkgs.writeText "buildbot.tac" ''
            import os

            from twisted.application import service
            from buildbot.master import BuildMaster

            basedir = '${varDir}/${project.name}'
            rotateLength = 10000000
            maxRotatedFiles = 10
            configfile = '${master-cfg}'

            # Default umask for server
            umask = None

            # if this is a relocatable tac file, get the directory containing the TAC
            if basedir == '.':
                import os
                basedir = os.path.abspath(os.path.dirname(__file__))

            # note: this line is matched against to check that this is a buildmaster
            # directory; do not edit it.
            application = service.Application('buildmaster')
            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)

            m = BuildMaster(basedir, configfile, umask)
            m.setServiceParent(application)
            m.log_rotation.rotateLength = rotateLength
            m.log_rotation.maxRotatedFiles = maxRotatedFiles
          '';
      in ''
      install -m 0755 -o buildbot -g buildbot -d /run/buildbot/
      install -m 0755 -o buildbot -g buildbot -d ${varDir}
      if [ ! -f ${varDir}/${project.name}/buildbot.tac ]; then
        $wrapperDir/sudo -u buildbot ${buildbot}/bin/buildbot create-master -c "${master-cfg}" "${varDir}/${project.name}"
        rm -f ${varDir}/${project.name}/master.cfg.sample
        rm -f ${varDir}/${project.name}/buildbot.tac
      fi
      ln -sf ${tac_file} ${varDir}/${project.name}/buildbot.tac
      install -Dm600 -o buildbot -g buildbot -T ${buildbot_key} ${varDir}/buildbot_key
      buildbot_secrets=${varDir}/${project.name}/secrets
      install -m 0600 -o buildbot -g buildbot -d $buildbot_secrets
      echo "${myconfig.env.buildbot.ldap.password}" > $buildbot_secrets/ldap
      ${builtins.concatStringsSep "\n" (lib.attrsets.mapAttrsToList
        (k: v: "echo ${lib.strings.escapeShellArg v} > $buildbot_secrets/${k}") project.secrets
      )}
      chown -R buildbot:buildbot $buildbot_secrets
      chmod -R u=rX,go=- $buildbot_secrets
      ${project.activationScript}
      '';
    }) myconfig.env.buildbot.projects;

    systemd.services = lib.attrsets.mapAttrs' (k: project: lib.attrsets.nameValuePair "buildbot-${project.name}" {
      description = "Buildbot Continuous Integration Server ${project.name}.";
      after = [ "network-online.target" ];
      wantedBy = [ "multi-user.target" ];
      path = project.packages pkgs ++ (project.pythonPackages buildbot.pythonModule pkgsNext);
      environment = let
        project_env = lib.attrsets.mapAttrs' (k: v: lib.attrsets.nameValuePair "BUILDBOT_${k}" v) project.environment;
        buildbot_config = pkgsNext.python3Packages.buildPythonPackage (rec {
          name = "buildbot_config-${project.name}";
          src = ./projects + "/${project.name}";
          format = "other";
          installPhase = ''
            mkdir -p $out/${pkgsNext.python3.pythonForBuild.sitePackages}
            cp -a $src $out/${pkgsNext.python3.pythonForBuild.sitePackages}/buildbot_config
            '';
        });
        HOME = "${varDir}/${project.name}";
        PYTHONPATH = "${buildbot.pythonModule.withPackages (self: project.pythonPackages self pkgsNext ++ [
          pkgsNext.python3Packages.treq pkgsNext.python3Packages.ldap3 buildbot
          pkgsNext.python3Packages.buildbot-worker
          buildbot_common buildbot_config
        ])}/${buildbot.pythonModule.sitePackages}${if project.pythonPathHome then ":${varDir}/${project.name}/.local/${pkgsNext.python3.pythonForBuild.sitePackages}" else ""}";
      in project_env // { inherit PYTHONPATH HOME; };

      serviceConfig = {
        Type = "forking";
        User = "buildbot";
        Group = "buildbot";
        WorkingDirectory = "${varDir}/${project.name}";
        ExecStart = "${buildbot}/bin/buildbot start";
      };
    }) myconfig.env.buildbot.projects;
  };
}