Commit | Line | Data |
---|---|---|
db4fdc04 | 1 | ;;; GNU Guix --- Functional package management for GNU |
c273d81b | 2 | ;;; Copyright © 2013, 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org> |
750a4239 | 3 | ;;; Copyright © 2017 Clément Lassieur <clement@lassieur.org> |
db4fdc04 LC |
4 | ;;; |
5 | ;;; This file is part of GNU Guix. | |
6 | ;;; | |
7 | ;;; GNU Guix is free software; you can redistribute it and/or modify it | |
8 | ;;; under the terms of the GNU General Public License as published by | |
9 | ;;; the Free Software Foundation; either version 3 of the License, or (at | |
10 | ;;; your option) any later version. | |
11 | ;;; | |
12 | ;;; GNU Guix is distributed in the hope that it will be useful, but | |
13 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | ;;; GNU General Public License for more details. | |
16 | ;;; | |
17 | ;;; You should have received a copy of the GNU General Public License | |
18 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. | |
19 | ||
0190c1c0 | 20 | (define-module (gnu services shepherd) |
116244df LC |
21 | #:use-module (guix ui) |
22 | #:use-module (guix sets) | |
b5f4e686 | 23 | #:use-module (guix gexp) |
e87f0591 | 24 | #:use-module (guix store) |
db4fdc04 | 25 | #:use-module (guix monads) |
0adfe95a | 26 | #:use-module (guix records) |
e87f0591 | 27 | #:use-module (guix derivations) ;imported-modules, etc. |
db4fdc04 | 28 | #:use-module (gnu services) |
7b44cae5 | 29 | #:use-module (gnu services herd) |
0adfe95a | 30 | #:use-module (gnu packages admin) |
db4fdc04 | 31 | #:use-module (ice-9 match) |
80a67734 | 32 | #:use-module (ice-9 vlist) |
db4fdc04 | 33 | #:use-module (srfi srfi-1) |
80a67734 | 34 | #:use-module (srfi srfi-26) |
116244df LC |
35 | #:use-module (srfi srfi-34) |
36 | #:use-module (srfi srfi-35) | |
d4053c71 AK |
37 | #:export (shepherd-root-service-type |
38 | %shepherd-root-service | |
39 | shepherd-service-type | |
40 | ||
41 | shepherd-service | |
42 | shepherd-service? | |
43 | shepherd-service-documentation | |
44 | shepherd-service-provision | |
240b57f0 | 45 | shepherd-service-canonical-name |
d4053c71 AK |
46 | shepherd-service-requirement |
47 | shepherd-service-respawn? | |
48 | shepherd-service-start | |
49 | shepherd-service-stop | |
50 | shepherd-service-auto-start? | |
51 | shepherd-service-modules | |
fae685b9 | 52 | |
fae685b9 | 53 | %default-modules |
80a67734 | 54 | |
240b57f0 LC |
55 | shepherd-service-file |
56 | ||
a5d78eb6 | 57 | shepherd-service-lookup-procedure |
7b44cae5 LC |
58 | shepherd-service-back-edges |
59 | shepherd-service-upgrade)) | |
db4fdc04 LC |
60 | |
61 | ;;; Commentary: | |
62 | ;;; | |
fe1ad5f5 | 63 | ;;; Instantiating system services as a shepherd configuration file. |
db4fdc04 LC |
64 | ;;; |
65 | ;;; Code: | |
66 | ||
0adfe95a | 67 | |
d4053c71 | 68 | (define (shepherd-boot-gexp services) |
fe1ad5f5 | 69 | (mlet %store-monad ((shepherd-conf (shepherd-configuration-file services))) |
0adfe95a LC |
70 | (return #~(begin |
71 | ;; Keep track of the booted system. | |
72 | (false-if-exception (delete-file "/run/booted-system")) | |
73 | (symlink (readlink "/run/current-system") | |
74 | "/run/booted-system") | |
75 | ||
76 | ;; Close any remaining open file descriptors to be on the safe | |
77 | ;; side. This must be the very last thing we do, because | |
78 | ;; Guile has internal FDs such as 'sleep_pipe' that need to be | |
79 | ;; alive. | |
80 | (let loop ((fd 3)) | |
81 | (when (< fd 1024) | |
82 | (false-if-exception (close-fdes fd)) | |
83 | (loop (+ 1 fd)))) | |
84 | ||
34044d55 | 85 | ;; Start shepherd. |
9fc037fe | 86 | (execl #$(file-append shepherd "/bin/shepherd") |
fe1ad5f5 | 87 | "shepherd" "--config" #$shepherd-conf))))) |
0adfe95a | 88 | |
d4053c71 | 89 | (define shepherd-root-service-type |
0adfe95a | 90 | (service-type |
d4053c71 AK |
91 | (name 'shepherd-root) |
92 | ;; Extending the root shepherd service (aka. PID 1) happens by | |
93 | ;; concatenating the list of services provided by the extensions. | |
0adfe95a LC |
94 | (compose concatenate) |
95 | (extend append) | |
d4053c71 AK |
96 | (extensions (list (service-extension boot-service-type |
97 | shepherd-boot-gexp) | |
c273d81b | 98 | (service-extension profile-service-type |
34044d55 | 99 | (const (list shepherd))))))) |
0adfe95a | 100 | |
d4053c71 AK |
101 | (define %shepherd-root-service |
102 | ;; The root shepherd service, aka. PID 1. Its parameter is a list of | |
103 | ;; <shepherd-service> objects. | |
104 | (service shepherd-root-service-type '())) | |
0adfe95a | 105 | |
d4053c71 AK |
106 | (define-syntax-rule (shepherd-service-type service-name proc) |
107 | "Return a <service-type> denoting a simple shepherd service--i.e., the type | |
108 | for a service that extends SHEPHERD-ROOT-SERVICE-TYPE and nothing else." | |
0adfe95a | 109 | (service-type |
00184239 | 110 | (name service-name) |
0adfe95a | 111 | (extensions |
d4053c71 | 112 | (list (service-extension shepherd-root-service-type |
0adfe95a LC |
113 | (compose list proc)))))) |
114 | ||
fae685b9 LC |
115 | (define %default-imported-modules |
116 | ;; Default set of modules imported for a service's consumption. | |
117 | '((guix build utils) | |
479b417b | 118 | (guix build syscalls))) |
fae685b9 LC |
119 | |
120 | (define %default-modules | |
121 | ;; Default set of modules visible in a service's file. | |
34044d55 | 122 | `((shepherd service) |
fae685b9 | 123 | (oop goops) |
fae685b9 | 124 | (guix build utils) |
479b417b | 125 | (guix build syscalls))) |
fae685b9 | 126 | |
d4053c71 AK |
127 | (define-record-type* <shepherd-service> |
128 | shepherd-service make-shepherd-service | |
129 | shepherd-service? | |
130 | (documentation shepherd-service-documentation ;string | |
0adfe95a | 131 | (default "[No documentation.]")) |
d4053c71 AK |
132 | (provision shepherd-service-provision) ;list of symbols |
133 | (requirement shepherd-service-requirement ;list of symbols | |
0adfe95a | 134 | (default '())) |
d4053c71 | 135 | (respawn? shepherd-service-respawn? ;Boolean |
0adfe95a | 136 | (default #t)) |
d4053c71 AK |
137 | (start shepherd-service-start) ;g-expression (procedure) |
138 | (stop shepherd-service-stop ;g-expression (procedure) | |
0adfe95a | 139 | (default #~(const #f))) |
d4053c71 | 140 | (auto-start? shepherd-service-auto-start? ;Boolean |
fae685b9 | 141 | (default #t)) |
d4053c71 | 142 | (modules shepherd-service-modules ;list of module names |
a91c3fc7 | 143 | (default %default-modules))) |
0adfe95a | 144 | |
240b57f0 LC |
145 | (define (shepherd-service-canonical-name service) |
146 | "Return the 'canonical name' of SERVICE." | |
147 | (first (shepherd-service-provision service))) | |
0adfe95a | 148 | |
2d2651e7 | 149 | (define (assert-valid-graph services) |
d4053c71 AK |
150 | "Raise an error if SERVICES does not define a valid shepherd service graph, |
151 | for instance if a service requires a nonexistent service, or if more than one | |
2d2651e7 | 152 | service uses a given name. |
116244df | 153 | |
d4053c71 AK |
154 | These are constraints that shepherd's 'register-service' verifies but we'd |
155 | better verify them here statically than wait until PID 1 halts with an | |
156 | assertion failure." | |
2d2651e7 LC |
157 | (define provisions |
158 | ;; The set of provisions (symbols). Bail out if a symbol is given more | |
159 | ;; than once. | |
160 | (fold (lambda (service set) | |
161 | (define (assert-unique symbol) | |
162 | (when (set-contains? set symbol) | |
163 | (raise (condition | |
164 | (&message | |
165 | (message | |
69daee23 | 166 | (format #f (G_ "service '~a' provided more than once") |
2d2651e7 LC |
167 | symbol))))))) |
168 | ||
d4053c71 AK |
169 | (for-each assert-unique (shepherd-service-provision service)) |
170 | (fold set-insert set (shepherd-service-provision service))) | |
171 | (setq 'shepherd) | |
2d2651e7 LC |
172 | services)) |
173 | ||
174 | (define (assert-satisfied-requirements service) | |
175 | ;; Bail out if the requirements of SERVICE aren't satisfied. | |
176 | (for-each (lambda (requirement) | |
177 | (unless (set-contains? provisions requirement) | |
178 | (raise (condition | |
179 | (&message | |
180 | (message | |
69daee23 | 181 | (format #f (G_ "service '~a' requires '~a', \ |
2c2ec261 | 182 | which is not provided by any service") |
d4053c71 | 183 | (match (shepherd-service-provision service) |
2d2651e7 LC |
184 | ((head . _) head) |
185 | (_ service)) | |
186 | requirement))))))) | |
d4053c71 | 187 | (shepherd-service-requirement service))) |
2d2651e7 LC |
188 | |
189 | (for-each assert-satisfied-requirements services)) | |
116244df | 190 | |
d4053c71 | 191 | (define (shepherd-service-file-name service) |
fae685b9 LC |
192 | "Return the file name where the initialization code for SERVICE is to be |
193 | stored." | |
194 | (let ((provisions (string-join (map symbol->string | |
d4053c71 AK |
195 | (shepherd-service-provision service))))) |
196 | (string-append "shepherd-" | |
fae685b9 LC |
197 | (string-map (match-lambda |
198 | (#\/ #\-) | |
750a4239 | 199 | (#\ #\-) |
fae685b9 LC |
200 | (chr chr)) |
201 | provisions) | |
202 | ".scm"))) | |
203 | ||
d4053c71 | 204 | (define (shepherd-service-file service) |
fae685b9 | 205 | "Return a file defining SERVICE." |
d4053c71 | 206 | (gexp->file (shepherd-service-file-name service) |
a91c3fc7 LC |
207 | (with-imported-modules %default-imported-modules |
208 | #~(begin | |
209 | (use-modules #$@(shepherd-service-modules service)) | |
210 | ||
211 | (make <service> | |
212 | #:docstring '#$(shepherd-service-documentation service) | |
213 | #:provides '#$(shepherd-service-provision service) | |
214 | #:requires '#$(shepherd-service-requirement service) | |
215 | #:respawn? '#$(shepherd-service-respawn? service) | |
216 | #:start #$(shepherd-service-start service) | |
217 | #:stop #$(shepherd-service-stop service)))))) | |
fae685b9 | 218 | |
fe1ad5f5 AK |
219 | (define (shepherd-configuration-file services) |
220 | "Return the shepherd configuration file for SERVICES." | |
2d2651e7 | 221 | (assert-valid-graph services) |
116244df | 222 | |
a91c3fc7 LC |
223 | (mlet %store-monad ((files (mapm %store-monad |
224 | shepherd-service-file services))) | |
23ed63a1 LC |
225 | (define config |
226 | #~(begin | |
081bd3bd LC |
227 | (use-modules (srfi srfi-34) |
228 | (system repl error-handling)) | |
b9c7ed71 | 229 | |
234ea8a7 LC |
230 | ;; Arrange to spawn a REPL if something goes wrong. This is better |
231 | ;; than a kernel panic. | |
b9c7ed71 LC |
232 | (call-with-error-handling |
233 | (lambda () | |
234ea8a7 LC |
234 | (apply register-services (map primitive-load '#$files)) |
235 | ||
236 | ;; guix-daemon 0.6 aborts if 'PATH' is undefined, so work around | |
237 | ;; it. | |
238 | (setenv "PATH" "/run/current-system/profile/bin") | |
239 | ||
240 | (format #t "starting services...~%") | |
241 | (for-each (lambda (service) | |
242 | ;; In the Shepherd 0.3 the 'start' method can raise | |
243 | ;; '&action-runtime-error' if it fails, so protect | |
244 | ;; against it. (XXX: 'action-runtime-error?' is not | |
245 | ;; exported is 0.3, hence 'service-error?'.) | |
246 | (guard (c ((service-error? c) | |
247 | (format (current-error-port) | |
248 | "failed to start service '~a'~%" | |
249 | service))) | |
250 | (start service))) | |
251 | '#$(append-map shepherd-service-provision | |
252 | (filter shepherd-service-auto-start? | |
253 | services))))))) | |
23ed63a1 | 254 | |
fe1ad5f5 | 255 | (gexp->file "shepherd.conf" config))) |
db4fdc04 | 256 | |
a5d78eb6 LC |
257 | (define* (shepherd-service-lookup-procedure services |
258 | #:optional | |
259 | (provision | |
260 | shepherd-service-provision)) | |
261 | "Return a procedure that, when passed a symbol, return the item among | |
262 | SERVICES that provides this symbol. PROVISION must be a one-argument | |
263 | procedure that takes a service and returns the list of symbols it provides." | |
264 | (let ((services (fold (lambda (service result) | |
265 | (fold (cut vhash-consq <> service <>) | |
266 | result | |
267 | (provision service))) | |
268 | vlist-null | |
269 | services))) | |
270 | (lambda (name) | |
271 | (match (vhash-assq name services) | |
272 | ((_ . service) service) | |
273 | (#f #f))))) | |
274 | ||
6673bddc LC |
275 | (define* (shepherd-service-back-edges services |
276 | #:key | |
277 | (provision shepherd-service-provision) | |
278 | (requirement shepherd-service-requirement)) | |
d4053c71 | 279 | "Return a procedure that, when given a <shepherd-service> from SERVICES, |
6673bddc LC |
280 | returns the list of <shepherd-service> that depend on it. |
281 | ||
282 | Use PROVISION and REQUIREMENT as one-argument procedures that return the | |
283 | symbols provided/required by a service." | |
80a67734 | 284 | (define provision->service |
6673bddc | 285 | (shepherd-service-lookup-procedure services provision)) |
80a67734 LC |
286 | |
287 | (define edges | |
288 | (fold (lambda (service edges) | |
289 | (fold (lambda (requirement edges) | |
290 | (vhash-consq (provision->service requirement) service | |
291 | edges)) | |
292 | edges | |
6673bddc | 293 | (requirement service))) |
80a67734 LC |
294 | vlist-null |
295 | services)) | |
296 | ||
297 | (lambda (service) | |
298 | (vhash-foldq* cons '() service edges))) | |
299 | ||
7b44cae5 LC |
300 | (define (shepherd-service-upgrade live target) |
301 | "Return two values: the subset of LIVE (a list of <live-service>) that needs | |
302 | to be unloaded, and the subset of TARGET (a list of <shepherd-service>) that | |
303 | needs to be loaded." | |
304 | (define (essential? service) | |
305 | (memq (first (live-service-provision service)) | |
306 | '(root shepherd))) | |
307 | ||
308 | (define lookup-target | |
309 | (shepherd-service-lookup-procedure target | |
310 | shepherd-service-provision)) | |
311 | ||
312 | (define lookup-live | |
313 | (shepherd-service-lookup-procedure live | |
314 | live-service-provision)) | |
315 | ||
316 | (define (running? service) | |
317 | (and=> (lookup-live (shepherd-service-canonical-name service)) | |
318 | live-service-running)) | |
319 | ||
320 | (define (stopped service) | |
321 | (match (lookup-live (shepherd-service-canonical-name service)) | |
322 | (#f #f) | |
323 | (service (and (not (live-service-running service)) | |
324 | service)))) | |
325 | ||
326 | (define live-service-dependents | |
327 | (shepherd-service-back-edges live | |
328 | #:provision live-service-provision | |
329 | #:requirement live-service-requirement)) | |
330 | ||
331 | (define (obsolete? service) | |
332 | (match (lookup-target (first (live-service-provision service))) | |
333 | (#f (every obsolete? (live-service-dependents service))) | |
334 | (_ #f))) | |
335 | ||
336 | (define to-load | |
337 | ;; Only load services that are either new or currently stopped. | |
338 | (remove running? target)) | |
339 | ||
340 | (define to-unload | |
341 | ;; Unload services that are (1) no longer required, or (2) are in TO-LOAD. | |
342 | (remove essential? | |
343 | (append (filter obsolete? live) | |
344 | (filter-map stopped to-load)))) | |
345 | ||
346 | (values to-unload to-load)) | |
347 | ||
0190c1c0 | 348 | ;;; shepherd.scm ends here |