]> git.immae.eu Git - perso/Immae/Config/Nix.git/blob - systems/eldiron/buildbot/default.nix
Squash changes containing private information
[perso/Immae/Config/Nix.git] / systems / eldiron / buildbot / default.nix
1 { lib, pkgs, config, buildbot, ... }:
2 let
3 varDir = "/var/lib/buildbot";
4 bb-python = buildbot.pythonModule;
5 in
6 {
7 options = {
8 myServices.buildbot.enable = lib.mkOption {
9 type = lib.types.bool;
10 default = false;
11 description = ''
12 Whether to enable buildbot.
13 '';
14 };
15 };
16
17 config = lib.mkIf config.myServices.buildbot.enable {
18 myEnv.buildbot.projects.test = {
19 name = "test";
20 workerPort = config.myEnv.ports.buildbot_test;
21 packages = [ pkgs.git pkgs.gzip pkgs.openssh ];
22 pythonPathHome = false;
23 secrets = {
24 apprise_webhooks = builtins.concatStringsSep "\n" [
25 "{{ .apprise_webhooks.matrix_immae_eu_alert }}"
26 ];
27 notify_xmpp_password = "{{ .xmpp.notify_bot }}";
28 };
29 activationScript = ''
30 install -m 0755 -o buildbot -g buildbot -d /var/lib/ftp/release.immae.eu/test
31 '';
32 webhookTokens = [
33 "{{ .buildbot.webhookTokens.Immae }}"
34 "{{ .buildbot.webhookTokens.Immae }}"
35 ];
36 };
37
38 myServices.chatonsProperties.hostings.buildbot = {
39 file.datetime = "2022-08-21T10:37:00";
40 hosting = {
41 name = "Buildbot";
42 description = "Python-based continuous integration testing framework";
43 type = "INSTANCE";
44 website = "https://git.immae.eu";
45 logo = "https://www.buildbot.net/img/icon.png";
46 status.level = "OK";
47 status.description = "OK";
48 registration.load = "OPEN";
49 install.type = "PACKAGE";
50 guide.user = "https://www.immae.eu/docs/forge-logicielle.html";
51 };
52 software = {
53 name = "Buildbot";
54 website = "https://www.buildbot.net/";
55 license.url = "https://github.com/buildbot/buildbot/blob/master/LICENSE";
56 license.name = "GNU General Public License v2.0";
57 version = pkgs.buildbot.version;
58 source.url = "https://github.com/buildbot/buildbot";
59 };
60 };
61 nixpkgs.overlays = [
62 (self: super: {
63 follow-systemd-unit = self.writeScriptBin "follow-systemd-unit" ''
64 #!${self.stdenv.shell}
65
66 set -euo pipefail
67
68 service=$1
69 before_invocation_id=$2
70
71 get_id() {
72 systemctl show -p InvocationID --value "$service"
73 }
74
75 while [ "$(get_id)" = "$before_invocation_id" ]; do sleep 1; done
76
77 invocation_id="$(get_id)"
78 cursor="$(mktemp)"
79 trap "rm -f $cursor" EXIT
80
81 get_logs() {
82 journalctl --quiet --cursor-file=$cursor INVOCATION_ID=$invocation_id + _SYSTEMD_INVOCATION_ID=$invocation_id
83 }
84
85 while [ -n "$(systemctl show -p Job --value "$service")" ]; do
86 get_logs
87 done
88 get_logs
89 '';
90 })
91 ];
92 ids.uids.buildbot = config.myEnv.buildbot.user.uid;
93 ids.gids.buildbot = config.myEnv.buildbot.user.gid;
94
95 users.groups.buildbot.gid = config.ids.gids.buildbot;
96 users.users.buildbot = {
97 name = "buildbot";
98 uid = config.ids.uids.buildbot;
99 group = "buildbot";
100 description = "Buildbot user";
101 home = varDir;
102 extraGroups = [ "keys" "systemd-journal" ];
103 useDefaultShell = true;
104 openssh.authorizedKeys.keys = [ config.myEnv.buildbot.ssh_key.public ];
105 };
106
107 services.websites.env.tools.watchPaths = lib.attrsets.mapAttrsToList
108 (k: project: config.secrets.fullPaths."buildbot/${project.name}/webhook-httpd-include")
109 config.myEnv.buildbot.projects;
110
111 services.websites.env.tools.vhostConfs.git.extraConfig = lib.attrsets.mapAttrsToList (k: project: ''
112 RedirectMatch permanent "^/buildbot/${project.name}$" "/buildbot/${project.name}/"
113 RewriteEngine On
114 RewriteRule ^/buildbot/${project.name}/ws(.*)$ unix:///run/buildbot/${project.name}.sock|ws://git.immae.eu/ws$1 [P,NE,QSA,L]
115 ProxyPass /buildbot/${project.name}/ unix:///run/buildbot/${project.name}.sock|http://${project.name}-git.immae.eu/
116 ProxyPassReverse /buildbot/${project.name}/ unix:///run/buildbot/${project.name}.sock|http://${project.name}-git.immae.eu/
117 <Location /buildbot/${project.name}/>
118 Use LDAPConnect
119 Require ldap-group cn=users,ou=${project.name},cn=buildbot,ou=services,dc=immae,dc=eu
120
121 SetEnvIf X-Url-Scheme https HTTPS=1
122 ProxyPreserveHost On
123 </Location>
124 <Location /buildbot/${project.name}/change_hook/base>
125 <RequireAny>
126 Require local
127 Require ldap-group cn=users,ou=${project.name},cn=buildbot,ou=services,dc=immae,dc=eu
128 Include ${config.secrets.fullPaths."buildbot/${project.name}/webhook-httpd-include"}
129 </RequireAny>
130 </Location>
131 '') config.myEnv.buildbot.projects;
132
133 system.activationScripts = lib.attrsets.mapAttrs' (k: project: lib.attrsets.nameValuePair "buildbot-${project.name}" {
134 deps = [ "users" "wrappers" ];
135 text = ''
136 install -m 755 -o buildbot -g buildbot -d ${varDir}/${project.name}
137
138 ${project.activationScript}
139 '';
140 }) config.myEnv.buildbot.projects;
141
142 secrets.keys = lib.listToAttrs (
143 lib.lists.flatten (
144 lib.attrsets.mapAttrsToList (k: project:
145 lib.attrsets.mapAttrsToList (k: v:
146 (lib.nameValuePair "buildbot/${project.name}/${k}" {
147 permissions = "0600";
148 user = "buildbot";
149 group = "buildbot";
150 text = v;
151 })
152 ) project.secrets
153 ++ [
154 (lib.nameValuePair "buildbot/${project.name}/webhook-httpd-include" {
155 permissions = "0600";
156 user = "wwwrun";
157 group = "wwwrun";
158 text = lib.optionalString (project.webhookTokens != null) ''
159 Require expr "req('Access-Key') in { ${builtins.concatStringsSep ", " (map (x: "'${x}'") project.webhookTokens)} }"
160 '';
161 })
162 (lib.nameValuePair "buildbot/${project.name}/environment_file" {
163 permissions = "0600";
164 user = "buildbot";
165 group = "buildbot";
166 keyDependencies = [ (buildbot.buildbot_config project).src ] ++ project.secretsDeps;
167 text = let
168 project_env = with lib.attrsets;
169 mapAttrs' (k: v: nameValuePair "BUILDBOT_${k}" v) project.environment //
170 {
171 BUILDBOT_PROJECT_DIR = (buildbot.buildbot_config project).src;
172 BUILDBOT_WORKER_PORT = builtins.toString project.workerPort;
173 BUILDBOT_HOST = config.hostEnv.fqdn;
174 BUILDBOT_VIRT_URL = "qemu+ssh://libvirt@dilion.immae.eu/system";
175 };
176 in builtins.concatStringsSep "\n"
177 (lib.mapAttrsToList (envK: envV: "${envK}=${envV}") project_env);
178 })
179 ]
180 ) config.myEnv.buildbot.projects
181 )
182 ) // {
183 "buildbot/ldap" = {
184 permissions = "0600";
185 user = "buildbot";
186 group = "buildbot";
187 text = config.myEnv.buildbot.ldap.password;
188 };
189 "buildbot/worker_password" = {
190 permissions = "0600";
191 user = "buildbot";
192 group = "buildbot";
193 text = config.myEnv.buildbot.workerPassword;
194 };
195 "buildbot/ssh_key" = {
196 permissions = "0600";
197 user = "buildbot";
198 group = "buildbot";
199 text = config.myEnv.buildbot.ssh_key.private;
200 };
201 "buildbot/ssh_known_hosts" = {
202 permissions = "0644";
203 user = "buildbot";
204 group = "buildbot";
205 text = ''
206 git.immae.eu ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIFbhFTl2A2RJn5L51yxJM4XfCS2ZaiSX/jo9jFSdghF
207 eldiron ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIFbhFTl2A2RJn5L51yxJM4XfCS2ZaiSX/jo9jFSdghF
208 phare.normalesup.org ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIN2GomItXICXpCtCFRMT2xuerqx2nLMO/3mNUuWyzFr1
209 '';
210 };
211 };
212
213 services.filesWatcher = lib.attrsets.mapAttrs' (k: project: lib.attrsets.nameValuePair "buildbot-${project.name}" {
214 restart = true;
215 paths = [
216 config.secrets.fullPaths."buildbot/ldap"
217 config.secrets.fullPaths."buildbot/worker_password"
218 config.secrets.fullPaths."buildbot/ssh_key"
219 config.secrets.fullPaths."buildbot/${project.name}/environment_file"
220 ] ++ lib.attrsets.mapAttrsToList (k: v: config.secrets.fullPaths."buildbot/${project.name}/${k}") project.secrets;
221 }) config.myEnv.buildbot.projects;
222
223 systemd.slices.buildbot = {
224 description = "buildbot slice";
225 };
226
227 networking.firewall.allowedTCPPorts = lib.attrsets.mapAttrsToList (k: v: v.workerPort) config.myEnv.buildbot.projects;
228 systemd.services = lib.attrsets.mapAttrs' (k: project: lib.attrsets.nameValuePair "buildbot-${project.name}" {
229 description = "Buildbot Continuous Integration Server ${project.name}.";
230 after = [ "network-online.target" ];
231 wantedBy = [ "multi-user.target" ];
232 path = project.packages;
233 preStart = let
234 master-cfg = "${buildbot.buildbot_common}/${bb-python.pythonForBuild.sitePackages}/buildbot_common/master.cfg";
235 tac_file = pkgs.writeText "buildbot.tac" ''
236 import os
237
238 from twisted.application import service
239 from buildbot.master import BuildMaster
240
241 basedir = '${varDir}/${project.name}'
242 rotateLength = 10000000
243 maxRotatedFiles = 10
244 configfile = '${master-cfg}'
245
246 # Default umask for server
247 umask = None
248
249 # if this is a relocatable tac file, get the directory containing the TAC
250 if basedir == '.':
251 import os
252 basedir = os.path.abspath(os.path.dirname(__file__))
253
254 # note: this line is matched against to check that this is a buildmaster
255 # directory; do not edit it.
256 application = service.Application('buildmaster')
257 from twisted.python.logfile import LogFile
258 from twisted.python.log import ILogObserver, FileLogObserver
259 logfile = LogFile.fromFullPath(os.path.join(basedir, "twistd.log"), rotateLength=rotateLength,
260 maxRotatedFiles=maxRotatedFiles)
261 application.setComponent(ILogObserver, FileLogObserver(logfile).emit)
262
263 m = BuildMaster(basedir, configfile, umask)
264 m.setServiceParent(application)
265 m.log_rotation.rotateLength = rotateLength
266 m.log_rotation.maxRotatedFiles = maxRotatedFiles
267 '';
268 in ''
269 if [ ! -f ${varDir}/${project.name}/buildbot.tac ]; then
270 ${buildbot}/bin/buildbot create-master -c "${master-cfg}" "${varDir}/${project.name}"
271 rm -f ${varDir}/${project.name}/master.cfg.sample
272 rm -f ${varDir}/${project.name}/buildbot.tac
273 fi
274 ln -sf ${tac_file} ${varDir}/${project.name}/buildbot.tac
275 # different buildbots may be trying that simultaneously, add the || true to avoid complaining in case of race
276 install -Dm600 -o buildbot -g buildbot -T ${config.secrets.fullPaths."buildbot/ssh_key"} ${varDir}/buildbot_key || true
277 install -Dm600 -o buildbot -g buildbot -T ${config.secrets.fullPaths."buildbot/ssh_known_hosts"} ${varDir}/buildbot_hosts || true
278 buildbot_secrets=${varDir}/${project.name}/secrets
279 install -m 0700 -o buildbot -g buildbot -d $buildbot_secrets
280 install -Dm600 -o buildbot -g buildbot -T ${config.secrets.fullPaths."buildbot/ldap"} $buildbot_secrets/ldap
281 install -Dm600 -o buildbot -g buildbot -T ${config.secrets.fullPaths."buildbot/worker_password"} $buildbot_secrets/worker_password
282 ${builtins.concatStringsSep "\n" (lib.attrsets.mapAttrsToList
283 (k: v: "install -Dm600 -o buildbot -g buildbot -T ${config.secrets.fullPaths."buildbot/${project.name}/${k}"} $buildbot_secrets/${k}") project.secrets
284 )}
285 ${buildbot}/bin/buildbot upgrade-master ${varDir}/${project.name}
286 '';
287 environment = let
288 HOME = "${varDir}/${project.name}";
289 PYTHONPATH = "${bb-python.withPackages (self:
290 buildbot.common_packages self ++
291 [ (buildbot.buildbot_config project) ]
292 )}/${bb-python.sitePackages}${if project.pythonPathHome then ":${varDir}/${project.name}/.local/${bb-python.sitePackages}" else ""}";
293 in { inherit PYTHONPATH HOME; };
294
295 serviceConfig = {
296 Slice = "buildbot.slice";
297 Type = "forking";
298 User = "buildbot";
299 Group = "buildbot";
300 RuntimeDirectory = "buildbot";
301 RuntimeDirectoryPreserve = "yes";
302 StateDirectory = "buildbot";
303 SupplementaryGroups = "keys";
304 WorkingDirectory = "${varDir}/${project.name}";
305 ExecStart = "${buildbot}/bin/buildbot start";
306 EnvironmentFile = config.secrets.fullPaths."buildbot/${project.name}/environment_file";
307 };
308 }) config.myEnv.buildbot.projects;
309 };
310 }