-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.go
201 lines (183 loc) · 5.48 KB
/
actions.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
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"strings"
"syscall"
"time"
log "github.com/Sirupsen/logrus"
"github.com/goware/prefixer"
. "github.com/larskluge/babl-server/utils"
"github.com/larskluge/babl-storage/download"
"github.com/larskluge/babl-storage/upload"
pbm "github.com/larskluge/babl/protobuf/messages"
)
func IO(req *pbm.BinRequest, maxReplySize int) (*pbm.BinReply, error) {
start := time.Now()
res := pbm.BinReply{Id: req.Id, Module: req.Module, Exitcode: 0}
l := log.WithFields(log.Fields{"rid": FmtRid(req.Id)})
done := make(chan bool, 1)
_, async := req.Env["BABL_ASYNC"]
if async {
done <- true
}
go func() {
cmd := exec.Command(command)
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
env := os.Environ()
cmd.Env = []string{} // {"FOO=BAR"}
vars := []string{}
for k, v := range req.Env {
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", k, v))
vars = append(vars, k)
}
cmd.Env = append(cmd.Env, env...)
cmd.Env = append(cmd.Env, "BABL_VARS="+strings.Join(vars, ","))
payload := req.Stdin
if len(payload) <= 0 && req.PayloadUrl != "" {
start := time.Now()
l.WithFields(log.Fields{"payload_url": req.PayloadUrl, "code": "req-downloading"}).Info("Downloading external payload")
var err error
payload, err = download.Download(req.PayloadUrl)
elapsed := float64(time.Since(start).Seconds() * 1000)
l := l.WithFields(log.Fields{"duration_ms": elapsed})
if err != nil {
l.WithError(err).Fatal("Payload download failed")
}
l.WithFields(log.Fields{"payload_size": len(payload), "code": "req-downloaded"}).Info("Payload download successful")
}
stdinBytes := len(payload)
stdin, err := cmd.StdinPipe()
if err != nil {
l.WithError(err).Error("cmd.StdinPipe")
}
stdout, err := cmd.StdoutPipe()
if err != nil {
l.WithError(err).Error("cmd.StdoutPipe")
}
stderr, err := cmd.StderrPipe()
if err != nil {
l.WithError(err).Error("cmd.StderrPipe")
}
var stderrBuf bytes.Buffer
// FIXME: prefixer breaks realtime reading of stderr
stderrCopy := io.TeeReader(prefixer.New(stderr, ModuleName+": "), &stderrBuf)
stderrCopied := make(chan bool, 1)
go func() {
req := bufio.NewScanner(stderrCopy)
for req.Scan() {
l.Debug(req.Text())
}
if err := req.Err(); err != nil {
l.WithError(err).Warn("Copy module exec stderr stream to logs failed")
}
stderrCopied <- true
}()
// write to stdin non-blocking so external process can start consuming data
// before buffer is full and everything blocks up
go func() {
stdin.Write(payload)
payload = nil
stdin.Close()
}()
err = cmd.Start()
if err != nil {
l.WithError(err).Error("cmd.Start")
}
timer := time.AfterFunc(CommandTimeout, func() {
res.Status = pbm.BinReply_EXECUTION_TIMEOUT
res.Error = fmt.Sprintf("Process calculation timed out after %s, killing process group", CommandTimeout)
l.Error(res.Error)
pgid, err := syscall.Getpgid(cmd.Process.Pid)
if err == nil {
syscall.Kill(-pgid, 15) // note the minus sign
}
// cmd.Process.Kill()
})
res.Stdout, err = ioutil.ReadAll(stdout)
if err != nil {
l.WithError(err).Error("ioutil.ReadAll(stdout)")
}
<-stderrCopied
res.Stderr = stderrBuf.Bytes()
if err := cmd.Wait(); err != nil {
res.Exitcode = 255
if exiterr, ok := err.(*exec.ExitError); ok {
// The program has exited with an exit code != 0
// This works on both Unix and Windows. Although package
// syscall is generally platform dependent, WaitStatus is
// defined for both Unix and Windows and in both cases has
// an ExitStatus() method with the same signature.
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
res.Exitcode = int32(status.ExitStatus())
}
} else {
l.WithError(err).Error("cmd.Wait")
}
}
timer.Stop()
stdoutBytes := len(res.Stdout)
if len(res.Stdout) > maxReplySize {
start := time.Now()
l.WithFields(log.Fields{"payload_size": stdoutBytes, "code": "reply-uploading"}).Info("Uploading payload")
up, err := upload.New(StorageEndpoint, bytes.NewReader(res.Stdout))
if err != nil {
l.WithError(err).Fatal("Payload upload failed")
}
l := l.WithFields(log.Fields{"blob_url": up.Url})
go func() {
up.WaitForCompletion()
elapsed := float64(time.Since(start).Seconds() * 1000)
l.WithFields(log.Fields{"duration_ms": elapsed, "payload_size": stdoutBytes, "code": "reply-uploaded"}).Info("Payload upload done")
}()
res.Stdout = []byte{}
res.PayloadUrl = up.Url
}
if res.Exitcode == 0 {
res.Status = pbm.BinReply_SUCCESS
} else {
if res.Status != pbm.BinReply_EXECUTION_TIMEOUT {
res.Status = pbm.BinReply_ERROR
}
}
elapsed := float64(time.Since(start).Seconds() * 1000)
fields := log.Fields{
"stdin_bytes": stdinBytes,
"stdout_bytes": stdoutBytes,
"stderr_bytes": len(res.Stderr),
"stderr": string(res.Stderr),
"exitcode": res.Exitcode,
"status": res.Status.String(),
"duration_ms": elapsed,
"code": "req-executed",
}
if async {
fields["mode"] = "async"
} else {
fields["mode"] = "sync"
}
l = l.WithFields(fields)
if res.Status == pbm.BinReply_SUCCESS {
l.Info("call")
} else {
l.Error("call")
}
done <- true
}()
select {
case <-done:
case <-time.After(CommandTimeout + 15*time.Second):
l.Fatal("Something went terribly wrong")
}
return &res, nil
}
func Ping(in *pbm.Empty) (*pbm.Pong, error) {
log.Info("ping")
res := pbm.Pong{Val: "pong"}
return &res, nil
}