Skip to content

Commit 89029d6

Browse files
committed
Added callback context in action execution result
1 parent 06a8538 commit 89029d6

9 files changed

+157
-53
lines changed

go.mod

+2-4
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,17 @@ replace (
99

1010
require (
1111
github.com/BurntSushi/toml v0.3.1 // indirect
12+
github.com/Microsoft/go-winio v0.4.12
1213
github.com/aws/aws-sdk-go v1.23.20
1314
github.com/go-git/go-git/v5 v5.2.0
14-
github.com/gogo/protobuf v1.3.2 // indirect
1515
github.com/google/uuid v1.1.1
1616
github.com/kardianos/service v1.0.0
17-
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
1817
github.com/pkg/errors v0.8.1
1918
github.com/prometheus/client_golang v1.1.0
2019
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4 // indirect
2120
github.com/prometheus/procfs v0.0.4 // indirect
22-
github.com/sirupsen/logrus v1.4.2
21+
github.com/sirupsen/logrus v1.2.0
2322
github.com/stretchr/testify v1.4.0
24-
golang.org/x/net v0.0.0-20201224014010-6772e930b67b // indirect
2523
gopkg.in/natefinch/lumberjack.v2 v2.0.0-20170531160350-a96e63847dc3
2624
gopkg.in/yaml.v2 v2.4.0
2725
)

go.sum

+6-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
22
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
3+
github.com/Microsoft/go-winio v0.4.12 h1:xAfWHN1IrQ0NJ9TBC0KBZoqLjzDTr1ML+4MywiUOryc=
4+
github.com/Microsoft/go-winio v0.4.12/go.mod h1:VhR8bwka0BXejwEJY73c50VrPtXAaKcyvVC4A4RozmA=
35
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7 h1:uSoVVbwJiQipAclBbw+8quDsfcvFjOpI5iCf4p/cqCs=
46
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7/go.mod h1:6zEj6s6u/ghQa61ZWa/C2Aw3RkjiTBOix7dkqa1VLIs=
57
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
@@ -36,7 +38,6 @@ github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2
3638
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
3739
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
3840
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
39-
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
4041
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
4142
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
4243
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
@@ -63,14 +64,11 @@ github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd h1:Coekwdh0v
6364
github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
6465
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
6566
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
67+
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
6668
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
67-
github.com/konsorten/go-windows-terminal-sequences v1.0.2 h1:DB17ag19krx9CFsz4o3enTrPXyIXCl+2iCXH/aMAp9s=
68-
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
6969
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
70-
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
7170
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
7271
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
73-
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
7472
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
7573
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
7674
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
@@ -108,9 +106,8 @@ github.com/prometheus/procfs v0.0.4 h1:w8DjqFMJDjuVwdZBQoOozr4MVWOnwF7RcL/7uxBjY
108106
github.com/prometheus/procfs v0.0.4/go.mod h1:4A/X28fw3Fc593LaREMrKMqOKvUAntwMDaekg4FpcdQ=
109107
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
110108
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
109+
github.com/sirupsen/logrus v1.2.0 h1:juTguoYk5qI21pwyTXY3B3Y5cOTH3ZUyZCg1v/mihuo=
111110
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
112-
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
113-
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
114111
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
115112
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
116113
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
@@ -125,7 +122,6 @@ golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnf
125122
golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
126123
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
127124
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
128-
golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073 h1:xMPOj6Pz6UipU1wXLkrtqpHbR0AVFnyPEQq/wRWz9lM=
129125
golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
130126
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
131127
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
@@ -136,11 +132,9 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn
136132
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
137133
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
138134
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
139-
golang.org/x/net v0.0.0-20200301022130-244492dfa37a h1:GuSPYbZzB5/dcLNCwLQLsg3obCJtX9IJhpXkvY7kzk0=
140135
golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
136+
golang.org/x/net v0.0.0-20201021035429-f5854403a974 h1:IX6qOQeG5uLjB/hjjwjedwfjND0hgjPMMyO1RoIXQNI=
141137
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
142-
golang.org/x/net v0.0.0-20201224014010-6772e930b67b h1:iFwSg7t5GZmB/Q5TjiEAsdoLDrdJRC1RiF2WhuV29Qw=
143-
golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
144138
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
145139
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
146140
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -152,14 +146,10 @@ golang.org/x/sys v0.0.0-20190204203706-41f3e6584952/go.mod h1:STP8DvDyc/dI5b8T5h
152146
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
153147
golang.org/x/sys v0.0.0-20190221075227-b4e8571b14e0/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
154148
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
155-
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
156149
golang.org/x/sys v0.0.0-20190801041406-cbf593c0f2f3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
157-
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 h1:uYVVQ9WP/Ds2ROhcaGPeIdVq0RIXVLwsHlnvJ+cT1So=
158150
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
151+
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f h1:+Nyd8tzPX9R7BWHguqsrbFdRx3WQ/1ib8I44HXV5yTA=
159152
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
160-
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
161-
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
162-
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
163153
golang.org/x/text v0.3.4 h1:0YWbFKbhXG/wIiuHDSKpS0Iy7FSA+u45VtBMfQcFTTc=
164154
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
165155
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
@@ -180,7 +170,6 @@ gopkg.in/natefinch/lumberjack.v2 v2.0.0-20170531160350-a96e63847dc3/go.mod h1:l0
180170
gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
181171
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
182172
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
183-
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
184173
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
185174
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
186175
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=

queue/message.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,11 @@ func (mh *messageHandler) Handle(message sqs.Message) (*runbook.ActionResultPayl
7171
}
7272

7373
start := time.Now()
74-
executionResult, err := mh.execute(&mappedAction, *message.Body)
74+
executionResult, callbackContext, err := mh.execute(&mappedAction, message)
7575
took := time.Since(start)
7676

77+
result.CallbackContext = callbackContext
78+
7779
switch err := err.(type) {
7880
case *runbook.ExecError:
7981
result.IsSuccessful = false
@@ -102,18 +104,18 @@ func (mh *messageHandler) Handle(message sqs.Message) (*runbook.ActionResultPayl
102104
return result, nil
103105
}
104106

105-
func (mh *messageHandler) execute(mappedAction *conf.MappedAction, messageBody string) (string, error) {
107+
func (mh *messageHandler) execute(mappedAction *conf.MappedAction, message sqs.Message) (string, string, error) {
106108

107109
sourceType := mappedAction.SourceType
108110
switch sourceType {
109111
case conf.GitSourceType:
110112
if mh.repositories == nil {
111-
return "", errors.New("Repositories should be provided.")
113+
return "", "", errors.New("Repositories should be provided.")
112114
}
113115

114116
repository, err := mh.repositories.Get(mappedAction.GitOptions.Url)
115117
if err != nil {
116-
return "", err
118+
return "", "", err
117119
}
118120

119121
repository.RLock()
@@ -122,7 +124,7 @@ func (mh *messageHandler) execute(mappedAction *conf.MappedAction, messageBody s
122124

123125
case conf.LocalSourceType:
124126
args := append(mh.actionSpecs.GlobalFlags.Args(), mappedAction.Flags.Args()...)
125-
args = append(args, []string{"-payload", messageBody}...)
127+
args = append(args, []string{"-payload", *message.Body}...)
126128
args = append(args, mh.actionSpecs.GlobalArgs...)
127129
args = append(args, mappedAction.Args...)
128130
env := append(mh.actionSpecs.GlobalEnv, mappedAction.Env...)
@@ -138,9 +140,9 @@ func (mh *messageHandler) execute(mappedAction *conf.MappedAction, messageBody s
138140
}
139141
stderr := mh.actionLoggers[mappedAction.Stderr]
140142

141-
err := runbook.ExecuteFunc(mappedAction.Filepath, args, env, stdout, stderr)
142-
return stdoutBuff.String(), err
143+
callbackContext, err := runbook.ExecuteFunc(*message.MessageId, mappedAction.Filepath, args, env, stdout, stderr)
144+
return stdoutBuff.String(), callbackContext, err
143145
default:
144-
return "", errors.Errorf("Unknown action sourceType[%s].", sourceType)
146+
return "", "", errors.Errorf("Unknown action sourceType[%s].", sourceType)
145147
}
146148
}

queue/message_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ var mockActionLoggers = map[string]io.Writer{
6363
"/path/to/stderr": mockStderr,
6464
}
6565

66-
func mockExecute(executablePath string, args, environmentVars []string, stdout, stderr io.Writer) error {
67-
return nil
66+
func mockExecute(messageId string, executablePath string, args, environmentVars []string, stdout, stderr io.Writer) (string, error) {
67+
return "", nil
6868
}
6969

7070
func TestProcess(t *testing.T) {
@@ -85,10 +85,10 @@ func testProcessSuccessfully(t *testing.T) {
8585
message := sqs.Message{Body: &body, MessageId: &id}
8686
queueMessage := NewMessageHandler(nil, mockActionSpecs, mockActionLoggers)
8787

88-
runbook.ExecuteFunc = func(executablePath string, args, environmentVars []string, stdout, stderr io.Writer) error {
88+
runbook.ExecuteFunc = func(messageId string, executablePath string, args, environmentVars []string, stdout, stderr io.Writer) (string, error) {
8989
assert.Equal(t, mockStdout, stdout)
9090
assert.Equal(t, mockStderr, stderr)
91-
return nil
91+
return "", nil
9292
}
9393

9494
result, err := queueMessage.Handle(message)
@@ -99,9 +99,9 @@ func testProcessSuccessfully(t *testing.T) {
9999
}
100100

101101
func testProcessHttpActionSuccessfully(t *testing.T) {
102-
runbook.ExecuteFunc = func(executablePath string, args, environmentVars []string, stdout, stderr io.Writer) error {
102+
runbook.ExecuteFunc = func(messageId string, executablePath string, args, environmentVars []string, stdout, stderr io.Writer) (string, error) {
103103
io.Copy(stdout, bytes.NewBufferString(`{"headers": {"Date": "Wed, 14 Oct 2020 08:59:30 GMT"},"body": "done", "statusCode": 200}`))
104-
return nil
104+
return "", nil
105105
}
106106

107107
body := `{"actionType":"http", "action":"Retrieve", "requestId": "RequestId"}`

runbook/callback_context_reader.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//go:build !windows
2+
// +build !windows
3+
4+
package runbook
5+
6+
import (
7+
"fmt"
8+
"github.com/sirupsen/logrus"
9+
"io/ioutil"
10+
"os"
11+
"syscall"
12+
)
13+
14+
var CallbackContextReaderFunc = CallbackContextReader
15+
var CreatePipeFunc = CreatePipe
16+
17+
func CreatePipe(pipePath string) interface{} {
18+
err := syscall.Mkfifo(pipePath, 0666)
19+
if err != nil {
20+
logrus.Debugf("Could not create named pipe with name %s. Error: %s", pipePath, err.Error())
21+
}
22+
return pipePath
23+
}
24+
25+
func CallbackContextReader(contextBuffer []byte, pipeListener interface{}) {
26+
pipePath, ok := pipeListener.(string)
27+
if !ok {
28+
logrus.Debug("Invalid type for pipeListener, could not retrieve pipePath.")
29+
return
30+
}
31+
file, err := os.OpenFile(pipePath, os.O_RDONLY, os.ModeNamedPipe)
32+
if err != nil {
33+
logrus.Debugf("Could not open named pipe. Error: %s", err.Error())
34+
}
35+
defer file.Close()
36+
37+
data, err := ioutil.ReadAll(file)
38+
_ = copy(contextBuffer, data)
39+
if err != nil {
40+
fmt.Println("Error reading from the named pipe:", err)
41+
}
42+
}
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//go:build windows
2+
// +build windows
3+
4+
package runbook
5+
6+
import (
7+
winio "github.com/Microsoft/go-winio"
8+
"github.com/sirupsen/logrus"
9+
"net"
10+
)
11+
12+
var CallbackContextReaderFunc = CallbackContextReaderForWindows
13+
var CreatePipeFunc = CreatePipeForWindows
14+
15+
func CreatePipeForWindows(pipePath string) interface{} {
16+
listener, err := winio.ListenPipe(pipePath, nil)
17+
if err != nil {
18+
logrus.Debugf("Could not create named pipe: %s Error: %s", pipePath, err.Error())
19+
}
20+
return listener
21+
}
22+
23+
func CallbackContextReaderForWindows(contextBuffer []byte, pipeListener interface{}) {
24+
listener, ok := pipeListener.(net.Listener)
25+
if !ok {
26+
logrus.Debug("Invalid pipeListener type for windows.")
27+
return
28+
}
29+
defer listener.Close()
30+
31+
pipe, err := listener.Accept()
32+
if err != nil {
33+
logrus.Debugf("Could not accept connection from pipe. Error: %s", err.Error())
34+
}
35+
defer pipe.Close()
36+
37+
_, err = pipe.Read(contextBuffer)
38+
if err != nil {
39+
logrus.Debugf("Could not read data from pipe. Error: %s", err.Error())
40+
}
41+
}

runbook/executor.go

+35-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ package runbook
22

33
import (
44
"bytes"
5+
"fmt"
6+
"github.com/sirupsen/logrus"
57
"io"
68
"os"
79
"os/exec"
810
"path/filepath"
11+
"runtime"
912
"strings"
13+
"sync"
1014
)
1115

1216
var ExecuteFunc = Execute
@@ -26,7 +30,23 @@ type ExecError struct {
2630
error
2731
}
2832

29-
func Execute(executablePath string, args, environmentVars []string, stdout, stderr io.Writer) error {
33+
func Execute(messageId string, executablePath string, args, environmentVars []string, stdout, stderr io.Writer) (string, error) {
34+
35+
callbackContextBuffer := make([]byte, 4096)
36+
37+
var waitGroup sync.WaitGroup
38+
39+
var pipePath string
40+
if runtime.GOOS == "windows" {
41+
pipePath = `\\.\pipe\jecCallbackPipe-` + messageId
42+
} else {
43+
pipePath = "jecCallbackPipe-" + messageId
44+
}
45+
46+
pipe := CreatePipeFunc(pipePath)
47+
48+
waitGroup.Add(1)
49+
go CallbackContextReaderFunc(callbackContextBuffer, pipe)
3050

3151
if args == nil {
3252
args = []string{}
@@ -45,7 +65,7 @@ func Execute(executablePath string, args, environmentVars []string, stdout, stde
4565
cmd = exec.Command(executablePath, args...)
4666
}
4767

48-
cmd.Env = append(os.Environ(), environmentVars...)
68+
cmd.Env = append(append(os.Environ(), fmt.Sprintf("JEC_CALLBACK_PIPE_PATH=%s", pipePath)), environmentVars...)
4969

5070
stderrBuff := &bytes.Buffer{}
5171
cmd.Stderr = stderrBuff
@@ -58,8 +78,19 @@ func Execute(executablePath string, args, environmentVars []string, stdout, stde
5878

5979
err := cmd.Run()
6080
if err != nil {
61-
return &ExecError{stderrBuff.String(), err}
81+
return "", &ExecError{stderrBuff.String(), err}
6282
}
6383

64-
return nil
84+
waitGroup.Done()
85+
86+
if runtime.GOOS != "windows" {
87+
err = os.Remove(pipePath)
88+
if err != nil {
89+
logrus.Debugf("Could not delete named pipe %s", pipePath)
90+
}
91+
}
92+
93+
callbackContext := bytes.NewBuffer(bytes.Trim(callbackContextBuffer, "\x00")).String()
94+
95+
return callbackContext, nil
6596
}

0 commit comments

Comments
 (0)