This repository has been archived by the owner on Mar 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
databases.scm
136 lines (131 loc) · 4.65 KB
/
databases.scm
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
(define-module (gds services utils databases)
#:use-module (srfi srfi-1)
#:use-module (gnu services)
#:use-module (gnu services shepherd)
#:use-module (gnu system shadow)
#:use-module ((gnu packages admin)
#:select (shadow sudo))
#:use-module (guix records)
#:use-module (guix gexp)
#:use-module (guix utils)
#:use-module (ice-9 match)
#:use-module (guix packages)
#:use-module (guix gexp)
#:use-module (system foreign)
#:use-module (rnrs bytevectors)
#:use-module (guix gcrypt)
#:use-module (gnu packages base)
#:use-module (gnu packages databases)
#:use-module (gds packages govuk)
#:use-module (gds packages mongodb)
#:use-module (gds services)
#:use-module (gds services utils databases postgresql)
#:use-module (gds services utils databases mysql)
#:use-module (gds services utils databases mongodb)
#:export (<redis-connection-config>
redis-connection-config
redis-connection-config?
redis-connection-config-port
redis-connection-config-namespace
database-connection-config?
database-connection-config->environment-variables
setup-blank-databases-on-service-startup))
(define-record-type* <redis-connection-config>
redis-connection-config make-redis-connection-config
redis-connection-config?
(host redis-connection-config-host
(default "localhost"))
(port redis-connection-config-port
(default 6379))
(db-number redis-connection-config-db-number
(default 0))
(namespace redis-connection-config-namespace
(default #f)))
(define (database-connection-config? config)
(or (postgresql-connection-config? config)
(mysql-connection-config? config)
(mongodb-connection-config? config)
(redis-connection-config? config)))
(define database-connection-config->environment-variables
(match-lambda
(($ <postgresql-connection-config> host user port database)
`(("DATABASE_URL" .
,(simple-format
#f
"postgres://~A:~A/~A"
host
port
database))))
(($ <mysql-connection-config> host user port database)
`(("DATABASE_URL" .
,(simple-format
#f
"mysql2://~A:~A/~A"
host
port
database))))
(($ <mongodb-connection-config> user password host port database)
`(("MONGO_URL" . ,host)
("MONGO_DB" . ,database)
("MONGODB_URI" .
,(simple-format
#f
"mongodb://~A:~A/~A"
host
port
database))))
(($ <redis-connection-config> host port db-number namespace)
`(("REDIS_URL" .
,(simple-format
#f
"redis://~A:~A/~A"
host
port
db-number))
("REDIS_HOST" . ,host)
("REDIS_PORT" . ,(number->string port))
,@(if namespace
`("REDIS_NAMESPACE" . ,namespace)
'())))
(unmatched
(error "get-database-environment-variables no match for ~A"
unmatched))))
(define (setup-blank-databases-on-service-startup s)
(let
((parameters (service-parameters s)))
(if (not (list? parameters))
s
(let* ((ssc (find service-startup-config? parameters))
(database-connection-configs
(filter database-connection-config? parameters)))
(service
(service-kind s)
(map
(lambda (parameter)
(if (service-startup-config? parameter)
(service-startup-config-add-pre-startup-scripts
parameter
(concatenate
(map
(lambda (config)
(cond
((postgresql-connection-config? config)
`((postgresql
.
,(postgresql-create-user-and-database-for-database-connection
config))))
((mysql-connection-config? config)
`((mysql
.
,(mysql-create-user-and-database-for-database-connection
config))))
((mongodb-connection-config? config)
'()) ;; TODO
((redis-connection-config? config)
'()) ;; redis does not require any setup
(else
(error "Unrecognised database config"))))
database-connection-configs))
#:run-as-root #t)
parameter))
parameters))))))