]> git.immae.eu Git - perso/Immae/Config/Nix.git/blob - modules/private/buildbot/default.nix
Remove duply-backup
[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 ids.uids.buildbot = config.myEnv.buildbot.user.uid;
28 ids.gids.buildbot = config.myEnv.buildbot.user.gid;
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;
37 extraGroups = [ "keys" ];
38 };
39
40 services.websites.env.tools.watchPaths = lib.attrsets.mapAttrsToList
41 (k: project: config.secrets.fullPaths."buildbot/${project.name}/webhook-httpd-include")
42 config.myEnv.buildbot.projects;
43
44 services.websites.env.tools.vhostConfs.git.extraConfig = lib.attrsets.mapAttrsToList (k: project: ''
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
52 Require ldap-group cn=users,ou=${project.name},cn=buildbot,ou=services,dc=immae,dc=eu
53
54 SetEnvIf X-Url-Scheme https HTTPS=1
55 ProxyPreserveHost On
56 </Location>
57 <Location /buildbot/${project.name}/change_hook/base>
58 <RequireAny>
59 Require local
60 Require ldap-group cn=users,ou=${project.name},cn=buildbot,ou=services,dc=immae,dc=eu
61 Include ${config.secrets.fullPaths."buildbot/${project.name}/webhook-httpd-include"}
62 </RequireAny>
63 </Location>
64 '') config.myEnv.buildbot.projects;
65
66 system.activationScripts = lib.attrsets.mapAttrs' (k: project: lib.attrsets.nameValuePair "buildbot-${project.name}" {
67 deps = [ "users" "wrappers" ];
68 text = ''
69 install -m 755 -o buildbot -g buildbot -d ${varDir}/${project.name}
70
71 ${project.activationScript}
72 '';
73 }) config.myEnv.buildbot.projects;
74
75 secrets.keys = lib.listToAttrs (
76 lib.lists.flatten (
77 lib.attrsets.mapAttrsToList (k: project:
78 lib.attrsets.mapAttrsToList (k: v:
79 (lib.nameValuePair "buildbot/${project.name}/${k}" {
80 permissions = "0600";
81 user = "buildbot";
82 group = "buildbot";
83 text = v;
84 })
85 ) project.secrets
86 ++ [
87 (lib.nameValuePair "buildbot/${project.name}/webhook-httpd-include" {
88 permissions = "0600";
89 user = "wwwrun";
90 group = "wwwrun";
91 text = lib.optionalString (project.webhookTokens != null) ''
92 Require expr "req('Access-Key') in { ${builtins.concatStringsSep ", " (map (x: "'${x}'") project.webhookTokens)} }"
93 '';
94 })
95 (lib.nameValuePair "buildbot/${project.name}/environment_file" {
96 permissions = "0600";
97 user = "buildbot";
98 group = "buildbot";
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) //
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 };
109 in builtins.concatStringsSep "\n"
110 (lib.mapAttrsToList (envK: envV: "${envK}=${envV}") project_env);
111 })
112 ]
113 ) config.myEnv.buildbot.projects
114 )
115 ) // {
116 "buildbot/ldap" = {
117 permissions = "0600";
118 user = "buildbot";
119 group = "buildbot";
120 text = config.myEnv.buildbot.ldap.password;
121 };
122 "buildbot/worker_password" = {
123 permissions = "0600";
124 user = "buildbot";
125 group = "buildbot";
126 text = config.myEnv.buildbot.workerPassword;
127 };
128 "buildbot/ssh_key" = {
129 permissions = "0600";
130 user = "buildbot";
131 group = "buildbot";
132 text = config.myEnv.buildbot.ssh_key.private;
133 };
134 };
135
136 services.filesWatcher = lib.attrsets.mapAttrs' (k: project: lib.attrsets.nameValuePair "buildbot-${project.name}" {
137 restart = true;
138 paths = [
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;
144 }) config.myEnv.buildbot.projects;
145
146 systemd.slices.buildbot = {
147 description = "buildbot slice";
148 };
149
150 networking.firewall.allowedTCPPorts = lib.attrsets.mapAttrsToList (k: v: v.workerPort) config.myEnv.buildbot.projects;
151 systemd.services = lib.attrsets.mapAttrs' (k: project: lib.attrsets.nameValuePair "buildbot-${project.name}" {
152 description = "Buildbot Continuous Integration Server ${project.name}.";
153 after = [ "network-online.target" ];
154 wantedBy = [ "multi-user.target" ];
155 path = project.packages pkgs ++ (project.pythonPackages buildbot.pythonModule pkgs);
156 preStart = let
157 master-cfg = "${buildbot_common}/${pkgs.python3.pythonForBuild.sitePackages}/buildbot_common/master.cfg";
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 '';
191 in ''
192 if [ ! -f ${varDir}/${project.name}/buildbot.tac ]; then
193 ${buildbot}/bin/buildbot create-master -c "${master-cfg}" "${varDir}/${project.name}"
194 rm -f ${varDir}/${project.name}/master.cfg.sample
195 rm -f ${varDir}/${project.name}/buildbot.tac
196 fi
197 ln -sf ${tac_file} ${varDir}/${project.name}/buildbot.tac
198 # different buildbots may be trying that simultaneously, add the || true to avoid complaining in case of race
199 install -Dm600 -o buildbot -g buildbot -T ${config.secrets.fullPaths."buildbot/ssh_key"} ${varDir}/buildbot_key || true
200 buildbot_secrets=${varDir}/${project.name}/secrets
201 install -m 0700 -o buildbot -g buildbot -d $buildbot_secrets
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
204 ${builtins.concatStringsSep "\n" (lib.attrsets.mapAttrsToList
205 (k: v: "install -Dm600 -o buildbot -g buildbot -T ${config.secrets.fullPaths."buildbot/${project.name}/${k}"} $buildbot_secrets/${k}") project.secrets
206 )}
207 ${buildbot}/bin/buildbot upgrade-master ${varDir}/${project.name}
208 '';
209 environment = let
210 buildbot_config = pkgs.python3Packages.buildPythonPackage (rec {
211 name = "buildbot_config-${project.name}";
212 src = ./projects + "/${project.name}";
213 format = "other";
214 installPhase = ''
215 mkdir -p $out/${pkgs.python3.pythonForBuild.sitePackages}
216 cp -a $src $out/${pkgs.python3.pythonForBuild.sitePackages}/buildbot_config
217 '';
218 });
219 HOME = "${varDir}/${project.name}";
220 PYTHONPATH = "${buildbot.pythonModule.withPackages (self: project.pythonPackages self pkgs ++ [
221 pkgs.python3Packages.libvirt
222 pkgs.python3Packages.wokkel
223 pkgs.python3Packages.treq pkgs.python3Packages.ldap3 buildbot
224 pkgs.python3Packages.buildbot-worker
225 buildbot_common buildbot_config
226 ])}/${buildbot.pythonModule.sitePackages}${if project.pythonPathHome then ":${varDir}/${project.name}/.local/${pkgs.python3.pythonForBuild.sitePackages}" else ""}";
227 in { inherit PYTHONPATH HOME; };
228
229 serviceConfig = {
230 Slice = "buildbot.slice";
231 Type = "forking";
232 User = "buildbot";
233 Group = "buildbot";
234 RuntimeDirectory = "buildbot";
235 RuntimeDirectoryPreserve = "yes";
236 StateDirectory = "buildbot";
237 SupplementaryGroups = "keys";
238 WorkingDirectory = "${varDir}/${project.name}";
239 ExecStart = "${buildbot}/bin/buildbot start";
240 EnvironmentFile = config.secrets.fullPaths."buildbot/${project.name}/environment_file";
241 };
242 }) config.myEnv.buildbot.projects;
243 };
244 }