summaryrefslogtreecommitdiff
path: root/gnu
diff options
context:
space:
mode:
authorOleg Pykhalov <go.wigust@gmail.com>2017-07-27 04:01:01 +0300
committerChristopher Baines <mail@cbaines.net>2017-09-23 21:07:27 +0100
commit9db7e9be59820190a97de22ba72e71fa25f9d168 (patch)
treef0091c714714c8fa469e389143d887cac844b360 /gnu
parentca5915a6e53dfda99cbd9e44c3e02408ae0d7df8 (diff)
gnu: Add rsync service.
* doc/guix.texi (Networking Services): Add rsync service documentation. * gnu/services/rsync.scm (<rsync-configuration>): New file. * gnu/tests/rsync.scm: New file. * gnu/local.mk (GNU_SYSTEM_MODULES): Add new files. Signed-off-by: Christopher Baines <mail@cbaines.net>
Diffstat (limited to 'gnu')
-rw-r--r--gnu/local.mk2
-rw-r--r--gnu/services/rsync.scm172
-rw-r--r--gnu/tests/rsync.scm126
3 files changed, 300 insertions, 0 deletions
diff --git a/gnu/local.mk b/gnu/local.mk
index d7a881eb5a0..711f38c6b4c 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -452,6 +452,7 @@ GNU_SYSTEM_MODULES = \
452 %D%/services/shepherd.scm \ 452 %D%/services/shepherd.scm \
453 %D%/services/herd.scm \ 453 %D%/services/herd.scm \
454 %D%/services/pm.scm \ 454 %D%/services/pm.scm \
455 %D%/services/rsync.scm \
455 %D%/services/sddm.scm \ 456 %D%/services/sddm.scm \
456 %D%/services/spice.scm \ 457 %D%/services/spice.scm \
457 %D%/services/ssh.scm \ 458 %D%/services/ssh.scm \
@@ -498,6 +499,7 @@ GNU_SYSTEM_MODULES = \
498 %D%/tests/mail.scm \ 499 %D%/tests/mail.scm \
499 %D%/tests/messaging.scm \ 500 %D%/tests/messaging.scm \
500 %D%/tests/networking.scm \ 501 %D%/tests/networking.scm \
502 %D%/tests/rsync.scm \
501 %D%/tests/ssh.scm \ 503 %D%/tests/ssh.scm \
502 %D%/tests/virtualization.scm \ 504 %D%/tests/virtualization.scm \
503 %D%/tests/web.scm 505 %D%/tests/web.scm
diff --git a/gnu/services/rsync.scm b/gnu/services/rsync.scm
new file mode 100644
index 00000000000..621e6c46d4b
--- /dev/null
+++ b/gnu/services/rsync.scm
@@ -0,0 +1,172 @@
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2017 Oleg Pykhalov <go.wigust@gmail.com>
3;;;
4;;; This file is part of GNU Guix.
5;;;
6;;; GNU Guix is free software; you can redistribute it and/or modify it
7;;; under the terms of the GNU General Public License as published by
8;;; the Free Software Foundation; either version 3 of the License, or (at
9;;; your option) any later version.
10;;;
11;;; GNU Guix is distributed in the hope that it will be useful, but
12;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19(define-module (gnu services rsync)
20 #:use-module (gnu services)
21 #:use-module (gnu services base)
22 #:use-module (gnu services shepherd)
23 #:use-module (gnu system shadow)
24 #:use-module (gnu packages rsync)
25 #:use-module (gnu packages admin)
26 #:use-module (guix records)
27 #:use-module (guix gexp)
28 #:use-module (srfi srfi-1)
29 #:use-module (srfi srfi-26)
30 #:use-module (ice-9 match)
31 #:export (rsync-configuration
32 rsync-configuration?
33 rsync-service-type))
34
35;;;; Commentary:
36;;;
37;;; This module implements a service that to run instance of Rsync,
38;;; files synchronization tool.
39;;;
40;;;; Code:
41
42(define-record-type* <rsync-configuration>
43 rsync-configuration
44 make-rsync-configuration
45 rsync-configuration?
46 (package rsync-configuration-package ; package
47 (default rsync))
48 (port-number rsync-configuration-port-number ; integer
49 (default 873))
50 (pid-file rsync-configuration-pid-file ; string
51 (default "/var/run/rsyncd/rsyncd.pid"))
52 (lock-file rsync-configuration-lock-file ; string
53 (default "/var/run/rsyncd/rsyncd.lock"))
54 (log-file rsync-configuration-log-file ; string
55 (default "/var/log/rsyncd.log"))
56 (use-chroot? rsync-configuration-use-chroot? ; boolean
57 (default #t))
58 (share-path rsync-configuration-share-path ; string
59 (default "/srv/rsyncd"))
60 (share-comment rsync-configuration-share-comment ; string
61 (default "Rsync share"))
62 (read-only? rsync-configuration-read-only? ; boolean
63 (default #f))
64 (timeout rsync-configuration-timeout ; integer
65 (default 300))
66 (user rsync-configuration-user ; string
67 (default "root"))
68 (group rsync-configuration-group ; string
69 (default "root"))
70 (uid rsync-configuration-uid ; string
71 (default "rsyncd"))
72 (gid rsync-configuration-gid ; string
73 (default "rsyncd")))
74
75(define (rsync-account config)
76 "Return the user accounts and user groups for CONFIG."
77 (let ((rsync-user (if (rsync-configuration-uid config)
78 (rsync-configuration-uid config)
79 (rsync-configuration-user config)))
80 (rsync-group (if (rsync-configuration-gid config)
81 (rsync-configuration-gid config)
82 (rsync-configuration-group config))))
83 (list (user-group (name rsync-group) (system? #t))
84 (user-account
85 (name rsync-user)
86 (system? #t)
87 (group rsync-group)
88 (comment "rsyncd privilege separation user")
89 (home-directory (string-append "/var/run/"
90 rsync-user))
91 (shell #~(string-append #$shadow "/sbin/nologin"))))))
92
93(define (rsync-activation config)
94 "Return the activation GEXP for CONFIG."
95 (with-imported-modules '((guix build utils))
96 #~(begin
97 (let ((share-directory #$(rsync-configuration-share-path config))
98 (user (getpw (if #$(rsync-configuration-uid config)
99 #$(rsync-configuration-uid config)
100 #$(rsync-configuration-user config))))
101 (group (getpw (if #$(rsync-configuration-gid config)
102 #$(rsync-configuration-gid config)
103 #$(rsync-configuration-group config)))))
104 (mkdir-p (dirname #$(rsync-configuration-pid-file config)))
105 (and=> share-directory mkdir-p)
106 (chown share-directory
107 (passwd:uid user)
108 (group:gid group))))))
109
110(define rsync-config-file
111 ;; Return the rsync configuration file corresponding to CONFIG.
112 (match-lambda
113 (($ <rsync-configuration> package port-number pid-file lock-file log-file
114 use-chroot? share-path share-comment read-only?
115 timeout user group uid gid)
116 (if (not (string=? user "root"))
117 (cond
118 ((<= port-number 1024)
119 (error (string-append "rsync-service: to run on port "
120 (number->string port-number)
121 ", user must be root.")))
122 (use-chroot?
123 (error (string-append "rsync-service: to run in a chroot"
124 ", user must be root.")))
125 (uid
126 (error "rsync-service: to use uid, user must be root."))
127 (gid
128 (error "rsync-service: to use gid, user must be root."))))
129 (mixed-text-file
130 "rsync.conf"
131 "# Generated by 'rsync-service'.\n\n"
132 "pid file = " pid-file "\n"
133 "lock file = " lock-file "\n"
134 "log file = " log-file "\n"
135 "port = " (number->string port-number) "\n"
136 "use chroot = " (if use-chroot? "true" "false") "\n"
137 (if uid (string-append "uid = " uid "\n") "")
138 "gid = " (if gid gid "nogroup") "\n" ; no group nobody
139 "\n"
140 "[files]\n"
141 "path = " share-path "\n"
142 "comment = " share-comment "\n"
143 "read only = " (if read-only? "true" "false") "\n"
144 "timeout = " (number->string timeout) "\n"))))
145
146(define (rsync-shepherd-service config)
147 "Return a <shepherd-service> for rsync with CONFIG."
148 (let* ((rsync (rsync-configuration-package config))
149 (pid-file (rsync-configuration-pid-file config))
150 (port-number (rsync-configuration-port-number config))
151 (user (rsync-configuration-user config))
152 (group (rsync-configuration-group config)))
153 (list (shepherd-service
154 (provision '(rsync))
155 (documentation "Run rsync daemon.")
156 (start #~(make-forkexec-constructor
157 (list (string-append #$rsync "/bin/rsync")
158 "--config" #$(rsync-config-file config)
159 "--daemon")
160 #:pid-file #$pid-file
161 #:user #$user
162 #:group #$group))
163 (stop #~(make-kill-destructor))))))
164
165(define rsync-service-type
166 (service-type
167 (name 'rsync)
168 (extensions
169 (list (service-extension shepherd-root-service-type rsync-shepherd-service)
170 (service-extension account-service-type rsync-account)
171 (service-extension activation-service-type rsync-activation)))
172 (default-value (rsync-configuration))))
diff --git a/gnu/tests/rsync.scm b/gnu/tests/rsync.scm
new file mode 100644
index 00000000000..c97836788bf
--- /dev/null
+++ b/gnu/tests/rsync.scm
@@ -0,0 +1,126 @@
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2017 Christopher Baines <mail@cbaines.net>
3;;;
4;;; This file is part of GNU Guix.
5;;;
6;;; GNU Guix is free software; you can redistribute it and/or modify it
7;;; under the terms of the GNU General Public License as published by
8;;; the Free Software Foundation; either version 3 of the License, or (at
9;;; your option) any later version.
10;;;
11;;; GNU Guix is distributed in the hope that it will be useful, but
12;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19(define-module (gnu tests rsync)
20 #:use-module (gnu packages rsync)
21 #:use-module (gnu tests)
22 #:use-module (gnu system)
23 #:use-module (gnu system file-systems)
24 #:use-module (gnu system shadow)
25 #:use-module (gnu system vm)
26 #:use-module (gnu services)
27 #:use-module (gnu services rsync)
28 #:use-module (gnu services networking)
29 #:use-module (guix gexp)
30 #:use-module (guix store)
31 #:export (%test-rsync))
32
33(define* (run-rsync-test rsync-os #:optional (rsync-port 873))
34 "Run tests in %RSYNC-OS, which has rsync running and listening on
35PORT."
36 (define os
37 (marionette-operating-system
38 rsync-os
39 #:imported-modules '((gnu services herd)
40 (guix combinators))))
41
42 (define vm
43 (virtual-machine
44 (operating-system os)
45 (port-forwardings '())))
46
47 (define test
48 (with-imported-modules '((gnu build marionette))
49 #~(begin
50 (use-modules (srfi srfi-11) (srfi srfi-64)
51 (gnu build marionette))
52
53 (define marionette
54 (make-marionette (list #$vm)))
55
56 (mkdir #$output)
57 (chdir #$output)
58
59 (test-begin "rsync")
60
61 ;; Wait for rsync to be up and running.
62 (test-eq "service running"
63 'running!
64 (marionette-eval
65 '(begin
66 (use-modules (gnu services herd))
67 (start-service 'rsync)
68 'running!)
69 marionette))
70
71 ;; Make sure the PID file is created.
72 (test-assert "PID file"
73 (marionette-eval
74 '(file-exists? "/var/run/rsyncd/rsyncd.pid")
75 marionette))
76
77 (test-assert "Test file copied to share"
78 (marionette-eval
79 '(begin
80 (call-with-output-file "/tmp/input"
81 (lambda (port)
82 (display "test-file-contents\n" port)))
83 (zero?
84 (system* "rsync" "/tmp/input"
85 (string-append "rsync://localhost:"
86 (number->string #$rsync-port)
87 "/files/input"))))
88 marionette))
89
90 (test-equal "Test file correctly received from share"
91 "test-file-contents"
92 (marionette-eval
93 '(begin
94 (use-modules (ice-9 rdelim))
95 (zero?
96 (system* "rsync"
97 (string-append "rsync://localhost:"
98 (number->string #$rsync-port)
99 "/files/input")
100 "/tmp/output"))
101 (call-with-input-file "/tmp/output"
102 (lambda (port)
103 (read-line port))))
104 marionette))
105
106 (test-end)
107 (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
108
109 (gexp->derivation "rsync-test" test))
110
111(define* %rsync-os
112 ;; Return operating system under test.
113 (let ((base-os
114 (simple-operating-system
115 (dhcp-client-service)
116 (service rsync-service-type))))
117 (operating-system
118 (inherit base-os)
119 (packages (cons* rsync
120 (operating-system-packages base-os))))))
121
122(define %test-rsync
123 (system-test
124 (name "rsync")
125 (description "Connect to a running RSYNC server.")
126 (value (run-rsync-test %rsync-os))))