]> git.immae.eu Git - perso/Immae/Config/Nix.git/blob - modules/private/buildbot/default.nix
Add backup module
[perso/Immae/Config/Nix.git] / modules / private / buildbot / default.nix
1 { lib, pkgs, config, myconfig, ... }:
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 services.backup.profiles.buildbot = {
28 rootDir = varDir;
29 };
30 ids.uids.buildbot = myconfig.env.buildbot.user.uid;
31 ids.gids.buildbot = myconfig.env.buildbot.user.gid;
32
33 users.groups.buildbot.gid = config.ids.gids.buildbot;
34 users.users.buildbot = {
35 name = "buildbot";
36 uid = config.ids.uids.buildbot;
37 group = "buildbot";
38 description = "Buildbot user";
39 home = varDir;
40 extraGroups = [ "keys" ];
41 };
42
43 services.websites.env.tools.watchPaths = lib.attrsets.mapAttrsToList
44 (k: project: "/var/secrets/buildbot/${project.name}/webhook-httpd-include")
45 myconfig.env.buildbot.projects;
46
47 services.websites.env.tools.vhostConfs.git.extraConfig = lib.attrsets.mapAttrsToList (k: project: ''
48 RedirectMatch permanent "^/buildbot/${project.name}$" "/buildbot/${project.name}/"
49 RewriteEngine On
50 RewriteRule ^/buildbot/${project.name}/ws(.*)$ unix:///run/buildbot/${project.name}.sock|ws://git.immae.eu/ws$1 [P,NE,QSA,L]
51 ProxyPass /buildbot/${project.name}/ unix:///run/buildbot/${project.name}.sock|http://${project.name}-git.immae.eu/
52 ProxyPassReverse /buildbot/${project.name}/ unix:///run/buildbot/${project.name}.sock|http://${project.name}-git.immae.eu/
53 <Location /buildbot/${project.name}/>
54 Use LDAPConnect
55 Require ldap-group cn=users,ou=${project.name},cn=buildbot,ou=services,dc=immae,dc=eu
56
57 SetEnvIf X-Url-Scheme https HTTPS=1
58 ProxyPreserveHost On
59 </Location>
60 <Location /buildbot/${project.name}/change_hook/base>
61 <RequireAny>
62 Require local
63 Require ldap-group cn=users,ou=${project.name},cn=buildbot,ou=services,dc=immae,dc=eu
64 Include /var/secrets/buildbot/${project.name}/webhook-httpd-include
65 </RequireAny>
66 </Location>
67 '') myconfig.env.buildbot.projects;
68
69 system.activationScripts = lib.attrsets.mapAttrs' (k: project: lib.attrsets.nameValuePair "buildbot-${project.name}" {
70 deps = [ "users" "wrappers" ];
71 text = project.activationScript;
72 }) myconfig.env.buildbot.projects;
73
74 secrets.keys = (
75 lib.lists.flatten (
76 lib.attrsets.mapAttrsToList (k: project:
77 lib.attrsets.mapAttrsToList (k: v:
78 {
79 permissions = "0600";
80 user = "buildbot";
81 group = "buildbot";
82 text = v;
83 dest = "buildbot/${project.name}/${k}";
84 }
85 ) project.secrets
86 ++ [
87 {
88 permissions = "0600";
89 user = "wwwrun";
90 group = "wwwrun";
91 text = lib.optionalString (lib.attrsets.hasAttr "webhookTokens" project) ''
92 Require expr "req('Access-Key') in { ${builtins.concatStringsSep ", " (map (x: "'${x}'") project.webhookTokens)} }"
93 '';
94 dest = "buildbot/${project.name}/webhook-httpd-include";
95 }
96 ]
97 ) myconfig.env.buildbot.projects
98 )
99 ) ++ [
100 {
101 permissions = "0600";
102 user = "buildbot";
103 group = "buildbot";
104 text = myconfig.env.buildbot.ldap.password;
105 dest = "buildbot/ldap";
106 }
107 {
108 permissions = "0600";
109 user = "buildbot";
110 group = "buildbot";
111 text = builtins.readFile "${myconfig.privateFiles}/buildbot_ssh_key";
112 dest = "buildbot/ssh_key";
113 }
114 ];
115
116 services.filesWatcher = lib.attrsets.mapAttrs' (k: project: lib.attrsets.nameValuePair "buildbot-${project.name}" {
117 restart = true;
118 paths = [
119 "/var/secrets/buildbot/ldap"
120 "/var/secrets/buildbot/ssh_key"
121 ] ++ lib.attrsets.mapAttrsToList (k: v: "/var/secrets/buildbot/${project.name}/${k}") project.secrets;
122 }) myconfig.env.buildbot.projects;
123
124 systemd.services = lib.attrsets.mapAttrs' (k: project: lib.attrsets.nameValuePair "buildbot-${project.name}" {
125 description = "Buildbot Continuous Integration Server ${project.name}.";
126 after = [ "network-online.target" ];
127 wantedBy = [ "multi-user.target" ];
128 path = project.packages pkgs ++ (project.pythonPackages buildbot.pythonModule pkgs);
129 preStart = let
130 master-cfg = "${buildbot_common}/${pkgs.python3.pythonForBuild.sitePackages}/buildbot_common/master.cfg";
131 tac_file = pkgs.writeText "buildbot.tac" ''
132 import os
133
134 from twisted.application import service
135 from buildbot.master import BuildMaster
136
137 basedir = '${varDir}/${project.name}'
138 rotateLength = 10000000
139 maxRotatedFiles = 10
140 configfile = '${master-cfg}'
141
142 # Default umask for server
143 umask = None
144
145 # if this is a relocatable tac file, get the directory containing the TAC
146 if basedir == '.':
147 import os
148 basedir = os.path.abspath(os.path.dirname(__file__))
149
150 # note: this line is matched against to check that this is a buildmaster
151 # directory; do not edit it.
152 application = service.Application('buildmaster')
153 from twisted.python.logfile import LogFile
154 from twisted.python.log import ILogObserver, FileLogObserver
155 logfile = LogFile.fromFullPath(os.path.join(basedir, "twistd.log"), rotateLength=rotateLength,
156 maxRotatedFiles=maxRotatedFiles)
157 application.setComponent(ILogObserver, FileLogObserver(logfile).emit)
158
159 m = BuildMaster(basedir, configfile, umask)
160 m.setServiceParent(application)
161 m.log_rotation.rotateLength = rotateLength
162 m.log_rotation.maxRotatedFiles = maxRotatedFiles
163 '';
164 in ''
165 if [ ! -f ${varDir}/${project.name}/buildbot.tac ]; then
166 ${buildbot}/bin/buildbot create-master -c "${master-cfg}" "${varDir}/${project.name}"
167 rm -f ${varDir}/${project.name}/master.cfg.sample
168 rm -f ${varDir}/${project.name}/buildbot.tac
169 fi
170 ln -sf ${tac_file} ${varDir}/${project.name}/buildbot.tac
171 # different buildbots may be trying that simultaneously, add the || true to avoid complaining in case of race
172 install -Dm600 -o buildbot -g buildbot -T /var/secrets/buildbot/ssh_key ${varDir}/buildbot_key || true
173 buildbot_secrets=${varDir}/${project.name}/secrets
174 install -m 0700 -o buildbot -g buildbot -d $buildbot_secrets
175 install -Dm600 -o buildbot -g buildbot -T /var/secrets/buildbot/ldap $buildbot_secrets/ldap
176 ${builtins.concatStringsSep "\n" (lib.attrsets.mapAttrsToList
177 (k: v: "install -Dm600 -o buildbot -g buildbot -T /var/secrets/buildbot/${project.name}/${k} $buildbot_secrets/${k}") project.secrets
178 )}
179 '';
180 environment = let
181 project_env = lib.attrsets.mapAttrs' (k: v: lib.attrsets.nameValuePair "BUILDBOT_${k}" v) project.environment;
182 buildbot_config = pkgs.python3Packages.buildPythonPackage (rec {
183 name = "buildbot_config-${project.name}";
184 src = ./projects + "/${project.name}";
185 format = "other";
186 installPhase = ''
187 mkdir -p $out/${pkgs.python3.pythonForBuild.sitePackages}
188 cp -a $src $out/${pkgs.python3.pythonForBuild.sitePackages}/buildbot_config
189 '';
190 });
191 HOME = "${varDir}/${project.name}";
192 PYTHONPATH = "${buildbot.pythonModule.withPackages (self: project.pythonPackages self pkgs ++ [
193 pkgs.python3Packages.wokkel
194 pkgs.python3Packages.treq pkgs.python3Packages.ldap3 buildbot
195 pkgs.python3Packages.buildbot-worker
196 buildbot_common buildbot_config
197 ])}/${buildbot.pythonModule.sitePackages}${if project.pythonPathHome then ":${varDir}/${project.name}/.local/${pkgs.python3.pythonForBuild.sitePackages}" else ""}";
198 in project_env // { inherit PYTHONPATH HOME; };
199
200 serviceConfig = {
201 Type = "forking";
202 User = "buildbot";
203 Group = "buildbot";
204 RuntimeDirectory = "buildbot";
205 RuntimeDirectoryPreserve = "yes";
206 StateDirectory = "buildbot";
207 SupplementaryGroups = "keys";
208 WorkingDirectory = "${varDir}/${project.name}";
209 ExecStart = "${buildbot}/bin/buildbot start";
210 };
211 }) myconfig.env.buildbot.projects;
212 };
213 }