-
Notifications
You must be signed in to change notification settings - Fork 0
/
magic.go
119 lines (101 loc) · 3.14 KB
/
magic.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
package main
import (
"bufio"
"encoding/gob"
"flag"
"github.com/ajackson/metron/contrail"
"io"
"net"
"os"
"os/exec"
"path/filepath"
"time"
)
var appId string
const (
maxMessageLength = 10 * 1024
)
func init() {
flag.StringVar(&appId, "appId", "", "Application ID to add to all messages, omit if neccessary")
}
func main() {
flag.Parse()
if flag.NArg() == 0 {
flag.Usage()
return
}
Exec(flag.Arg(0), flag.Args()[1:])
}
func Exec(cmdString string, args []string) error {
stdoutReader, stdoutWriter := io.Pipe()
stderrReader, stderrWriter := io.Pipe()
defer stdoutReader.Close()
defer stderrReader.Close()
cmd := exec.Command(cmdString, args...)
cmd.Stdout = stdoutWriter
cmd.Stderr = stderrWriter
os.MkdirAll(filepath.Join(os.TempDir(), "iomagic", cmdString), 0700)
inSock := filepath.Join(os.TempDir(), "iomagic", cmdString, time.Now().Format(time.RFC3339Nano))
conn, err := net.ListenPacket("unixgram", inSock)
if err != nil {
panic(err)
}
defer os.Remove(inSock)
defer conn.Close()
statusAddr, _ := net.ResolveUnixAddr("unixgram", "/tmp/metron_status.sock")
statusEncoder := gob.NewEncoder(&PacketConnWriter{conn: conn, addr: statusAddr})
go listenToStdOut(cmdString, conn, stdoutReader)
go listenToStdErr(cmdString, conn, stderrReader)
println("Running Command", cmdString)
err = cmd.Start()
ps := contrail.NewProcessStatusStart(appId, cmdString, cmd.Process)
statusEncoder.Encode(ps)
if (err != nil) {
pc := contrail.NewProcessStatusComplete(appId, cmdString, cmd.ProcessState)
statusEncoder.Encode(pc)
return err
}
err = cmd.Wait()
pc := contrail.NewProcessStatusComplete(appId, cmdString, cmd.ProcessState)
statusEncoder.Encode(pc)
return err
}
func processConnection(metronWriter io.Writer, processOutput io.Reader, logMessage contrail.LogLine) {
encoder := gob.NewEncoder(metronWriter)
err := bufio.ErrTooLong
for err == bufio.ErrTooLong {
scanner := bufio.NewScanner(processOutput)
for scanner.Scan() {
message := scanner.Bytes()
messageLength := len(message)
if messageLength == 0 {
continue
}
if messageLength > maxMessageLength {
message = message[:maxMessageLength]
}
logMessage.Message = message
logMessage.Timestamp = time.Now()
logMessage.Sequence++
encoder.Encode(logMessage)
}
err = scanner.Err()
}
}
func listenToStdOut(cmdString string, conn net.PacketConn, processReader io.Reader) {
stdOutAddr, _ := net.ResolveUnixAddr("unixgram", "/tmp/metron_out.sock")
logMessage := contrail.LogLine{AppId: appId, Command: cmdString, OutputType: contrail.Stdout}
processConnection(&PacketConnWriter{conn: conn, addr: stdOutAddr}, processReader, logMessage)
}
func listenToStdErr(cmdString string, conn net.PacketConn, processReader io.Reader) {
stdErrAddr, _ := net.ResolveUnixAddr("unixgram", "/tmp/metron_err.sock")
logMessage := contrail.LogLine{AppId: appId, Command: cmdString, OutputType: contrail.Stderr}
processConnection(&PacketConnWriter{conn: conn, addr: stdErrAddr}, processReader, logMessage)
}
type PacketConnWriter struct {
conn net.PacketConn
addr net.Addr
}
func (w *PacketConnWriter) Write(p []byte) (n int, err error) {
return w.conn.WriteTo(p, w.addr)
}