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