|
77 | 77 | (format "%s/Library/Containers/com.docker.docker/Data/backend.sock" (System/getenv "HOME"))]]
|
78 | 78 | (some unix-socket-file coll)))
|
79 | 79 |
|
| 80 | +(defn- get-jfs-socket [] |
| 81 | + (let [coll [(or (System/getenv "JFS_SOCKET_PATH") "/jfs.sock") |
| 82 | + (format "%s/Library/Containers/com.docker.docker/Data/jfs.sock" (System/getenv "HOME"))]] |
| 83 | + (some unix-socket-file coll))) |
| 84 | + |
| 85 | +(defn jfs-get-secret [s] |
| 86 | + (curl/get |
| 87 | + (format "http://localhost/secrets/%s" s) |
| 88 | + {:raw-args ["--unix-socket" (get-jfs-socket)] |
| 89 | + :throw false})) |
| 90 | + |
80 | 91 | (defn backend-is-logged-in? [_]
|
81 | 92 | (curl/get
|
82 | 93 | "http://localhost/registry/is-logged-in"
|
|
228 | 239 |
|
229 | 240 | (defn inspect-image [{:keys [Name Id]}]
|
230 | 241 | (curl/get
|
231 |
| - (format "http://localhost/images/%s/json" (or Name Id)) |
| 242 | + (format "http://localhost/images/%s/json" (or Name Id)) |
232 | 243 | {:raw-args ["--unix-socket" "/var/run/docker.sock"]
|
233 | 244 | :throw false}))
|
234 | 245 |
|
|
310 | 321 | (def pull (comp (status? 200 "pull-image") pull-image))
|
311 | 322 | (def images (comp ->json list-images))
|
312 | 323 | (def containers (comp ->json (status? 200 "list-containers") list-containers))
|
| 324 | +(def secrets-get (comp ->json (status? 200 "secrets-get") jfs-get-secret)) |
313 | 325 |
|
314 | 326 | (defn add-latest [image]
|
315 | 327 | (let [[_ tag] (re-find #".*(:.*)$" image)]
|
|
367 | 379 | [s])
|
368 | 380 | (string/join " ; ")))
|
369 | 381 |
|
| 382 | +(defn get-secrets [{:keys [secrets]}] |
| 383 | + (->> secrets |
| 384 | + (map (fn [[k v]] |
| 385 | + [v (:value (secrets-get (name k)))])) |
| 386 | + (into {}))) |
| 387 | + |
370 | 388 | (defn inject-secret-transform [container-definition]
|
371 | 389 | (check-then-pull container-definition)
|
372 |
| - (let [{:keys [Entrypoint Cmd Env]} |
| 390 | + (let [{:keys [Entrypoint Cmd Env User]} |
373 | 391 | (->
|
374 | 392 | (image-inspect {:Name (:image container-definition)})
|
375 | 393 | :Config)
|
376 | 394 | real-entrypoint (string/join " " (concat
|
377 | 395 | (or (:entrypoint container-definition) Entrypoint)
|
378 | 396 | (or (:command container-definition) Cmd)))]
|
379 |
| - (-> container-definition |
380 |
| - (assoc :entrypoint ["/bin/sh" "-c" (injected-entrypoint |
381 |
| - (:secrets container-definition) |
382 |
| - (concat |
383 |
| - Env |
384 |
| - (->> (:environment container-definition) |
385 |
| - (map (fn [[k v]] (format "%s=%s" (if (keyword? k) (name k) k) v))) |
386 |
| - (into []))) |
387 |
| - real-entrypoint)]) |
388 |
| - (dissoc :command)))) |
| 397 | + (if (#{"" "root"} User) |
| 398 | + (-> container-definition |
| 399 | + (assoc :entrypoint ["/bin/sh" "-c" (injected-entrypoint |
| 400 | + (:secrets container-definition) |
| 401 | + (concat |
| 402 | + Env |
| 403 | + (->> (:environment container-definition) |
| 404 | + (map (fn [[k v]] (format "%s=%s" (if (keyword? k) (name k) k) v))) |
| 405 | + (into []))) |
| 406 | + real-entrypoint)]) |
| 407 | + (dissoc :command)) |
| 408 | + (-> container-definition |
| 409 | + (update :environment (fnil merge {}) (get-secrets container-definition)))))) |
389 | 410 |
|
390 | 411 | (defn run-streaming-function-with-no-stdin
|
391 | 412 | "run container function with no stdin, and no timeout, but streaming stdout"
|
|
402 | 423 | finished-channel (async/promise-chan)]
|
403 | 424 | (start x)
|
404 | 425 |
|
405 |
| - (async/go |
406 |
| - (try |
407 |
| - (let [s (:body (attach-container-stream-stdout x))] |
408 |
| - (doseq [line (line-seq (java.io.BufferedReader. (java.io.InputStreamReader. s)))] |
409 |
| - (cb line))) |
410 |
| - (catch Throwable e |
411 |
| - (println e)))) |
| 426 | + (.start ^Thread |
| 427 | + (Thread. |
| 428 | + (fn [] |
| 429 | + (try |
| 430 | + (let [s (:body (attach-container-stream-stdout x))] |
| 431 | + (doseq [line (line-seq (java.io.BufferedReader. (java.io.InputStreamReader. s)))] |
| 432 | + (cb line))) |
| 433 | + (catch Throwable e |
| 434 | + (logger/error "run-streaming-function" e)))))) |
412 | 435 |
|
413 | 436 | ;; watch the container
|
414 |
| - (async/go |
415 |
| - (wait x) |
416 |
| - (async/>! finished-channel {:done :exited})) |
| 437 | + (.start ^Thread |
| 438 | + (Thread. |
| 439 | + (fn [] |
| 440 | + (wait x) |
| 441 | + (async/put! finished-channel {:done :exited})))) |
417 | 442 |
|
418 | 443 | {:container x
|
419 | 444 | ;; stopped channel
|
|
547 | 572 | (let [header-buf (ByteBuffer/allocate 8)
|
548 | 573 | stdout (PipedOutputStream.)
|
549 | 574 | stdout-reader (io/reader (PipedInputStream. stdout))]
|
550 |
| - (async/go-loop [] |
551 |
| - (when-let [line (.readLine stdout-reader)] |
552 |
| - (async/put! c {:stdout line}) |
553 |
| - (recur))) |
| 575 | + (.start ^Thread |
| 576 | + (Thread. |
| 577 | + (fn [] |
| 578 | + (loop [] |
| 579 | + (when-let [line (.readLine stdout-reader)] |
| 580 | + (async/put! c {:stdout line}) |
| 581 | + (recur)))))) |
554 | 582 | (loop [offset 0]
|
555 | 583 | (let [result (.read ^SocketChannel in header-buf)]
|
556 | 584 | (cond
|
|
656 | 684 | c (async/chan)
|
657 | 685 | output-channel (async/chan)]
|
658 | 686 | (start x)
|
659 |
| - (async/thread (read-loop socket-channel c)) |
| 687 | + (.start ^Thread |
| 688 | + (Thread. |
| 689 | + (fn [] |
| 690 | + (read-loop socket-channel c)))) |
660 | 691 | (async/go
|
661 | 692 | (docker/wait x)
|
662 | 693 | (async/>! c :stopped)
|
|
0 commit comments