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