forked from harness/gitness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
295 lines (256 loc) · 7 KB
/
handler.go
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Copyright 2019 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by the Drone Non-Commercial License
// that can be found in the LICENSE file.
// +build !oss
/*
/rpc/v2/stage POST (request)
/rpc/v2/stage/{stage}?machine= POST (accept, details)
/rpc/v2/stage/{stage} PUT (beforeAll, afterAll)
/rpc/v2/stage/{stage}/steps/{step} PUT (before, after)
/rpc/v2/build/{build}/watch POST (watch)
/rpc/v2/stage/{stage}/logs/batch POST (batch)
/rpc/v2/stage/{stage}/logs/upload POST (upload)
*/
package rpc2
import (
"context"
"encoding/json"
"io"
"net/http"
"strconv"
"time"
"github.com/go-chi/chi"
"github.com/drone/drone/core"
"github.com/drone/drone/operator/manager"
"github.com/drone/drone/store/shared/db"
)
// default http request timeout
var defaultTimeout = time.Second * 30
var noContext = context.Background()
// HandleJoin returns an http.HandlerFunc that makes an
// http.Request to join the cluster.
//
// POST /rpc/v2/nodes/:machine
func HandleJoin() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
writeOK(w) // this is a no-op
}
}
// HandleLeave returns an http.HandlerFunc that makes an
// http.Request to leave the cluster.
//
// DELETE /rpc/v2/nodes/:machine
func HandleLeave() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
writeOK(w) // this is a no-op
}
}
// HandlePing returns an http.HandlerFunc that makes an
// http.Request to ping the server and confirm connectivity.
//
// GET /rpc/v2/ping
func HandlePing() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
writeOK(w) // this is a no-op
}
}
// HandleRequest returns an http.HandlerFunc that processes an
// http.Request to reqeust a stage from the queue for execution.
//
// POST /rpc/v2/stage
func HandleRequest(m manager.BuildManager) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
defer cancel()
req := new(manager.Request)
err := json.NewDecoder(r.Body).Decode(req)
if err != nil {
writeError(w, err)
return
}
stage, err := m.Request(ctx, req)
if err != nil {
writeError(w, err)
} else {
writeJSON(w, stage)
}
}
}
// HandleAccept returns an http.HandlerFunc that processes an
// http.Request to accept ownership of the stage.
//
// POST /rpc/v2/stage/{stage}?machine=
func HandleAccept(m manager.BuildManager) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
stage, _ := strconv.ParseInt(
chi.URLParam(r, "stage"), 10, 64)
out, err := m.Accept(noContext, stage, r.FormValue("machine"))
if err != nil {
writeError(w, err)
} else {
writeJSON(w, out)
}
}
}
// HandleInfo returns an http.HandlerFunc that processes an
// http.Request to get the build details.
//
// POST /rpc/v2/build/{build}
func HandleInfo(m manager.BuildManager) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
stage, _ := strconv.ParseInt(
chi.URLParam(r, "stage"), 10, 64)
res, err := m.Details(noContext, stage)
if err != nil {
writeError(w, err)
return
}
netrc, err := m.Netrc(noContext, res.Repo.ID)
if err != nil {
writeError(w, err)
return
}
writeJSON(w, &details{
Context: res,
Netrc: netrc,
Repo: &repositroy{
Repository: res.Repo,
Secret: res.Repo.Secret,
},
})
}
}
// HandleUpdateStage returns an http.HandlerFunc that processes
// an http.Request to update a stage.
//
// PUT /rpc/v2/stage/{stage}
func HandleUpdateStage(m manager.BuildManager) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
dst := new(core.Stage)
err := json.NewDecoder(r.Body).Decode(dst)
if err != nil {
writeError(w, err)
return
}
if dst.Status == core.StatusPending ||
dst.Status == core.StatusRunning {
err = m.BeforeAll(noContext, dst)
} else {
err = m.AfterAll(noContext, dst)
}
if err != nil {
writeError(w, err)
} else {
writeJSON(w, dst)
}
}
}
// HandleUpdateStep returns an http.HandlerFunc that processes
// an http.Request to update a step.
//
// POST /rpc/v2/step/{step}
func HandleUpdateStep(m manager.BuildManager) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
dst := new(core.Step)
err := json.NewDecoder(r.Body).Decode(dst)
if err != nil {
writeError(w, err)
return
}
if dst.Status == core.StatusPending ||
dst.Status == core.StatusRunning {
err = m.Before(noContext, dst)
} else {
err = m.After(noContext, dst)
}
if err != nil {
writeError(w, err)
} else {
writeJSON(w, dst)
}
}
}
// HandleWatch returns an http.HandlerFunc that accepts a
// blocking http.Request that watches a build for cancellation
// events.
//
// GET /rpc/v2/build/{build}/watch
func HandleWatch(m manager.BuildManager) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
defer cancel()
build, _ := strconv.ParseInt(
chi.URLParamFromCtx(ctx, "build"), 10, 64)
_, err := m.Watch(ctx, build)
if err != nil {
writeError(w, err)
} else {
writeOK(w)
}
}
}
// HandleLogBatch returns an http.HandlerFunc that accepts an
// http.Request to submit a stream of logs to the system.
//
// POST /rpc/v2/step/{step}/logs/batch
func HandleLogBatch(m manager.BuildManager) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
step, _ := strconv.ParseInt(
chi.URLParam(r, "step"), 10, 64)
lines := []*core.Line{}
err := json.NewDecoder(r.Body).Decode(&lines)
if err != nil {
writeError(w, err)
return
}
// TODO(bradrydzewski) modify the write function to
// accept a slice of lines.
for _, line := range lines {
err := m.Write(noContext, step, line)
if err != nil {
writeError(w, err)
return
}
}
writeOK(w)
}
}
// HandleLogUpload returns an http.HandlerFunc that accepts an
// http.Request to upload and persist logs for a pipeline stage.
//
// POST /rpc/v2/step/{step}/logs/upload
func HandleLogUpload(m manager.BuildManager) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
step, _ := strconv.ParseInt(
chi.URLParam(r, "step"), 10, 64)
err := m.Upload(noContext, step, r.Body)
if err != nil {
writeError(w, err)
} else {
writeOK(w)
}
}
}
// write a 200 Status OK to the response body.
func writeJSON(w http.ResponseWriter, v interface{}) {
json.NewEncoder(w).Encode(v)
}
// write a 200 Status OK to the response body.
func writeOK(w http.ResponseWriter) {
w.WriteHeader(http.StatusOK)
}
// write an error message to the response body.
func writeError(w http.ResponseWriter, err error) {
if err == context.DeadlineExceeded {
w.WriteHeader(204) // should retry
} else if err == context.Canceled {
w.WriteHeader(204) // should retry
} else if err == db.ErrOptimisticLock {
w.WriteHeader(409) // should abort
} else {
w.WriteHeader(500) // should fail
}
io.WriteString(w, err.Error())
}