Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added callback context in action execution result #5

Merged
merged 2 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ RUN addgroup -S jec && \
COPY --from=builder /jec /opt/jec
RUN mkdir -p /var/log/jec && \
chown -R jec:jec /var/log/jec && \
chown -R jec:jec /opt/jec
chown -R jec:jec /opt/jec && \
mkdir -p /var/tmp/jec && \
chown -R jec:jec /var/tmp/jec
USER jec
ENTRYPOINT ["/opt/jec"]
ENTRYPOINT ["/opt/jec"]
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,17 @@ replace (

require (
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/Microsoft/go-winio v0.4.12
github.com/aws/aws-sdk-go v1.23.20
github.com/go-git/go-git/v5 v5.2.0
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/uuid v1.1.1
github.com/kardianos/service v1.0.0
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are the indirect ones removed? also, I guess go-windows-terminal-sequences is downgraded

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these were unused packages which got removed after running go mod tidy

github.com/pkg/errors v0.8.1
github.com/prometheus/client_golang v1.1.0
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4 // indirect
github.com/prometheus/procfs v0.0.4 // indirect
github.com/sirupsen/logrus v1.4.2
github.com/stretchr/testify v1.4.0
golang.org/x/net v0.0.0-20201224014010-6772e930b67b // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0-20170531160350-a96e63847dc3
gopkg.in/yaml.v2 v2.4.0
)
19 changes: 5 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Microsoft/go-winio v0.4.12 h1:xAfWHN1IrQ0NJ9TBC0KBZoqLjzDTr1ML+4MywiUOryc=
github.com/Microsoft/go-winio v0.4.12/go.mod h1:VhR8bwka0BXejwEJY73c50VrPtXAaKcyvVC4A4RozmA=
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7 h1:uSoVVbwJiQipAclBbw+8quDsfcvFjOpI5iCf4p/cqCs=
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7/go.mod h1:6zEj6s6u/ghQa61ZWa/C2Aw3RkjiTBOix7dkqa1VLIs=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
Expand Down Expand Up @@ -36,7 +38,6 @@ github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand All @@ -63,14 +64,11 @@ github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd h1:Coekwdh0v
github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.2 h1:DB17ag19krx9CFsz4o3enTrPXyIXCl+2iCXH/aMAp9s=
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
Expand Down Expand Up @@ -125,7 +123,6 @@ golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnf
golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073 h1:xMPOj6Pz6UipU1wXLkrtqpHbR0AVFnyPEQq/wRWz9lM=
golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
Expand All @@ -136,11 +133,9 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200301022130-244492dfa37a h1:GuSPYbZzB5/dcLNCwLQLsg3obCJtX9IJhpXkvY7kzk0=
golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974 h1:IX6qOQeG5uLjB/hjjwjedwfjND0hgjPMMyO1RoIXQNI=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20201224014010-6772e930b67b h1:iFwSg7t5GZmB/Q5TjiEAsdoLDrdJRC1RiF2WhuV29Qw=
golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -154,12 +149,9 @@ golang.org/x/sys v0.0.0-20190221075227-b4e8571b14e0/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190801041406-cbf593c0f2f3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 h1:uYVVQ9WP/Ds2ROhcaGPeIdVq0RIXVLwsHlnvJ+cT1So=
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f h1:+Nyd8tzPX9R7BWHguqsrbFdRx3WQ/1ib8I44HXV5yTA=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.4 h1:0YWbFKbhXG/wIiuHDSKpS0Iy7FSA+u45VtBMfQcFTTc=
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand All @@ -180,7 +172,6 @@ gopkg.in/natefinch/lumberjack.v2 v2.0.0-20170531160350-a96e63847dc3/go.mod h1:l0
gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
Expand Down
18 changes: 10 additions & 8 deletions queue/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ func (mh *messageHandler) Handle(message sqs.Message) (*runbook.ActionResultPayl
}

start := time.Now()
executionResult, err := mh.execute(&mappedAction, *message.Body)
executionResult, callbackContext, err := mh.execute(&mappedAction, &message)
took := time.Since(start)

result.CallbackContext = callbackContext

switch err := err.(type) {
case *runbook.ExecError:
result.IsSuccessful = false
Expand Down Expand Up @@ -102,18 +104,18 @@ func (mh *messageHandler) Handle(message sqs.Message) (*runbook.ActionResultPayl
return result, nil
}

func (mh *messageHandler) execute(mappedAction *conf.MappedAction, messageBody string) (string, error) {
func (mh *messageHandler) execute(mappedAction *conf.MappedAction, message *sqs.Message) (string, string, error) {

sourceType := mappedAction.SourceType
switch sourceType {
case conf.GitSourceType:
if mh.repositories == nil {
return "", errors.New("Repositories should be provided.")
return "", "", errors.New("Repositories should be provided.")
}

repository, err := mh.repositories.Get(mappedAction.GitOptions.Url)
if err != nil {
return "", err
return "", "", err
}

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

case conf.LocalSourceType:
args := append(mh.actionSpecs.GlobalFlags.Args(), mappedAction.Flags.Args()...)
args = append(args, []string{"-payload", messageBody}...)
args = append(args, []string{"-payload", *message.Body}...)
args = append(args, mh.actionSpecs.GlobalArgs...)
args = append(args, mappedAction.Args...)
env := append(mh.actionSpecs.GlobalEnv, mappedAction.Env...)
Expand All @@ -138,9 +140,9 @@ func (mh *messageHandler) execute(mappedAction *conf.MappedAction, messageBody s
}
stderr := mh.actionLoggers[mappedAction.Stderr]

err := runbook.ExecuteFunc(mappedAction.Filepath, args, env, stdout, stderr)
return stdoutBuff.String(), err
callbackContext, err := runbook.ExecuteFunc(*message.MessageId, mappedAction.Filepath, args, env, stdout, stderr)
return stdoutBuff.String(), callbackContext, err
default:
return "", errors.Errorf("Unknown action sourceType[%s].", sourceType)
return "", "", errors.Errorf("Unknown action sourceType[%s].", sourceType)
}
}
12 changes: 6 additions & 6 deletions queue/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ var mockActionLoggers = map[string]io.Writer{
"/path/to/stderr": mockStderr,
}

func mockExecute(executablePath string, args, environmentVars []string, stdout, stderr io.Writer) error {
return nil
func mockExecute(executionId string, executablePath string, args, environmentVars []string, stdout, stderr io.Writer) (string, error) {
return "", nil
}

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

runbook.ExecuteFunc = func(executablePath string, args, environmentVars []string, stdout, stderr io.Writer) error {
runbook.ExecuteFunc = func(executionId string, executablePath string, args, environmentVars []string, stdout, stderr io.Writer) (string, error) {
assert.Equal(t, mockStdout, stdout)
assert.Equal(t, mockStderr, stderr)
return nil
return "", nil
}

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

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

body := `{"actionType":"http", "action":"Retrieve", "requestId": "RequestId"}`
Expand Down
7 changes: 7 additions & 0 deletions runbook/callback_context_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package runbook

type CallbackContextHandler interface {
CreatePipe()
Read()
ClosePipe()
}
70 changes: 70 additions & 0 deletions runbook/callback_context_handler_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//go:build !windows
// +build !windows

package runbook

import (
"github.com/sirupsen/logrus"
"io/ioutil"
"os"
"path/filepath"
"syscall"
)

type CallbackContextHandlerUnix struct {
pipePath string
callbackContextBuffer []byte
pipeOpenedByScript bool
}

func NewCallbackContextHandler(executionId string) *CallbackContextHandlerUnix {
return &CallbackContextHandlerUnix{
pipePath: filepath.Join("/var", "tmp", "jec", `jecCallbackPipe-`+executionId),
callbackContextBuffer: make([]byte, 4096),
pipeOpenedByScript: false,
}
}

func (callbackContextHandler *CallbackContextHandlerUnix) CreatePipe() {
err := syscall.Mkfifo(callbackContextHandler.pipePath, 0666)
if err != nil {
logrus.Debugf("Could not create named pipe with name %s. Error: %s", callbackContextHandler.pipePath, err.Error())
}
}

func (callbackContextHandler *CallbackContextHandlerUnix) Read() {
file, err := os.OpenFile(callbackContextHandler.pipePath, os.O_RDONLY, os.ModeNamedPipe)
if err != nil {
logrus.Debugf("Could not open named pipe. Error: %s", err.Error())
}
defer file.Close()
callbackContextHandler.pipeOpenedByScript = true

data, err := ioutil.ReadAll(file)
_ = copy(callbackContextHandler.callbackContextBuffer, data)
if err != nil {
logrus.Debug("Error reading from the named pipe:", err)
}

err = os.Remove(callbackContextHandler.pipePath)
if err != nil {
logrus.Debugf("Could not delete named pipe %s", callbackContextHandler.pipePath)
}
}

func (callbackContextHandler *CallbackContextHandlerUnix) ClosePipe() {
if !callbackContextHandler.pipeOpenedByScript {
// If Read() go routine has not read callback context from script execution then write default message, so that can terminate
file, err := os.OpenFile(callbackContextHandler.pipePath, os.O_WRONLY, os.ModeNamedPipe)
if err != nil {
logrus.Debug("Could not open named pipe to write default callback context")
}
defer file.Close()

data := []byte("Execution Finished!")
_, err = file.Write(data)
if err != nil {
logrus.Debug("Could not write to named pipe")
}
}
}
66 changes: 66 additions & 0 deletions runbook/callback_context_handler_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//go:build windows
// +build windows

package runbook

import (
winio "github.com/Microsoft/go-winio"
"github.com/sirupsen/logrus"
"net"
)

type CallbackContextHandlerWindows struct {
pipePath string
pipeListener net.Listener
callbackContextBuffer []byte
pipeOpenedByScript bool
}

func NewCallbackContextHandler(executionId string) *CallbackContextHandlerWindows {
return &CallbackContextHandlerWindows{
pipePath: `\\.\pipe\jecCallbackPipe-` + executionId,
callbackContextBuffer: make([]byte, 4096),
pipeOpenedByScript: false,
}
}

func (callbackContextHandler *CallbackContextHandlerWindows) CreatePipe() {
listener, err := winio.ListenPipe(callbackContextHandler.pipePath, nil)
callbackContextHandler.pipeListener = listener
if err != nil {
logrus.Debugf("Could not create named pipe: %s Error: %s", callbackContextHandler.pipePath, err.Error())
}
}

func (callbackContextHandler *CallbackContextHandlerWindows) Read() {
defer callbackContextHandler.pipeListener.Close()
pipe, err := callbackContextHandler.pipeListener.Accept()
if err != nil {
logrus.Debugf("Could not accept connection from pipe. Error: %s", err.Error())
return
}
defer pipe.Close()
callbackContextHandler.pipeOpenedByScript = true

_, err = pipe.Read(callbackContextHandler.callbackContextBuffer)
if err != nil {
logrus.Debugf("Could not read data from pipe. Error: %s", err.Error())
}
}

func (callbackContextHandler *CallbackContextHandlerWindows) ClosePipe() {
if !callbackContextHandler.pipeOpenedByScript {
// If Read() go routine has not read callback context from script execution then write empty string, so that can terminate
pipe, err := winio.DialPipe(callbackContextHandler.pipePath, nil)
if err != nil {
logrus.Debug("Could not connect to named pipe")
}
defer pipe.Close()

data := []byte("Execution Finished!")
_, err = pipe.Write(data)
if err != nil {
logrus.Debug("Could not write to named pipe")
}
}
}
19 changes: 16 additions & 3 deletions runbook/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package runbook

import (
"bytes"
"github.com/sirupsen/logrus"
"io"
"os"
"os/exec"
Expand All @@ -26,14 +27,21 @@ type ExecError struct {
error
}

func Execute(executablePath string, args, environmentVars []string, stdout, stderr io.Writer) error {
func Execute(executionId string, executablePath string, args, environmentVars []string, stdout, stderr io.Writer) (string, error) {

callbackContextHandler := NewCallbackContextHandler(executionId)
callbackContextHandler.CreatePipe()

go callbackContextHandler.Read()

if args == nil {
args = []string{}
} else if environmentVars == nil {
environmentVars = []string{}
}

args = append(args, []string{"--jecNamedPipe", callbackContextHandler.pipePath}...)

var cmd *exec.Cmd
fileExt := filepath.Ext(strings.ToLower(executablePath))
command, exist := executables[fileExt]
Expand All @@ -57,9 +65,14 @@ func Execute(executablePath string, args, environmentVars []string, stdout, stde
}

err := cmd.Run()

callbackContextHandler.ClosePipe()

if err != nil {
return &ExecError{stderrBuff.String(), err}
return "", &ExecError{stderrBuff.String(), err}
}

return nil
callbackContext := bytes.NewBuffer(bytes.Trim(callbackContextHandler.callbackContextBuffer, "\x00")).String()
logrus.Debug("Callback context: " + callbackContext)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be removed

return callbackContext, nil
}
Loading
Loading