From 55275099196354b8b2daf754a80c059cdb7b408f Mon Sep 17 00:00:00 2001 From: Dawid Rusnak Date: Thu, 21 Dec 2023 09:52:41 +0100 Subject: [PATCH 1/3] fix: put pre-run and post-run script files in the data volume Relates to: testkube#4719 --- contrib/executor/init/pkg/runner/runner.go | 23 +++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/contrib/executor/init/pkg/runner/runner.go b/contrib/executor/init/pkg/runner/runner.go index b6916a3c19b..0218dd5dec6 100755 --- a/contrib/executor/init/pkg/runner/runner.go +++ b/contrib/executor/init/pkg/runner/runner.go @@ -4,6 +4,7 @@ import ( "context" "os" "path/filepath" + "strconv" "strings" "github.com/pkg/errors" @@ -73,14 +74,20 @@ func (r *InitRunner) Run(ctx context.Context, execution testkube.Execution) (res } if execution.PreRunScript != "" || execution.PostRunScript != "" { - command := "#!" + defaultShell + shell := defaultShell if execution.ContainerShell != "" { - command = "#!" + execution.ContainerShell + shell = execution.ContainerShell } - command += "\n" + + shebang := "#!" + shell + "\n" + command := shebang + preRunScript := shebang + postRunScript := shebang if execution.PreRunScript != "" { - command += filepath.Join(r.Params.WorkingDir, preRunScriptName) + "\n" + command += strconv.Quote(filepath.Join(r.Params.DataDir, preRunScriptName)) + "\n" + preRunScript += "cd " + strconv.Quote(filepath.Join(r.Params.WorkingDir)) + "\n" + preRunScript += execution.PreRunScript } if len(execution.Command) != 0 { @@ -89,7 +96,9 @@ func (r *InitRunner) Run(ctx context.Context, execution testkube.Execution) (res } if execution.PostRunScript != "" { - command += filepath.Join(r.Params.WorkingDir, postRunScriptName) + "\n" + command += strconv.Quote(filepath.Join(r.Params.DataDir, postRunScriptName)) + "\n" + postRunScript += "cd " + strconv.Quote(filepath.Join(r.Params.WorkingDir)) + "\n" + postRunScript += execution.PreRunScript } var scripts = []struct { @@ -98,9 +107,9 @@ func (r *InitRunner) Run(ctx context.Context, execution testkube.Execution) (res data string comment string }{ - {r.Params.WorkingDir, preRunScriptName, execution.PreRunScript, "prerun"}, + {r.Params.DataDir, preRunScriptName, preRunScript, "prerun"}, {r.Params.DataDir, containerexecutor.EntrypointScriptName, command, "entrypoint"}, - {r.Params.WorkingDir, postRunScriptName, execution.PostRunScript, "postrun"}, + {r.Params.DataDir, postRunScriptName, postRunScript, "postrun"}, } for _, script := range scripts { From 5f3e747b2d8767fe62d0249d19ece5500746ff03 Mon Sep 17 00:00:00 2001 From: Dawid Rusnak Date: Thu, 21 Dec 2023 10:04:23 +0100 Subject: [PATCH 2/3] chore: simplify entrypoint.sh --- contrib/executor/init/pkg/runner/runner.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/contrib/executor/init/pkg/runner/runner.go b/contrib/executor/init/pkg/runner/runner.go index 0218dd5dec6..f72848133ba 100755 --- a/contrib/executor/init/pkg/runner/runner.go +++ b/contrib/executor/init/pkg/runner/runner.go @@ -24,6 +24,7 @@ import ( const ( defaultShell = "/bin/sh" preRunScriptName = "prerun.sh" + commandScriptName = "command.sh" postRunScriptName = "postrun.sh" ) @@ -80,24 +81,24 @@ func (r *InitRunner) Run(ctx context.Context, execution testkube.Execution) (res } shebang := "#!" + shell + "\n" + entrypoint := shebang command := shebang preRunScript := shebang postRunScript := shebang if execution.PreRunScript != "" { - command += strconv.Quote(filepath.Join(r.Params.DataDir, preRunScriptName)) + "\n" - preRunScript += "cd " + strconv.Quote(filepath.Join(r.Params.WorkingDir)) + "\n" + entrypoint += strconv.Quote(filepath.Join(r.Params.DataDir, preRunScriptName)) + "\n" preRunScript += execution.PreRunScript } if len(execution.Command) != 0 { + entrypoint += strconv.Quote(filepath.Join(r.Params.DataDir, postRunScriptName)) + "\n" command += strings.Join(execution.Command, " ") command += " \"$@\"\n" } if execution.PostRunScript != "" { - command += strconv.Quote(filepath.Join(r.Params.DataDir, postRunScriptName)) + "\n" - postRunScript += "cd " + strconv.Quote(filepath.Join(r.Params.WorkingDir)) + "\n" + entrypoint += strconv.Quote(filepath.Join(r.Params.DataDir, postRunScriptName)) + "\n" postRunScript += execution.PreRunScript } @@ -108,8 +109,9 @@ func (r *InitRunner) Run(ctx context.Context, execution testkube.Execution) (res comment string }{ {r.Params.DataDir, preRunScriptName, preRunScript, "prerun"}, - {r.Params.DataDir, containerexecutor.EntrypointScriptName, command, "entrypoint"}, + {r.Params.DataDir, commandScriptName, command, "command"}, {r.Params.DataDir, postRunScriptName, postRunScript, "postrun"}, + {r.Params.DataDir, containerexecutor.EntrypointScriptName, entrypoint, "entrypoint"}, } for _, script := range scripts { From 75050d060265aefbbe4f15b80dc164b278f03b2b Mon Sep 17 00:00:00 2001 From: Dawid Rusnak Date: Thu, 21 Dec 2023 10:05:51 +0100 Subject: [PATCH 3/3] fix: fail on pre-run/post-run script error --- contrib/executor/init/pkg/runner/runner.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/executor/init/pkg/runner/runner.go b/contrib/executor/init/pkg/runner/runner.go index f72848133ba..462db5ecb5a 100755 --- a/contrib/executor/init/pkg/runner/runner.go +++ b/contrib/executor/init/pkg/runner/runner.go @@ -80,7 +80,7 @@ func (r *InitRunner) Run(ctx context.Context, execution testkube.Execution) (res shell = execution.ContainerShell } - shebang := "#!" + shell + "\n" + shebang := "#!" + shell + "\nset -e\n" entrypoint := shebang command := shebang preRunScript := shebang