/
log_api.go
190 lines (137 loc) · 4.86 KB
/
log_api.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
package rest
import(
"io"
"fmt"
"time"
"strings"
"net/http"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
"hyperflow.in/server/pkg/base"
"hyperflow.in/server/pkg/base/structs"
ws "hyperflow.in/server/pkg/workspace"
)
func serveLogStream(sc *ServerContext, w http.ResponseWriter, r *http.Request) error{
var err error
req_params := mux.Vars(r)
task_id := req_params["taskId"]
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
base.Error("websocket connection upgrade error:", err)
return base.HTTPErrorf(http.StatusBadRequest, "Could not upgrade HTTP connection to websocket")
}
defer c.Close()
reader, err := sc.flowAPI.LogStream(task_id)
if err != nil {
base.Error("failed to read log stream: ", err)
return base.HTTPErrorf(http.StatusBadRequest, "Failed to read log stream: " + err.Error())
}
msg_buf := make([]byte, 128)
//todo: utils.GetBuffer()
//defer utils.PutBuffer()
for {
n, err := reader.Read(msg_buf)
if err != nil{
if err == io.EOF {
//should handle any remaining bytes.
if err = c.WriteMessage(websocket.TextMessage, msg_buf[:n]); err != nil {
break
}
break
}
base.Error(err.Error())
}
if err = c.WriteMessage(websocket.TextMessage, msg_buf[:n]); err != nil {
return base.HTTPErrorf(http.StatusBadRequest, "Failed to write to websocket: " + err.Error())
}
}
return nil
}
func (h *Handler) handleGetTaskLog() error {
if (h.rq.Method != "GET") {
return base.HTTPErrorf(http.StatusMethodNotAllowed, "Invalid method %s", h.rq.Method)
}
task_id, _ := h.getMandatoryUrlParam("taskId")
log_path := h.server.logAPI.GetObjectPath(h.server.flowAPI.GetTaskLogPath(task_id))
rs, err := h.server.logAPI.ReadSeeker(log_path, 0, 0)
if err != nil {
if err != io.EOF {
fmt.Println("Failed to fetch log from :", log_path)
return base.HTTPErrorf(http.StatusBadRequest, "Error occurred while fetching log object.")
}
}
h.setHeader("Content-Disposition", fmt.Sprintf("attachment; filename=\"%v\"", log_path))
http.ServeContent(h.response, h.rq, log_path, time.Time{}, rs)
return nil
}
func errLogObjectMissing(err error) bool {
if err == nil {
return false
}
err_string := err.Error()
if strings.Contains(err_string, "object doesn't exist") {
return true
}
return false
}
func (h *Handler) handleGetCommandLog() error {
if (h.rq.Method != "GET") {
return base.HTTPErrorf(http.StatusMethodNotAllowed, "Invalid method %s", h.rq.Method)
}
task_id, _ := h.getMandatoryUrlParam("taskId")
log_path := h.server.flowAPI.GetCommandLogPath(task_id)
log_path = h.server.logAPI.GetObjectPath(log_path)
log_path = log_path + ".log"
rs, err := h.server.logAPI.ReadSeeker(log_path, 0, 0)
if errLogObjectMissing(err) {
log_path := h.server.logAPI.GetObjectPath(h.server.flowAPI.GetTaskLogPath(task_id))
rs, err = h.server.logAPI.ReadSeeker(log_path, 0, 0)
}
if err != nil {
if errLogObjectMissing(err) {
return nil
}
if err != io.EOF {
return base.HTTPErrorf(http.StatusBadRequest, "Error occurred while fetching log object.")
}
}
h.setHeader("Content-Disposition", fmt.Sprintf("attachment; filename=\"%v\"", log_path))
http.ServeContent(h.response, h.rq, log_path, time.Time{}, rs)
return nil
}
func (h *Handler) handlePostCommandLog() error {
if (h.rq.Method != "POST") {
return base.HTTPErrorf(http.StatusMethodNotAllowed, "Invalid method %s", h.rq.Method)
}
var response map[string]interface{}
task_id, err := h.getMandatoryUrlParam("taskId")
if err != nil {
return base.HTTPErrorf(http.StatusBadRequest, "One of the params is missing: taskId")
}
if h.rq.Body != nil {
log_path := h.server.flowAPI.GetCommandLogPath(task_id)
log_path = h.server.logAPI.GetObjectPath(log_path)
base.Info("[Handler.handlePostTaskLog] log_path: ", log_path)
obj_path, chksm, size, err := h.server.logAPI.SaveObject(log_path, "", h.rq.Body, false)
fmt.Println("size: ", size)
if err != nil {
base.Debug("[Handler.handlePostFlowLog] Error occurred writing log on server ", task_id, err)
return base.HTTPErrorf(http.StatusBadRequest, "Failed to save log object: ", err)
}
if size == 0 {
base.Debug("[Handler.handlePostFlowLog] The input file is empty for flow log request: ", task_id)
return base.HTTPErrorf(http.StatusBadRequest, "Input file is empty")
}
obj := &ws.Object{
Path: obj_path,
CheckSum: chksm,
Size: int(size) }
response = structs.Map(obj)
h.writeJSON(response)
return nil
} else {
base.Debug("[Handler.handlePostFlowLog] Empty request body was sent for task: ", task_id)
return base.HTTPErrorf(http.StatusBadRequest, "Request body is empty")
}
return nil
}