-
Notifications
You must be signed in to change notification settings - Fork 18
/
cm-minion.go
67 lines (51 loc) · 1.11 KB
/
cm-minion.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
package main
import (
"flag"
"fmt"
"gopkg.in/project-iris/iris-go.v1"
"log"
"os/exec"
"runtime"
)
type cmdEvent struct{}
var cmd = flag.String("cmd", "uptime", "command to execute at minion")
var shell, f = getShellAndFlag()
func runCmd(args string) []byte {
cmd := exec.Command(shell, f, args)
output, err := cmd.CombinedOutput()
if err != nil {
return []byte("error")
} else {
return output
}
}
func getShellAndFlag() (string, string) {
if runtime.GOOS == "windows" {
return "cmd", "/C"
} else {
return "/bin/sh", "-c"
}
}
func (c cmdEvent) HandleEvent(event []byte) {
output := runCmd(string(event))
conn.Publish("reply", output)
}
var conn *iris.Connection
func main() {
topicLimits := &iris.TopicLimits{
EventThreads: runtime.NumCPU(),
EventMemory: 64 * 1024 * 1024,
}
flag.Parse()
var err error
conn, err = iris.Connect(55555)
if err != nil {
log.Fatalf("failed to connect to the Iris relay: %v.", err)
} else {
log.Println("Connected to port 55555")
}
var cmdHandler = new(cmdEvent)
conn.Subscribe("cmd", cmdHandler, topicLimits)
defer conn.Close()
fmt.Scanln()
}