aboutsummaryrefslogtreecommitdiff
path: root/guix/forge/gunicorn.scm
blob: 0f7ec91d1e8f1314ad6046614ffe0fe9fdd8d25a (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
;;; guix-forge --- Guix software forge meta-service
;;; Copyright © 2023 Arun Isaac <arunisaac@systemreboot.net>
;;;
;;; This file is part of guix-forge.
;;;
;;; guix-forge is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published
;;; by the Free Software Foundation, either version 3 of the License,
;;; or (at your option) any later version.
;;;
;;; guix-forge is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with guix-forge.  If not, see
;;; <https://www.gnu.org/licenses/>.

(define-module (forge gunicorn)
  #:use-module (forge socket)
  #:use-module (gnu build linux-container)
  #:use-module ((gnu packages admin) #:select (shadow))
  #:use-module ((gnu packages python) #:select (python-minimal))
  #:use-module ((gnu packages python-web) #:select (gunicorn))
  #:use-module (gnu services)
  #:use-module (gnu services shepherd)
  #:use-module (gnu system file-systems)
  #:use-module (gnu system shadow)
  #:use-module (guix gexp)
  #:use-module (guix least-authority)
  #:use-module (guix modules)
  #:use-module (guix monads)
  #:use-module (guix profiles)
  #:use-module (guix records)
  #:use-module (guix search-paths)
  #:use-module (guix store)
  #:use-module (ice-9 match)
  #:use-module (srfi srfi-1)
  #:export (gunicorn-service-type
            gunicorn-configuration
            gunicorn-configuration?
            gunicorn-configuration-package
            gunicorn-configuration-apps
            gunicorn-app
            gunicorn-app?
            gunicorn-app-name
            gunicorn-app-package
            gunicorn-app-wsgi-app-module
            gunicorn-app-sockets
            gunicorn-app-workers
            gunicorn-app-environment-variables
            gunicorn-app-mappings))

(define-record-type* <gunicorn-configuration>
  gunicorn-configuration make-gunicorn-configuration
  gunicorn-configuration?
  (package gunicorn-configuration-package
           (default gunicorn))
  (apps gunicorn-configuration-apps
        (default '())))

(define-record-type* <gunicorn-app>
  gunicorn-app make-gunicorn-app
  gunicorn-app?
  this-gunicorn-app
  (name gunicorn-app-name)
  (package gunicorn-app-package)
  (wsgi-app-module gunicorn-app-wsgi-app-module)
  (sockets gunicorn-app-sockets
           (default (list (forge-unix-socket
                           (path (string-append "/var/run/gunicorn/"
                                                (gunicorn-app-name this-gunicorn-app)
                                                "/socket")))))
           (thunked))
  (workers gunicorn-app-workers
           (default 1))
  (environment-variables gunicorn-app-environment-variables
                         (default '()))
  (mappings gunicorn-app-mappings
            (default '())))

(define (gunicorn-app-account-name app)
  "Return name used for user and group running gunicorn @var{app}."
  (string-append "gunicorn-" (gunicorn-app-name app)))

(define (gunicorn-accounts config)
  (append-map (lambda (app)
                (let ((name (gunicorn-app-account-name app)))
                  (list (user-account
                         (name name)
                         (group name)
                         (system? #t)
                         (comment (string-append "gunicorn user for app "
                                                 (gunicorn-app-name app)))
                         (home-directory "/var/empty")
                         (shell (file-append shadow "/sbin/nologin")))
                        (user-group
                         (name name)
                         (system? #t)))))
              (gunicorn-configuration-apps config)))

(define (gunicorn-activation config)
  (with-imported-modules '((guix build utils))
    #~(begin
        (use-modules (guix build utils))

        ;; Create socket directories and set ownership.
        (for-each (match-lambda
                    ((username socket-directories ...)
                     (for-each (lambda (socket-directory)
                                 (mkdir-p socket-directory)
                                 (let ((user (getpw username)))
                                   (chown socket-directory (passwd:uid user) (passwd:gid user))))
                               socket-directories)))
                  '#$(map (lambda (app)
                            (cons (gunicorn-app-account-name app)
                                  (filter-map (lambda (socket)
                                                (and (forge-unix-socket? socket)
                                                     (dirname (forge-unix-socket-path socket))))
                                              (gunicorn-app-sockets app))))
                          (gunicorn-configuration-apps config))))))

(define socket->gunicorn-bind
  (match-lambda
    (($ <forge-host-socket> hostname port)
     (string-append hostname ":" port))
    (($ <forge-ip-socket> (? ipv4-address? ip) port)
     (string-append ip ":" (number->string port)))
    (($ <forge-ip-socket> (? ipv6-address? ip) port)
     (string-append "[" ip "]:" (number->string port)))
    (($ <forge-unix-socket> path)
     (string-append "unix:" path))))

(define (gunicorn-shepherd-services config)
  (map (lambda (app)
         (shepherd-service
          (documentation (string-append "Run gunicorn for app "
                                        (gunicorn-app-name app)
                                        "."))
          (provision '(gunicorn))
          (requirement '(networking))
          (modules '((guix search-paths)
                     (ice-9 match)))
          (start
           (let* ((name (string-append "gunicorn-" (gunicorn-app-name app)))
                  (app-manifest (packages->manifest
                                 (list python-minimal
                                       (gunicorn-app-package app))))
                  (app-profile (profile
                                (content app-manifest)
                                (allow-collisions? #t))))
             (with-imported-modules (source-module-closure '((guix search-paths)))
               #~(make-forkexec-constructor
                  (cons* #$(least-authority-wrapper
                            (file-append (gunicorn-configuration-package config)
                                         "/bin/gunicorn")
                            #:name (string-append name "-pola-wrapper")
                            #:mappings (cons (file-system-mapping
                                              ;; Mapping the app package
                                              (source app-profile)
                                              (target source))
                                             (append
                                              ;; Mappings for Unix socket directories
                                              (filter-map (lambda (socket)
                                                            (and (forge-unix-socket? socket)
                                                                 (file-system-mapping
                                                                  (source (dirname (forge-unix-socket-path socket)))
                                                                  (target source)
                                                                  (writable? #t))))
                                                          (gunicorn-app-sockets app))
                                              ;; Additional mappings
                                              (gunicorn-app-mappings app)))
                            #:preserved-environment-variables
                            (map search-path-specification-variable
                                 (manifest-search-paths app-manifest))
                            ;; TODO: If socket is a Unix socket, run in a
                            ;; network namespace. We can't do this yet due to
                            ;; https://yhetil.org/guix/m1ilknoi5r.fsf@fastmail.net/
                            #:namespaces (delq 'net %namespaces))
                         "--workers" #$(number->string (gunicorn-app-workers app))
                         '#$(append (append-map (lambda (socket)
                                                  (list "--bind"
                                                        (socket->gunicorn-bind socket)))
                                                (gunicorn-app-sockets app))
                                    (append-map (match-lambda
                                                  ((variable . value)
                                                   (list "--env"
                                                         (string-append variable "=" value))))
                                                (gunicorn-app-environment-variables app))
                                    (list (gunicorn-app-wsgi-app-module app))))
                  #:user #$name
                  #:group #$name
                  #:environment-variables
                  (map (match-lambda
                         ((spec . value)
                          (string-append (search-path-specification-variable spec)
                                         "="
                                         value)))
                       (evaluate-search-paths
                        (map sexp->search-path-specification
                             '#$(map search-path-specification->sexp
                                     (manifest-search-paths app-manifest)))
                        (list #$app-profile)))
                  #:log-file #$(string-append "/var/log/" name ".log")))))
          (stop #~(make-kill-destructor))))
       (gunicorn-configuration-apps config)))

(define gunicorn-service-type
  (service-type
   (name 'gunicorn)
   (description "Run gunicorn.")
   (extensions (list (service-extension account-service-type
                                        gunicorn-accounts)
                     (service-extension activation-service-type
                                        gunicorn-activation)
                     (service-extension shepherd-root-service-type
                                        gunicorn-shepherd-services)))
   (compose concatenate)
   (extend (lambda (config apps)
             (gunicorn-configuration
              (inherit config)
              (apps (append (gunicorn-configuration-apps config)
                            apps)))))
   (default-value (gunicorn-configuration))))