Skip to content

Commit

Permalink
log-streaming implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
TimoKramer committed Feb 24, 2020
1 parent bef9118 commit 5c33c05
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 26 deletions.
65 changes: 46 additions & 19 deletions src/bob/execution/internals.clj
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
; This file is part of Bob.
;
; Bob is free software: you can redistribute it and/or modify
; it under the terms of the GNU Affero General Public License as published by
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
;
; Bob is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
; GNU Affero General Public License for more details.
; GNU General Public License for more details.
;
; You should have received a copy of the GNU Affero General Public License
; You should have received a copy of the GNU General Public License
; along with Bob. If not, see <http://www.gnu.org/licenses/>.

(ns bob.execution.internals
Expand Down Expand Up @@ -114,10 +114,10 @@
working-dir)
evars-nilled (if (empty? evars) nil evars)
result (docker/invoke states/containers {:op :ContainerCreate
:params {:body {:Image image
:Cmd cmd
:Env evars-nilled
:WorkingDir working-dir}}})]
:params {:body {:Image image
:Cmd cmd
:Env evars-nilled
:WorkingDir working-dir}}})]
(if (contains? result :message)
(u/log-and-fail "Could not build image:" (:message result))
(:Id result)))))
Expand All @@ -133,21 +133,40 @@
(u/log-and-fail (format "Could not fetch container status: %s" (:message result)))
{:running? running? :exit-code exit-code})))

(defn logs-live
"Get stdout and stderr logs from a container."
[id reaction-fn]
(let [log-client (docker/client {:category :containers
:conn (docker/connect {:uri "unix:///var/run/docker.sock"
:connect-timeout 10
:read-timeout 2000
:write-timeout 2000
:call-timeout 30000})})
log-stream (docker/invoke log-client {:op :ContainerLogs
:params
{:id id
:follow true
:stdout true}
:as :stream})]
(future
(with-open [rdr (clojure.java.io/reader log-stream)]
(loop [r (java.io.BufferedReader. rdr)]
(when-let [line (.readLine r)]
(reaction-fn line)
(recur r)))))))

; TODO should be start-container?
; TODO log-streaming
(defn run
"Synchronously starts up a previously built container by id.
Returns the id when complete or an error in case on non-zero exit."
Takes container-id and run-id which is the id of the build.
Returns the id when complete or an error in case on non-zero exit."
[id run-id]
(f/try-all [_ (log/debugf "Starting container %s" id)
_ (docker/invoke states/containers {:op :ContainerStart :params {:id id}})
_ (log/debugf "Attaching to container %s for logs" id)
;;_ (docker/logs-live states/docker-conn
;; id
;; #(u/log-to-db % run-id))
status (-> (docker/invoke states/containers {:op :ContainerInspect :params {:id id}})
:State
:ExitCode)]
_ (logs-live id #(u/log-to-db % run-id))
status (:StatusCode (docker/invoke states/containers {:op :ContainerWait :params {:id id}}))]
(do (f/when-failed [err]
(u/log-and-fail "Error in running container"
(str id ":")
Expand Down Expand Up @@ -194,8 +213,6 @@
(docker/invoke states/images {:op :ImageCreate :params {:fromImage "clojure" :tag "latest"}})
(docker/invoke states/images {:op :ImageDelete :params {:name "clojure:latest"}})



(docker/invoke states/containers {:op :ContainerCreate
:params {:body {:Image "oracle/graalvm-ce:19.3.0"
:Cmd "gu install native-image"
Expand Down Expand Up @@ -247,8 +264,16 @@
(status-of "lucid_pasteur")
(:ExitCode (:State (docker/invoke states/containers {:op :ContainerInspect :params {:id "82edce1bfdea"}})))

(docker/invoke states/containers {:op :ContainerStart :params {:id "upbeat_neumann"}})
(run "lucid_pasteur" "run-id")
(future
(with-open [rdr (clojure.java.io/reader (char-array "foobar\n"))]
(loop [r (java.io.BufferedReader. rdr)]
(when-let [line (.readLine r)]
((fn [l] (prn l)) line)
(recur r)))))
((fn [l] l) "foobar")

(docker/invoke states/containers {:op :ContainerStart :params {:id "kind_burnell"}})
(run "kind_burnell" "run-id")

(log/infof "Creating new container with:
image: %s
Expand All @@ -261,6 +286,8 @@
:k2 "v2"}
"/root")

(docker/invoke states/containers {:op :ContainerWait :params {:id "civicrm"}})

(docker/invoke states/containers {:op :ContainerDelete :params {:id "16a4b4e860b4" :force true}})
(delete-container "3c12417d5a7e" true)

Expand Down
20 changes: 13 additions & 7 deletions test/bob/execution/internals_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -107,32 +107,38 @@
(with-redefs-fn {#'docker/invoke (fn [_ id] {:message (format "No such container: %s" id)})}
#(is (f/failed? (status-of "id"))))))

;(deftest logs-live-test
; (testing "successfully pulling logs from container"
; (with-redefs-fn {#'docker/client (constantly true)
; #'docker/invoke (constantly (char-array "foobar"))}
; #(is (= (future "foobar") (logs-live "12121212" (fn [log] (str log))))))))

(deftest container-runs
(testing "successful run"
(let [id "11235813213455"]
(with-redefs-fn {;#'docker/logs-live (constantly true)
#'docker/invoke (fn [_ cid]
(with-redefs-fn {#'docker/invoke (fn [_ cid]
(tu/check-and-fail
#(= id (-> cid
:params
:id)))
{:State {:ExitCode 0}})}
{:StatusCode 0})
#'logs-live (constantly true)}
#(is (= "112358132134" (run id "run-id"))))))

(testing "successful run, non-zero exit"
(let [id "11235813213455"]
(with-redefs-fn {;#'docker/logs-live (constantly true)
#'docker/invoke (fn [_ cid]
(with-redefs-fn {#'docker/invoke (fn [_ cid]
(tu/check-and-fail
#(= id (-> cid
:params
:id)))
{:State {:ExitCode 1}})}
{:StatusCode 1})
#'logs-live (constantly true)}
#(let [result (run id "run-id")]
(is (and (f/failed? result)))))))

(testing "unsuccessful run"
(with-redefs-fn {;#'docker/logs-live (constantly true)
(with-redefs-fn {#'logs-live (constantly true)
#'docker/invoke (fn [_ _] (throw (Exception. "Failed")))}
#(is (f/failed? (run "id" "run-id"))))))

Expand Down

0 comments on commit 5c33c05

Please sign in to comment.