services: mpd; Refactor start slot directory initialization.

* gnu/services/audio.scm (mpd-shepherd-service): Standardize the way the log
file parent and other directories are initialized in the start slot.
(mympd-shepherd-service): Likewise.
This commit is contained in:
Maxim Cournoyer 2023-04-28 22:10:42 -04:00
parent 98a46c9da6
commit a5d611c19b
No known key found for this signature in database
GPG key ID: 1260E46482E63562

View file

@ -24,6 +24,7 @@ (define-module (gnu services audio)
#:use-module (guix deprecation) #:use-module (guix deprecation)
#:use-module (guix diagnostics) #:use-module (guix diagnostics)
#:use-module (guix i18n) #:use-module (guix i18n)
#:use-module (guix modules)
#:use-module (gnu services) #:use-module (gnu services)
#:use-module (gnu services admin) #:use-module (gnu services admin)
#:use-module (gnu services configuration) #:use-module (gnu services configuration)
@ -575,36 +576,45 @@ (define (mpd-log-rotation config)
(with-shepherd-action 'mpd ('reopen) #f)))))) (with-shepherd-action 'mpd ('reopen) #f))))))
(define (mpd-shepherd-service config) (define (mpd-shepherd-service config)
(match-record config <mpd-configuration> (user package shepherd-requirement (match-record config <mpd-configuration>
log-file playlist-directory (user package shepherd-requirement
db-file state-file sticker-file log-file playlist-directory
environment-variables) db-file state-file sticker-file
environment-variables)
(let ((config-file (mpd-serialize-configuration config)) (let ((config-file (mpd-serialize-configuration config))
(username (user-account-name user))) (username (user-account-name user)))
(shepherd-service (shepherd-service
(documentation "Run the MPD (Music Player Daemon)") (documentation "Run the MPD (Music Player Daemon)")
(requirement `(user-processes loopback ,@shepherd-requirement)) (requirement `(user-processes loopback ,@shepherd-requirement))
(provision '(mpd)) (provision '(mpd))
(start #~(begin (start
(and=> #$(maybe-value log-file) (with-imported-modules (source-module-closure
(compose mkdir-p dirname)) '((gnu build activation)))
#~(begin
(use-modules (gnu build activation))
(let ((user (getpw #$username))) (let ((user (getpw #$username)))
(for-each
(lambda (x)
(when (and x (not (file-exists? x)))
(mkdir-p x)
(chown x (passwd:uid user) (passwd:gid user))))
(list #$(maybe-value playlist-directory)
(and=> #$(maybe-value db-file) dirname)
(and=> #$(maybe-value state-file) dirname)
(and=> #$(maybe-value sticker-file) dirname))))
(make-forkexec-constructor (define (init-directory directory)
(list #$(file-append package "/bin/mpd") (unless (file-exists? directory)
"--no-daemon" (mkdir-p/perms directory user #o755)))
#$config-file)
#:environment-variables '#$environment-variables))) (for-each
init-directory
'#$(map dirname
;; XXX: Delete the potential "syslog"
;; log-file value, which is not a directory.
(delete "syslog"
(filter-map maybe-value
(list db-file
log-file
state-file
sticker-file))))))
(make-forkexec-constructor
(list #$(file-append package "/bin/mpd") "--no-daemon"
#$config-file)
#:environment-variables '#$environment-variables))))
(stop #~(make-kill-destructor)) (stop #~(make-kill-destructor))
(actions (actions
(list (shepherd-configuration-action config-file) (list (shepherd-configuration-action config-file)
@ -871,37 +881,49 @@ (define (serialize-field filename value)
filename-to-field))))) filename-to-field)))))
(define (mympd-shepherd-service config) (define (mympd-shepherd-service config)
(match-record config <mympd-configuration> (package shepherd-requirement (match-record config <mympd-configuration>
user work-directory (package shepherd-requirement user work-directory cache-directory
cache-directory log-level log-to) log-level log-to)
(let ((log-level* (format #f "MYMPD_LOGLEVEL=~a" log-level)) (shepherd-service
(username (user-account-name user))) (documentation "Run the myMPD daemon.")
(shepherd-service (requirement `(loopback user-processes
(documentation "Run the myMPD daemon.") ,@(if (eq? log-to 'syslog)
(requirement `(loopback user-processes '(syslog)
,@(if (eq? log-to 'syslog) '())
'(syslog) ,@shepherd-requirement))
'()) (provision '(mympd))
,@shepherd-requirement)) (start
(provision '(mympd)) (let ((username (user-account-name user)))
(start #~(begin (with-imported-modules (source-module-closure
(let* ((pw (getpwnam #$username)) '((gnu build activation)))
(uid (passwd:uid pw)) #~(begin
(gid (passwd:gid pw))) (use-modules (gnu build activation))
(for-each (lambda (dir)
(mkdir-p dir)
(chown dir uid gid))
(list #$work-directory #$cache-directory)))
(make-forkexec-constructor (let ((user (getpw #$username)))
`(#$(file-append package "/bin/mympd")
"--user" #$username (define (init-directory directory)
#$@(if (eq? log-to 'syslog) '("--syslog") '()) (unless (file-exists? directory)
"--workdir" #$work-directory (mkdir-p/perms directory user #o755)))
"--cachedir" #$cache-directory)
#:environment-variables (list #$log-level*) (for-each
#:log-file #$(if (string? log-to) log-to #f)))) init-directory
(stop #~(make-kill-destructor)))))) '#$(map dirname
;; XXX: Delete the potential 'syslog log-file value,
;; which is not a directory.
(delete 'syslog
(filter-map maybe-value
(list log-to
work-directory
cache-directory))))))
(make-forkexec-constructor
`(#$(file-append package "/bin/mympd")
"--user" #$username
#$@(if (eq? log-to 'syslog) '("--syslog") '())
"--workdir" #$work-directory
"--cachedir" #$cache-directory)
#:environment-variables
(list #$(format #f "MYMPD_LOGLEVEL=~a" log-level))
#:log-file #$(if (string? log-to) log-to #f)))))))))
(define (mympd-accounts config) (define (mympd-accounts config)
(match-record config <mympd-configuration> (user group) (match-record config <mympd-configuration> (user group)