]> git.immae.eu Git - perso/Immae/Config/Nix.git/blame - nixops/modules/buildbot/default.nix
Move buildbot plugin buildslist to pkgs
[perso/Immae/Config/Nix.git] / nixops / modules / buildbot / default.nix
CommitLineData
587b9e34 1{ lib, pkgs, config, myconfig, mylibs, ... }:
9fb4205e
IB
2let
3 varDir = "/var/lib/buildbot";
caa08508 4 buildbot_common = pkgs.python3Packages.buildPythonPackage rec {
e2b96bf5
IB
5 name = "buildbot_common";
6 src = ./common;
9fb4205e
IB
7 format = "other";
8 installPhase = ''
caa08508
IB
9 mkdir -p $out/${pkgs.python3.pythonForBuild.sitePackages}
10 cp -a $src $out/${pkgs.python3.pythonForBuild.sitePackages}/buildbot_common
9fb4205e 11 '';
e2b96bf5 12 };
b798cf6d 13 buildbot = pkgs.python3Packages.buildbot-full;
9fb4205e
IB
14in
15{
16 options = {
17 services.buildbot.enable = lib.mkOption {
18 type = lib.types.bool;
19 default = false;
20 description = ''
21 Whether to enable buildbot.
22 '';
23 };
24 };
25
26 config = lib.mkIf config.services.buildbot.enable {
27 ids.uids.buildbot = myconfig.env.buildbot.user.uid;
28 ids.gids.buildbot = myconfig.env.buildbot.user.gid;
29
30 users.groups.buildbot.gid = config.ids.gids.buildbot;
31 users.users.buildbot = {
32 name = "buildbot";
33 uid = config.ids.uids.buildbot;
34 group = "buildbot";
35 description = "Buildbot user";
36 home = varDir;
6984f454 37 extraGroups = [ "keys" ];
9fb4205e
IB
38 };
39
40 services.myWebsites.tools.vhostConfs.git.extraConfig = lib.attrsets.mapAttrsToList (k: project: ''
41 RedirectMatch permanent "^/buildbot/${project.name}$" "/buildbot/${project.name}/"
42 RewriteEngine On
43 RewriteRule ^/buildbot/${project.name}/ws(.*)$ unix:///run/buildbot/${project.name}.sock|ws://git.immae.eu/ws$1 [P,NE,QSA,L]
44 ProxyPass /buildbot/${project.name}/ unix:///run/buildbot/${project.name}.sock|http://${project.name}-git.immae.eu/
45 ProxyPassReverse /buildbot/${project.name}/ unix:///run/buildbot/${project.name}.sock|http://${project.name}-git.immae.eu/
46 <Location /buildbot/${project.name}/>
47 Use LDAPConnect
e2b96bf5 48 Require ldap-group cn=users,ou=${project.name},cn=buildbot,ou=services,dc=immae,dc=eu
9fb4205e
IB
49
50 SetEnvIf X-Url-Scheme https HTTPS=1
51 ProxyPreserveHost On
52 </Location>
53 <Location /buildbot/${project.name}/change_hook/base>
85817848
IB
54 <RequireAny>
55 Require local
56 Require ldap-group cn=users,ou=${project.name},cn=buildbot,ou=services,dc=immae,dc=eu
ca330baa 57 Include /var/secrets/buildbot/${project.name}/webhook-httpd-include
85817848 58 </RequireAny>
9fb4205e
IB
59 </Location>
60 '') myconfig.env.buildbot.projects;
61
62 system.activationScripts = lib.attrsets.mapAttrs' (k: project: lib.attrsets.nameValuePair "buildbot-${project.name}" {
63 deps = [ "users" "wrappers" ];
6984f454
IB
64 text = ''
65 install -m 0755 -o buildbot -g buildbot -d /run/buildbot/
66 install -m 0755 -o buildbot -g buildbot -d ${varDir}
67 ${project.activationScript}
68 '';
69 }) myconfig.env.buildbot.projects;
70
1a718805 71 secrets.keys = (
6984f454
IB
72 lib.lists.flatten (
73 lib.attrsets.mapAttrsToList (k: project:
74 lib.attrsets.mapAttrsToList (k: v:
ca330baa 75 {
6984f454
IB
76 permissions = "0600";
77 user = "buildbot";
78 group = "buildbot";
79 text = v;
ca330baa 80 dest = "buildbot/${project.name}/${k}";
6984f454
IB
81 }
82 ) project.secrets
83 ++ [
ca330baa 84 {
6984f454
IB
85 permissions = "0600";
86 user = "wwwrun";
87 group = "wwwrun";
88 text = lib.optionalString (lib.attrsets.hasAttr "webhookTokens" project) ''
89 Require expr "req('Access-Key') in { ${builtins.concatStringsSep ", " (map (x: "'${x}'") project.webhookTokens)} }"
90 '';
ca330baa
IB
91 dest = "buildbot/${project.name}/webhook-httpd-include";
92 }
6984f454
IB
93 ]
94 ) myconfig.env.buildbot.projects
95 )
ca330baa
IB
96 ) ++ [
97 {
6984f454
IB
98 permissions = "0600";
99 user = "buildbot";
100 group = "buildbot";
101 text = myconfig.env.buildbot.ldap.password;
ca330baa
IB
102 dest = "buildbot/ldap";
103 }
104 {
6984f454
IB
105 permissions = "0600";
106 user = "buildbot";
107 group = "buildbot";
108 text = builtins.readFile "${myconfig.privateFiles}/buildbot_ssh_key";
ca330baa
IB
109 dest = "buildbot/ssh_key";
110 }
111 ];
6984f454
IB
112
113 systemd.services = lib.attrsets.mapAttrs' (k: project: lib.attrsets.nameValuePair "buildbot-${project.name}" {
114 description = "Buildbot Continuous Integration Server ${project.name}.";
ca330baa 115 after = [ "network-online.target" ];
6984f454
IB
116 wantedBy = [ "multi-user.target" ];
117 path = project.packages pkgs ++ (project.pythonPackages buildbot.pythonModule pkgs);
118 preStart = let
caa08508 119 master-cfg = "${buildbot_common}/${pkgs.python3.pythonForBuild.sitePackages}/buildbot_common/master.cfg";
e2b96bf5
IB
120 tac_file = pkgs.writeText "buildbot.tac" ''
121 import os
122
123 from twisted.application import service
124 from buildbot.master import BuildMaster
125
126 basedir = '${varDir}/${project.name}'
127 rotateLength = 10000000
128 maxRotatedFiles = 10
129 configfile = '${master-cfg}'
130
131 # Default umask for server
132 umask = None
133
134 # if this is a relocatable tac file, get the directory containing the TAC
135 if basedir == '.':
136 import os
137 basedir = os.path.abspath(os.path.dirname(__file__))
138
139 # note: this line is matched against to check that this is a buildmaster
140 # directory; do not edit it.
141 application = service.Application('buildmaster')
142 from twisted.python.logfile import LogFile
143 from twisted.python.log import ILogObserver, FileLogObserver
144 logfile = LogFile.fromFullPath(os.path.join(basedir, "twistd.log"), rotateLength=rotateLength,
145 maxRotatedFiles=maxRotatedFiles)
146 application.setComponent(ILogObserver, FileLogObserver(logfile).emit)
147
148 m = BuildMaster(basedir, configfile, umask)
149 m.setServiceParent(application)
150 m.log_rotation.rotateLength = rotateLength
151 m.log_rotation.maxRotatedFiles = maxRotatedFiles
152 '';
9fb4205e 153 in ''
9fb4205e 154 if [ ! -f ${varDir}/${project.name}/buildbot.tac ]; then
6984f454 155 ${buildbot}/bin/buildbot create-master -c "${master-cfg}" "${varDir}/${project.name}"
9fb4205e 156 rm -f ${varDir}/${project.name}/master.cfg.sample
e2b96bf5 157 rm -f ${varDir}/${project.name}/buildbot.tac
9fb4205e 158 fi
e2b96bf5 159 ln -sf ${tac_file} ${varDir}/${project.name}/buildbot.tac
ca330baa
IB
160 # different buildbots may be trying that simultaneously, add the || true to avoid complaining in case of race
161 install -Dm600 -o buildbot -g buildbot -T /var/secrets/buildbot/ssh_key ${varDir}/buildbot_key || true
9fb4205e 162 buildbot_secrets=${varDir}/${project.name}/secrets
6984f454 163 install -m 0700 -o buildbot -g buildbot -d $buildbot_secrets
ca330baa 164 install -Dm600 -o buildbot -g buildbot -T /var/secrets/buildbot/ldap $buildbot_secrets/ldap
9fb4205e 165 ${builtins.concatStringsSep "\n" (lib.attrsets.mapAttrsToList
ca330baa 166 (k: v: "install -Dm600 -o buildbot -g buildbot -T /var/secrets/buildbot/${project.name}/${k} $buildbot_secrets/${k}") project.secrets
9fb4205e 167 )}
9fb4205e 168 '';
9fb4205e
IB
169 environment = let
170 project_env = lib.attrsets.mapAttrs' (k: v: lib.attrsets.nameValuePair "BUILDBOT_${k}" v) project.environment;
caa08508 171 buildbot_config = pkgs.python3Packages.buildPythonPackage (rec {
9fb4205e 172 name = "buildbot_config-${project.name}";
e2b96bf5 173 src = ./projects + "/${project.name}";
9fb4205e
IB
174 format = "other";
175 installPhase = ''
caa08508
IB
176 mkdir -p $out/${pkgs.python3.pythonForBuild.sitePackages}
177 cp -a $src $out/${pkgs.python3.pythonForBuild.sitePackages}/buildbot_config
9fb4205e
IB
178 '';
179 });
180 HOME = "${varDir}/${project.name}";
caa08508 181 PYTHONPATH = "${buildbot.pythonModule.withPackages (self: project.pythonPackages self pkgs ++ [
256d607c 182 pkgs.python3Packages.wokkel
caa08508
IB
183 pkgs.python3Packages.treq pkgs.python3Packages.ldap3 buildbot
184 pkgs.python3Packages.buildbot-worker
9fb4205e 185 buildbot_common buildbot_config
caa08508 186 ])}/${buildbot.pythonModule.sitePackages}${if project.pythonPathHome then ":${varDir}/${project.name}/.local/${pkgs.python3.pythonForBuild.sitePackages}" else ""}";
9fb4205e
IB
187 in project_env // { inherit PYTHONPATH HOME; };
188
189 serviceConfig = {
190 Type = "forking";
191 User = "buildbot";
192 Group = "buildbot";
6984f454 193 SupplementaryGroups = "keys";
9fb4205e
IB
194 WorkingDirectory = "${varDir}/${project.name}";
195 ExecStart = "${buildbot}/bin/buildbot start";
196 };
197 }) myconfig.env.buildbot.projects;
198 };
199}