|
1 | 1 | (ns docker |
| 2 | + "run-container runs containers with a few flavors |
| 3 | + - run with stdin content |
| 4 | + - run a container but don't wait for it to stop |
| 5 | + - run in the background but wait for one message on stdout and then stop |
| 6 | + - simply run function without stdin |
| 7 | + |
| 8 | + attach-socket - to running container |
| 9 | + write-stdin - |
| 10 | + read-loop - input: attached socket output: channel messages |
| 11 | + write-to-stdin -input: attached socket, string |
| 12 | + inject-secret-transform - transform container entrypoint for secrets |
| 13 | + |
| 14 | + run-streaming-function-with-no-stdin - runs continuously |
| 15 | + and read stdout to a callback (used in watcher)" |
2 | 16 | (:require |
3 | 17 | [babashka.curl :as curl] |
4 | 18 | [babashka.fs :as fs] |
|
129 | 143 | [(format "%s/%s" (:port container) (:protocol container)) [{:HostPort (:port host)}]]) |
130 | 144 | (into {}))) |
131 | 145 |
|
132 | | -(comment |
133 | | - (port-bindings ["9222:9222"]) |
134 | | - (exposed-ports ["9222:9222"])) |
135 | | - |
136 | 146 | ;; check for 201 |
137 | 147 | ;; entrypoint is an array of strings |
138 | 148 | ;; env is a map |
|
290 | 300 | image |
291 | 301 | (str image ":latest")))) |
292 | 302 |
|
293 | | -(comment |
294 | | - (add-latest "vonwig/go-linguist") |
295 | | - (add-latest "blah/what:tag")) |
296 | | - |
297 | 303 | (defn -pull [m] |
298 | 304 | (pull (merge m |
299 | 305 | {:image (add-latest (:image m))} |
|
336 | 342 | [s]) |
337 | 343 | (string/join " ; "))) |
338 | 344 |
|
339 | | -(comment |
340 | | - (injected-entrypoint {:a "A"} ["BLAH=whatever"] "my command") |
341 | | - (injected-entrypoint nil nil "my command") |
342 | | - (injected-entrypoint {:a "A"} nil "my command") |
343 | | - (injected-entrypoint nil nil nil)) |
344 | | - |
345 | 345 | (defn inject-secret-transform [container-definition] |
346 | 346 | (check-then-pull container-definition) |
347 | 347 | (let [{:keys [Entrypoint Cmd Env]} |
|
627 | 627 | (delete x)) |
628 | 628 | (async/go-loop |
629 | 629 | [block (async/<! c)] |
630 | | - (logger/info "background socket read loop " block) |
631 | 630 | (cond |
632 | 631 | (#{:stopped :timeout} block) |
633 | | - (do |
634 | | - (logger/info "socket read loop " block) |
635 | | - (async/put! output-channel block)) |
| 632 | + (async/put! output-channel block) |
636 | 633 | (nil? block) |
637 | 634 | (async/put! output-channel :closed) |
638 | 635 | :else |
|
645 | 642 | :write (fn [s] (write-to-stdin socket-channel s))))) |
646 | 643 |
|
647 | 644 | (defn finish-call |
648 | | - "This is a blocking call that waits for the container to finish and then returns the output and exit code." |
| 645 | + "Waits (blocking) for call to container to finish, retrieves stdout logs |
| 646 | + params |
| 647 | + running container |
| 648 | + returns ::container-response" |
649 | 649 | [{:keys [timeout] :or {timeout 10000} :as x}] |
650 | 650 | ;; close stdin socket |
651 | 651 | (.close ^SocketChannel (:socket x)) |
|
708 | 708 | - deletes the container" |
709 | 709 | [m] |
710 | 710 | (let [x (docker/function-call-with-stdin |
711 | | - (assoc m :content (or (-> m :stdin :content) (slurp (-> m :stdin :file)))))] |
| 711 | + (assoc m :content (or |
| 712 | + (-> m :stdin :content) |
| 713 | + (slurp (-> m :stdin :file)))))] |
712 | 714 | (async/<!! (async/thread |
713 | 715 | (Thread/sleep 10) |
714 | 716 | (docker/finish-call x))))) |
|
717 | 719 | " params ::container-definition |
718 | 720 | returns ::container-response" |
719 | 721 | [m] |
720 | | - ;; (schema/validate :schema/container-definition) |
| 722 | + ;; (schema/validate :schema/tool-container) |
721 | 723 | (cond |
722 | 724 | (-> m :stdin) |
723 | 725 | (run-with-stdin-content m) |
|
728 | 730 | :else |
729 | 731 | (run-function m))) |
730 | 732 |
|
731 | | -(comment |
732 | | - (repl/setup-stdout-logger) |
733 | | - (run-container |
734 | | - {:image "vonwig/gdrive:latest" |
735 | | - :background-callback true |
736 | | - :workdir "/app" |
737 | | - :ports ["3000:3000"] |
738 | | - :volumes ["mcp-gdrive:/gdrive-server"] |
739 | | - :environment {"GDRIVE_CREDENTIALS_PATH" "/gdrive-server/credentials.json" |
740 | | - "GDRIVE_OAUTH_PATH" "/secret/google.gcp-oauth.keys.json"} |
741 | | - :secrets {:google.gcp-oauth.keys.json "GDRIVE"} |
742 | | - :command ["auth"]})) |
743 | | - |
744 | 733 | (defn get-login-info-from-desktop-backend |
745 | 734 | "returns token or nil if not logged in or backend.sock is not available" |
746 | 735 | [] |
|
0 commit comments