aboutsummaryrefslogtreecommitdiff
path: root/container.scm
blob: af37846ac725550517017947e3e2fcb018dabe1d (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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
;; SPDX-License-Identifier: CC0-1.0

;; Copyright (C) 2022 Wojtek Kosior <koszko@koszko.org>
;;
;; Available under the terms of Creative Commons Zero v1.0 Universal.

(use-modules (gnu)
             (koszko-org-website)
             (sheets-websites)
             (hydrilla-website)
             (hydrilla-json-schemas)
             (hydrilla)
             (ice-9 match)
             ;; The following 4 are needed to construct GUIX_PYTHONPATH for
             ;; Hydrilla WSGI scripts.
             (guix build-system python)
             (guix packages)
             (guix search-paths)
             (guix modules))
(use-package-modules web)
(use-package-modules python)
(use-service-modules web)
(use-service-modules shepherd)

(define %here
  (getcwd))

(define* (g-string-join elements #:optional (joiner " "))
  #~(string-join (list #$@elements) #$joiner))

(define* (g-string-append #:rest args)
  #~(string-append #$@args))

(define (g-package-file package file)
  (g-string-append package "/" file))

(define (httpd-conf-token arg)
  (match arg
    ((? string?)
     (if (or-map (lambda (substr) (string-contains arg substr)) '(" " "\""))
         (format #f "~s" arg)
         arg))
    ((? symbol?)
     (httpd-conf-token (symbol->string arg)))
    ((? gexp?)
     #~(let ((gexp-value #$arg))
         (if (string-contains gexp-value " ")
             (format #f "~s" gexp-value)
             gexp-value)))))

(define* (httpd-directive name #:rest args)
  #~(format #f "~a~%"
            #$(g-string-join (map httpd-conf-token (cons name args)))))

(define* (httpd-tag name args #:rest body)
  (let ((tag-name (httpd-conf-token name)))
    #~(format #f "<~a ~a>~%~a</~a>~%"
              #$tag-name
              #$(g-string-join (map httpd-conf-token args))
              #$(apply g-string-append body)
              #$tag-name)))

(define* (httpd-simple-wsgi-alias package wsgi-path #:key (aliased-path "/"))
  (let ((wsgi-file (g-package-file package wsgi-path)))
    (g-string-append
     (httpd-tag 'Files (list wsgi-file)
                (httpd-directive 'Require 'all 'granted))
     (httpd-directive 'WSGIScriptAlias aliased-path wsgi-file))))

(define* (koszko-httpd-server-meta server-name #:optional (use-www-alias #t))
  (let ((basic-dirs (list (httpd-directive 'ServerName server-name)
                          (httpd-directive 'ServerAdmin "koszko@koszko.org"))))
    (if use-www-alias
        (cons (httpd-directive 'ServerAlias (string-append "www." server-name))
              basic-dirs)
        basic-dirs)))

(define (koszko-httpd-redirect-if-other-host host-name)
  (httpd-tag 'If (list (format #f "%{HTTP_HOST} != '~a'" host-name))
             (httpd-directive
              'Redirect 'permanent "/" (format #f "http://~a/" host-name))))

(define (koszko-httpd-virtualhost-redirect from to)
  (httpd-virtualhost
   "*:80"
   `(,@(koszko-httpd-server-meta from)
     ,(httpd-directive 'Redirect 'permanent "/" (format #f "http://~a" to)))))

(define %koszko-org-virtualhost
  (httpd-virtualhost
   "*:80"
   `(,@(koszko-httpd-server-meta "koszko.org")
     ,(httpd-directive 'DocumentRoot "/srv/http/koszko.org")

     ,(koszko-httpd-redirect-if-other-host "koszko.org")

     ,(httpd-directive 'Alias "/sideload" "/srv/http/koszko.org")

     ,(httpd-simple-wsgi-alias
       koszko-org-website "/share/koszko-org-website/wsgi.py"))))

(define %koszkonutek-tmp-virtualhost
  (koszko-httpd-virtualhost-redirect "koszkonutek-tmp.pl.eu.org" "koszko.org"))

(define %sheets-virtualhosts
  (map
   (lambda (name)
     (httpd-virtualhost
      "*:80"
      `(,@(koszko-httpd-server-meta (string-append name ".koszko.org"))
        ,(httpd-directive
          'DocumentRoot (g-package-file
                         sheets-websites
                         (string-append "/share/" name "-website")))

        ,(koszko-httpd-redirect-if-other-host
          (string-append name ".koszko.org")))))
   '("sheets" "pray")))

(define %hydrillabugs-virtualhost
  (httpd-virtualhost
   "*:80"
   `(,@(koszko-httpd-server-meta "hydrillabugs.koszko.org")

     ,(httpd-tag 'Proxy '("*")
                 (httpd-directive
                  'Redirect 'permanent
                  "/projects/hachette" "/projects/haketilo")
                 ;; I don't remember why I added the following line so I'm
                 ;; keeping it just in case.
                 (httpd-directive 'RequestHeader 'unset 'Accept-Encoding))

     ,(httpd-directive
       'ProxyPass "/projects/haketilo"
       "http://10.207.87.1:21011/projects/hachette")
     ,(httpd-directive
       'ProxyPassReverse "/projects/haketilo"
       "http://10.207.87.1:21011/projects/hachette")

     ,(httpd-directive 'ProxyPass "/" "http://10.207.87.1:21011/")
     ,(httpd-directive 'ProxyPassReverse "/" "http://10.207.87.1:21011/"))))

(define %hachettebugs-virtualhost
  (koszko-httpd-virtualhost-redirect
   "hachettebugs.koszko.org" "hydrillabugs.koszko.org"))

(define %haketilo-virtualhost
  (httpd-virtualhost
   "*:80"
   `(,@(koszko-httpd-server-meta "haketilo.koszko.org")

     ,(koszko-httpd-redirect-if-other-host "haketilo.koszko.org")

     ,(httpd-simple-wsgi-alias
       hydrilla-website "/share/hydrilla-website/wsgi.py"))))

(define %python-path-spec-sexp
   (search-path-specification->sexp
    (guix-pythonpath-search-path (package-version (default-python)))))

(define %hydrilla-pythonpath-inputs
  (cons hydrilla (map cadr (package-transitive-target-inputs hydrilla))))

(define %hydrilla-pythonpath-gexp
  (with-imported-modules (source-module-closure '((guix search-paths)))
    #~(begin
        (use-modules (guix search-paths))
        (let ((evaluated-list (evaluate-search-paths
                      (list (sexp->search-path-specification
                             '#$%python-path-spec-sexp))
                      '#$%hydrilla-pythonpath-inputs)))
          (cdar evaluated-list)))))

(define %hydrilla-virtualhost
  (httpd-virtualhost
   "*:80"
   `(,@(koszko-httpd-server-meta "hydrilla.koszko.org")

     ,(koszko-httpd-redirect-if-other-host "hydrilla.koszko.org")

     ,(httpd-directive
       'Alias "/downloads"
       "/srv/http/hydrilla.koszko.org/downloads")

     ,(httpd-directive
       'Alias "/schemas"
       (g-package-file hydrilla-json-schemas "/share/hydrilla-json-schemas"))

     ,(httpd-directive 'DocumentRoot "/var/lib/hydrilla/malcontent_dirs")

     ,(httpd-tag 'Location '("~" "^/api_v[^/]+/(resource|mapping)/")
                 (httpd-directive 'ForceType 'application/json))

     ,(httpd-directive
       'SetEnvIf 'Request_URI "^/(api_v[0-9]+)/"
       "MALCONENT_DIR=/var/lib/hydrilla/malcontent_dirs/$1")

     ,(httpd-directive
       'SetEnvIf 'Request_URI "^/api_v[0-9]+/"
       (g-string-append "HYDRILLA_GUIX_PYTHONPATH=" %hydrilla-pythonpath-gexp))

     ,(httpd-directive
       'WSGIScriptAliasMatch
       "^/api_v[^/]+/((resource|mapping)/[^/]+[.]json|query|list_all)$"
       (g-string-append (local-file (string-append %here "/hydrilla-wsgi.py"))
                        "/$1")))))

(define %hydrillarepos-virtualhost
  (httpd-virtualhost
   "*:80"
   `(,@(koszko-httpd-server-meta "hydrillarepos.koszko.org" #f)
     ,(httpd-directive 'DocumentRoot "/srv/http/hydrillarepos.koszko.org"))))

(define %wsgi-module
  (httpd-module
   (name "wsgi_module")
   (file (file-append mod-wsgi "/modules/mod_wsgi.so"))))

(define %proxy-http-modules
  (list (httpd-module
         (name "proxy_module")
         (file (file-append httpd "/modules/mod_proxy.so")))
        (httpd-module
         (name "proxy_http_module")
         (file (file-append httpd "/modules/mod_proxy_http.so")))))

;; logio is needed for the '%O' log format directive
(define %logio-module
  (httpd-module
   (name "logio_module")
   (file (file-append httpd "/modules/mod_logio.so"))))

(define %logformat-combined
  "\"%h %l %u %t \\\"%r\\\" %>s %O \\\"%{Referer}i\\\" \\\"%{User-Agent}i\\\"\"")

(define %koszko-httpd-service
  (service
   httpd-service-type
   (httpd-configuration
    (config
     (httpd-config-file
      (server-name "koszko.org")
      (error-log "/var/log/httpd/error.log")
      (modules `(,%wsgi-module
                 ,@%proxy-http-modules
                 ,%logio-module
                 ,@%default-httpd-modules))
      (extra-config
       (list
        (string-join `("LogFormat" ,%logformat-combined "combined")) "\n"
        "CustomLog /var/log/httpd/access.log combined" "\n"
        )))))))

(operating-system
  (host-name "koszko")
  (timezone "Europe/Warsaw")
  (groups (cons* (user-group
                  ;; The `httpd` group must have an explicit id so that the host
                  ;; can provide files that are readable by Apache and not
                  ;; readable by the world.
                  (name "httpd")
                  (id 133)
                  (system? #t))
                 %base-groups))
  (file-systems (cons (file-system
                        (device (file-system-label "does-not-matter"))
                        (mount-point "/")
                        (type "ext4"))
                      %base-file-systems))
  (bootloader (bootloader-configuration
               (bootloader grub-bootloader)
               (targets '("/dev/sdDOES-NOT-MATTER"))))
  (services
   (cons* %koszko-httpd-service
          (simple-service 'koszko-org-website httpd-service-type
                          `(,%koszko-org-virtualhost
                            ,%koszkonutek-tmp-virtualhost
                            ,@%sheets-virtualhosts
                            ,%hydrillabugs-virtualhost
                            ,%hachettebugs-virtualhost
                            ,%haketilo-virtualhost
                            ,%hydrilla-virtualhost
                            ,%hydrillarepos-virtualhost))
          (service
           (shepherd-service-type
            'dummy-network
            (const
             (shepherd-service
              (documentation "Provide 'networking' without actually doing anything")
              (provision '(networking))
              (start #~(const #t))
              (stop #~(const #t))
              (respawn? #f)))
            (description "Make other services assume network is there."))
           #f)
          %base-services)))