]> git.immae.eu Git - perso/Immae/Config/Nix.git/blame - modules/private/buildbot/default.nix
Move secrets to flakes
[perso/Immae/Config/Nix.git] / modules / private / buildbot / default.nix
CommitLineData
ab8f306d 1{ lib, pkgs, config, ... }:
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 = {
8d213e2b 17 myServices.buildbot.enable = lib.mkOption {
9fb4205e
IB
18 type = lib.types.bool;
19 default = false;
20 description = ''
21 Whether to enable buildbot.
22 '';
23 };
24 };
25
8d213e2b 26 config = lib.mkIf config.myServices.buildbot.enable {
d2e703c5 27 services.duplyBackup.profiles.buildbot = {
6a8252b1 28 rootDir = varDir;
546864bc 29 remotes = [ "eriomem" "ovh" ];
6a8252b1 30 };
ab8f306d
IB
31 ids.uids.buildbot = config.myEnv.buildbot.user.uid;
32 ids.gids.buildbot = config.myEnv.buildbot.user.gid;
9fb4205e
IB
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;
6984f454 41 extraGroups = [ "keys" ];
9fb4205e
IB
42 };
43
29f8cb85 44 services.websites.env.tools.watchPaths = lib.attrsets.mapAttrsToList
da30ae4f 45 (k: project: config.secrets.fullPaths."buildbot/${project.name}/webhook-httpd-include")
ab8f306d 46 config.myEnv.buildbot.projects;
17f6eae9 47
29f8cb85 48 services.websites.env.tools.vhostConfs.git.extraConfig = lib.attrsets.mapAttrsToList (k: project: ''
9fb4205e
IB
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
e2b96bf5 56 Require ldap-group cn=users,ou=${project.name},cn=buildbot,ou=services,dc=immae,dc=eu
9fb4205e
IB
57
58 SetEnvIf X-Url-Scheme https HTTPS=1
59 ProxyPreserveHost On
60 </Location>
61 <Location /buildbot/${project.name}/change_hook/base>
85817848
IB
62 <RequireAny>
63 Require local
64 Require ldap-group cn=users,ou=${project.name},cn=buildbot,ou=services,dc=immae,dc=eu
da30ae4f 65 Include ${config.secrets.fullPaths."buildbot/${project.name}/webhook-httpd-include"}
85817848 66 </RequireAny>
9fb4205e 67 </Location>
ab8f306d 68 '') config.myEnv.buildbot.projects;
9fb4205e
IB
69
70 system.activationScripts = lib.attrsets.mapAttrs' (k: project: lib.attrsets.nameValuePair "buildbot-${project.name}" {
71 deps = [ "users" "wrappers" ];
8fa7ff2c
IB
72 text = ''
73 install -m 755 -o buildbot -g buildbot -d ${varDir}/${project.name}
74
75 ${project.activationScript}
76 '';
ab8f306d 77 }) config.myEnv.buildbot.projects;
6984f454 78
1a718805 79 secrets.keys = (
6984f454
IB
80 lib.lists.flatten (
81 lib.attrsets.mapAttrsToList (k: project:
82 lib.attrsets.mapAttrsToList (k: v:
ca330baa 83 {
6984f454
IB
84 permissions = "0600";
85 user = "buildbot";
86 group = "buildbot";
87 text = v;
ca330baa 88 dest = "buildbot/${project.name}/${k}";
6984f454
IB
89 }
90 ) project.secrets
91 ++ [
ca330baa 92 {
6984f454
IB
93 permissions = "0600";
94 user = "wwwrun";
95 group = "wwwrun";
ab8f306d 96 text = lib.optionalString (project.webhookTokens != null) ''
6984f454
IB
97 Require expr "req('Access-Key') in { ${builtins.concatStringsSep ", " (map (x: "'${x}'") project.webhookTokens)} }"
98 '';
ca330baa
IB
99 dest = "buildbot/${project.name}/webhook-httpd-include";
100 }
dcb8ad4c
IB
101 {
102 permissions = "0600";
103 user = "buildbot";
104 group = "buildbot";
105 dest = "buildbot/${project.name}/environment_file";
106 text = let
107 project_env = with lib.attrsets;
108 mapAttrs' (k: v: nameValuePair "BUILDBOT_${k}" v) project.environment //
109 mapAttrs' (k: v: nameValuePair "BUILDBOT_PATH_${k}" (v pkgs)) (attrByPath ["builderPaths"] {} project) //
200690c9
IB
110 {
111 BUILDBOT_PROJECT_DIR = ./projects + "/${project.name}";
112 BUILDBOT_WORKER_PORT = builtins.toString project.workerPort;
113 BUILDBOT_HOST = config.hostEnv.fqdn;
114 BUILDBOT_VIRT_URL = "qemu+ssh://libvirt@dilion.immae.eu/system";
115 };
dcb8ad4c
IB
116 in builtins.concatStringsSep "\n"
117 (lib.mapAttrsToList (envK: envV: "${envK}=${envV}") project_env);
118 }
6984f454 119 ]
ab8f306d 120 ) config.myEnv.buildbot.projects
6984f454 121 )
ca330baa
IB
122 ) ++ [
123 {
6984f454
IB
124 permissions = "0600";
125 user = "buildbot";
126 group = "buildbot";
ab8f306d 127 text = config.myEnv.buildbot.ldap.password;
ca330baa
IB
128 dest = "buildbot/ldap";
129 }
200690c9
IB
130 {
131 permissions = "0600";
132 user = "buildbot";
133 group = "buildbot";
134 text = config.myEnv.buildbot.workerPassword;
135 dest = "buildbot/worker_password";
136 }
ca330baa 137 {
6984f454
IB
138 permissions = "0600";
139 user = "buildbot";
140 group = "buildbot";
282c67a1 141 text = config.myEnv.buildbot.ssh_key.private;
ca330baa
IB
142 dest = "buildbot/ssh_key";
143 }
144 ];
6984f454 145
17f6eae9
IB
146 services.filesWatcher = lib.attrsets.mapAttrs' (k: project: lib.attrsets.nameValuePair "buildbot-${project.name}" {
147 restart = true;
148 paths = [
da30ae4f
IB
149 config.secrets.fullPaths."buildbot/ldap"
150 config.secrets.fullPaths."buildbot/worker_password"
151 config.secrets.fullPaths."buildbot/ssh_key"
152 config.secrets.fullPaths."buildbot/${project.name}/environment_file"
153 ] ++ lib.attrsets.mapAttrsToList (k: v: config.secrets.fullPaths."buildbot/${project.name}/${k}") project.secrets;
ab8f306d 154 }) config.myEnv.buildbot.projects;
17f6eae9 155
850adcf4
IB
156 systemd.slices.buildbot = {
157 description = "buildbot slice";
158 };
159
200690c9 160 networking.firewall.allowedTCPPorts = lib.attrsets.mapAttrsToList (k: v: v.workerPort) config.myEnv.buildbot.projects;
6984f454
IB
161 systemd.services = lib.attrsets.mapAttrs' (k: project: lib.attrsets.nameValuePair "buildbot-${project.name}" {
162 description = "Buildbot Continuous Integration Server ${project.name}.";
ca330baa 163 after = [ "network-online.target" ];
6984f454
IB
164 wantedBy = [ "multi-user.target" ];
165 path = project.packages pkgs ++ (project.pythonPackages buildbot.pythonModule pkgs);
166 preStart = let
caa08508 167 master-cfg = "${buildbot_common}/${pkgs.python3.pythonForBuild.sitePackages}/buildbot_common/master.cfg";
e2b96bf5
IB
168 tac_file = pkgs.writeText "buildbot.tac" ''
169 import os
170
171 from twisted.application import service
172 from buildbot.master import BuildMaster
173
174 basedir = '${varDir}/${project.name}'
175 rotateLength = 10000000
176 maxRotatedFiles = 10
177 configfile = '${master-cfg}'
178
179 # Default umask for server
180 umask = None
181
182 # if this is a relocatable tac file, get the directory containing the TAC
183 if basedir == '.':
184 import os
185 basedir = os.path.abspath(os.path.dirname(__file__))
186
187 # note: this line is matched against to check that this is a buildmaster
188 # directory; do not edit it.
189 application = service.Application('buildmaster')
190 from twisted.python.logfile import LogFile
191 from twisted.python.log import ILogObserver, FileLogObserver
192 logfile = LogFile.fromFullPath(os.path.join(basedir, "twistd.log"), rotateLength=rotateLength,
193 maxRotatedFiles=maxRotatedFiles)
194 application.setComponent(ILogObserver, FileLogObserver(logfile).emit)
195
196 m = BuildMaster(basedir, configfile, umask)
197 m.setServiceParent(application)
198 m.log_rotation.rotateLength = rotateLength
199 m.log_rotation.maxRotatedFiles = maxRotatedFiles
200 '';
9fb4205e 201 in ''
9fb4205e 202 if [ ! -f ${varDir}/${project.name}/buildbot.tac ]; then
6984f454 203 ${buildbot}/bin/buildbot create-master -c "${master-cfg}" "${varDir}/${project.name}"
9fb4205e 204 rm -f ${varDir}/${project.name}/master.cfg.sample
e2b96bf5 205 rm -f ${varDir}/${project.name}/buildbot.tac
9fb4205e 206 fi
e2b96bf5 207 ln -sf ${tac_file} ${varDir}/${project.name}/buildbot.tac
ca330baa 208 # different buildbots may be trying that simultaneously, add the || true to avoid complaining in case of race
da30ae4f 209 install -Dm600 -o buildbot -g buildbot -T ${config.secrets.fullPaths."buildbot/ssh_key"} ${varDir}/buildbot_key || true
9fb4205e 210 buildbot_secrets=${varDir}/${project.name}/secrets
6984f454 211 install -m 0700 -o buildbot -g buildbot -d $buildbot_secrets
da30ae4f
IB
212 install -Dm600 -o buildbot -g buildbot -T ${config.secrets.fullPaths."buildbot/ldap"} $buildbot_secrets/ldap
213 install -Dm600 -o buildbot -g buildbot -T ${config.secrets.fullPaths."buildbot/worker_password"} $buildbot_secrets/worker_password
9fb4205e 214 ${builtins.concatStringsSep "\n" (lib.attrsets.mapAttrsToList
da30ae4f 215 (k: v: "install -Dm600 -o buildbot -g buildbot -T ${config.secrets.fullPaths."buildbot/${project.name}/${k}"} $buildbot_secrets/${k}") project.secrets
9fb4205e 216 )}
5400b9b6 217 ${buildbot}/bin/buildbot upgrade-master ${varDir}/${project.name}
9fb4205e 218 '';
9fb4205e 219 environment = let
caa08508 220 buildbot_config = pkgs.python3Packages.buildPythonPackage (rec {
9fb4205e 221 name = "buildbot_config-${project.name}";
e2b96bf5 222 src = ./projects + "/${project.name}";
9fb4205e
IB
223 format = "other";
224 installPhase = ''
caa08508
IB
225 mkdir -p $out/${pkgs.python3.pythonForBuild.sitePackages}
226 cp -a $src $out/${pkgs.python3.pythonForBuild.sitePackages}/buildbot_config
9fb4205e
IB
227 '';
228 });
229 HOME = "${varDir}/${project.name}";
caa08508 230 PYTHONPATH = "${buildbot.pythonModule.withPackages (self: project.pythonPackages self pkgs ++ [
200690c9 231 pkgs.python3Packages.libvirt
256d607c 232 pkgs.python3Packages.wokkel
caa08508
IB
233 pkgs.python3Packages.treq pkgs.python3Packages.ldap3 buildbot
234 pkgs.python3Packages.buildbot-worker
9fb4205e 235 buildbot_common buildbot_config
caa08508 236 ])}/${buildbot.pythonModule.sitePackages}${if project.pythonPathHome then ":${varDir}/${project.name}/.local/${pkgs.python3.pythonForBuild.sitePackages}" else ""}";
dcb8ad4c 237 in { inherit PYTHONPATH HOME; };
9fb4205e
IB
238
239 serviceConfig = {
850adcf4 240 Slice = "buildbot.slice";
9fb4205e
IB
241 Type = "forking";
242 User = "buildbot";
243 Group = "buildbot";
81b9ff89
IB
244 RuntimeDirectory = "buildbot";
245 RuntimeDirectoryPreserve = "yes";
246 StateDirectory = "buildbot";
6984f454 247 SupplementaryGroups = "keys";
9fb4205e
IB
248 WorkingDirectory = "${varDir}/${project.name}";
249 ExecStart = "${buildbot}/bin/buildbot start";
da30ae4f 250 EnvironmentFile = config.secrets.fullPaths."buildbot/${project.name}/environment_file";
9fb4205e 251 };
ab8f306d 252 }) config.myEnv.buildbot.projects;
9fb4205e
IB
253 };
254}