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