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

fix: put pre-run and post-run script files in the data volume #4823

Merged
merged 3 commits into from
Dec 21, 2023
Merged
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
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
Loading