summaryrefslogtreecommitdiff
path: root/px/services/databases.scm
blob: 85107a0da1e796ea7afa681ac1364e50d12cd2d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
;;; Databases service definitions for PantherX
;;; Reza Alizadeh Majd (r.majd@pantherx.org)

(define-module (px services databases)
  #:use-module (gnu services)
  #:use-module (gnu services shepherd)
  #:use-module (gnu system shadow)
  #:use-module (gnu packages admin)
  #:use-module (guix modules)
  #:use-module (guix records)
  #:use-module (guix gexp)
  #:use-module (px packages databases)
  #:use-module (srfi srfi-1)
  #:use-module (ice-9 match)
  #:export (mongodb-configuration 
            mongodb-configuration?
            mongodb-configuration-mongodb
            mongodb-configuration-config-file
            mongodb-configuration-data-directory
            mongodb-service-type))

;;;
;;; MongoDB
;;;
;;; @subsubheading MongoDB
;;; @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
;;; @lisp
;;; (service mongodb-service-type)
;;; @end lisp
;;; @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

(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)
                (description "Run the MongoDB document database server.")
                (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))))