From 2cbbf8a9bf36edb153e04445bb8d52cd056d2767 Mon Sep 17 00:00:00 2001 From: Oleg Pykhalov Date: Thu, 3 Aug 2017 18:12:48 +0300 Subject: [PATCH] services: rsync: Clean up code. * gnu/services/rsync.scm: Clean up. --- gnu/services/rsync.scm | 175 +++++++++++++++++++++---------------------------- 1 file changed, 75 insertions(+), 100 deletions(-) diff --git a/gnu/services/rsync.scm b/gnu/services/rsync.scm index 9cf2bc89a..e1a014a63 100644 --- a/gnu/services/rsync.scm +++ b/gnu/services/rsync.scm @@ -32,61 +32,59 @@ rsync-configuration? rsync-service-type)) +;;;; Commentary: ;;; -;;; Rsync. +;;; This module implements a service that to run instance of Rsync, +;;; files synchronization tool. ;;; +;;;; Code: (define-record-type* rsync-configuration make-rsync-configuration rsync-configuration? - ;; - (package rsync-configuration-package - (default rsync)) - ;; integer - (port-number rsync-configuration-port-number - (default 873)) - ;; string - (pid-file rsync-configuration-pid-file - (default "/var/run/rsyncd.pid")) - ;; string - (lock-file rsync-configuration-lock-file - (default "/var/run/rsyncd.lock")) - ;; string - (log-file rsync-configuration-log-file - (default "/var/log/rsyncd.log")) - ;; Boolean - (use-chroot? rsync-configuration-use-chroot? - (default #f)) - ;; string - (share-path rsync-configuration-share-path - (default "/srv/rsync")) - ;; string - (share-comment rsync-configuration-share-comment + (package rsync-configuration-package ;package + (default rsync)) + (port-number rsync-configuration-port-number ;integer + (default 873)) + (pid-file rsync-configuration-pid-file ;string + (default "/var/run/rsyncd.pid")) + (lock-file rsync-configuration-lock-file ;string + (default "/var/run/rsyncd.lock")) + (log-file rsync-configuration-log-file ;string + (default "/var/log/rsyncd.log")) + (use-chroot? rsync-configuration-use-chroot? ;boolean + (default #f)) + (share-path rsync-configuration-share-path ;string + (default "/srv/rsync")) + (share-comment rsync-configuration-share-comment ;string (default "Rsync share")) - ;; Boolean - (read-only? rsync-configuration-read-only? - (default #f)) - ;; integer - (timeout rsync-configuration-timeout - (default 300))) + (read-only? rsync-configuration-read-only? ;boolean + (default #f)) + (timeout rsync-configuration-timeout ;integer + (default 300)) + (user rsync-configuration-user ;string + (default "rsyncd")) + (group rsync-configuration-group ;string + (default "rsyncd"))) -(define %rsync-accounts - ;; User account and group for rsync. - (list (user-group (name "rsyncd") (system? #t)) - (user-account - (name "rsyncd") - (system? #t) - (group "rsyncd") - (comment "rsyncd privilege separation user") - (home-directory "/var/run/rsyncd") - (shell #~(string-append #$shadow "/sbin/nologin"))))) +(define (rsync-account config) + "Return the user accounts and user groups for CONFIG." + (let ((rsync-user (rsync-configuration-user config))) + (list (user-group (name "rsyncd") (system? #t)) + (user-account + (name rsync-user) + (system? #t) + (group rsync-user) + (comment "rsyncd privilege separation user") + (home-directory "/var/run/rsyncd") + (shell #~(string-append #$shadow "/sbin/nologin")))))) (define (rsync-activation config) "Return the activation GEXP for CONFIG." #~(begin (use-modules (guix build utils)) (let ((share-directory #$(rsync-configuration-share-path config)) - (user (getpw "rsyncd"))) + (user (getpw #$(rsync-configuration-user config)))) (and=> share-directory mkdir-p) (chown share-directory (passwd:uid user) @@ -94,70 +92,47 @@ (define (rsync-config-file config) "Return the rsync configuration file corresponding to CONFIG." - (computed-file - "rsync.conf" - #~(begin - (call-with-output-file #$output - (lambda (port) - (display "# Generated by 'rsync-service'.\n" port) - (format port "pid file = ~a\n" - #$(rsync-configuration-pid-file config)) - (format port "lock file = ~a\n" - #$(rsync-configuration-lock-file config)) - (format port "log file = ~a\n" - #$(rsync-configuration-log-file config)) - (format port "port = ~a\n" - #$(number->string - (rsync-configuration-port-number config))) - (format port "use chroot = ~a\n" - #$(if (rsync-configuration-use-chroot? config) - "true" "false")) - (display "gid = rsyncd\n" port) - (display "[files]\n" port) - (format port "path = ~a\n" - #$(rsync-configuration-share-path config)) - (format port "comment = ~a\n" - #$(rsync-configuration-share-comment config)) - (format port "read only = ~a\n" - #$(if (rsync-configuration-read-only? config) - "true" "false")) - (format port "timeout = ~a\n" - #$(number->string - (rsync-configuration-timeout config))) - #t))))) + (match config + (($ package port-number pid-file + lock-file log-file use-chroot? share-path + share-comment read-only? timeout user group) + (mixed-text-file "rsync.conf" + "# Generated by 'rsync-service'.\n" + "pid file = " pid-file "\n" + "lock file = " lock-file "\n" + "log file = " log-file "\n" + "port = " (number->string port-number) "\n" + "use chroot = " (if use-chroot? "true" "false") "\n" + "gid = " group "\n" + "[files]\n" + "path = " share-path "\n" + "comment = " share-comment "\n" + "read only = " (if read-only? "true" "false") "\n" + "timeout = " (number->string timeout) "\n")))) (define (rsync-shepherd-service config) "Return a for rsync with CONFIG." - - (define rsync-command - #~(list (string-append #$(rsync-configuration-package config) "/bin/rsync") - "--daemon" "--config" #$(rsync-config-file config))) - - (define pid-file - (rsync-configuration-pid-file config)) - - (define user - (let ((port (rsync-configuration-port-number config))) - (if (> port 1024) - "rsyncd" - "root"))) - - (list (shepherd-service - (provision '(rsync)) - (documentation "Run rsync daemon.") - (start #~(make-forkexec-constructor #$rsync-command - #:pid-file #$pid-file - #:user #$user - #:group #$user)) - (stop #~(make-kill-destructor))))) + (let* ((rsync (rsync-configuration-package config)) + (pid-file (rsync-configuration-pid-file config)) + (port (rsync-configuration-port-number config)) + (user (if (> port 1024) (rsync-configuration-user config) "root"))) + (list (shepherd-service + (provision '(rsync)) + (documentation "Run rsync daemon.") + (start #~(make-forkexec-constructor + (list (string-append #$rsync "/bin/rsync") + "--config" #$(rsync-config-file config) + "--daemon") + #:pid-file #$pid-file + #:user #$user + #:group #$user)) + (stop #~(make-kill-destructor)))))) (define rsync-service-type (service-type (name 'rsync) (extensions - (list (service-extension shepherd-root-service-type - rsync-shepherd-service) - (service-extension account-service-type - (const %rsync-accounts)) - (service-extension activation-service-type - rsync-activation))))) + (list (service-extension shepherd-root-service-type rsync-shepherd-service) + (service-extension account-service-type rsync-account) + (service-extension activation-service-type rsync-activation))) + (default-value (rsync-configuration)))) -- 2.13.3