-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-boom.go
134 lines (106 loc) · 2.75 KB
/
docker-boom.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
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"strings"
"syscall"
"github.com/nlopes/slack"
)
type ChannelType struct {
Recipients []string `json:recipients`
}
type configType struct {
Channels ChannelType
}
// BufferMaxLines store and send only last numer of stdout and stderr lines
var BufferMaxLines = 15
func prepareCommand() *exec.Cmd {
if len(os.Args) > 1 {
return exec.Command(os.Args[1], os.Args[2:]...)
}
return nil
}
func ignoreEmpty(prefix string, buffer []string) string {
if len(buffer) > 0 {
return prefix + strings.Join(buffer, "\n")
} else {
return ""
}
}
func formatMessage(stdoutBuffer []string, stderrBuffer []string) string {
return strings.Join([]string{
"```",
"CMD: " + strings.Join(os.Args[1:], " "),
ignoreEmpty("STDERR:\n", stderrBuffer),
ignoreEmpty("STDOUT:\n", stdoutBuffer),
"```"}, "\n")
}
func readStd(stdout io.ReadCloser, output *os.File) (buffer []string) {
in := bufio.NewScanner(stdout)
for in.Scan() {
buffer = append(buffer, in.Text())
if len(buffer) > BufferMaxLines {
buffer = append(buffer[:0], buffer[1:]...)
}
fmt.Fprintln(output, in.Text())
}
if err := in.Err(); err != nil {
fmt.Println("error: %s", err)
}
return buffer
}
func runCommand(cmd *exec.Cmd) (stdoutBuffer []string, stderrBuffer []string, exitCode int) {
stdout, outerr := cmd.StdoutPipe()
stderr, errerr := cmd.StderrPipe()
if outerr != nil || errerr != nil {
return stdoutBuffer, stderrBuffer, -1
}
if err := cmd.Start(); err != nil {
return stdoutBuffer, stderrBuffer, -1
}
stdoutBuffer = readStd(stdout, os.Stdout)
stderrBuffer = readStd(stderr, os.Stderr)
if err := cmd.Wait(); err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
exitCode = status.ExitStatus()
}
}
}
return stdoutBuffer, stderrBuffer, exitCode
}
func Configuration() (config map[string]ChannelType) {
raw, _ := ioutil.ReadFile(".docker-boom.json")
json.Unmarshal(raw, &config)
return config
}
func sendLogs(stdoutBuffer []string, stderrBuffer []string, exitCode int, conf map[string]ChannelType) {
if exitCode == 0 || len(stdoutBuffer) < 1 && len(stderrBuffer) < 1 {
return
}
if _, ok := conf["slack"]; !ok {
return
}
slackToken := os.Getenv("SLACK_TOKEN")
if slackToken == "" {
return
}
api := slack.New(slackToken)
params := slack.PostMessageParameters{}
for _, re := range conf["slack"].Recipients {
api.PostMessage(re, formatMessage(stdoutBuffer, stderrBuffer), params)
}
}
func main() {
conf := Configuration()
if cmd := prepareCommand(); cmd != nil {
stdoutBuffer, stderrBuffer, exitCode := runCommand(cmd)
sendLogs(stdoutBuffer, stderrBuffer, exitCode, conf)
os.Exit(exitCode)
}
}