aboutsummaryrefslogtreecommitdiff
path: root/gn/services/science.scm
blob: f0f43d0e61131b4bad7c8053ca3f20f006357d07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
(define-module (gn services science)
  #:export (munge-configuration
            munge-configuration?
            munge-service-type

            slurm-configuration
            slurm-configuration?
            slurm-service-type))

(use-modules (gnu)
             (guix records)
             (ice-9 match))
(use-service-modules shepherd)
(use-package-modules admin parallel)

;; TODO: Make id/uid configurable
(define %munge-accounts
  (list (user-group
          (name "munge")
          (id 900)
          (system? #t))
        (user-account
          (name "munge")
          (group "munge")
          (uid 900)
          (system? #t)
          (comment "Munge User")
          (home-directory "/var/lib/munge")
          (shell (file-append shadow "/sbin/nologin")))))

(define-record-type* <munge-configuration>
  munge-configuration
  make-munge-configuration
  munge-configuration?
  (package      munge-configuration-package
                (default munge))
  (socket       munge-configuration-socket
                (default "/var/run/munge/munge.socket.2"))
  (pid-file     munge-configuration-pid-file
                (default "/var/run/munge/munged.pid"))
  (log-file     munge-configuration-log-file
                (default "/var/log/munge/munged.log"))
  (key          munge-configuration-key
                (default "/etc/munge/munge.key")))

(define (munge-activation config)
  "Return the activation GEXP for CONFIG for the munge service."
  (with-imported-modules '((guix build utils))
    #~(begin
        (define %user (getpw "munge"))
        (let* ((homedir     (passwd:dir %user))
               (key         #$(munge-configuration-key config))
               (etc-dir     (dirname key))
               (run-dir     (dirname #$(munge-configuration-pid-file config)))
               (log-dir     (dirname #$(munge-configuration-log-file config))))
          (for-each (lambda (dir)
                      (unless (file-exists? dir)
                        (mkdir-p dir))
                      (chown dir (passwd:uid %user) (passwd:gid %user))
                      (chmod dir #o700))
                    (list homedir etc-dir log-dir))
          (unless (file-exists? key)
            (invoke #$(file-append (munge-configuration-package config)
                                 "/sbin/mungekey")
                    "--create"
                    (string-append "--bits=" (number->string (* 8 1024))) ; bits, not bytes
                    (string-append "--keyfile=" key)))
          (chown key (passwd:uid %user) (passwd:gid %user))
          (chmod key #o400)
          (unless (file-exists? run-dir)
            (mkdir-p run-dir))
          (chown run-dir (passwd:uid %user) (passwd:gid %user))))))

(define munge-shepherd-service
  (match-lambda
    (($ <munge-configuration> package socket pid-file log-file key)
     (list
       (shepherd-service
         (documentation "Munge server")
         (provision '(munge))
         (requirement '(loopback user-processes))
         (start #~(make-forkexec-constructor
                    (list #$(file-append package "/sbin/munged")
                          "--foreground"    ; "--force"
                          (string-append "--socket=" #$socket)
                          (string-append "--key-file=" #$key)
                          (string-append "--pid-file=" #$pid-file)
                          (string-append "--log-file=" #$log-file))
                    #:user "munge"
                    #:group "munge"
                    #:pid-file #$pid-file
                    #:log-file #$log-file))
         (stop #~(lambda _
                   (not (and
                          (list #$(file-append package "/sbin/munged")
                                (string-append "--socket=" #$socket)
                                "--stop")
                          ;; This seems to not be removed by default.
                          (delete-file (string-append #$socket ".lock"))))))
         (auto-start? #t))))))

(define munge-service-type
  (service-type
    (name 'munge)
    (extensions
      (list
        (service-extension shepherd-root-service-type
                           munge-shepherd-service)
        (service-extension activation-service-type
                           munge-activation)
        (service-extension account-service-type
                           (const %munge-accounts))
        (service-extension profile-service-type
                           (compose list munge-configuration-package))))
    (default-value (munge-configuration))
    (description
     "Run @url{https://dun.github.io/munge/,Munge}, an authentication service.")))

;; Initial documentation for upstreaming:
;@subsubheading Munge
;
;The following example describes a Munge service with the default configuration.
;
;@lisp
;(service munge-service-type)
;@end lisp
;
;@deftp {Data Type} munge-configuration
;Data type representing the configuration for the @code{munge-service-type}.
;
;@table @asis
;@item @code{package}
;Munge package to use for the service.
;
;@item @code{socket} (default "/var/run/munge/munge.socket.2")
;The socket Munge should use.
;
;@item @code{pid-file} (default "/var/run/munge/munged.pid")
;The PID file which Munge should use.
;
;@item @code{log-file} (default "/var/log/munge/munged.log")
;The location of the log file Munge should write to.
;
;@item @code{key} (default "/etc/munge/munge.key")
;The location of the shared key Munge should use.  Since this a shared secret key between the different nodes it should not be added to the store.
;
;@end table
;@end deftp


;; TODO: Make id/uid configurable
(define %slurm-accounts
  (list (user-group
          (name "slurm")
          (id 901)
          (system? #t))
        (user-account
          (name "slurm")
          (group "slurm")
          (uid 901)
          (system? #t)
          (comment "Slurm User")
          (home-directory "/var/lib/slurm"))))

(define-record-type* <slurm-configuration>
  slurm-configuration
  make-slurm-configuration
  slurm-configuration?
  ;; As I understand it, all the services depend on also running slurmd on
  ;; that machine.  Therefore it makes sense to have one config section with
  ;; "common" and "extended" options.  With all the possible options and
  ;; versions we only cover the ones which affect the services.
  (package              slurm-configuration-package
                        (default slurm))
  (slurm-conf-file      slurm-configuration-slurm-conf-file
                        (default "/etc/slurm/slurm.conf"))
  (slurmd-log-file      slurm-configuration-slurmd-log-file
                        (default #f))               ; #f for syslog
  (slurmd-pid-file      slurm-configuration-slurmd-pid-file
                        (default "/var/run/slurmd.pid"))

  (slurmd-spooldir      slurm-configuration-slurmd-spooldir
                        (default "/var/spool/slurmd"))

  (run-slurmctld?       slurm-configuration-run-slurmctld
                        (default #f))
  (slurmctld-log-file   slurm-configuration-slurmctld-log-file
                        (default #f))               ; #f for syslog
  (slurmctld-pid-file   slurm-configuration-slurmctld-pid-file
                        (default "/var/run/slurmctld.pid"))

  (run-slurmdbd?        slurm-configuration-run-slurmdbd
                        (default #f))
  (slurmdbd-conf-file   slurm-configuration-slurmdbd-conf-file
                        (default "/etc/slurm/slurmdbd.conf"))
  (slurmdbd-pid-file    slurm-configuration-slurmdbd-pid-file
                        (default "/var/run/slurmdbd.pid")))


(define (slurm-activation config)
  "Return the activation GEXP for CONFIG for the slurm service."
  (with-imported-modules '((guix build utils))
    #~(begin
        (use-modules (guix build utils))
        (let* ((%user       (getpw "slurm"))
               (spooldir    #$(slurm-configuration-slurmd-spooldir config))
               (logdir      (dirname (or #$(slurm-configuration-slurmd-log-file config)
                                         #$(slurm-configuration-slurmctld-log-file config)
                                         "/var/log/slurmd.log")))
               (piddir      (dirname #$(slurm-configuration-slurmd-pid-file config))))
          (unless (file-exists? spooldir)
            (mkdir-p spooldir))
          (chown spooldir (passwd:uid %user) (passwd:gid %user))
          (when logdir
            (unless (file-exists? logdir)
              (mkdir-p logdir))
            (when (> (string-length logdir) (string-length "/var/log"))
              (chown logdir (passwd:uid %user) (passwd:gid %user))))
          (unless (file-exists? piddir)
            (mkdir-p piddir)))
        ;; /etc/slurm/slurm.conf needs to exist.
        (file-exists? #$(slurm-configuration-slurm-conf-file config)))))

(define slurmd-shepherd-service
  (match-lambda
    (($ <slurm-configuration> package slurm-conf-file slurmd-log-file slurmd-pid-file)
     (list
       (shepherd-service
         (documentation "Slurmd server")
         (provision '(slurmd))
         (requirement '(loopback munge))
         (start #~(make-forkexec-constructor
                    (list #$(file-append package "/sbin/slurmd")
                          ;"-L" #$slurmd-log-file
                          "-f" #$slurm-conf-file)
                    #:pid-file #$slurmd-pid-file))
         (stop #~(make-kill-destructor)))))))

(define slurmctld-shepherd-service
  (match-lambda
    (($ <slurm-configuration> package slurm-conf-file _ _ _ run-slurmctld? slurmctld-log-file slurmctld-pid-file)
     (list
       (shepherd-service
         (documentation "Slurmctld server")
         (provision '(slurmctld))
         (requirement '(loopback munge))
         (start #~(make-forkexec-constructor
                    (list #$(file-append package "/sbin/slurmctld")
                          ;"-L" #$slurmctld-log-file
                          "-f" #$slurm-conf-file)
                    #:pid-file #$slurmctld-pid-file))
         (stop #~(make-kill-destructor))
         (auto-start? run-slurmctld?))))))

(define (slurmdbd-activation config)
  "Test the Slurmdbd configration exists."
  (when (slurm-configuration-run-slurmdbd config)
    (file-exists?
      (slurm-configuration-slurmdbd-conf-file config)))
  #t)

(define slurmdbd-shepherd-service
  (match-lambda
    (($ <slurm-configuration> package _ _ _ _ _ _ _ run-slurmdbd? slurmdbd-conf-file slurmdbd-pid-file)
     (list
       (shepherd-service
         (documentation "Slurmdbd server")
         (provision '(slurmdbd))
         (requirement '(loopback munge))
         (start #~(make-forkexec-constructor
                    (list #$(file-append package "/sbin/slurmdbd"))
                    #:pid-file #$slurmdbd-pid-file))
         (stop #~(make-kill-destructor))
         (auto-start? run-slurmdbd?))))))

(define (slurm-services-to-run config)
  (append (slurmd-shepherd-service config)
          (if (slurm-configuration-run-slurmctld config)
            (slurmctld-shepherd-service config)
            '())
          (if (slurm-configuration-run-slurmdbd config)
            (slurmdbd-shepherd-service config)
            '())))

(define slurm-service-type
  (service-type
    (name 'slurm)
    (extensions
      (list
        (service-extension shepherd-root-service-type
                           slurm-services-to-run)
        (service-extension activation-service-type
                           slurm-activation)
        (service-extension activation-service-type
                           slurmdbd-activation)
        (service-extension account-service-type
                           (const %slurm-accounts))
        (service-extension profile-service-type
                           (compose list slurm-configuration-package))))
    (default-value (slurm-configuration))
    (description
     "Run @url{https://slurm.schedmd.com/slurm.html,Slurm}, a workflow manager
service.  Optionally also run @code{slurmctld} and @code{slurmdbd}.")))