]> git.immae.eu Git - perso/Immae/Config/Nix.git/blame - modules/private/buildbot/default.nix
Use attrs for secrets instead of lists
[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
4c4652aa 79 secrets.keys = lib.listToAttrs (
6984f454
IB
80 lib.lists.flatten (
81 lib.attrsets.mapAttrsToList (k: project:
82 lib.attrsets.mapAttrsToList (k: v:
4c4652aa 83 (lib.nameValuePair "buildbot/${project.name}/${k}" {
6984f454
IB
84 permissions = "0600";
85 user = "buildbot";
86 group = "buildbot";
87 text = v;
4c4652aa 88 })
6984f454
IB
89 ) project.secrets
90 ++ [
4c4652aa 91 (lib.nameValuePair "buildbot/${project.name}/webhook-httpd-include" {
6984f454
IB
92 permissions = "0600";
93 user = "wwwrun";
94 group = "wwwrun";
ab8f306d 95 text = lib.optionalString (project.webhookTokens != null) ''
6984f454
IB
96 Require expr "req('Access-Key') in { ${builtins.concatStringsSep ", " (map (x: "'${x}'") project.webhookTokens)} }"
97 '';
4c4652aa
IB
98 })
99 (lib.nameValuePair "buildbot/${project.name}/environment_file" {
dcb8ad4c
IB
100 permissions = "0600";
101 user = "buildbot";
102 group = "buildbot";
dcb8ad4c
IB
103 text = let
104 project_env = with lib.attrsets;
105 mapAttrs' (k: v: nameValuePair "BUILDBOT_${k}" v) project.environment //
106 mapAttrs' (k: v: nameValuePair "BUILDBOT_PATH_${k}" (v pkgs)) (attrByPath ["builderPaths"] {} project) //
200690c9
IB
107 {
108 BUILDBOT_PROJECT_DIR = ./projects + "/${project.name}";
109 BUILDBOT_WORKER_PORT = builtins.toString project.workerPort;
110 BUILDBOT_HOST = config.hostEnv.fqdn;
111 BUILDBOT_VIRT_URL = "qemu+ssh://libvirt@dilion.immae.eu/system";
112 };
dcb8ad4c
IB
113 in builtins.concatStringsSep "\n"
114 (lib.mapAttrsToList (envK: envV: "${envK}=${envV}") project_env);
4c4652aa 115 })
6984f454 116 ]
ab8f306d 117 ) config.myEnv.buildbot.projects
6984f454 118 )
4c4652aa
IB
119 ) // {
120 "buildbot/ldap" = {
6984f454
IB
121 permissions = "0600";
122 user = "buildbot";
123 group = "buildbot";
ab8f306d 124 text = config.myEnv.buildbot.ldap.password;
4c4652aa
IB
125 };
126 "buildbot/worker_password" = {
200690c9
IB
127 permissions = "0600";
128 user = "buildbot";
129 group = "buildbot";
130 text = config.myEnv.buildbot.workerPassword;
4c4652aa
IB
131 };
132 "buildbot/ssh_key" = {
6984f454
IB
133 permissions = "0600";
134 user = "buildbot";
135 group = "buildbot";
282c67a1 136 text = config.myEnv.buildbot.ssh_key.private;
4c4652aa
IB
137 };
138 };
6984f454 139
17f6eae9
IB
140 services.filesWatcher = lib.attrsets.mapAttrs' (k: project: lib.attrsets.nameValuePair "buildbot-${project.name}" {
141 restart = true;
142 paths = [
da30ae4f
IB
143 config.secrets.fullPaths."buildbot/ldap"
144 config.secrets.fullPaths."buildbot/worker_password"
145 config.secrets.fullPaths."buildbot/ssh_key"
146 config.secrets.fullPaths."buildbot/${project.name}/environment_file"
147 ] ++ lib.attrsets.mapAttrsToList (k: v: config.secrets.fullPaths."buildbot/${project.name}/${k}") project.secrets;
ab8f306d 148 }) config.myEnv.buildbot.projects;
17f6eae9 149
850adcf4
IB
150 systemd.slices.buildbot = {
151 description = "buildbot slice";
152 };
153
200690c9 154 networking.firewall.allowedTCPPorts = lib.attrsets.mapAttrsToList (k: v: v.workerPort) config.myEnv.buildbot.projects;
6984f454
IB
155 systemd.services = lib.attrsets.mapAttrs' (k: project: lib.attrsets.nameValuePair "buildbot-${project.name}" {
156 description = "Buildbot Continuous Integration Server ${project.name}.";
ca330baa 157 after = [ "network-online.target" ];
6984f454
IB
158 wantedBy = [ "multi-user.target" ];
159 path = project.packages pkgs ++ (project.pythonPackages buildbot.pythonModule pkgs);
160 preStart = let
caa08508 161 master-cfg = "${buildbot_common}/${pkgs.python3.pythonForBuild.sitePackages}/buildbot_common/master.cfg";
e2b96bf5
IB
162 tac_file = pkgs.writeText "buildbot.tac" ''
163 import os
164
165 from twisted.application import service
166 from buildbot.master import BuildMaster
167
168 basedir = '${varDir}/${project.name}'
169 rotateLength = 10000000
170 maxRotatedFiles = 10
171 configfile = '${master-cfg}'
172
173 # Default umask for server
174 umask = None
175
176 # if this is a relocatable tac file, get the directory containing the TAC
177 if basedir == '.':
178 import os
179 basedir = os.path.abspath(os.path.dirname(__file__))
180
181 # note: this line is matched against to check that this is a buildmaster
182 # directory; do not edit it.
183 application = service.Application('buildmaster')
184 from twisted.python.logfile import LogFile
185 from twisted.python.log import ILogObserver, FileLogObserver
186 logfile = LogFile.fromFullPath(os.path.join(basedir, "twistd.log"), rotateLength=rotateLength,
187 maxRotatedFiles=maxRotatedFiles)
188 application.setComponent(ILogObserver, FileLogObserver(logfile).emit)
189
190 m = BuildMaster(basedir, configfile, umask)
191 m.setServiceParent(application)
192 m.log_rotation.rotateLength = rotateLength
193 m.log_rotation.maxRotatedFiles = maxRotatedFiles
194 '';
9fb4205e 195 in ''
9fb4205e 196 if [ ! -f ${varDir}/${project.name}/buildbot.tac ]; then
6984f454 197 ${buildbot}/bin/buildbot create-master -c "${master-cfg}" "${varDir}/${project.name}"
9fb4205e 198 rm -f ${varDir}/${project.name}/master.cfg.sample
e2b96bf5 199 rm -f ${varDir}/${project.name}/buildbot.tac
9fb4205e 200 fi
e2b96bf5 201 ln -sf ${tac_file} ${varDir}/${project.name}/buildbot.tac
ca330baa 202 # different buildbots may be trying that simultaneously, add the || true to avoid complaining in case of race
da30ae4f 203 install -Dm600 -o buildbot -g buildbot -T ${config.secrets.fullPaths."buildbot/ssh_key"} ${varDir}/buildbot_key || true
9fb4205e 204 buildbot_secrets=${varDir}/${project.name}/secrets
6984f454 205 install -m 0700 -o buildbot -g buildbot -d $buildbot_secrets
da30ae4f
IB
206 install -Dm600 -o buildbot -g buildbot -T ${config.secrets.fullPaths."buildbot/ldap"} $buildbot_secrets/ldap
207 install -Dm600 -o buildbot -g buildbot -T ${config.secrets.fullPaths."buildbot/worker_password"} $buildbot_secrets/worker_password
9fb4205e 208 ${builtins.concatStringsSep "\n" (lib.attrsets.mapAttrsToList
da30ae4f 209 (k: v: "install -Dm600 -o buildbot -g buildbot -T ${config.secrets.fullPaths."buildbot/${project.name}/${k}"} $buildbot_secrets/${k}") project.secrets
9fb4205e 210 )}
5400b9b6 211 ${buildbot}/bin/buildbot upgrade-master ${varDir}/${project.name}
9fb4205e 212 '';
9fb4205e 213 environment = let
caa08508 214 buildbot_config = pkgs.python3Packages.buildPythonPackage (rec {
9fb4205e 215 name = "buildbot_config-${project.name}";
e2b96bf5 216 src = ./projects + "/${project.name}";
9fb4205e
IB
217 format = "other";
218 installPhase = ''
caa08508
IB
219 mkdir -p $out/${pkgs.python3.pythonForBuild.sitePackages}
220 cp -a $src $out/${pkgs.python3.pythonForBuild.sitePackages}/buildbot_config
9fb4205e
IB
221 '';
222 });
223 HOME = "${varDir}/${project.name}";
caa08508 224 PYTHONPATH = "${buildbot.pythonModule.withPackages (self: project.pythonPackages self pkgs ++ [
200690c9 225 pkgs.python3Packages.libvirt
256d607c 226 pkgs.python3Packages.wokkel
caa08508
IB
227 pkgs.python3Packages.treq pkgs.python3Packages.ldap3 buildbot
228 pkgs.python3Packages.buildbot-worker
9fb4205e 229 buildbot_common buildbot_config
caa08508 230 ])}/${buildbot.pythonModule.sitePackages}${if project.pythonPathHome then ":${varDir}/${project.name}/.local/${pkgs.python3.pythonForBuild.sitePackages}" else ""}";
dcb8ad4c 231 in { inherit PYTHONPATH HOME; };
9fb4205e
IB
232
233 serviceConfig = {
850adcf4 234 Slice = "buildbot.slice";
9fb4205e
IB
235 Type = "forking";
236 User = "buildbot";
237 Group = "buildbot";
81b9ff89
IB
238 RuntimeDirectory = "buildbot";
239 RuntimeDirectoryPreserve = "yes";
240 StateDirectory = "buildbot";
6984f454 241 SupplementaryGroups = "keys";
9fb4205e
IB
242 WorkingDirectory = "${varDir}/${project.name}";
243 ExecStart = "${buildbot}/bin/buildbot start";
da30ae4f 244 EnvironmentFile = config.secrets.fullPaths."buildbot/${project.name}/environment_file";
9fb4205e 245 };
ab8f306d 246 }) config.myEnv.buildbot.projects;
9fb4205e
IB
247 };
248}