This repository has been archived by the owner on Jun 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.go
106 lines (84 loc) · 2.16 KB
/
report.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
package main
import (
"crypto/tls"
"net/url"
"time"
"encoding/json"
"github.com/codegangsta/cli"
"github.com/go-redis/redis"
)
//Report structure
type Report struct {
//ID of the sandbox
ID string `json:"-"`
//Stdout captured stdout of the sandbox process
Stdout string `json:"stdout"`
//Stderr captured stderr of the sandbox process
Stderr string `json:"stderr"`
//Error containers the str representation of the exit error (ususally exit code)
Error string `json:"error"`
ExitTime time.Time `json:"time"`
}
//Reporter defines a reporter callback
type Reporter func(u *url.URL, report *Report) error
var (
reporters = map[string]Reporter{
"redis": redisReporter,
"redis+tls": redisReporter,
}
)
func report(ctx *cli.Context, stdout, stderr []byte, result error) error {
recievers := ctx.GlobalStringSlice("report")
report := Report{
ID: ctx.GlobalString("id"),
ExitTime: time.Now(),
//we convert both stdout and stderr to string, to make it humand readable
//in the json representation
Stdout: string(stdout),
Stderr: string(stderr),
Error: result.Error(),
}
for _, receiver := range recievers {
u, err := url.Parse(receiver)
if err != nil {
log.Errorf("invalid report url: %v", err)
continue
}
reporter, ok := reporters[u.Scheme]
if !ok {
log.Errorf("no reporter handler for scheme '%v'", u.Scheme)
continue
}
if err := reporter(u, &report); err != nil {
log.Errorf("failed to report to '%v': %v", receiver, err)
}
}
return nil
}
//redis reporter, report error to redis
//accepts the following URL
// redis[+tls]://[password@]host:port
func redisReporter(u *url.URL, report *Report) error {
var config *tls.Config
if u.Scheme == "redis+tls" {
config = &tls.Config{
ServerName: u.Hostname(),
InsecureSkipVerify: true, // TODO: this should be removed in production
}
}
var password string
if u.User != nil {
password = u.User.String()
}
cl := redis.NewClient(&redis.Options{
Addr: u.Host,
Password: password,
TLSConfig: config,
})
defer cl.Close()
data, err := json.Marshal(report)
if err != nil {
return err
}
return cl.LPush(report.ID, string(data)).Err()
}