Subject: [bug#30803] [PATCH 2/2] services: Add elasticsearch.

From: Christopher Baines <[email-masked]>
Date: Tue, 13 Mar 2018 19:17:27 +0000

---
 gnu/services/databases.scm | 107 +++++++++++++++++++++++++++++++++++++++++++++
 gnu/tests/databases.scm    |  57 +++++++++++++++++++++++-
 2 files changed, 163 insertions(+), 1 deletion(-)

diff --git a/gnu/services/databases.scm b/gnu/services/databases.scm
index 72927c453..cbb9e1699 100644
--- a/gnu/services/databases.scm
+++ b/gnu/services/databases.scm
@@ -51,6 +51,18 @@
             postgresql-service
             postgresql-service-type
 
+            <elasticsearch-configuration>
+            elasticsearch-configuration
+            elasticsearch-configuration?
+            elasticsearch-configuration-elasticsearch
+            elasticsearch-configuration-data-path
+            elasticsearch-configuration-logs-path
+            elasticsearch-configuration-port
+            elasticsearch-configuration-transport-port
+
+            elasticsearch-service
+            elasticsearch-service-type
+
             memcached-service-type
             <memcached-configuration>
             memcached-configuration
@@ -260,6 +272,101 @@ and stores the database cluster in @var{data-directory}."
             (data-directory data-directory))))
 
 
+;;;
+;;; Elasticsearch
+;;;
+
+(define-record-type* <elasticsearch-configuration>
+  elasticsearch-configuration make-elasticsearch-configuration
+  elasticsearch-configuration?
+  (elasticsearch     elasticsearch-configuration-elasticsearch
+                     (default elasticsearch))
+  (data-path         elasticsearch-configuration-data-path
+                     (default "/var/lib/"))
+  (logs-path         elasticsearch-configuration-logs-path
+                     (default "/var/log/elasticsearch"))
+  (http-port         elasticsearch-configuration-port
+                     (default 9200))
+  (transport-port    elasticsearch-configuration-transport-port
+                     (default 9300)))
+
+(define (elasticsearch-configuration-directory
+         data-path logs-path http-port transport-port)
+  (computed-file
+   "elasticsearch-config"
+   #~(begin
+       (mkdir #$output)
+       (mkdir (string-append #$output "/scripts"))
+       (call-with-output-file (string-append #$output "/elasticsearch.yml")
+         (lambda (port)
+           (display
+            (string-append
+             "path.data: " #$data-path "\n"
+             "path.logs: " #$logs-path "\n"
+             "http.port: " #$(number->string http-port) "\n"
+             "transport.tcp.port: " #$(number->string transport-port) "\n")
+            port))))))
+
+(define %elasticsearch-accounts
+  (list (user-group (name "elasticsearch") (system? #t))
+        (user-account
+         (name "elasticsearch")
+         (group "elasticsearch")
+         (system? #t)
+         (comment "Elasticsearch server user")
+         (home-directory "/var/empty")
+         (shell (file-append shadow "/sbin/nologin")))))
+
+(define elasticsearch-activation
+  (match-lambda
+    (($ <elasticsearch-configuration> elasticsearch data-path logs-path
+                                      http-port transport-port)
+     #~(begin
+         (use-modules (guix build utils)
+                      (ice-9 match))
+
+         (let ((user (getpwnam "elasticsearch")))
+           ;; Create db state directory.
+           (for-each
+            (lambda (path)
+              (mkdir-p path)
+              (chown path (passwd:uid user) (passwd:gid user)))
+            '(#$data-path #$logs-path "/var/run/elasticsearch")))))))
+
+(define elasticsearch-shepherd-service
+  (match-lambda
+    (($ <elasticsearch-configuration> elasticsearch data-path logs-path
+                                      http-port transport-port)
+     (list (shepherd-service
+            (provision '(elasticsearch))
+            (documentation "Run the Elasticsearch daemon.")
+            (requirement '(user-processes syslogd))
+            (start #~(make-forkexec-constructor
+                      (list
+                       (string-append #$elasticsearch "/bin/elasticsearch")
+                       "-d"
+                       "-p" "/var/run/elasticsearch/pid"
+                       (string-append
+                        "-Dpath.conf="
+                        #$(elasticsearch-configuration-directory
+                           data-path logs-path http-port transport-port)))
+                      #:user "elasticsearch"
+                      #:pid-file "/var/run/elasticsearch/pid"
+                      #:log-file "/var/log/elasticsearch.log"))
+            (stop #~(make-kill-destructor)))))))
+
+(define elasticsearch-service-type
+  (service-type (name 'elasticsearch)
+                (extensions
+                 (list (service-extension shepherd-root-service-type
+                                          elasticsearch-shepherd-service)
+                       (service-extension activation-service-type
+                                          elasticsearch-activation)
+                       (service-extension account-service-type
+                                          (const %elasticsearch-accounts))))
+                (default-value (elasticsearch-configuration))))
+
+
 ;;;
 ;;; Memcached
 ;;;
diff --git a/gnu/tests/databases.scm b/gnu/tests/databases.scm
index 5c8ca85c1..e07a1f9dd 100644
--- a/gnu/tests/databases.scm
+++ b/gnu/tests/databases.scm
@@ -31,7 +31,8 @@
   #:export (%test-memcached
             %test-mongodb
             %test-postgresql
-            %test-mysql))
+            %test-mysql
+            %test-elasticsearch))
 
 (define %memcached-os
   (simple-operating-system
@@ -319,3 +320,57 @@
    (name "mysql")
    (description "Start the MySQL service.")
    (value (run-mysql-test))))
+
+
+;;;
+;;; The Elasticsearch service.
+;;;
+
+(define %elasticsearch-os
+  (simple-operating-system
+   (service elasticsearch-service-type)))
+
+(define (run-elasticsearch-test)
+  "Run tests in %ELASTICSEARCH-OS."
+  (define os
+    (marionette-operating-system
+     %elasticsearch-os
+     #:imported-modules '((gnu services herd)
+                          (guix combinators))))
+
+  (define vm
+    (virtual-machine
+     (operating-system os)
+     (memory-size 512)))
+
+  (define test
+    (with-imported-modules '((gnu build marionette))
+      #~(begin
+          (use-modules (srfi srfi-64)
+                       (gnu build marionette))
+
+          (define marionette
+            (make-marionette (list #$vm)))
+
+          (mkdir #$output)
+          (chdir #$output)
+
+          (test-begin "elasticsearch")
+
+          (test-assert "service running"
+            (marionette-eval
+             '(begin
+                (use-modules (gnu services herd))
+                (start-service 'elasticsearch))
+             marionette))
+
+          (test-end)
+          (exit (= (test-runner-fail-count (test-runner-current)) 0)))))
+
+  (gexp->derivation "elasticsearch-test" test))
+
+(define %test-elasticsearch
+  (system-test
+   (name "elasticsearch")
+   (description "Start the Elasticsearch service.")
+   (value (run-elasticsearch-test))))
-- 
2.16.2