-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathdocker.clj
780 lines (707 loc) · 27.3 KB
/
docker.clj
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
(ns docker
"run-container runs containers with a few flavors
- run with stdin content
- run a container but don't wait for it to stop
- run in the background but wait for one message on stdout and then stop
- simply run function without stdin
attach-socket - to running container
write-stdin -
read-loop - input: attached socket output: channel messages
write-to-stdin -input: attached socket, string
inject-secret-transform - transform container entrypoint for secrets
run-streaming-function-with-no-stdin - runs continuously
and read stdout to a callback (used in watcher)"
(:require
[babashka.curl :as curl]
[babashka.fs :as fs]
[cheshire.core :as json]
[clojure.core.async :as async]
[clojure.java.io :as io]
[clojure.string :as string]
[creds]
jsonrpc
[jsonrpc.logger :as logger]
logging
repl
schema
shutdown)
(:import
[java.io PipedInputStream PipedOutputStream]
[java.net UnixDomainSocketAddress]
[java.nio ByteBuffer]
[java.nio.channels SocketChannel]
[java.util Arrays Base64]))
(set! *warn-on-reflection* true)
(defn encode [to-encode]
(.encodeToString (Base64/getEncoder) (.getBytes ^String to-encode)))
;; https://index.docker.io/v1/ does not return IdentityTokens so we
;; probably won't use this endpoint
#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(defn auth
[creds]
(curl/post
"http://localhost/auth"
{:raw-args ["--unix-socket" "/var/run/docker.sock"]
:body (json/generate-string creds)}))
(defn pull-image [{:keys [image identity-token creds]}]
(curl/post
(format "http://localhost/images/create?fromImage=%s" image)
(merge
{:raw-args ["--unix-socket" (let [f (fs/file "/var/run/docker.raw.sock")]
(if (.exists f)
"/var/run/docker.raw.sock"
"/var/run/docker.sock"))]
:throw false}
(when (or creds identity-token)
{:headers {"X-Registry-Auth"
;; I don't think we'll be pulling images
;; from registries that support identity tokens
(-> (cond
identity-token {:identitytoken identity-token}
creds creds)
(json/generate-string)
(encode))}}))))
(defn unix-socket-file [s]
(when (.exists (fs/file s))
s))
(defn- get-backend-socket []
(let [coll [(or (System/getenv "DOCKER_DESKTOP_SOCKET_PATH") "/backend.sock")
(format "%s/Library/Containers/com.docker.docker/Data/backend.sock" (System/getenv "HOME"))]]
(some unix-socket-file coll)))
(defn backend-is-logged-in? [_]
(curl/get
"http://localhost/registry/is-logged-in"
{:raw-args ["--unix-socket" (get-backend-socket)]
:throw false}))
(defn backend-login-info [_]
(curl/get
"http://localhost/registry/info"
{:raw-args ["--unix-socket" (get-backend-socket)]
:throw false}))
(defn backend-get-token [_]
(curl/get
"http://localhost/registry/token"
{:raw-args ["--unix-socket" (get-backend-socket)]
:throw false}))
(comment
(let [pat (string/trim (slurp "/Users/slim/.secrets/dockerhub-pat-ai-tools-for-devs.txt"))]
(pull-image {:image "vonwig/go-linguist:latest"
:creds {:username "jimclark106"
:password pat
:serveraddress "https://index.docker.io/v1/"}})))
(defn list-containers [m]
(curl/get
"http://localhost/containers/json"
{:raw-args ["--unix-socket" "/var/run/docker.sock"]
:throw false}))
(defn list-images [m]
(curl/get
"http://localhost/images/json"
{:raw-args ["--unix-socket" "/var/run/docker.sock"]
:query-params {:filters (json/generate-string m)}
:throw false}))
(defn delete-image [{:keys [image]}]
(curl/delete
(format "http://localhost/images/%s?force=true" image)
{:raw-args ["--unix-socket" "/var/run/docker.sock"]
:throw false}))
(defn container->archive [{:keys [Id path]}]
(curl/get
(format "http://localhost/containers/%s/archive?path=%s" Id path)
{:raw-args ["--unix-socket" "/var/run/docker.sock"]
:throw false
:as :stream}))
(defn parse-port [s]
(let [[_ host container] (re-find #"(.*):(.*)" s)]
{:host {:port host}
:container {:port container
:protocol "tcp"}}))
(defn exposed-ports
" params
- coll of strings (e.g. [\"9222:9222\"])"
[coll]
(->> (for [s coll :let [{:keys [container]} (parse-port s)]]
[(format "%s/%s" (:port container) (:protocol container)) {}])
(into {})))
(defn port-bindings
" params
- coll of strings (e.g. [\"9222:9222\"])"
[coll]
(->>
(for [s coll :let [{:keys [container host]} (parse-port s)]]
[(format "%s/%s" (:port container) (:protocol container)) [{:HostPort (:port host)}]])
(into {})))
;; check for 201
;; entrypoint is an array of strings
;; env is a map
;; Env is an array of name=value strings
;;
;; opts
;;
;; Tty wraps the process in a pseudo terminal
;; StdinOnce closes the stdin after the first client detaches
;; OpenStdin just opens stdin
(defn create-container [{:keys [image entrypoint workdir command host-dir
environment thread-id opts mounts volumes
ports network_mode secrets labels]
:or {opts {:Tty true}}
:as m}]
(let [payload (json/generate-string
(merge
{:Image image}
opts
(when environment
{:Env (->> environment
(map (fn [[k v]] (format "%s=%s" (name k) v)))
(into []))})
{:Labels (->> (concat
(->> secrets
keys
(map (fn [secret-key]
(let [s (name secret-key)]
[(format "x-secret:%s" s) (string/trim (format "/secret/%s" s))]))))
(seq labels))
(into {}))}
{:HostConfig
(merge
{:Binds
(concat
(when host-dir [(format "%s:/project:rw" host-dir)])
(when thread-id [(format "%s:/thread:rw" thread-id)])
(or volumes mounts))}
(when network_mode
{:NetworkMode network_mode})
(when ports
{:PortBindings (port-bindings ports)}))
:WorkingDir (or workdir "/project")}
(when ports {:ExposedPorts (exposed-ports ports)})
(when entrypoint {:Entrypoint entrypoint})
(when command {:Cmd command})))
ascii-payload (String. (.getBytes payload "ASCII"))]
(curl/post
(format
"http://localhost/containers/create%s"
(if (:name m) (format "?name=%s" (:name m)) ""))
{:raw-args ["--unix-socket" "/var/run/docker.sock"]
:throw false
:body ascii-payload
:headers {"Content-Type" "application/json"
"Content-Length" (count ascii-payload)}})))
(defn create-volume [{:keys [Name]}]
(curl/post "http://localhost/volumes/create"
{:raw-args ["--unix-socket" "/var/run/docker.sock"]
:throw false
:body (json/generate-string {:Name Name})
:headers {"Content-Type" "application/json"}}))
(defn remove-volume [{:keys [Name]}]
(curl/delete (format "http://localhost/volumes/%s" Name)
{:raw-args ["--unix-socket" "/var/run/docker.sock"]
:throw false}))
(defn inspect-container [{:keys [Id]}]
(curl/get
(format "http://localhost/containers/%s/json" Id)
{:raw-args ["--unix-socket" "/var/run/docker.sock"]
:throw false}))
(defn inspect-image [{:keys [Name Id]}]
(curl/get
(format "http://localhost/images/%s/json" (or Name Id))
{:raw-args ["--unix-socket" "/var/run/docker.sock"]
:throw false}))
(defn start-container [{:keys [Id]}]
(curl/post
(format "http://localhost/containers/%s/start" Id)
{:raw-args ["--unix-socket" "/var/run/docker.sock"]
:throw false}))
;; check for 204
(defn delete-container [{:keys [Id]}]
(curl/delete
(format "http://localhost/containers/%s" Id)
{:raw-args ["--unix-socket" "/var/run/docker.sock"]
:throw false}))
;; container was created with a Terminal (output is not-multiplexed)
;; use after the container has stopped (not streaming)
(defn attach-container [{:keys [Id]}]
;; logs is true (as opposed to stream=true) so we run this after the container has stopped
;; TTY is true above so this is the just the raw data sent to the PTY (not multiplexed)
(curl/post
(format "http://localhost/containers/%s/attach?stdout=true&logs=true" Id)
{:raw-args ["--unix-socket" "/var/run/docker.sock"]
:throw false}))
;; container was created without a Terminal (output is multiplexed so read as bytes)
(defn attach-container-stdout-logs [{:keys [Id]}]
;; this assumes no Tty so the output will be multiplexed back
(curl/post
(format "http://localhost/containers/%s/attach?stdout=true&stderr=true&logs=true" Id)
{:raw-args ["--unix-socket" "/var/run/docker.sock"]
:as :bytes
:throw false}))
;; container must be created with a Terminal (so we can stream with a Reader)
(defn attach-container-stream-stdout [{:keys [Id]}]
(curl/post
(format "http://localhost/containers/%s/attach?stderr=false&stdout=true&stream=true" Id)
{:raw-args ["--unix-socket" "/var/run/docker.sock"]
:as :stream
:throw false}))
;; should be 200 and then will have a StatusCode
(defn wait-container [{:keys [Id]}]
(curl/post
(format "http://localhost/containers/%s/wait?condition=not-running" Id)
{:raw-args ["--unix-socket" "/var/run/docker.sock"]
:throw false}))
(defn kill-container [{:keys [Id]}]
(curl/post
(format "http://localhost/containers/%s/kill" Id)
{:raw-args ["--unix-socket" "/var/run/docker.sock"]
:throw false}))
(defn ->json [response]
(json/parse-string (:body response) keyword))
(defn status? [code s]
(fn [response]
(if (= code (:status response))
response
(throw (ex-info (format "%s -- (%d != %s)" s (:status response) code) response)))))
(def is-logged-in? (comp ->json (status? 200 "backend-is-logged-in") backend-is-logged-in?))
(def get-token (comp ->json (status? 200 "backend-get-token") backend-get-token))
(def get-login-info (comp ->json (status? 200 "backend-login-info") backend-login-info))
(def create (comp ->json (status? 201 "create-container") create-container))
(def thread-volume (comp (status? 201 "create-volume") create-volume))
(def delete-thread-volume (comp (status? 204 "remove-volume") remove-volume))
(def inspect (comp ->json (status? 200 "inspect container") inspect-container))
(def image-inspect (comp ->json (status? 200 "inspect image") inspect-image))
(def start (comp (status? 204 "start-container") start-container))
(def wait (comp (status? 200 "wait-container") wait-container))
(def attach (comp (status? 200 "attach-container") attach-container))
(def delete (comp (status? 204 "delete-container") delete-container))
(def get-archive (comp (status? 200 "container->archive") container->archive))
(def pull (comp (status? 200 "pull-image") pull-image))
(def images (comp ->json list-images))
(def containers (comp ->json (status? 200 "list-containers") list-containers))
(defn add-latest [image]
(let [[_ tag] (re-find #".*(:.*)$" image)]
(if tag
image
(str image ":latest"))))
(defn -pull [m]
(pull (merge m
{:image (add-latest (:image m))}
{:serveraddress "https://index.docker.io/v1/"}
(when (and (:user m)
(or (:jwt m) (:pat m)))
{:creds {:username (:user m)
:password (or (:jwt m) (:pat m))}}))))
(defn -add-token [m]
(merge
m
(try
(when (is-logged-in? m)
{:jwt (get-token m)
:user (:id (get-login-info m))})
(catch Throwable t
(logger/warn "user is not logged in to Docker Destkop")))))
(defn has-image? [image]
(let [[_ digest] (re-find #".*@(.*)" image)]
(try
(image-inspect (if digest {:Id digest} {:Name image}))
(catch Throwable _))))
(defn check-then-pull [container-definition]
(when (not (has-image? (:image container-definition)))
(logger/info "pulling image " (:image container-definition))
((comp -pull -add-token) container-definition)))
(defn injected-entrypoint [secrets environment s]
(->> (concat
(let [s (->> secrets
(map (fn [[k v]]
(format "export %s=$(cat /secret/%s | sed -e \"s/^[[:space:]]*//\")" v (name k))))
(string/join " ; "))]
(when (and s (not (= "" s)))
[s]))
(let [env (->> environment
(map (fn [s] (when-let [[_ k v] (and s (re-find #"(.*)=(.*)" s))]
[k v])))
(filter identity)
(map (fn [[k v]]
(format "export %s=%s" k v)))
(string/join " ; "))]
(when (and env (not (= "" env)))
[env]))
[s])
(string/join " ; ")))
(defn inject-secret-transform [container-definition]
(check-then-pull container-definition)
(let [{:keys [Entrypoint Cmd Env]}
(->
(image-inspect {:Name (:image container-definition)})
:Config)
real-entrypoint (string/join " " (concat
(or (:entrypoint container-definition) Entrypoint)
(or (:command container-definition) Cmd)))]
(-> container-definition
(assoc :entrypoint ["/bin/sh" "-c" (injected-entrypoint
(:secrets container-definition)
(concat
Env
(->> (:environment container-definition)
(map (fn [[k v]] (format "%s=%s" (if (keyword? k) (name k) k) v)))
(into [])))
real-entrypoint)])
(dissoc :command))))
(defn run-streaming-function-with-no-stdin
"run container function with no stdin, and no timeout, but streaming stdout"
[m cb]
(check-then-pull m)
(let [x (-> m
(update :opts
(fnil merge {})
{:Tty true
:StdinOnce false
:OpenStdin false
:AttachStdin false})
(create))
finished-channel (async/promise-chan)]
(start x)
(async/go
(try
(let [s (:body (attach-container-stream-stdout x))]
(doseq [line (line-seq (java.io.BufferedReader. (java.io.InputStreamReader. s)))]
(cb line)))
(catch Throwable e
(println e))))
;; watch the container
(async/go
(wait x)
(async/>! finished-channel {:done :exited}))
{:container x
;; stopped channel
:stopped (async/go
;; body is raw PTY output
(let [finish-reason (async/<!! finished-channel)
s (:body (attach x))
info (inspect x)]
(delete x)
(merge
finish-reason
{:pty-output s
:exit-code (-> info :State :ExitCode)
:info info})))}))
(defn run-background-function
"run container function with no stdin, and no streaming output"
[m]
(check-then-pull m)
(let [x (create m)]
(start x)
(shutdown/schedule-container-shutdown
(fn []
(kill-container x)
(delete x)))
{:done :running
:pty-output (str "started up Ts" (:image m))}))
(defn run-function
"run container function with no stdin, and no streaming output"
[{:keys [timeout] :or {timeout 600000} :as m}]
(check-then-pull m)
(let [x (create m)
finished-channel (async/promise-chan)]
(start x)
;; timeout process
(async/go
(async/<! (async/timeout timeout))
(async/>! finished-channel {:timeout timeout
:done :timeout
:kill-container (kill-container x)}))
;; watch the container
(async/go
(wait x)
(async/>! finished-channel {:done :exited}))
;; body is raw PTY output
(let [finish-reason (async/<!! finished-channel)
s (:body (attach x))
info (inspect x)]
(delete x)
(merge
finish-reason
{:pty-output s
:exit-code (-> info :State :ExitCode)
:info info}))))
;; not curl - opens a real socket to write to the container stdin
;; returns the open socket - closing the socket will signal the program to finish
;; if it's waiting on input
(defn write-stdin [container-id content]
(let [buf (ByteBuffer/allocate (* 1024 20))
address (UnixDomainSocketAddress/of "/var/run/docker.sock")
client (SocketChannel/open address)]
;;(while (not (.finishConnect client)))
(.configureBlocking client true)
(.clear buf)
(.put buf (.getBytes (String. (format "POST /containers/%s/attach?stdin=true&stream=true HTTP/1.1\n" container-id))))
(.put buf (.getBytes (String. "Host: localhost\nConnection: Upgrade\nUpgrade: tcp\n\n")))
(try
(.put ^ByteBuffer buf (.getBytes ^String content))
(.flip ^ByteBuffer buf)
(while (.hasRemaining buf)
(.write client buf))
(catch Throwable ex
(logger/error "write-stdin error " ex)
client))
client))
(defn get-block [x-bytes start]
(let [[type _ _ _ n1 n2 n3 n4] (Arrays/copyOfRange ^bytes x-bytes ^int start ^int (+ start 8))]
(let [size (.getInt (ByteBuffer/wrap (Arrays/copyOfRange ^bytes x-bytes ^int (+ start 4) ^int (+ start 8))))]
[(case (int type) 0 :stdin 1 :stdout 2 :stderr)
size
(String. (Arrays/copyOfRange ^bytes x-bytes ^int (+ start 8) ^int (+ start 8 size)))])))
(defn process-bytes [x-bytes]
(loop [agg {:stdout "" :stderr "" :stdin ""} start 0]
(if (> (- (count x-bytes) start) 8)
(let [[type size s] (get-block x-bytes start)]
(recur
(update agg type str s)
(long (+ start size 8))))
agg)))
(defn docker-stream-format->stdout [bytes]
(try
(let [{:keys [stdout stderr]} (process-bytes bytes)]
(str stdout stderr))
(catch Throwable t
(logger/error "processing docker engine attach bytes: " t)
"")))
(defn write-to-stdin
" params
client - SocketChannel for attached container"
[client s]
(let [buf (ByteBuffer/allocate (* 1024 20))]
(.clear ^ByteBuffer buf)
(try
(.put ^ByteBuffer buf (.getBytes ^String s))
(.flip ^ByteBuffer buf)
(while (.hasRemaining buf)
(.write ^SocketChannel client buf))
(catch Throwable t
(logger/error "write-string error " t)))))
(defn read-loop
"the socket read loop for the stdout/stderr streams of a container
de-plexes the streams and writes out blocks of stdout and stderr to a channel
The channel should eventually close becuase the socket will close when the process exits
params
in - SocketChannel for attached container
c - channel to write multiplexed stdout stderr blocks"
[in c]
(try
(let [header-buf (ByteBuffer/allocate 8)
stdout (PipedOutputStream.)
stdout-reader (io/reader (PipedInputStream. stdout))]
(async/go-loop []
(when-let [line (.readLine stdout-reader)]
(async/put! c {:stdout line})
(recur)))
(loop [offset 0]
(let [result (.read ^SocketChannel in header-buf)]
(cond
;;;;;;;;;;
(= -1 result)
(do
(.close stdout)
(async/close! c))
;;;;;;;;;;
(= 8 (+ offset result))
(let [size (.getInt (ByteBuffer/wrap (Arrays/copyOfRange ^bytes (.array ^ByteBuffer header-buf) 4 8)))
stream-type (case (int (nth (.array ^ByteBuffer header-buf) 0))
0 :stdin
1 :stdout
2 :stderr)
buf (ByteBuffer/allocate size)]
(.clear ^ByteBuffer header-buf)
(loop [offset 0]
(let [result (.read ^SocketChannel in buf)]
(cond
;;;;;;;;;;
(= -1 result)
(async/close! c)
;;;;;;;;;;
(= size (+ offset result))
(if (= stream-type :stdout)
(do
(.write stdout ^bytes (.array buf))
(.flush stdout))
(async/put! c {stream-type (String. ^bytes (.array buf))}))
;;;;;;;;;;
:else
(recur (+ offset result)))))
(.clear ^ByteBuffer buf)
(recur 0))
;;;;;;;;;;
:else
(recur (+ offset result))))))
(catch Throwable t
(logger/error "streaming exception " t)
(async/close! c))))
(defn attach-socket
" returns SocketChannel (upgraded attach connection with all streams active)"
[container-id]
(let [buf (ByteBuffer/allocate (* 1024 20))
address (UnixDomainSocketAddress/of "/var/run/docker.sock")
client (SocketChannel/open address)]
(.configureBlocking client true)
(.clear buf)
;; make HTTP call
(.put buf (.getBytes (String. (format "POST /containers/%s/attach?stdin=true&stdout=true&stderr=true&stream=true HTTP/1.1\n" container-id))))
(.put buf (.getBytes (String. "Host: localhost\nConnection: Upgrade\nUpgrade: tcp\n\n")))
(try
(.flip ^ByteBuffer buf)
(while (.hasRemaining buf)
(.write client buf))
;; TODO if successful, we should get a 101 UPGRADED response that will be exactly 117 bytes
(let [buf (ByteBuffer/allocate 117)]
;; TODO read the HTTP upgrade message
(loop [size 0]
(let [result (.read client buf)
bytes-read (+ size result)]
(when (not (= 117 bytes-read))
(recur bytes-read)))))
(catch Throwable _
client))
client))
(defn function-call-with-stdin
"creates and starts container, then writes to stdin process
returns container map with Id, and socket - socket is open socket to stdin"
[m]
(check-then-pull m)
(let [x (merge
m
(create (assoc m
:opts {:StdinOnce true
:OpenStdin true
:AttachStdin true})))]
(start x)
(assoc x :socket (write-stdin (:Id x) (:content m)))))
(defn function-call-with-attached-socket
"create and start a container with an attached socket
this does not wait for the container to finish
returns a map with an
:output-channel for stdout/stderr/:stopped/:timeout/:closed messages
:write function to write to the stdin of the container"
[container]
(check-then-pull container)
(let [x (docker/create (assoc container
:opts {:StdinOnce true
:OpenStdin true
:AttachStding true}))
socket-channel (attach-socket (:Id x))
c (async/chan)
output-channel (async/chan)]
(start x)
(async/thread (read-loop socket-channel c))
(async/go
(docker/wait x)
(async/>! c :stopped)
(delete x))
(async/go-loop
[block (async/<! c)]
(cond
(#{:stopped :timeout} block)
(async/put! output-channel block)
(nil? block)
(async/put! output-channel :closed)
:else
(do
(async/put! output-channel block)
(recur (async/<! c)))))
(assoc
x
:output-channel output-channel
:write (fn [s] (write-to-stdin socket-channel s)))))
(defn finish-call
"Waits (blocking) for call to container to finish, retrieves stdout logs
params
running container
returns ::container-response"
[{:keys [timeout] :or {timeout 10000} :as x}]
;; close stdin socket
(.close ^SocketChannel (:socket x))
;; timeout process
(let [finished-channel (async/promise-chan)]
(async/go
(async/<! (async/timeout timeout))
(async/>! finished-channel {:timeout timeout
:done :timeout
:kill-container (kill-container x)}))
;; watch the container
(async/go
(wait x)
(async/>! finished-channel {:done :exited}))
;; body is raw PTY output - could we
;; have just been reading from the socket since it's two way?
(try
(let [finish-reason (async/<!! finished-channel)
s (docker-stream-format->stdout
(:body
(attach-container-stdout-logs x)))
info (inspect x)]
(delete x)
(merge
finish-reason
{:pty-output s
:exit-code (-> info :State :ExitCode)
:info info}))
(catch Throwable _
(delete x)
{}))))
(defn run-in-background-with-one-message [m]
(let [{:keys [output-channel]} (function-call-with-attached-socket m)]
(async/go-loop
[]
(let [m (async/<! output-channel)]
(cond
(#{:timeout} m)
{:done :timeout :timeout "timeout waiting for message"}
(#{:stopped :closed} m)
{:done :exited :pty-output "tool stopped before responding on stdout" :exit-code 1}
(and (map? m) (contains? m :stdout))
{:pty-output (:stdout m)
:done :running}
:else
(do
(logger/info "background stderr " m)
(recur)))))))
(defn run-with-stdin-content
"run container with stdin read from file or from string
this is several engine calls
- create container
- start container
- write-stdin which creates a socket, upgrades the connection, and writes the bytes
- closes the socket
- wait for container to exit or kill it if there's a timeout
- attaches to the container and downloads the bytes for both the stdout and stderr
- deletes the container"
[m]
(let [x (docker/function-call-with-stdin
(assoc m :content (or
(-> m :stdin :content)
(slurp (-> m :stdin :file)))))]
(async/<!! (async/thread
(Thread/sleep 10)
(docker/finish-call x)))))
(defn run-container
" params ::container-definition
returns ::container-response"
[m]
;; (schema/validate :schema/tool-container)
(cond
(-> m :stdin)
(run-with-stdin-content m)
(true? (:background m))
(run-background-function m)
(true? (:background-callback m))
(async/<!! (run-in-background-with-one-message m))
:else
(run-function m)))
(defn get-login-info-from-desktop-backend
"returns token or nil if not logged in or backend.sock is not available"
[]
(try
(when (is-logged-in? {})
(merge
{:is-logged-in? true}
(try
(get-login-info {})
(catch Throwable _))))
(catch Throwable _)))