|
283 | 283 | (def pull (comp (status? 200 "pull-image") pull-image))
|
284 | 284 | (def images (comp ->json list-images))
|
285 | 285 |
|
286 |
| -(defn injected-entrypoint [secrets s] |
287 |
| - (format "%s ; %s" |
288 |
| - (->> secrets |
289 |
| - (map (fn [[k v]] |
290 |
| - (format "export %s=$(cat /secret/%s | sed -e \"s/^[[:space:]]*//\")" v (name k)))) |
291 |
| - (string/join " ; ")) |
292 |
| - s)) |
293 |
| - |
294 |
| -(defn inject-secret-transform [container-definition] |
295 |
| - (let [{:keys [Entrypoint Cmd]} |
296 |
| - (-> |
297 |
| - (image-inspect |
298 |
| - (-> (images {"reference" [(:image container-definition)]}) |
299 |
| - first)) |
300 |
| - :Config) |
301 |
| - real-entrypoint (string/join " " (concat Entrypoint (or (:command container-definition) Cmd)))] |
302 |
| - (-> container-definition |
303 |
| - (assoc :entrypoint ["/bin/sh" "-c" (injected-entrypoint (:secrets container-definition) real-entrypoint)]) |
304 |
| - (dissoc :command)))) |
305 |
| - |
306 | 286 | (defn add-latest [image]
|
307 | 287 | (let [[_ tag] (re-find #".*(:.*)$" image)]
|
308 | 288 | (if tag
|
|
331 | 311 | (and digest (= digest Id))))
|
332 | 312 | (images {}))))
|
333 | 313 |
|
| 314 | +(defn check-then-pull [container-definition] |
| 315 | + (when (not (has-image? (:image container-definition))) |
| 316 | + (-pull container-definition))) |
| 317 | + |
| 318 | +(defn injected-entrypoint [secrets environment s] |
| 319 | + (->> (concat |
| 320 | + (let [s (->> secrets |
| 321 | + (map (fn [[k v]] |
| 322 | + (format "export %s=$(cat /secret/%s | sed -e \"s/^[[:space:]]*//\")" v (name k)))) |
| 323 | + (string/join " ; "))] |
| 324 | + (when (and s (not (= "" s))) |
| 325 | + [s])) |
| 326 | + (let [env (->> environment |
| 327 | + (map (fn [s] (when-let [[_ k v] (and s (re-find #"(.*)=(.*)" s))] |
| 328 | + [k v]))) |
| 329 | + (filter identity) |
| 330 | + (map (fn [[k v]] |
| 331 | + (format "export %s=%s" k v))) |
| 332 | + (string/join " ; "))] |
| 333 | + (when (and env (not (= "" env))) |
| 334 | + [env])) |
| 335 | + [s]) |
| 336 | + (string/join " ; "))) |
| 337 | + |
| 338 | +(comment |
| 339 | + (injected-entrypoint {:a "A"} ["BLAH=whatever"] "my command") |
| 340 | + (injected-entrypoint nil nil "my command") |
| 341 | + (injected-entrypoint {:a "A"} nil "my command") |
| 342 | + (injected-entrypoint nil nil nil) |
| 343 | + ) |
| 344 | + |
| 345 | +(defn inject-secret-transform [container-definition] |
| 346 | + (check-then-pull container-definition) |
| 347 | + (let [{:keys [Entrypoint Cmd Env]} |
| 348 | + (-> |
| 349 | + (image-inspect |
| 350 | + (-> (images {"reference" [(:image container-definition)]}) |
| 351 | + first)) |
| 352 | + :Config) |
| 353 | + real-entrypoint (string/join " " (concat |
| 354 | + (or (:entrypoint container-definition) Entrypoint) |
| 355 | + (or (:command container-definition) Cmd)))] |
| 356 | + (-> container-definition |
| 357 | + (assoc :entrypoint ["/bin/sh" "-c" (injected-entrypoint (:secrets container-definition) Env real-entrypoint)]) |
| 358 | + (dissoc :command)))) |
| 359 | + |
334 | 360 | (defn run-streaming-function-with-no-stdin
|
335 | 361 | "run container function with no stdin, and no timeout, but streaming stdout"
|
336 | 362 | [m cb]
|
337 |
| - (when (not (has-image? (:image m))) |
338 |
| - (-pull m)) |
| 363 | + (check-then-pull m) |
339 | 364 | (let [x (-> m
|
340 | 365 | (update :opts
|
341 | 366 | (fnil merge {})
|
|
377 | 402 | (defn run-background-function
|
378 | 403 | "run container function with no stdin, and no streaming output"
|
379 | 404 | [m]
|
380 |
| - (when (not (has-image? (:image m))) |
381 |
| - (-pull m)) |
| 405 | + (check-then-pull m) |
382 | 406 | (let [x (create m)]
|
383 | 407 | (start x)
|
384 | 408 | (shutdown/schedule-container-shutdown
|
|
391 | 415 | (defn run-function
|
392 | 416 | "run container function with no stdin, and no streaming output"
|
393 | 417 | [{:keys [timeout] :or {timeout 600000} :as m}]
|
394 |
| - (when (not (has-image? (:image m))) |
395 |
| - (-pull m)) |
| 418 | + (check-then-pull m) |
396 | 419 | (let [x (create m)
|
397 | 420 | finished-channel (async/promise-chan)]
|
398 | 421 | (start x)
|
|
0 commit comments