-
Notifications
You must be signed in to change notification settings - Fork 4
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package runbook | ||
|
||
type CallbackContextHandler interface { | ||
CreatePipe() | ||
Read() | ||
ClosePipe() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package runbook | |
|
||
import ( | ||
"bytes" | ||
"github.com/sirupsen/logrus" | ||
"io" | ||
"os" | ||
"os/exec" | ||
|
@@ -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] | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be removed |
||
return callbackContext, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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