unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
From: Christopher Baines <mail@cbaines.net>
To: 28198@debbugs.gnu.org
Subject: [bug#28198] [PATCH 3/3] services: Add MongoDB.
Date: Sat, 30 Sep 2017 08:59:42 +0100	[thread overview]
Message-ID: <20170930075942.31332-3-mail@cbaines.net> (raw)
In-Reply-To: <20170930075942.31332-1-mail@cbaines.net>

* gnu/services/databases.scm (%default-mongodb-configuration-file,
  %mongodb-accounts, mongodb-service-type): New variables.
  (<mongodb-configuration>): New record type.
  (mongodb-activation, mongodb-shepherd-service): New procedures.
* gnu/tests/databases.scm (%test-mongodb): New variable.
* doc/guix.text (Database Services): Add MongoDB documentation.
---
 doc/guix.texi              | 26 ++++++++++++++
 gnu/services/databases.scm | 88 ++++++++++++++++++++++++++++++++++++++++++++++
 gnu/tests/databases.scm    | 86 +++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 199 insertions(+), 1 deletion(-)

diff --git a/doc/guix.texi b/doc/guix.texi
index dd0a46a63..5be427ebd 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -12112,6 +12112,32 @@ Additional command line options to pass to @code{memcached}.
 @end table
 @end deftp
 
+@defvr {Scheme Variable} mongodb-service-type
+This is the service type for @uref{https://www.mongodb.com/, MongoDB}.
+The value for the service type is a @code{mongodb-configuration} object.
+@end defvr
+
+@example
+(service mongodb-service-type)
+@end example
+
+@deftp {Data Type} mongodb-configuration
+Data type representing the configuration of mongodb.
+
+@table @asis
+@item @code{mongodb} (default: @code{mongodb})
+The MongoDB package to use.
+
+@item @code{config-file} (default: @code{%default-mongodb-configuration-file})
+The configuration file for MongoDB.
+
+@item @code{data-directory} (default: @code{"/var/lib/mongodb"})
+This value is used to create the directory, so that it exists and is
+owned by the mongodb user.  It should match the data-directory which
+MongoDB is configured to use through the configuration file.
+@end table
+@end deftp
+
 @defvr {Scheme Variable} redis-service-type
 This is the service type for the @uref{https://redis.io/, Redis}
 key/value store, whose value is a @code{redis-configuration} object.
diff --git a/gnu/services/databases.scm b/gnu/services/databases.scm
index de1f6b841..251dbde3d 100644
--- a/gnu/services/databases.scm
+++ b/gnu/services/databases.scm
@@ -44,6 +44,14 @@
             memcached-configuration-udp-port
             memcached-configuration-additional-options
 
+            <mongodb-configuration>
+            mongodb-configuration
+            mongodb-configuration?
+            mongodb-configuration-mongodb
+            mongodb-configuration-config-file
+            mongodb-configuration-data-directory
+            mongodb-service-type
+
             mysql-service
             mysql-service-type
             mysql-configuration
@@ -263,6 +271,86 @@ and stores the database cluster in @var{data-directory}."
                 (default-value (memcached-configuration))))
 
 \f
+;;;
+;;; MongoDB
+;;;
+
+(define %default-mongodb-configuration-file
+  (plain-file
+   "mongodb.yaml"
+   "# GNU Guix: MongoDB default configuration file
+processManagement:
+  pidFilePath: /var/run/mongodb/pid
+storage:
+  dbPath: /var/lib/mongodb
+"))
+
+
+(define-record-type* <mongodb-configuration>
+  mongodb-configuration make-mongodb-configuration
+  mongodb-configuration?
+  (mongodb             mongodb-configuration-mongodb
+                       (default mongodb))
+  (config-file         mongodb-configuration-config-file
+                       (default %default-mongodb-configuration-file))
+  (data-directory      mongodb-configuration-data-directory
+                       (default "/var/lib/mongodb")))
+
+(define %mongodb-accounts
+  (list (user-group (name "mongodb") (system? #t))
+        (user-account
+         (name "mongodb")
+         (group "mongodb")
+         (system? #t)
+         (comment "Mongodb server user")
+         (home-directory "/var/lib/mongodb")
+         (shell (file-append shadow "/sbin/nologin")))))
+
+(define mongodb-activation
+  (match-lambda
+    (($ <mongodb-configuration> mongodb config-file data-directory)
+     #~(begin
+         (use-modules (guix build utils))
+         (let ((user (getpwnam "mongodb")))
+           (for-each
+            (lambda (directory)
+              (mkdir-p directory)
+              (chown directory
+                     (passwd:uid user) (passwd:gid user)))
+            '("/var/run/mongodb" #$data-directory)))))))
+
+(define mongodb-shepherd-service
+  (match-lambda
+    (($ <mongodb-configuration> mongodb config-file data-directory)
+     (shepherd-service
+      (provision '(mongodb))
+      (documentation "Run the Mongodb daemon.")
+      (requirement '(user-processes loopback))
+      (start #~(make-forkexec-constructor
+                `(,(string-append #$mongodb "/bin/mongod")
+                  "--config"
+                  ,#$config-file)
+                #:user "mongodb"
+                #:group "mongodb"
+                #:pid-file "/var/run/mongodb/pid"
+                #:log-file "/var/log/mongodb.log"))
+      (stop #~(make-kill-destructor))))))
+
+(define mongodb-service-type
+  (service-type
+   (name 'mongodb)
+   (extensions
+    (list (service-extension shepherd-root-service-type
+                             (compose list
+                                      mongodb-shepherd-service))
+          (service-extension activation-service-type
+                             mongodb-activation)
+          (service-extension account-service-type
+                             (const %mongodb-accounts))))
+   (default-value
+     (mongodb-configuration))))
+
+\f
 ;;;
 ;;; MySQL.
 ;;;
diff --git a/gnu/tests/databases.scm b/gnu/tests/databases.scm
index 9d9a75374..9e335b27c 100644
--- a/gnu/tests/databases.scm
+++ b/gnu/tests/databases.scm
@@ -25,9 +25,11 @@
   #:use-module (gnu services)
   #:use-module (gnu services databases)
   #:use-module (gnu services networking)
+  #:use-module (gnu packages databases)
   #:use-module (guix gexp)
   #:use-module (guix store)
-  #:export (%test-memcached))
+  #:export (%test-memcached
+            %test-mongodb))
 
 (define %memcached-os
   (simple-operating-system
@@ -121,3 +123,85 @@
    (name "memcached")
    (description "Connect to a running MEMCACHED server.")
    (value (run-memcached-test))))
+
+(define %mongodb-os
+  (operating-system
+    (inherit
+     (simple-operating-system
+      (dhcp-client-service)
+      (service mongodb-service-type)))
+    (packages (cons* mongodb
+                     %base-packages))))
+
+(define* (run-mongodb-test #:optional (port 27017))
+  "Run tests in %MONGODB-OS, forwarding PORT."
+  (define os
+    (marionette-operating-system
+     %mongodb-os
+     #:imported-modules '((gnu services herd)
+                          (guix combinators))))
+
+  (define vm
+    (virtual-machine
+     (operating-system os)
+     (memory-size 1024)
+     (disk-image-size (* 1024 (expt 2 20)))
+     (port-forwardings `((27017 . ,port)))))
+
+  (define test
+    (with-imported-modules '((gnu build marionette))
+      #~(begin
+          (use-modules (srfi srfi-11) (srfi srfi-64)
+                       (gnu build marionette)
+                       (ice-9 popen)
+                       (ice-9 rdelim))
+
+          (define marionette
+            (make-marionette (list #$vm)))
+
+          (mkdir #$output)
+          (chdir #$output)
+
+          (test-begin "mongodb")
+
+          (test-assert "service running"
+            (marionette-eval
+             '(begin
+                (use-modules (gnu services herd))
+                (match (start-service 'mongodb)
+                  (#f #f)
+                  (('service response-parts ...)
+                   (match (assq-ref response-parts 'running)
+                     ((pid) (number? pid))))))
+             marionette))
+
+          (test-eq "test insert"
+            0
+            (system* (string-append #$mongodb "/bin/mongo")
+                     "test"
+                     "--eval"
+                     "db.testCollection.insert({data: 'test-data'})"))
+
+          (test-equal "test find"
+            "test-data"
+            (let* ((port (open-pipe*
+                          OPEN_READ
+                          (string-append #$mongodb "/bin/mongo")
+                          "test"
+                          "--quiet"
+                          "--eval"
+                          "db.testCollection.findOne().data"))
+                   (output (read-line port))
+                   (status (close-pipe port)))
+              output))
+
+          (test-end)
+          (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
+
+  (gexp->derivation "mongodb-test" test))
+
+(define %test-mongodb
+  (system-test
+   (name "mongodb")
+   (description "Connect to a running MONGODB server.")
+   (value (run-mongodb-test))))
-- 
2.14.1

  parent reply	other threads:[~2017-09-30  8:00 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-23  9:18 [bug#28198] [PATCH] Add MongoDB package and service Christopher Baines
2017-08-23  9:25 ` [bug#28198] [PATCH 1/4] vm: Add disk-image-size to <virtual-machine> Christopher Baines
2017-08-23  9:25   ` [bug#28198] [PATCH 2/4] gnu: Add mongodb Christopher Baines
2017-08-31 12:32     ` Ludovic Courtès
2017-09-30  8:04       ` Christopher Baines
2017-08-23  9:25   ` [bug#28198] [PATCH 3/4] services: Add MongoDB Christopher Baines
2017-08-31 12:34     ` Ludovic Courtès
2017-09-30  8:05       ` Christopher Baines
2017-08-23  9:25   ` [bug#28198] [PATCH 4/4] tests: databases: Add MongoDB test Christopher Baines
2017-08-31 12:37     ` Ludovic Courtès
2017-09-30  8:09       ` Christopher Baines
2017-08-31 12:29   ` [bug#28198] [PATCH 1/4] vm: Add disk-image-size to <virtual-machine> Ludovic Courtès
2017-09-25 20:36     ` Christopher Baines
2017-09-26  7:14       ` Ludovic Courtès
2017-09-29  6:48         ` [bug#28635] " Christopher Baines
2017-08-26 22:33 ` [bug#28198] [PATCH] Add MongoDB package and service Roel Janssen
2017-08-27 22:30   ` Christopher Baines
2017-09-30  7:59 ` [bug#28198] [PATCH 1/3] vm: Add disk-image-size to <virtual-machine> Christopher Baines
2017-09-30  7:59   ` [bug#28198] [PATCH 2/3] gnu: Add mongodb Christopher Baines
2017-10-04 14:40     ` Ludovic Courtès
2017-09-30  7:59   ` Christopher Baines [this message]
2017-10-04 14:44     ` [bug#28198] [PATCH 3/3] services: Add MongoDB Ludovic Courtès
2017-10-06 20:29       ` bug#28198: " Christopher Baines

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://guix.gnu.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170930075942.31332-3-mail@cbaines.net \
    --to=mail@cbaines.net \
    --cc=28198@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).