summaryrefslogtreecommitdiff
path: root/web/config.scm
blob: 9b9fd38c2747f0637f79adc908f3e193f0a2e07d (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
;;; config.scm -- Web server's config
;;; Copyright © 2020, 2021 Masaya Tojo <masaya@tojo.tokyo>
;;;
;;; This program 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.
;;;
;;; This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.

(use-modules (gnu)
             (gnu packages admin)
             (guix modules)
             (gnu packages tls)
             (tojo-tokyo packages guile-xyz))
(use-service-modules networking ssh web certbot admin mcron)

(define heartbeat-job
  #~(job '(next-minute (range 0 60 5))
         #$(with-extensions (list guile-tojo-tokyo-monitoring
                                  gnutls)
             (program-file
              "heartbeat"
              #~(begin
                  (use-modules (tojo-tokyo monitoring))
                  (heartbeat
                   ((disk-use%-over? 80) (display "[heartbeat] disk usage warning!\n"))
                   ((disk-iuse%-over? 60) (display "[heartbeat] inode usage warning!\n"))))))))

(define %nginx-deploy-hook
  (program-file
   "nginx-deploy-hook"
   #~(let ((pid (call-with-input-file "/var/run/nginx/pid" read)))
       (kill pid SIGHUP))))

(define %reopen-nginx-logs
  #~(let ((pid (call-with-input-file "/var/run/nginx/pid" read)))
      (kill pid SIGUSR1)))

(operating-system
  (locale "en_US.utf8")
  (timezone "Asia/Tokyo")
  (keyboard-layout (keyboard-layout "us"))
  (host-name "web")
  (groups (cons* (user-group (name "ci"))
                 %base-groups))
  (users (cons* (user-account
                  (name "masaya")
                  (comment "Masaya Tojo")
                  (group "users")
                  (home-directory "/home/masaya")
                  (supplementary-groups
                    '("wheel" "netdev" "audio" "video")))
                (user-account
                  (name "ci")
                  (comment "To deploy from ci.tojo.tokyo")
                  (group "ci"))
                %base-user-accounts))
  (packages
    (append
     (list (specification->package "nss-certs")
           (specification->package "rsync"))
     %base-packages))
  (services
    (append
     (list (service dhcp-client-service-type)
           (service ntp-service-type)
           (simple-service 'my-cron-jobs
                           mcron-service-type
                           (list heartbeat-job))
           (service openssh-service-type
                    (openssh-configuration
                      (permit-root-login #f)
                      (password-authentication? #f)
                      (authorized-keys
                       `(("masaya" ,(local-file "masaya.pub"))
                         ("ci" ,(local-file "laminar.pub"))))))
           (service nginx-service-type
                    (nginx-configuration
                      (server-blocks
                        (list
                         (nginx-server-configuration
                           (server-name '("tojo.tokyo"))
                           (ssl-certificate "/etc/letsencrypt/live/tojo.tokyo/fullchain.pem")
                           (ssl-certificate-key "/etc/letsencrypt/live/tojo.tokyo/privkey.pem")
                           (listen '("443 ssl" "[::]:443 ssl"))
                           (raw-content
                            (list
                             ;; Opting Out of Computation of FloC
                             ;; @see https://github.com/WICG/floc#opting-out-of-computation
                             "add_header Permissions-Policy interest-cohort=();"

                             "return 301 https://www.tojo.tokyo;")))
                         (nginx-server-configuration
                           (server-name '("ci.tojo.tokyo"))
                           (ssl-certificate "/etc/letsencrypt/live/ci.tojo.tokyo/fullchain.pem")
                           (ssl-certificate-key "/etc/letsencrypt/live/ci.tojo.tokyo/privkey.pem")
                           (listen '("443 ssl" "[::]:443 ssl"))
                           (locations
                            (list
                             (nginx-location-configuration
                              (uri "/")
                              (body '("proxy_pass http://server.git.tojo.tokyo:8080;")))))
                           (raw-content
                            (list
                             ;; Opting Out of Computation of FloC
                             ;; @see https://github.com/WICG/floc#opting-out-of-computation
                             "add_header Permissions-Policy interest-cohort=();"

                             "access_log /var/log/nginx/ci/access.log;"
                             "error_log /var/log/nginx/ci/error.log;")))
                         (nginx-server-configuration
                           (server-name '("www.tojo.tokyo"))
                           (ssl-certificate "/etc/letsencrypt/live/www.tojo.tokyo/fullchain.pem")
                           (ssl-certificate-key "/etc/letsencrypt/live/www.tojo.tokyo/privkey.pem")
                           (listen '("443 ssl" "[::]:443 ssl"))
                           (root "/srv/www")
                           (locations
                            (list
                             (nginx-location-configuration
                              (uri "/static/image")
                              (body '("expires 5m;")))))
                           (raw-content
                            (list
                             ;; Opting Out of Computation of FloC
                             ;; @see https://github.com/WICG/floc#opting-out-of-computation
                             "add_header Permissions-Policy interest-cohort=();"

                             "access_log /var/log/nginx/www/access.log;"
                             "error_log /var/log/nginx/www/error.log;"
                             "error_page 404 /404-not-found.html;")))
                         (nginx-server-configuration
                           (server-name '("test.tojo.tokyo"))
                           (ssl-certificate "/etc/letsencrypt/live/test.tojo.tokyo/fullchain.pem")
                           (ssl-certificate-key "/etc/letsencrypt/live/test.tojo.tokyo/privkey.pem")
                           (listen '("443 ssl" "[::]:443 ssl"))
                           (root "/srv/test-www")
                           (locations
                            (list
                             (nginx-location-configuration
                              (uri "/static/image")
                              (body '("expires 5m;")))))
                           (raw-content
                            (list
                             ;; Access from a vpn ip only.
                             #~(string-append "include "
                                              #$(local-file "nginx-allow-vpn-ip-only.conf")
                                              ";")

                             ;; Opting Out of Computation of FloC
                             ;; @see https://github.com/WICG/floc#opting-out-of-computation
                             "add_header Permissions-Policy interest-cohort=();"

                             "access_log /var/log/nginx/test/access.log;"
                             "error_log /var/log/nginx/test/error.log;"
                             "error_page 404 /404-not-found.html;")))
                         (nginx-server-configuration
                           (server-name '("files.tojo.tokyo"))
                           (ssl-certificate "/etc/letsencrypt/live/files.tojo.tokyo/fullchain.pem")
                           (ssl-certificate-key "/etc/letsencrypt/live/files.tojo.tokyo/privkey.pem")
                           (listen '("443 ssl" "[::]:443 ssl"))
                           (locations
                            (list
                             (nginx-location-configuration
                              (uri "/")
                              (body '("proxy_pass https://files-tojo-tokyo.ewr1.vultrobjects.com;")))))
                           (raw-content
                            (list
                             ;; Opting Out of Computation of FloC
                             ;; @see https://github.com/WICG/floc#opting-out-of-computation
                             "add_header Permissions-Policy interest-cohort=();"

                             "access_log /var/log/nginx/files/access.log;"
                             "error_log /var/log/nginx/files/error.log;")))
                         (nginx-server-configuration
                           (server-name '("git.tojo.tokyo"))
                           (ssl-certificate "/etc/letsencrypt/live/git.tojo.tokyo/fullchain.pem")
                           (ssl-certificate-key "/etc/letsencrypt/live/git.tojo.tokyo/privkey.pem")
                           (listen '("443 ssl" "[::]:443 ssl"))
                           (locations
                            (list
                             (nginx-location-configuration
                              (uri "~* \\.git/(?:info/refs|git-upload-pack)$")
                              (body '("proxy_pass http://server.git.tojo.tokyo:1080;")))
                             (nginx-location-configuration
                              (uri "/")
                              (body '("proxy_pass http://server.git.tojo.tokyo;")))))
                           (raw-content
                            (list
                             ;; Opting Out of Computation of FloC
                             ;; @see https://github.com/WICG/floc#opting-out-of-computation
                             "add_header Permissions-Policy interest-cohort=();"

                             "access_log /var/log/nginx/git/access.log;"
                             "error_log /var/log/nginx/git/error.log;")))))))
           (service certbot-service-type
                    (certbot-configuration
                      (email "masaya@tojo.tokyo")
                      (certificates
                        (list
                         (certificate-configuration
                           (domains '("tojo.tokyo"))
                           (deploy-hook %nginx-deploy-hook))
                         (certificate-configuration
                           (domains '("www.tojo.tokyo"))
                           (deploy-hook %nginx-deploy-hook))
                         (certificate-configuration
                           (domains '("files.tojo.tokyo"))
                           (deploy-hook %nginx-deploy-hook))
                         (certificate-configuration
                           (domains '("git.tojo.tokyo"))
                           (deploy-hook %nginx-deploy-hook))
                         (certificate-configuration
                           (domains '("ci.tojo.tokyo"))
                           (deploy-hook %nginx-deploy-hook))
                         (certificate-configuration
                           (domains '("test.tojo.tokyo"))
                           (deploy-hook %nginx-deploy-hook))))))
           (simple-service 'rotate-nginx
                           rottlog-service-type
                           (list (log-rotation
                                   (frequency 'daily)
                                   (files (list "/var/log/nginx/access.log"
                                                "/var/log/nginx/error.log"
                                                "/var/log/nginx/www/access.log"
                                                "/var/log/nginx/www/error.log"
                                                "/var/log/nginx/test/access.log"
                                                "/var/log/nginx/test/error.log"
                                                "/var/log/nginx/files/access.log"
                                                "/var/log/nginx/files/error.log"
                                                "/var/log/nginx/ci/access.log"
                                                "/var/log/nginx/ci/error.log"
                                                "/var/log/nginx/git/access.log"
                                                "/var/log/nginx/git/error.log"))
                                   (options '("nostoredir"))
                                   (post-rotate %reopen-nginx-logs)))))
     %base-services))
  (bootloader
    (bootloader-configuration
      (bootloader grub-bootloader)
      (targets '("/dev/vda"))
      (keyboard-layout keyboard-layout)))
  (swap-devices (list "/dev/vda2"))
  (file-systems
    (cons* (file-system
             (mount-point "/")
             (device
               (uuid "de03171e-695e-4d5c-8955-2ef0795c9130"
                     'ext4))
             (type "ext4"))
           %base-file-systems)))