1 { lib, pkgs, config, buildbot, ... }:
3 varDir = "/var/lib/buildbot";
4 bb-python = buildbot.pythonModule;
8 myServices.buildbot.enable = lib.mkOption {
12 Whether to enable buildbot.
17 config = lib.mkIf config.myServices.buildbot.enable {
18 myEnv.buildbot.projects.test = {
20 workerPort = config.myEnv.ports.buildbot_test;
21 packages = [ pkgs.git pkgs.gzip pkgs.openssh ];
22 pythonPathHome = false;
24 apprise_webhooks = builtins.concatStringsSep "\n" [
25 "{{ .apprise_webhooks.matrix_immae_eu_alert }}"
27 notify_xmpp_password = "{{ .xmpp.notify_bot }}";
30 install -m 0755 -o buildbot -g buildbot -d /var/lib/ftp/release.immae.eu/test
33 "{{ .buildbot.webhookTokens.Immae }}"
34 "{{ .buildbot.webhookTokens.Immae }}"
38 myServices.chatonsProperties.hostings.buildbot = {
39 file.datetime = "2022-08-21T10:37:00";
42 description = "Python-based continuous integration testing framework";
44 website = "https://git.immae.eu";
45 logo = "https://www.buildbot.net/img/icon.png";
47 status.description = "OK";
48 registration.load = "OPEN";
49 install.type = "PACKAGE";
50 guide.user = "https://www.immae.eu/docs/forge-logicielle.html";
54 website = "https://www.buildbot.net/";
55 license.url = "https://github.com/buildbot/buildbot/blob/master/LICENSE";
56 license.name = "GNU General Public License v2.0";
57 version = pkgs.buildbot.version;
58 source.url = "https://github.com/buildbot/buildbot";
63 follow-systemd-unit = self.writeScriptBin "follow-systemd-unit" ''
64 #!${self.stdenv.shell}
69 before_invocation_id=$2
72 systemctl show -p InvocationID --value "$service"
75 while [ "$(get_id)" = "$before_invocation_id" ]; do sleep 1; done
77 invocation_id="$(get_id)"
79 trap "rm -f $cursor" EXIT
82 journalctl --quiet --cursor-file=$cursor INVOCATION_ID=$invocation_id + _SYSTEMD_INVOCATION_ID=$invocation_id
85 while [ -n "$(systemctl show -p Job --value "$service")" ]; do
92 ids.uids.buildbot = config.myEnv.buildbot.user.uid;
93 ids.gids.buildbot = config.myEnv.buildbot.user.gid;
95 users.groups.buildbot.gid = config.ids.gids.buildbot;
96 users.users.buildbot = {
98 uid = config.ids.uids.buildbot;
100 description = "Buildbot user";
102 extraGroups = [ "keys" "systemd-journal" ];
103 useDefaultShell = true;
104 openssh.authorizedKeys.keys = [ config.myEnv.buildbot.ssh_key.public ];
107 services.websites.env.tools.watchPaths = lib.attrsets.mapAttrsToList
108 (k: project: config.secrets.fullPaths."buildbot/${project.name}/webhook-httpd-include")
109 config.myEnv.buildbot.projects;
111 services.websites.env.tools.vhostConfs.git.extraConfig = lib.attrsets.mapAttrsToList (k: project: ''
112 RedirectMatch permanent "^/buildbot/${project.name}$" "/buildbot/${project.name}/"
114 RewriteRule ^/buildbot/${project.name}/ws(.*)$ unix:///run/buildbot/${project.name}.sock|ws://git.immae.eu/ws$1 [P,NE,QSA,L]
115 ProxyPass /buildbot/${project.name}/ unix:///run/buildbot/${project.name}.sock|http://${project.name}-git.immae.eu/
116 ProxyPassReverse /buildbot/${project.name}/ unix:///run/buildbot/${project.name}.sock|http://${project.name}-git.immae.eu/
117 <Location /buildbot/${project.name}/>
119 Require ldap-group cn=users,ou=${project.name},cn=buildbot,ou=services,dc=immae,dc=eu
121 SetEnvIf X-Url-Scheme https HTTPS=1
124 <Location /buildbot/${project.name}/change_hook/base>
127 Require ldap-group cn=users,ou=${project.name},cn=buildbot,ou=services,dc=immae,dc=eu
128 Include ${config.secrets.fullPaths."buildbot/${project.name}/webhook-httpd-include"}
131 '') config.myEnv.buildbot.projects;
133 system.activationScripts = lib.attrsets.mapAttrs' (k: project: lib.attrsets.nameValuePair "buildbot-${project.name}" {
134 deps = [ "users" "wrappers" ];
136 install -m 755 -o buildbot -g buildbot -d ${varDir}/${project.name}
138 ${project.activationScript}
140 }) config.myEnv.buildbot.projects;
142 secrets.keys = lib.listToAttrs (
144 lib.attrsets.mapAttrsToList (k: project:
145 lib.attrsets.mapAttrsToList (k: v:
146 (lib.nameValuePair "buildbot/${project.name}/${k}" {
147 permissions = "0600";
154 (lib.nameValuePair "buildbot/${project.name}/webhook-httpd-include" {
155 permissions = "0600";
158 text = lib.optionalString (project.webhookTokens != null) ''
159 Require expr "req('Access-Key') in { ${builtins.concatStringsSep ", " (map (x: "'${x}'") project.webhookTokens)} }"
162 (lib.nameValuePair "buildbot/${project.name}/environment_file" {
163 permissions = "0600";
166 keyDependencies = [ (buildbot.buildbot_config project).src ] ++ project.secretsDeps;
168 project_env = with lib.attrsets;
169 mapAttrs' (k: v: nameValuePair "BUILDBOT_${k}" v) project.environment //
171 BUILDBOT_PROJECT_DIR = (buildbot.buildbot_config project).src;
172 BUILDBOT_WORKER_PORT = builtins.toString project.workerPort;
173 BUILDBOT_HOST = config.hostEnv.fqdn;
174 BUILDBOT_VIRT_URL = "qemu+ssh://libvirt@dilion.immae.eu/system";
176 in builtins.concatStringsSep "\n"
177 (lib.mapAttrsToList (envK: envV: "${envK}=${envV}") project_env);
180 ) config.myEnv.buildbot.projects
184 permissions = "0600";
187 text = config.myEnv.buildbot.ldap.password;
189 "buildbot/worker_password" = {
190 permissions = "0600";
193 text = config.myEnv.buildbot.workerPassword;
195 "buildbot/ssh_key" = {
196 permissions = "0600";
199 text = config.myEnv.buildbot.ssh_key.private;
201 "buildbot/ssh_known_hosts" = {
202 permissions = "0644";
206 git.immae.eu ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIFbhFTl2A2RJn5L51yxJM4XfCS2ZaiSX/jo9jFSdghF
207 eldiron ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIFbhFTl2A2RJn5L51yxJM4XfCS2ZaiSX/jo9jFSdghF
208 phare.normalesup.org ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIN2GomItXICXpCtCFRMT2xuerqx2nLMO/3mNUuWyzFr1
213 services.filesWatcher = lib.attrsets.mapAttrs' (k: project: lib.attrsets.nameValuePair "buildbot-${project.name}" {
216 config.secrets.fullPaths."buildbot/ldap"
217 config.secrets.fullPaths."buildbot/worker_password"
218 config.secrets.fullPaths."buildbot/ssh_key"
219 config.secrets.fullPaths."buildbot/${project.name}/environment_file"
220 ] ++ lib.attrsets.mapAttrsToList (k: v: config.secrets.fullPaths."buildbot/${project.name}/${k}") project.secrets;
221 }) config.myEnv.buildbot.projects;
223 systemd.slices.buildbot = {
224 description = "buildbot slice";
227 networking.firewall.allowedTCPPorts = lib.attrsets.mapAttrsToList (k: v: v.workerPort) config.myEnv.buildbot.projects;
228 systemd.services = lib.attrsets.mapAttrs' (k: project: lib.attrsets.nameValuePair "buildbot-${project.name}" {
229 description = "Buildbot Continuous Integration Server ${project.name}.";
230 after = [ "network-online.target" ];
231 wantedBy = [ "multi-user.target" ];
232 path = project.packages;
234 master-cfg = "${buildbot.buildbot_common}/${bb-python.pythonForBuild.sitePackages}/buildbot_common/master.cfg";
235 tac_file = pkgs.writeText "buildbot.tac" ''
238 from twisted.application import service
239 from buildbot.master import BuildMaster
241 basedir = '${varDir}/${project.name}'
242 rotateLength = 10000000
244 configfile = '${master-cfg}'
246 # Default umask for server
249 # if this is a relocatable tac file, get the directory containing the TAC
252 basedir = os.path.abspath(os.path.dirname(__file__))
254 # note: this line is matched against to check that this is a buildmaster
255 # directory; do not edit it.
256 application = service.Application('buildmaster')
257 from twisted.python.logfile import LogFile
258 from twisted.python.log import ILogObserver, FileLogObserver
259 logfile = LogFile.fromFullPath(os.path.join(basedir, "twistd.log"), rotateLength=rotateLength,
260 maxRotatedFiles=maxRotatedFiles)
261 application.setComponent(ILogObserver, FileLogObserver(logfile).emit)
263 m = BuildMaster(basedir, configfile, umask)
264 m.setServiceParent(application)
265 m.log_rotation.rotateLength = rotateLength
266 m.log_rotation.maxRotatedFiles = maxRotatedFiles
269 if [ ! -f ${varDir}/${project.name}/buildbot.tac ]; then
270 ${buildbot}/bin/buildbot create-master -c "${master-cfg}" "${varDir}/${project.name}"
271 rm -f ${varDir}/${project.name}/master.cfg.sample
272 rm -f ${varDir}/${project.name}/buildbot.tac
274 ln -sf ${tac_file} ${varDir}/${project.name}/buildbot.tac
275 # different buildbots may be trying that simultaneously, add the || true to avoid complaining in case of race
276 install -Dm600 -o buildbot -g buildbot -T ${config.secrets.fullPaths."buildbot/ssh_key"} ${varDir}/buildbot_key || true
277 install -Dm600 -o buildbot -g buildbot -T ${config.secrets.fullPaths."buildbot/ssh_known_hosts"} ${varDir}/buildbot_hosts || true
278 buildbot_secrets=${varDir}/${project.name}/secrets
279 install -m 0700 -o buildbot -g buildbot -d $buildbot_secrets
280 install -Dm600 -o buildbot -g buildbot -T ${config.secrets.fullPaths."buildbot/ldap"} $buildbot_secrets/ldap
281 install -Dm600 -o buildbot -g buildbot -T ${config.secrets.fullPaths."buildbot/worker_password"} $buildbot_secrets/worker_password
282 ${builtins.concatStringsSep "\n" (lib.attrsets.mapAttrsToList
283 (k: v: "install -Dm600 -o buildbot -g buildbot -T ${config.secrets.fullPaths."buildbot/${project.name}/${k}"} $buildbot_secrets/${k}") project.secrets
285 ${buildbot}/bin/buildbot upgrade-master ${varDir}/${project.name}
288 HOME = "${varDir}/${project.name}";
289 PYTHONPATH = "${bb-python.withPackages (self:
290 buildbot.common_packages self ++
291 [ (buildbot.buildbot_config project) ]
292 )}/${bb-python.sitePackages}${if project.pythonPathHome then ":${varDir}/${project.name}/.local/${bb-python.sitePackages}" else ""}";
293 in { inherit PYTHONPATH HOME; };
296 Slice = "buildbot.slice";
300 RuntimeDirectory = "buildbot";
301 RuntimeDirectoryPreserve = "yes";
302 StateDirectory = "buildbot";
303 SupplementaryGroups = "keys";
304 WorkingDirectory = "${varDir}/${project.name}";
305 ExecStart = "${buildbot}/bin/buildbot start";
306 EnvironmentFile = config.secrets.fullPaths."buildbot/${project.name}/environment_file";
308 }) config.myEnv.buildbot.projects;