-
Notifications
You must be signed in to change notification settings - Fork 0
/
rci-run.go
155 lines (126 loc) · 3.2 KB
/
rci-run.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
package rci
import (
"bytes"
"encoding/json"
"fmt"
"os/exec"
"strings"
"time"
rciApi "github.com/ndmsystems/go-rci/api"
)
// Replyer ...
type Replyer interface {
Format([]byte) ([]byte, error)
}
// Run ...
func (s *svc) Run(
token []byte, hook string, args map[string]string) ([]byte, error) {
s.mu.RLock()
cmd, ok := s.hooks[hook]
s.mu.RUnlock()
if !ok {
return nil, fmt.Errorf("hook '%s' does not exist", hook)
}
switch cmd.Type {
case rciApi.CommandTypeShellScript:
if cmd.Sync {
return s.runShellScript(token, cmd, args)
}
return s.runShellScriptAsync(token, cmd, args)
case rciApi.CommandTypeBuiltIn:
return s.runBuiltIn(token, cmd, args)
default:
return nil, fmt.Errorf("unsupported command type '%s'", cmd.Type)
}
}
func (s *svc) runShellScript(
token []byte, hook *rciApi.Hook, args map[string]string) ([]byte, error) {
if len(hook.Data.Execute) < 1 {
return nil, fmt.Errorf("empty 'execute' of hook '%s'", hook.Hook)
}
cmd := exec.Command("sh", "-c", hook.Data.Execute[0]+" 2>&1")
ret := "result"
output, err := cmd.CombinedOutput()
if err != nil {
ret = "error"
errOutput := bytes.TrimSpace([]byte(err.Error()))
errOutput = append(errOutput, byte(10))
errOutput = append(errOutput, output...)
output = errOutput
}
return formatShellScript(hook, ret, bytes.TrimSpace(output))
}
func (s *svc) runBuiltIn(
token []byte, hook *rciApi.Hook, args map[string]string) ([]byte, error) {
if hook.Data.BuiltIn == nil {
return nil, fmt.Errorf("built-in hook '%s' is nil", hook.Hook)
}
return hook.Data.BuiltIn(token, hook, args)
}
func formatShellScript(
command *rciApi.Hook,
ret string, // "result" | "error"
data []byte) ([]byte, error) {
parts := strings.Split(strings.TrimPrefix(command.Hook, "/rci/"), "/")
var buf bytes.Buffer
buf.WriteString("{")
for _, part := range parts {
buf.WriteString("\"")
buf.WriteString(part)
buf.WriteString("\":{")
}
// hasNewLines := bytes.Contains(data, []byte{10})
// if hasNewLines {
buf.WriteString("\"" + ret + "\":[")
lines := bytes.Split(data, []byte{10})
for i, line := range lines {
jsonValue, err := json.Marshal(string(line))
if err != nil {
buf.WriteString("error json.Marshal():" + err.Error())
break
}
buf.Write(jsonValue)
if i < len(lines)-1 {
buf.WriteString(",")
}
}
buf.WriteString("]")
for i := 0; i < len(parts); i++ {
buf.WriteString("}")
}
buf.WriteString("}")
return buf.Bytes(), nil
}
// Running hooks: allow single running script hook
func (s *svc) markScriptRunning(uid, cmd string) (string, error) {
s.runningHooksLock.Lock()
defer s.runningHooksLock.Unlock()
now := time.Now()
r, ok := s.runningHooks[cmd]
if ok {
return r.UID, fmt.Errorf("running %s",
now.Sub(r.TS).Truncate(time.Millisecond))
}
s.runningHooks[cmd] = ActiveHook{
UID: uid,
TS: now,
}
return "", nil
}
func (s *svc) remarkScriptRunning(uid, cmd string) {
s.runningHooksLock.Lock()
defer s.runningHooksLock.Unlock()
if _, ok := s.runningHooks[cmd]; ok {
return
}
now := time.Now()
s.runningHooks[cmd] = ActiveHook{
UID: uid,
TS: now,
}
}
func (s *svc) unmarkScriptRunning(cmd string) {
s.runningHooksLock.Lock()
defer s.runningHooksLock.Unlock()
delete(s.runningHooks, cmd)
}