]>
Commit | Line | Data |
---|---|---|
1 | { lib, pkgs, config, ... }: | |
2 | let | |
3 | varDir = "/var/lib/buildbot"; | |
4 | bb-python = buildbot.pythonModule; | |
5 | buildbot = pkgs.immae-buildbot; | |
6 | in | |
7 | { | |
8 | options = { | |
9 | myServices.buildbot.enable = lib.mkOption { | |
10 | type = lib.types.bool; | |
11 | default = false; | |
12 | description = '' | |
13 | Whether to enable buildbot. | |
14 | ''; | |
15 | }; | |
16 | }; | |
17 | ||
18 | config = lib.mkIf config.myServices.buildbot.enable { | |
19 | nixpkgs.overlays = [ | |
20 | (self: super: { | |
21 | follow-systemd-unit = self.writeScriptBin "follow-systemd-unit" '' | |
22 | #!${self.stdenv.shell} | |
23 | ||
24 | set -euo pipefail | |
25 | ||
26 | service=$1 | |
27 | before_invocation_id=$2 | |
28 | ||
29 | get_id() { | |
30 | systemctl show -p InvocationID --value "$service" | |
31 | } | |
32 | ||
33 | while [ "$(get_id)" = "$before_invocation_id" ]; do sleep 1; done | |
34 | ||
35 | invocation_id="$(get_id)" | |
36 | cursor="$(mktemp)" | |
37 | trap "rm -f $cursor" EXIT | |
38 | ||
39 | get_logs() { | |
40 | journalctl --quiet --cursor-file=$cursor INVOCATION_ID=$invocation_id + _SYSTEMD_INVOCATION_ID=$invocation_id | |
41 | } | |
42 | ||
43 | while [ -n "$(systemctl show -p Job --value "$service")" ]; do | |
44 | get_logs | |
45 | done | |
46 | get_logs | |
47 | ''; | |
48 | }) | |
49 | ]; | |
50 | ids.uids.buildbot = config.myEnv.buildbot.user.uid; | |
51 | ids.gids.buildbot = config.myEnv.buildbot.user.gid; | |
52 | ||
53 | users.groups.buildbot.gid = config.ids.gids.buildbot; | |
54 | users.users.buildbot = { | |
55 | name = "buildbot"; | |
56 | uid = config.ids.uids.buildbot; | |
57 | group = "buildbot"; | |
58 | description = "Buildbot user"; | |
59 | home = varDir; | |
60 | extraGroups = [ "keys" "systemd-journal" ]; | |
61 | useDefaultShell = true; | |
62 | openssh.authorizedKeys.keys = [ config.myEnv.buildbot.ssh_key.public ]; | |
63 | }; | |
64 | ||
65 | services.websites.env.tools.watchPaths = lib.attrsets.mapAttrsToList | |
66 | (k: project: config.secrets.fullPaths."buildbot/${project.name}/webhook-httpd-include") | |
67 | config.myEnv.buildbot.projects; | |
68 | ||
69 | services.websites.env.tools.vhostConfs.git.extraConfig = lib.attrsets.mapAttrsToList (k: project: '' | |
70 | RedirectMatch permanent "^/buildbot/${project.name}$" "/buildbot/${project.name}/" | |
71 | RewriteEngine On | |
72 | RewriteRule ^/buildbot/${project.name}/ws(.*)$ unix:///run/buildbot/${project.name}.sock|ws://git.immae.eu/ws$1 [P,NE,QSA,L] | |
73 | ProxyPass /buildbot/${project.name}/ unix:///run/buildbot/${project.name}.sock|http://${project.name}-git.immae.eu/ | |
74 | ProxyPassReverse /buildbot/${project.name}/ unix:///run/buildbot/${project.name}.sock|http://${project.name}-git.immae.eu/ | |
75 | <Location /buildbot/${project.name}/> | |
76 | Use LDAPConnect | |
77 | Require ldap-group cn=users,ou=${project.name},cn=buildbot,ou=services,dc=immae,dc=eu | |
78 | ||
79 | SetEnvIf X-Url-Scheme https HTTPS=1 | |
80 | ProxyPreserveHost On | |
81 | </Location> | |
82 | <Location /buildbot/${project.name}/change_hook/base> | |
83 | <RequireAny> | |
84 | Require local | |
85 | Require ldap-group cn=users,ou=${project.name},cn=buildbot,ou=services,dc=immae,dc=eu | |
86 | Include ${config.secrets.fullPaths."buildbot/${project.name}/webhook-httpd-include"} | |
87 | </RequireAny> | |
88 | </Location> | |
89 | '') config.myEnv.buildbot.projects; | |
90 | ||
91 | system.activationScripts = lib.attrsets.mapAttrs' (k: project: lib.attrsets.nameValuePair "buildbot-${project.name}" { | |
92 | deps = [ "users" "wrappers" ]; | |
93 | text = '' | |
94 | install -m 755 -o buildbot -g buildbot -d ${varDir}/${project.name} | |
95 | ||
96 | ${project.activationScript} | |
97 | ''; | |
98 | }) config.myEnv.buildbot.projects; | |
99 | ||
100 | secrets.keys = lib.listToAttrs ( | |
101 | lib.lists.flatten ( | |
102 | lib.attrsets.mapAttrsToList (k: project: | |
103 | lib.attrsets.mapAttrsToList (k: v: | |
104 | (lib.nameValuePair "buildbot/${project.name}/${k}" { | |
105 | permissions = "0600"; | |
106 | user = "buildbot"; | |
107 | group = "buildbot"; | |
108 | text = if builtins.isFunction v then v pkgs config else v; | |
109 | }) | |
110 | ) project.secrets | |
111 | ++ [ | |
112 | (lib.nameValuePair "buildbot/${project.name}/webhook-httpd-include" { | |
113 | permissions = "0600"; | |
114 | user = "wwwrun"; | |
115 | group = "wwwrun"; | |
116 | text = lib.optionalString (project.webhookTokens != null) '' | |
117 | Require expr "req('Access-Key') in { ${builtins.concatStringsSep ", " (map (x: "'${x}'") project.webhookTokens)} }" | |
118 | ''; | |
119 | }) | |
120 | (lib.nameValuePair "buildbot/${project.name}/environment_file" { | |
121 | permissions = "0600"; | |
122 | user = "buildbot"; | |
123 | group = "buildbot"; | |
124 | text = let | |
125 | project_env = with lib.attrsets; | |
126 | mapAttrs' (k: v: nameValuePair "BUILDBOT_${k}" (if builtins.isFunction v then v pkgs else v)) project.environment // | |
127 | mapAttrs' (k: v: nameValuePair "BUILDBOT_PATH_${k}" (v pkgs)) (attrByPath ["builderPaths"] {} project) // | |
128 | { | |
129 | BUILDBOT_PROJECT_DIR = ./projects + "/${project.name}"; | |
130 | BUILDBOT_WORKER_PORT = builtins.toString project.workerPort; | |
131 | BUILDBOT_HOST = config.hostEnv.fqdn; | |
132 | BUILDBOT_VIRT_URL = "qemu+ssh://libvirt@dilion.immae.eu/system"; | |
133 | }; | |
134 | in builtins.concatStringsSep "\n" | |
135 | (lib.mapAttrsToList (envK: envV: "${envK}=${envV}") project_env); | |
136 | }) | |
137 | ] | |
138 | ) config.myEnv.buildbot.projects | |
139 | ) | |
140 | ) // { | |
141 | "buildbot/ldap" = { | |
142 | permissions = "0600"; | |
143 | user = "buildbot"; | |
144 | group = "buildbot"; | |
145 | text = config.myEnv.buildbot.ldap.password; | |
146 | }; | |
147 | "buildbot/worker_password" = { | |
148 | permissions = "0600"; | |
149 | user = "buildbot"; | |
150 | group = "buildbot"; | |
151 | text = config.myEnv.buildbot.workerPassword; | |
152 | }; | |
153 | "buildbot/ssh_key" = { | |
154 | permissions = "0600"; | |
155 | user = "buildbot"; | |
156 | group = "buildbot"; | |
157 | text = config.myEnv.buildbot.ssh_key.private; | |
158 | }; | |
159 | }; | |
160 | ||
161 | services.filesWatcher = lib.attrsets.mapAttrs' (k: project: lib.attrsets.nameValuePair "buildbot-${project.name}" { | |
162 | restart = true; | |
163 | paths = [ | |
164 | config.secrets.fullPaths."buildbot/ldap" | |
165 | config.secrets.fullPaths."buildbot/worker_password" | |
166 | config.secrets.fullPaths."buildbot/ssh_key" | |
167 | config.secrets.fullPaths."buildbot/${project.name}/environment_file" | |
168 | ] ++ lib.attrsets.mapAttrsToList (k: v: config.secrets.fullPaths."buildbot/${project.name}/${k}") project.secrets; | |
169 | }) config.myEnv.buildbot.projects; | |
170 | ||
171 | systemd.slices.buildbot = { | |
172 | description = "buildbot slice"; | |
173 | }; | |
174 | ||
175 | networking.firewall.allowedTCPPorts = lib.attrsets.mapAttrsToList (k: v: v.workerPort) config.myEnv.buildbot.projects; | |
176 | systemd.services = lib.attrsets.mapAttrs' (k: project: lib.attrsets.nameValuePair "buildbot-${project.name}" { | |
177 | description = "Buildbot Continuous Integration Server ${project.name}."; | |
178 | after = [ "network-online.target" ]; | |
179 | wantedBy = [ "multi-user.target" ]; | |
180 | path = project.packages pkgs; | |
181 | preStart = let | |
182 | master-cfg = "${buildbot.buildbot_common}/${bb-python.pythonForBuild.sitePackages}/buildbot_common/master.cfg"; | |
183 | tac_file = pkgs.writeText "buildbot.tac" '' | |
184 | import os | |
185 | ||
186 | from twisted.application import service | |
187 | from buildbot.master import BuildMaster | |
188 | ||
189 | basedir = '${varDir}/${project.name}' | |
190 | rotateLength = 10000000 | |
191 | maxRotatedFiles = 10 | |
192 | configfile = '${master-cfg}' | |
193 | ||
194 | # Default umask for server | |
195 | umask = None | |
196 | ||
197 | # if this is a relocatable tac file, get the directory containing the TAC | |
198 | if basedir == '.': | |
199 | import os | |
200 | basedir = os.path.abspath(os.path.dirname(__file__)) | |
201 | ||
202 | # note: this line is matched against to check that this is a buildmaster | |
203 | # directory; do not edit it. | |
204 | application = service.Application('buildmaster') | |
205 | from twisted.python.logfile import LogFile | |
206 | from twisted.python.log import ILogObserver, FileLogObserver | |
207 | logfile = LogFile.fromFullPath(os.path.join(basedir, "twistd.log"), rotateLength=rotateLength, | |
208 | maxRotatedFiles=maxRotatedFiles) | |
209 | application.setComponent(ILogObserver, FileLogObserver(logfile).emit) | |
210 | ||
211 | m = BuildMaster(basedir, configfile, umask) | |
212 | m.setServiceParent(application) | |
213 | m.log_rotation.rotateLength = rotateLength | |
214 | m.log_rotation.maxRotatedFiles = maxRotatedFiles | |
215 | ''; | |
216 | in '' | |
217 | if [ ! -f ${varDir}/${project.name}/buildbot.tac ]; then | |
218 | ${buildbot}/bin/buildbot create-master -c "${master-cfg}" "${varDir}/${project.name}" | |
219 | rm -f ${varDir}/${project.name}/master.cfg.sample | |
220 | rm -f ${varDir}/${project.name}/buildbot.tac | |
221 | fi | |
222 | ln -sf ${tac_file} ${varDir}/${project.name}/buildbot.tac | |
223 | # different buildbots may be trying that simultaneously, add the || true to avoid complaining in case of race | |
224 | install -Dm600 -o buildbot -g buildbot -T ${config.secrets.fullPaths."buildbot/ssh_key"} ${varDir}/buildbot_key || true | |
225 | buildbot_secrets=${varDir}/${project.name}/secrets | |
226 | install -m 0700 -o buildbot -g buildbot -d $buildbot_secrets | |
227 | install -Dm600 -o buildbot -g buildbot -T ${config.secrets.fullPaths."buildbot/ldap"} $buildbot_secrets/ldap | |
228 | install -Dm600 -o buildbot -g buildbot -T ${config.secrets.fullPaths."buildbot/worker_password"} $buildbot_secrets/worker_password | |
229 | ${builtins.concatStringsSep "\n" (lib.attrsets.mapAttrsToList | |
230 | (k: v: "install -Dm600 -o buildbot -g buildbot -T ${config.secrets.fullPaths."buildbot/${project.name}/${k}"} $buildbot_secrets/${k}") project.secrets | |
231 | )} | |
232 | ${buildbot}/bin/buildbot upgrade-master ${varDir}/${project.name} | |
233 | ''; | |
234 | environment = let | |
235 | HOME = "${varDir}/${project.name}"; | |
236 | PYTHONPATH = "${bb-python.withPackages (self: | |
237 | buildbot.common_packages self ++ | |
238 | [ (buildbot.buildbot_config project) ] | |
239 | )}/${bb-python.sitePackages}${if project.pythonPathHome then ":${varDir}/${project.name}/.local/${bb-python.sitePackages}" else ""}"; | |
240 | in { inherit PYTHONPATH HOME; }; | |
241 | ||
242 | serviceConfig = { | |
243 | Slice = "buildbot.slice"; | |
244 | Type = "forking"; | |
245 | User = "buildbot"; | |
246 | Group = "buildbot"; | |
247 | RuntimeDirectory = "buildbot"; | |
248 | RuntimeDirectoryPreserve = "yes"; | |
249 | StateDirectory = "buildbot"; | |
250 | SupplementaryGroups = "keys"; | |
251 | WorkingDirectory = "${varDir}/${project.name}"; | |
252 | ExecStart = "${buildbot}/bin/buildbot start"; | |
253 | EnvironmentFile = config.secrets.fullPaths."buildbot/${project.name}/environment_file"; | |
254 | }; | |
255 | }) config.myEnv.buildbot.projects; | |
256 | }; | |
257 | } |