1 { lib, pkgs, config, ... }:
3 varDir = "/var/lib/buildbot";
4 buildbot_common = pkgs.python3Packages.buildPythonPackage rec {
5 name = "buildbot_common";
9 mkdir -p $out/${pkgs.python3.pythonForBuild.sitePackages}
10 cp -a $src $out/${pkgs.python3.pythonForBuild.sitePackages}/buildbot_common
13 buildbot = pkgs.python3Packages.buildbot-full;
17 myServices.buildbot.enable = lib.mkOption {
18 type = lib.types.bool;
21 Whether to enable buildbot.
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;
30 users.groups.buildbot.gid = config.ids.gids.buildbot;
31 users.users.buildbot = {
33 uid = config.ids.uids.buildbot;
35 description = "Buildbot user";
37 extraGroups = [ "keys" ];
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;
44 services.websites.env.tools.vhostConfs.git.extraConfig = lib.attrsets.mapAttrsToList (k: project: ''
45 RedirectMatch permanent "^/buildbot/${project.name}$" "/buildbot/${project.name}/"
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}/>
52 Require ldap-group cn=users,ou=${project.name},cn=buildbot,ou=services,dc=immae,dc=eu
54 SetEnvIf X-Url-Scheme https HTTPS=1
57 <Location /buildbot/${project.name}/change_hook/base>
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"}
64 '') config.myEnv.buildbot.projects;
66 system.activationScripts = lib.attrsets.mapAttrs' (k: project: lib.attrsets.nameValuePair "buildbot-${project.name}" {
67 deps = [ "users" "wrappers" ];
69 install -m 755 -o buildbot -g buildbot -d ${varDir}/${project.name}
71 ${project.activationScript}
73 }) config.myEnv.buildbot.projects;
75 secrets.keys = lib.listToAttrs (
77 lib.attrsets.mapAttrsToList (k: project:
78 lib.attrsets.mapAttrsToList (k: v:
79 (lib.nameValuePair "buildbot/${project.name}/${k}" {
87 (lib.nameValuePair "buildbot/${project.name}/webhook-httpd-include" {
91 text = lib.optionalString (project.webhookTokens != null) ''
92 Require expr "req('Access-Key') in { ${builtins.concatStringsSep ", " (map (x: "'${x}'") project.webhookTokens)} }"
95 (lib.nameValuePair "buildbot/${project.name}/environment_file" {
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) //
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";
109 in builtins.concatStringsSep "\n"
110 (lib.mapAttrsToList (envK: envV: "${envK}=${envV}") project_env);
113 ) config.myEnv.buildbot.projects
117 permissions = "0600";
120 text = config.myEnv.buildbot.ldap.password;
122 "buildbot/worker_password" = {
123 permissions = "0600";
126 text = config.myEnv.buildbot.workerPassword;
128 "buildbot/ssh_key" = {
129 permissions = "0600";
132 text = config.myEnv.buildbot.ssh_key.private;
136 services.filesWatcher = lib.attrsets.mapAttrs' (k: project: lib.attrsets.nameValuePair "buildbot-${project.name}" {
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;
146 systemd.slices.buildbot = {
147 description = "buildbot slice";
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);
157 master-cfg = "${buildbot_common}/${pkgs.python3.pythonForBuild.sitePackages}/buildbot_common/master.cfg";
158 tac_file = pkgs.writeText "buildbot.tac" ''
161 from twisted.application import service
162 from buildbot.master import BuildMaster
164 basedir = '${varDir}/${project.name}'
165 rotateLength = 10000000
167 configfile = '${master-cfg}'
169 # Default umask for server
172 # if this is a relocatable tac file, get the directory containing the TAC
175 basedir = os.path.abspath(os.path.dirname(__file__))
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)
186 m = BuildMaster(basedir, configfile, umask)
187 m.setServiceParent(application)
188 m.log_rotation.rotateLength = rotateLength
189 m.log_rotation.maxRotatedFiles = maxRotatedFiles
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
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
207 ${buildbot}/bin/buildbot upgrade-master ${varDir}/${project.name}
210 buildbot_config = pkgs.python3Packages.buildPythonPackage (rec {
211 name = "buildbot_config-${project.name}";
212 src = ./projects + "/${project.name}";
215 mkdir -p $out/${pkgs.python3.pythonForBuild.sitePackages}
216 cp -a $src $out/${pkgs.python3.pythonForBuild.sitePackages}/buildbot_config
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; };
230 Slice = "buildbot.slice";
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";
242 }) config.myEnv.buildbot.projects;