-
Notifications
You must be signed in to change notification settings - Fork 73
/
outputs.go
85 lines (67 loc) 路 2.12 KB
/
outputs.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
package daemon
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"github.com/testground/testground/pkg/api"
"github.com/testground/testground/pkg/client"
"github.com/testground/testground/pkg/logging"
"github.com/testground/testground/pkg/rpc"
)
func (d *Daemon) outputsHandler(engine api.Engine) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
log := logging.S().With("req_id", r.Header.Get("X-Request-ID"))
log.Debugw("handle request", "command", "collect outputs")
defer log.Debugw("request handled", "command", "collect outputs")
var req api.OutputsRequest
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
log.Errorw("collect outputs json decode", "err", err.Error())
w.WriteHeader(http.StatusBadRequest)
return
}
tgw := rpc.NewOutputWriter(w, r)
result := false
defer func() {
tgw.WriteResult(result)
}()
err = engine.DoCollectOutputs(r.Context(), req.RunID, tgw)
if err != nil {
log.Warnw("collect outputs error", "err", err.Error())
return
}
result = true
}
}
func (d *Daemon) getOutputsHandler(engine api.Engine) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
log := logging.S().With("req_id", r.Header.Get("X-Request-ID"))
log.Debugw("handle request", "command", "get outputs")
defer log.Debugw("request handled", "command", "get outputs")
runId := r.URL.Query().Get("run_id")
if runId == "" {
fmt.Fprintf(w, "url param `run_id` is missing")
return
}
w.Header().Set("Content-Type", "application/tar+gzip")
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s.tgz\"", runId))
req := api.OutputsRequest{
RunID: runId,
}
rr, ww := io.Pipe()
tgw := rpc.NewFileOutputWriter(ww)
go func() {
_, err := client.ParseCollectResponse(rr, w, os.Stdout)
if err != nil {
fmt.Fprintf(w, "error while parsing collect response: %s", err.Error())
}
}()
err := engine.DoCollectOutputs(r.Context(), req.RunID, tgw)
if err != nil {
log.Warnw("collect outputs error", "err", err.Error())
return
}
}
}