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