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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
(import scheme
(chicken base)
(chicken process-context)
(chicken format)
(chicken pretty-print)
(chicken condition)
(chicken port)
(chicken io)
medea
matchable
test
http-client
intarweb
uri-common
)
;;;;;;;;;;;;;;;
;; Utilities ;;
;;;;;;;;;;;;;;;
(define *should-run-inline-tests?*
(let ((v (get-environment-variable "INLINE_TESTS")))
(and (string? v)
(string=? v "1"))))
(define *debug?*
(let ((v (get-environment-variable "DEBUG")))
(and (string? v)
(string=? v "1"))))
(define-syntax inline-tests
(syntax-rules ()
((_ expr ...)
(if *should-run-inline-tests?*
(begin expr ...)))))
(define (debug-print text)
(if *debug?*
(print "debug: " text)
(void)))
(define (try-catch catcher fn)
"Tries (fn) and calls (catcher exn) if fn throws"
(handle-exceptions exn (catcher exn) (fn)))
(define default-cfg
`((user-cfg-path
,(string-append (get-environment-variable "HOME") "/" ".desmorc"))
(mgmt-api-url
"http://localhost:9939")
(mgmt-api-key "")))
(define (is-flag-like? args)
(let ([first-char (string-ref (car args) 0)])
(eq? #\- first-char)))
(inline-tests
(test-group "is-flag-like?"
(test "returns true for valid list" #t (is-flag-like? '("-c" "desmo.scm")))
(test "returns false for invalid list" #f (is-flag-like? '("apply")))))
(define (assert-parse-or-exit result)
(if (eq? (car result) 'parse-error)
(begin (print (cadr result))
(exit 1))
#f))
(define (to-json-string datum)
(with-output-to-string (lambda () (write-json datum))))
(define (from-json-string json)
(read-json json))
(inline-tests
(test-group "to-json-string"
(test "transforms alist to json object" "{\"a\":123}"
(to-json-string '((a . 123))))
(test "transforms vector to json array" "[1,2,3]"
(to-json-string #(1 2 3)))
(test "transforms bool to json bool" "true"
(to-json-string #t)))
(test-group "from-json-string"
(test "transforms json object to alist" '((a . 123))
(from-json-string "{\"a\":123}"))
(test "transforms json array to vector" #(1 2 3)
(from-json-string "[1,2,3]"))
(test "transforms json bool to bool" #(#t)
(from-json-string "[true]"))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Parse & eval "apply" subcommand ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define apply-usage-string
(format (string-append
"Usage: desmoctl apply [-f path] SUBCOMMAND~%"
"~%"
"Apply the config at PATH.~%"
"~%"
"Subcommands:~%"
" help Show this help text"
)))
(define (parse-apply-flags args)
(match args
[("-f" path)
`(parse-ok ((apply-config-path ,path)))]
[("help")
`(parse-ok subcmd-apply-help)]
[_
`(parse-error ,apply-usage-string)]))
(inline-tests
(test-group "parse-apply-flags"
(test "returns parse-ok for valid args (-f some-path)" 'parse-ok
(car (parse-apply-flags '("-f" "some-path"))))
(test "returns parse-ok for valid args (help)" 'parse-ok
(car (parse-apply-flags '("help"))))
(test "returns parse-error for invalid args" 'parse-error
(car (parse-apply-flags '("foo" "bar"))))
(test "returns parse-error for empty args" 'parse-error
(car (parse-apply-flags '())))))
(define (parse-subcommand-apply args)
(match (parse-apply-flags args)
[('parse-ok 'subcmd-apply-help)
`(parse-ok subcmd-apply-help ())]
[('parse-ok . rest)
`(parse-ok subcmd-apply ,(car rest))]
[other
other]))
(inline-tests
(test-group "parse-subcommand-apply"
(test "returns parse-ok for help command" '(parse-ok subcmd-apply-help ())
(parse-subcommand-apply '("help")))
(test "returns parse-ok for -f foo.txt" '(parse-ok subcmd-apply ((apply-config-path "foo.txt")))
(parse-subcommand-apply '("-f" "foo.txt")))))
(define (mock-post-fn . rest)
(print (format "mock-post-fn called with: ~A" rest))
rest)
(define (post-fn url api-key json)
(with-input-from-request
(make-request method: 'POST
uri: (uri-reference url)
headers: (headers `((x-api-key ,api-key))))
json read-string))
(define (post-prison post-fn cfg prison-json)
(let* ((api-url (cadr (assoc 'mgmt-api-url cfg)))
(api-key (cadr (assoc 'mgmt-api-key cfg)))
(req-url (string-append api-url "/prisons")))
(post-fn req-url api-key prison-json)))
(define (run-apply cfg)
(define apply-config-content
(let* ((path (cadr (assoc 'apply-config-path cfg)))
(catcher (lambda (e)
(print (format "Error: no user config found at ~A" path))
(exit 1)))
(open-apply-cfg (lambda () (read (open-input-file path)))))
(try-catch catcher open-apply-cfg)))
(if *debug?*
(begin
(debug-print "apply cfg:")
(pretty-print apply-config-content)))
(define apply-lst (vector->list apply-config-content))
(print (format "Applying all defined prisons (~A)..." (length apply-lst)))
(define (apply-prison prison)
(print (format "Applying prison \"~A\"..." (cdr (assoc 'name prison))))
(define apply-json (to-json-string prison))
(debug-print "apply json:")
(debug-print apply-json)
(define api-response
(post-prison post-fn cfg apply-json))
(debug-print "api-response:")
(debug-print (format "~A" api-response)))
(for-each apply-prison apply-lst)
(print "Apply complete"))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Parse flags and subcommand ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define usage-string
(format (string-append
"Usage: desmoctl [-c cfg-path] SUBCOMMAND~%"
"~%"
"Flags:~%"
" -c cfg-path Path to your user config file (default: $HOME/.desmorc)~%"
"~%"
"Workload subcommands:~%"
" build Build a workload archive~%"
" push Push a workload archive~%"
"~%"
"Orchestration subcommands:~%"
" apply Apply prison config~%"
" status Show prison status~%"
" logs Show logs~%"
" help Show this help text~%"
"~%"
"Run \"desmoctl SUBCOMMAND help\" to see help for a subcommand."
)))
(define (parse-top-level-flags opts args)
"Returns a list of parsed options and the remaining arguments."
(match args
[()
`(parse-ok ,opts '())]
[("-c" user-cfg-path . rest)
(parse-top-level-flags (cons `(user-cfg-path ,user-cfg-path) opts)
rest)]
[(? is-flag-like?)
`(parse-error ,usage-string)]
[_
`(parse-ok ,opts ,args)]))
(inline-tests
(test-group "parse-top-level-flags"
(test "returns parse-error for invalid flag" 'parse-error
(car (parse-top-level-flags default-cfg '("--invalid-flag"))))
(test "returns parse-ok for empty args" 'parse-ok
(car (parse-top-level-flags default-cfg '())))
(test "returns parse-ok for valid flag" 'parse-ok
(car (parse-top-level-flags default-cfg '("-c" "foobar.scm" "status"))))
(test "returns parse-ok for valid flag and subcommand" 'parse-ok
(car (parse-top-level-flags default-cfg '("-c" "desmo.scm" "apply"))))))
(define (parse-subcommand args)
(match args
[("build")
'(parse-ok subcmd-build ())]
[("push")
'(parse-ok subcmd-push ())]
[("apply" . rest)
(parse-subcommand-apply rest)]
[("status")
'(parse-ok subcmd-status ())]
[("logs")
'(parse-ok subcmd-logs ())]
[("help")
'(parse-ok subcmd-help ())]
[_
`(parse-error ,usage-string)]))
(inline-tests
(test-group "parse-subcommand"
(test "returns parse-error for invalid subcommand" 'parse-error
(car (parse-subcommand '("invalid-subcommand"))))
(test "returns parse-ok for valid subcommand" 'parse-ok
(car (parse-subcommand '("apply" "-f" "foo.conf"))))))
;; Evaluate subcommand
(define (eval-subcommand subcmd cfg)
(match subcmd
['subcmd-apply (run-apply cfg)]
['subcmd-status (print "TODO status")
(exit 1)]
['subcmd-logs (print "TODO logs")
(exit 1)]
['subcmd-help
(print usage-string) 'done]
['subcmd-apply-help
(print apply-usage-string) 'done]
[other
(print `(eval-error ,(format "invalid subcommand: ~A" other)))]))
;;;;;;;;;;;;;;;;;
;; Run the CLI ;;
;;;;;;;;;;;;;;;;;
(define (run-desmoctl)
(define top-level-flags-parse-result
(parse-top-level-flags default-cfg
(command-line-arguments)))
(assert-parse-or-exit top-level-flags-parse-result)
(define cfg-with-flags
(cadr top-level-flags-parse-result))
(define user-cfg
(let* ((path (cadr (assoc 'user-cfg-path cfg-with-flags)))
(catcher (lambda (e)
(print (format "Note: no user config found at ~A" path))
'()))
(open-user-cfg (lambda () (read (open-input-file path)))))
(try-catch catcher open-user-cfg)))
(if *debug?*
(begin
(debug-print "user-cfg:")
(pretty-print user-cfg)))
(define subcommand-parse-result
(parse-subcommand (caddr top-level-flags-parse-result)))
(assert-parse-or-exit subcommand-parse-result)
(define subcommand
(cadr subcommand-parse-result))
(define subcommand-cfg
(caddr subcommand-parse-result))
(if *debug?*
(begin
(debug-print "subcommand-cfg:")
(pretty-print subcommand-cfg)))
(define cfg (append subcommand-cfg
user-cfg
default-cfg))
(if *debug?*
(begin
(debug-print "cfg:")
(pretty-print cfg)))
(eval-subcommand subcommand cfg))
;; When compiled, run the CLI when executable is run
;; Interpreter should load ./run.scm
(cond-expand
(compiling (run-desmoctl))
(else))
|