This repository has been archived by the owner on Jan 3, 2022. It is now read-only.
/
cloud-init.scm
291 lines (273 loc) · 12.3 KB
/
cloud-init.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
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
;; -*- mode: scheme; coding: utf-8; -*-
;; Copyright © 2021 David Dashyan <mail@davie.li>
;;
;; This file is part of bigchaindb-guix
;;
;; bigchaindb-guix 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.
;;
;; bigchaindb-guix 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 bigchaindb-guix. If not, see <http://www.gnu.org/licenses/>.
;; Comment: This module provides cloud-init replacement for Guix system. It
;; contains services that can be added to custom cloud images. They will read
;; data provided by cloud metadata services and configure system accordingly.
;; NOTE Experimental and only works with digitalocean at the moment.
(define-module (bigchaindb-guix services cloud-init)
#:use-module (gnu services base)
#:use-module (gnu services)
#:use-module (gnu services shepherd)
#:use-module (gnu packages guile)
#:use-module (gnu packages guile-xyz)
#:use-module (gnu packages package-management)
#:use-module (guix channels)
#:use-module (guix describe)
#:use-module (guix gexp)
#:use-module (guix inferior)
#:use-module (guix modules)
#:use-module (guix records)
#:use-module (ice-9 match)
#:use-module (json)
#:use-module (rnrs bytevectors)
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-11)
#:use-module (web client)
#:use-module (web response)
#:use-module (web uri)
#:export (cloud-init-service-type
cloud-init-config
cloud-init-config?
cloud-init-config-metadata-host
cloud-init-config-metadata-port
make-cloud-init-config))
(define %metadata-host
(make-parameter "169.254.169.254"))
(define %metadata-port
(make-parameter 80))
(define %metadata-path
(make-parameter "/metadata/v1.json"))
(define-record-type* <cloud-init-config>
cloud-init-config make-cloud-init-config
cloud-init-config?
(metadata-host cloud-init-config-metadata-host
(default (%metadata-host)))
(metadata-path cloud-init-config-metadata-path
(default (%metadata-path)))
(log-file cloud-init-config-file
(default "/var/log/cloud-init.log"))
(requirements cloud-init-config-requirements
(default '()))) ;; + loopback
(define* (query-metadata)
(let ((select? (match-lambda
(('json rest ...) #t)
(_ #f))))
(with-extensions (list guile-json-4)
(with-imported-modules (source-module-closure '((json)) #:select? select?)
#~(begin
(use-modules (json)
(ice-9 match)
(web client)
(web request)
(web response)
(web uri)
(rnrs bytevectors))
(let* ((xhost #$(%metadata-host))
(xpath #$(%metadata-path))
(xport #$(%metadata-port))
(uri (build-uri
'http #:host xhost #:path xpath #:port xport))
(addr (cond
((equal? xhost "localhost") INADDR_LOOPBACK)
((string? xhost) (inet-pton AF_INET xhost))
(else (throw 'invalid-host))))
(sock (socket PF_INET SOCK_STREAM 0))
(port-available? (lambda ()
(let loop ((i 0))
(catch 'system-error
(lambda ()
(connect sock AF_INET addr xport)
(close-port sock)
#t)
(lambda _
(if (< i 20)
(begin
(sleep 1)
(loop (+ 1 i)))
#f)))))))
(call-with-values
(lambda ()
(if (port-available?)
(http-request uri #:method 'GET)
(throw 'port-unavailable)))
(lambda (response body)
(if
(eq? (response-code response) 200)
(match body
((? string?) (json-string->scm body))
((? bytevector?) (json-string->scm (utf8->string body))))
(throw 'metadata-query-error response))))))))))
(define hwaddress-inerface-name-alist-gexp
;; XXX: Linux specific
#~(begin
(use-modules (ice-9 match))
(map
(lambda (name)
;; Make (mac-address . interface-name) pair
(call-with-input-file
(string-append "/sys/class/net/" name "/address")
(lambda (file) (cons (string-trim-both (get-string-all file))
name))))
;; interface names list
(let ((dir-stream (opendir "/sys/class/net")))
(let rec ((iface-names '())
(iname (readdir dir-stream)))
(match iname
((? eof-object?) (begin (closedir dir-stream) iface-names))
((or "." "..") (rec iface-names (readdir dir-stream)))
(else (rec (cons iname iface-names)
(readdir dir-stream)))))))))
(define resize-filesystem-gexp
(let ((select? (match-lambda
(('parted rest ...) #t)
(('bytestructures rest ...) #t)
(_ #f))))
(with-extensions (list guile-parted guile-bytestructures)
(with-imported-modules (source-module-closure '((parted))
#:select? select?)
#~(begin
(use-modules (parted))
(let* ((device (get-device "/dev/vda"))
(disk (disk-new device))
(partition (disk-get-partition disk 2))
(constraint (device-get-optimal-aligned-constraint
device)))
(disk-maximize-partition disk partition constraint)
(disk-commit disk)
(system* "/run/current-system/profile/sbin/resize2fs"
"/dev/vda2")))))))
(define (make-cloud-init-gexp)
(with-imported-modules '((guix build syscalls))
#~(begin
(use-modules (guix build syscalls)
(ice-9 match)
(ice-9 textual-ports)
(guix build utils)
(srfi srfi-43))
(let-syntax ((ref (syntax-rules ()
;; nested assoc-ref for working with json alists
;; (ref '((a . ((b . hello!)))) 'a 'b) => 'hello!
((_ alist key key* ...)
(let rec ((al alist) (kl (list key key* ...)))
(match kl
(() al)
(_ (rec (assoc-ref al (car kl))
(cdr kl)))))))))
;; random conf NOTE: do we need it?
(configure-network-interface
"eth0"
(make-socket-address AF_INET (inet-pton AF_INET "169.254.100.100") 0)
(logior IFF_UP 0)
#:netmask (make-socket-address AF_INET (inet-pton AF_INET "255.255.0.0") 0))
;; Metadata vars
(let* ((metadata #$(query-metadata))
(dhcp-enabled (ref metadata "features" "dhcp_enabled"))
(public-keys (ref metadata "public_keys"))
(public-interfaces (ref metadata "interfaces" "public"))
(private-interfaces (ref metadata "interfaces" "private"))
(name-servers (ref metadata "dns" "nameservers"))
(hwaddr-name-alist #$hwaddress-inerface-name-alist-gexp))
;; set iface down NOTE: do we need it?
(let ((sock (socket AF_INET SOCK_STREAM 0)))
(set-network-interface-flags sock "eth0" 0) ;; FIXME
(close-port sock))
;; Configuring network
(for-each
(lambda (iface-info)
(let* ((addr-str (ref iface-info "ipv4" "ip_address"))
(addr (inet-pton AF_INET addr-str))
(sockaddr (make-socket-address AF_INET addr 0))
(mask-str (ref iface-info "ipv4" "netmask"))
(mask (inet-pton AF_INET mask-str))
(maskaddr (make-socket-address AF_INET mask 0))
(gateway-str (ref iface-info "ipv4" "gateway"))
(gateway (inet-pton AF_INET gateway-str))
(gatewayaddr (make-socket-address AF_INET gateway 0))
(iface-name (assoc-ref hwaddr-name-alist
(string-downcase
(ref iface-info "mac")))))
(configure-network-interface
;; ignoring loopback here
iface-name sockaddr IFF_UP #:netmask maskaddr)
(when gateway
(let ((sock (socket AF_INET SOCK_DGRAM 0)))
(add-network-route/gateway sock gatewayaddr)
(close-port sock)))))
(vector->list public-interfaces))
(display "Inserting ssh keys\n")
(mkdir-p "/root/.ssh")
(call-with-output-file "/root/.ssh/authorized_keys"
(lambda (f)
(for-each (lambda (key) (display key f) (newline f))
;; vector-for-each is unbound here for some reason
(vector->list (ref metadata "public_keys")))))
(display "Populating /etc/resolv.conf...\n")
(call-with-output-file "/etc/resolv.conf"
(lambda (f)
(let ((content (string-append
"# Generated by 'cloud-init-service'.\n"
(string-join
(map (lambda (x) (string-append "nameserver " x))
(vector->list name-servers))
"\n" 'suffix))))
(display content f))))
(display "Resizing partition...\n")
#$resize-filesystem-gexp)))))
(define (with-gexp-logger file xgexp)
#~(with-output-to-file #$file
(lambda ()
(with-exception-handler
(lambda (err)
(display ";;; Exiting with error:\n")
(display err)
(newline)
(force-output))
(lambda ()
(display ";;; Log start\n")
#$xgexp)))))
(define (cloud-init-shepherd-service config)
(match-record config <cloud-init-config>
(metadata-host metadata-path log-file requirements)
(shepherd-service
(provision '(cloud-init))
(documentation "Configure system")
(requirement `(user-processes loopback ,@requirements))
(start #~(lambda _
#$(with-gexp-logger
log-file
(parameterize ((%metadata-host metadata-host)
(%metadata-path metadata-path))
(make-cloud-init-gexp)))))
(stop #~(lambda _ #t))
(respawn? #f)
(modules `((json)
(ice-9 match)
(guix build syscalls)
(web client)
(web request)
(web response)
(web uri)
,@%default-modules)))))
(define cloud-init-service-type
(service-type
(name 'cloud-init)
(description "")
(extensions
(list (service-extension shepherd-root-service-type
(compose list cloud-init-shepherd-service))))
(default-value (cloud-init-config))))