Skip to content

Commit

Permalink
refactored function build to create-container because that is more ac…
Browse files Browse the repository at this point in the history
…curate
  • Loading branch information
TimoKramer committed Mar 2, 2020
1 parent aec1d81 commit 50ea2fc
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 42 deletions.
52 changes: 25 additions & 27 deletions src/bob/execution/internals.clj
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
(do (log/debugf "Found image locally: %s" name)
name))))

;; TODO default to latest?
(defn pull-image
"Pulls in an image if it's not present locally.
Returns the name or the error if any."
Expand Down Expand Up @@ -88,38 +87,38 @@
(u/log-and-fail "Could not delete image:" (:message result))
result)))

; TODO should be create-container
(defn build
"Builds a container.
Takes the base image and the entry point command.
(defn create-container
"Creates a container.
Takes the base image and optionally the step and evars.
Returns the id of the built container."
([image] (let [_ (log/debugf "Creating new container with:
image: %s" image)
result (docker/invoke states/containers {:op :ContainerCreate
:params {:body {:Image image}}})]
(if (contains? result :message)
(u/log-and-fail "Could not build image:" (:message result))
(u/log-and-fail "Could not build container with image:" (:message result))
(:Id result))))
([image step] (create-container image step {}))
([image step evars] (let [resource (:needs_resource step)
working-dir (when resource (str "/root/" resource))
cmd (u/sh-tokenize! (:cmd step))
formatted-evars (u/format-env-vars evars)
_ (log/debugf "Creating new container with:
image: %s
cmd: %s
evars: %s
working-dir: %s"
image
cmd
(vec formatted-evars)
working-dir)
result (docker/invoke states/containers {:op :ContainerCreate
:params {:body {:Image image
:Cmd cmd
:Env formatted-evars
:WorkingDir working-dir}}})]
working-dir (when resource (str "/root/" resource))
cmd (u/sh-tokenize! (:cmd step))
formatted-evars (u/format-env-vars evars)
_ (log/debugf "Creating new container with:
image: %s
cmd: %s
evars: %s
working-dir: %s"
image
cmd
(vec formatted-evars)
working-dir)
result (docker/invoke states/containers {:op :ContainerCreate
:params {:body {:Image image
:Cmd cmd
:Env formatted-evars
:WorkingDir working-dir}}})]
(if (contains? result :message)
(u/log-and-fail "Could not build image:" (:message result))
(u/log-and-fail "Could not build container with image:" (:message result))
(:Id result)))))

(defn status-of
Expand Down Expand Up @@ -215,10 +214,9 @@
(docker/invoke states/containers {:op :ContainerStart :params {:id myid}})
(:StatusCode (docker/invoke states/containers {:op :ContainerWait :params {:id myid}}))

(build "bobcd/bob:latest"
(create-container "bobcd/bob:latest"
{:needs_resource "source"
:cmd "ls"}
{:FOO "bar"})
:cmd "ls"})

(defn validate-name [name]
(let [split-name (cs/split name #":")]
Expand Down
4 changes: 2 additions & 2 deletions src/bob/pipeline/internals.clj
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
(resourceful-step step pipeline image run-id)
image)
_ (mark-image-for-gc image run-id)
result {:id (e/build image step evars)
result {:id (e/create-container image step evars)
:mounted mounted}
_ (log/debugf "Built a resourceful container: %s" (:id build-state))]
(if mount-needed?
Expand Down Expand Up @@ -196,7 +196,7 @@
_ (mark-image-for-gc image run-id)
image (resourceful-step first-step pipeline image run-id)
_ (mark-image-for-gc image run-id)
id (e/build image first-step evars)
id (e/create-container image first-step evars)
id (update-pid id run-id)
id (let [result (e/run id run-id)]
(when (f/failed? result)
Expand Down
2 changes: 1 addition & 1 deletion src/bob/resource/internals.clj
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
TarInputStream.
(prefix-dir-on-tar! resource-name))
_ (log/debug "Creating temp container for resource mount")
container-id (e/build image)
container-id (e/create-container image)
_ (log/debug "Copying resources to container")
_ (put-container-archive container-id archive "/root")
_ (-> archive File. .delete)
Expand Down
10 changes: 7 additions & 3 deletions test/bob/execution/internals_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,22 @@
(deftest container-build
(testing "successful build"
(with-redefs-fn {#'docker/invoke (constantly {:Id "83996a1d", :Warnings []})}
#(is (= "83996a1d" (build "foo:bar"
#(is (= "83996a1d" (create-container "foo:bar"
{:needs_resource "source"
:cmd "ls"}
{:FOO "bar"})))))

(testing "successful build single param"
(with-redefs-fn {#'docker/invoke (constantly {:Id "83996a1d", :Warnings []})}
#(is (= "83996a1d" (build "foo:bar")))))
#(is (= "83996a1d" (create-container "foo:bar")))))

(testing "successful build double param"
(with-redefs-fn {#'docker/invoke (constantly {:Id "83996a1d", :Warnings []})}
#(is (= "83996a1d" (create-container "foo:bar" {:cmd "ls"})))))

(testing "failing build"
(with-redefs-fn {#'docker/invoke (constantly {:message "failed"})}
#(is (f/failed? (build "foo:bar"
#(is (f/failed? (create-container "foo:bar"
{:needs_resource "source"
:cmd "ls"}
{:FOO "bar"}))))))
Expand Down
18 changes: 9 additions & 9 deletions test/bob/pipeline/internals_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
(testing "next images generation with resource mount"
(with-redefs-fn {#'e/commit-image (constantly "1212121212")
#'resourceful-step (constantly "img")
#'e/build (constantly "id")
#'e/create-container (constantly "id")
#'e/delete-container (constantly nil)
#'mark-image-for-gc (constantly nil)}
#(is (= {:id "id"
Expand All @@ -116,7 +116,7 @@
(testing "next image generation without resource mount"
(with-redefs-fn {#'e/commit-image (constantly "1212121212")
#'resourceful-step (constantly "img")
#'e/build (constantly "id")
#'e/create-container (constantly "id")
#'e/delete-container (constantly nil)
#'mark-image-for-gc (constantly nil)}
#(is (= {:id "id"
Expand All @@ -128,10 +128,10 @@
"run-id")))))

(testing "failed next image generation"
(with-redefs-fn {#'e/commit-image #(throw (Exception. "nope"))
#'resourceful-step (constantly "img")
#'e/build (constantly "id")
#'mark-image-for-gc (constantly nil)}
(with-redefs-fn {#'e/commit-image #(throw (Exception. "nope"))
#'resourceful-step (constantly "img")
#'e/create-container (constantly "id")
#'mark-image-for-gc (constantly nil)}
#(is (f/failed? (next-step {:id "id" :mounted []}
{:cmd "hello"}
{}
Expand Down Expand Up @@ -278,7 +278,7 @@
(= "img" img)
(= "run-id" run-id)))
"img")
#'e/build (fn [img step evars]
#'e/create-container (fn [img step evars]
(tu/check-and-fail
#(and (= "img" img)
(= first-step
Expand Down Expand Up @@ -344,7 +344,7 @@
(= "img" img)
(= "run-id" run-id)))
"img")
#'e/build (fn [img step evars]
#'e/create-container (fn [img step evars]
(tu/check-and-fail
#(and (= "img" img)
(= first-step
Expand Down Expand Up @@ -402,7 +402,7 @@
#'e/pull-image (constantly (f/fail "shizz"))
#'db/status-of (constantly {:status "running"})
#'resourceful-step nein
#'e/build nein
#'e/create-container nein
#'update-pid nein
#'e/run nein
#'reduce nein
Expand Down

0 comments on commit 50ea2fc

Please sign in to comment.