Skip to content

Commit

Permalink
fix: put pre-run and post-run script files in the data volume (#4823)
Browse files Browse the repository at this point in the history
* fix: put pre-run and post-run script files in the data volume

Relates to: testkube#4719

* chore: simplify entrypoint.sh

* fix: fail on pre-run/post-run script error
  • Loading branch information
rangoo94 committed Dec 21, 2023
1 parent 73e2909 commit bba2819
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions contrib/executor/init/pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/pkg/errors"
Expand All @@ -23,6 +24,7 @@ import (
const (
defaultShell = "/bin/sh"
preRunScriptName = "prerun.sh"
commandScriptName = "command.sh"
postRunScriptName = "postrun.sh"
)

Expand Down Expand Up @@ -73,23 +75,31 @@ 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 + "\nset -e\n"
entrypoint := shebang
command := shebang
preRunScript := shebang
postRunScript := shebang

if execution.PreRunScript != "" {
command += filepath.Join(r.Params.WorkingDir, preRunScriptName) + "\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 += filepath.Join(r.Params.WorkingDir, postRunScriptName) + "\n"
entrypoint += strconv.Quote(filepath.Join(r.Params.DataDir, postRunScriptName)) + "\n"
postRunScript += execution.PreRunScript
}

var scripts = []struct {
Expand All @@ -98,9 +108,10 @@ 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, containerexecutor.EntrypointScriptName, command, "entrypoint"},
{r.Params.WorkingDir, postRunScriptName, execution.PostRunScript, "postrun"},
{r.Params.DataDir, preRunScriptName, preRunScript, "prerun"},
{r.Params.DataDir, commandScriptName, command, "command"},
{r.Params.DataDir, postRunScriptName, postRunScript, "postrun"},
{r.Params.DataDir, containerexecutor.EntrypointScriptName, entrypoint, "entrypoint"},
}

for _, script := range scripts {
Expand Down

0 comments on commit bba2819

Please sign in to comment.