forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline_execution_log.go
140 lines (125 loc) · 3.39 KB
/
pipeline_execution_log.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
package pipeline
import (
"context"
"github.com/gorilla/websocket"
"github.com/rancher/norman/types"
"github.com/rancher/rancher/pkg/pipeline/engine"
"github.com/rancher/rancher/pkg/ref"
"github.com/rancher/rancher/pkg/ticker"
"github.com/sirupsen/logrus"
"net/http"
"strconv"
"strings"
"time"
)
const (
logSyncInterval = 2 * time.Second
writeWait = time.Second
longLogThreshold = 100000
checkTailLength = 1000
)
var upgrader = websocket.Upgrader{
HandshakeTimeout: 5 * time.Second,
CheckOrigin: func(r *http.Request) bool { return true },
Error: onError,
}
func onError(rw http.ResponseWriter, _ *http.Request, code int, err error) {
rw.WriteHeader(code)
rw.Write([]byte(err.Error()))
}
func (h *ExecutionHandler) handleLog(apiContext *types.APIContext) error {
stageInput := apiContext.Request.URL.Query().Get("stage")
stepInput := apiContext.Request.URL.Query().Get("step")
stage, err := strconv.Atoi(stageInput)
if err != nil {
return err
}
step, err := strconv.Atoi(stepInput)
if err != nil {
return err
}
ns, name := ref.Parse(apiContext.ID)
execution, err := h.PipelineExecutionLister.Get(ns, name)
if err != nil {
return err
}
clusterName, _ := ref.Parse(execution.Spec.ProjectName)
userContext, err := h.ClusterManager.UserContext(clusterName)
if err != nil {
return err
}
pipelineEngine := engine.New(userContext)
c, err := upgrader.Upgrade(apiContext.Response, apiContext.Request, nil)
if err != nil {
return err
}
defer c.Close()
cancelCtx, cancel := context.WithCancel(apiContext.Request.Context())
apiContext.Request = apiContext.Request.WithContext(cancelCtx)
go func() {
for {
if _, _, err := c.NextReader(); err != nil {
cancel()
c.Close()
break
}
}
}()
prevLog := ""
for range ticker.Context(cancelCtx, logSyncInterval) {
execution, err = h.PipelineExecutionLister.Get(ns, name)
if err != nil {
logrus.Debugf("error in execution get: %v", err)
if prevLog == "" {
writeData(c, []byte("Log is unavailable."))
}
c.WriteControl(websocket.CloseMessage, []byte{}, time.Now().Add(writeWait))
return nil
}
log, err := pipelineEngine.GetStepLog(execution, stage, step)
if err != nil {
logrus.Debug(err)
if prevLog == "" {
writeData(c, []byte("Log is unavailable."))
}
c.WriteControl(websocket.CloseMessage, []byte{}, time.Now().Add(writeWait))
return nil
}
newLog := getNewLog(prevLog, log)
prevLog = log
if newLog != "" {
if err := writeData(c, []byte(newLog)); err != nil {
logrus.Debugf("error in writeData: %v", err)
return nil
}
}
if execution.Status.Stages[stage].Steps[step].Ended != "" {
c.WriteControl(websocket.CloseMessage, []byte{}, time.Now().Add(writeWait))
return nil
}
}
return nil
}
func writeData(c *websocket.Conn, buf []byte) error {
messageWriter, err := c.NextWriter(websocket.TextMessage)
if err != nil {
return err
}
defer messageWriter.Close()
if _, err := messageWriter.Write(buf); err != nil {
return err
}
return nil
}
func getNewLog(prevLog string, currLog string) string {
if len(prevLog) < longLogThreshold {
return strings.TrimPrefix(currLog, prevLog)
}
//long logs from Jenkins are trimmed so we use previous log tail to do comparison
prevLogTail := prevLog[len(prevLog)-checkTailLength:]
idx := strings.Index(currLog, prevLogTail)
if idx >= 0 {
return currLog[idx+checkTailLength:]
}
return currLog
}