-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.clj
192 lines (180 loc) · 7.19 KB
/
commands.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
(ns monkey.ci.commands
"Event handlers for commands"
(:require [clojure.tools.logging :as log]
[monkey.ci
[build :as b]
[jobs :as jobs]
[runtime :as rt]
[script :as script]
[sidecar :as sidecar]
[utils :as u]]
[monkey.ci.events.core :as ec]
[monkey.ci.web.handler :as h]
[aleph.http :as http]
[clj-commons.byte-streams :as bs]
[manifold
[deferred :as md]
[time :as mt]]))
(defn prepare-build-ctx
"Updates the runtime for the build runner, by adding a `build` object"
[rt]
(assoc rt :build (b/make-build-ctx rt)))
;; (defn- print-result [state]
;; (log/info "Build summary:")
;; (let [{:keys [pipelines]} @state]
;; (doseq [[pn p] pipelines]
;; (log/info "Pipeline:" pn)
;; (doseq [[sn {:keys [name status start-time end-time]}] (:steps p)]
;; (log/info " Step:" (or name sn)
;; ", result:" (clojure.core/name status)
;; ", elapsed:" (- end-time start-time) "ms")))))
;; (defn- report-evt [ctx e]
;; (rt/report ctx
;; {:type :build/event
;; :event e}))
;; (defn result-accumulator
;; "Returns a map of event types and handlers that can be registered in the bus.
;; These handlers will monitor the build progress and update an internal state
;; accordingly. When the build completes, the result is logged."
;; [ctx]
;; (let [state (atom {})
;; now (fn [] (System/currentTimeMillis))]
;; {:state state
;; :handlers
;; {:step/start
;; (fn [{:keys [index name pipeline] :as e}]
;; (report-evt ctx e)
;; (swap! state assoc-in [:pipelines (:name pipeline) :steps index] {:start-time (now)
;; :name name}))
;; :step/end
;; (fn [{:keys [index pipeline status] :as e}]
;; (report-evt ctx e)
;; (swap! state update-in [:pipelines (:name pipeline) :steps index]
;; assoc :end-time (now) :status status))
;; :build/completed
;; (fn [_]
;; (print-result state))}}))
;; (defn register-all-handlers [bus m]
;; (when bus
;; (doseq [[t h] m]
;; (e/register-handler bus t h))))
(defn run-build
"Performs a build, using the runner from the context. Returns a deferred
that will complete when the build finishes."
[rt]
(let [r (:runner rt)]
#_(report-evt ctx {:type :script/start})
#_(register-all-handlers event-bus (:handlers acc))
(try
(-> rt
(prepare-build-ctx)
(r))
(catch Exception ex
(log/error "Unable to start build" ex)
(let [exit-code 1]
(rt/post-events rt (b/build-end-evt (assoc (rt/build rt) :message (ex-message ex))
exit-code))
exit-code)))))
(defn verify-build
"Verifies the build in the current directory by loading the script files in-process
and resolving the jobs. This is useful when checking if there are any compilation
errors in the script."
[rt]
(try
(log/debug "Verifying build with runtime" (prepare-build-ctx rt))
;; TODO Git branch and other options
(let [jobs (-> rt
(prepare-build-ctx)
(script/load-jobs))]
(rt/report
rt
(if (not-empty jobs)
{:type :verify/success
:jobs jobs}
{:type :verify/failed
:message "No jobs found in build script"}))
(if (empty? jobs) 1 0))
(catch Exception ex
(log/error "Error verifying build" ex)
(rt/report rt {:type :verify/failed
:message (ex-message ex)})
2)))
(defn list-builds [rt]
(->> (http/get (apply format "%s/customer/%s/repo/%s/builds"
((juxt :url :customer-id :repo-id) (rt/account rt)))
{:headers {"accept" "application/edn"}})
(deref)
:body
(bs/to-reader)
(u/parse-edn)
(hash-map :type :build/list :builds)
(rt/report rt)))
(defn http-server
"Starts the server by invoking the function in the runtime. This function is supposed
to return another function that can be invoked to stop the http server. Returns a
deferred that resolves when the server is stopped."
[{:keys [http] :as rt}]
(rt/report rt (-> rt
(rt/config)
(select-keys [:http])
(assoc :type :server/started)))
;; Start the server and wait for it to shut down
(h/on-server-close (http rt)))
(defn watch
"Starts listening for events and prints the results. The arguments determine
the event filter (all for a customer, project, or repo)."
[rt]
(let [url (rt/api-url rt)
d (md/deferred)
pipe-events (fn [r]
(let [read-next (fn [] (u/parse-edn r {:eof ::done}))]
(loop [m (read-next)]
(if (= ::done m)
(do
(log/info "Event stream closed")
(md/success! d 0)) ; Exit code 0
(do
(log/debug "Got event:" m)
(rt/report rt {:type :build/event :event m})
(recur (read-next)))))))]
(log/info "Watching the server at" url "for events...")
(rt/report rt {:type :watch/started
:url url})
;; TODO Trailing slashes
;; TODO Customer and other filtering
(-> (md/chain
(http/get (str url "/events"))
:body
bs/to-reader)
(md/on-realized pipe-events
(fn [err]
(log/error "Unable to receive server events:" err))))
;; Return a deferred that only resolves when the event stream stops
d))
(defn sidecar
"Runs the application as a sidecar, that is meant to capture events
and logs from a container process. This is necessary because when
running containers from third-party images, we don't have control
over them. Instead, we launch a sidecar and execute the commands
in the container in a script that writes events and output to files,
which are then picked up by the sidecar to dispatch or store.
The sidecar loop will stop when the events file is deleted."
[rt]
(let [sid (b/get-sid rt)
;; Add job info from the sidecar config
{:keys [job] :as rt} (merge rt (get-in rt [rt/config :sidecar :job-config]))]
(let [result (try
(rt/post-events rt {:type :sidecar/start
:sid sid
:job (jobs/job->event job)})
(let [r @(sidecar/run rt)
e (:exit r)]
(ec/make-result (b/exit-code->status e) e (:message r)))
(catch Throwable t
(ec/exception-result t)))]
(log/info "Sidecar terminated")
(rt/post-events rt (-> {:type :sidecar/end
:sid sid
:job (jobs/job->event job)}
(ec/set-result result)))
(:exit result))))