Skip to content

Commit

Permalink
timeout -> timeoutSeconds, add stdErr output to utils.exec
Browse files Browse the repository at this point in the history
  • Loading branch information
jeff-mccoy committed Apr 25, 2022
1 parent 5f98b79 commit 3b5fd4f
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion examples/component-scripts/zarf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ components:
# This script will fail after 1 seconds
- name: fails
scripts:
timeout: 1
timeoutSeconds: 1
before:
- "sleep 30"
2 changes: 1 addition & 1 deletion src/cmd/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var destroyCmd = &cobra.Command{
// Iterate over al matching zarf-clean scripts and exec them
for _, script := range scripts {
// Run the matched script
_, err := utils.ExecCommandWithContext(context.TODO(), true, script)
_, _, err := utils.ExecCommandWithContext(context.TODO(), true, script)
if errors.Is(err, os.ErrPermission) {
message.Warnf("Got a 'permission denied' when trying to execute the script (%v). Are you the right user and/or do you have the right kube-context?\n", script)

Expand Down
6 changes: 3 additions & 3 deletions src/internal/packager/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,11 @@ func loopScriptUntilSuccess(script string, scripts types.ZarfComponentScripts) {
// Oherwise try running the script
default:
ctx, cancel = context.WithTimeout(context.Background(), duration)
output, err := utils.ExecCommandWithContext(ctx, scripts.ShowOutput, "sh", "-c", script)
output, errOut, err := utils.ExecCommandWithContext(ctx, scripts.ShowOutput, "sh", "-c", script)
defer cancel()

if err != nil {
message.Debug(err, output)
message.Debug(err, output, errOut)
// If retry, let the script run again
if scripts.Retry {
continue
Expand All @@ -273,7 +273,7 @@ func loopScriptUntilSuccess(script string, scripts types.ZarfComponentScripts) {

// Dump the script output in debug if output not already streamed
if !scripts.ShowOutput {
message.Debug(output)
message.Debug(output, errOut)
}

// Close the function now that we are done
Expand Down
4 changes: 2 additions & 2 deletions src/internal/packager/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,15 +316,15 @@ func handleDataInjection(wg *sync.WaitGroup, data types.ZarfDataInjection, compo
}

// Do the actual data injection
_, err := utils.ExecCommandWithContext(context.TODO(), true, "kubectl", cpPodExecArgs...)
_, _, err := utils.ExecCommandWithContext(context.TODO(), true, "kubectl", cpPodExecArgs...)
if err != nil {
message.Warnf("Error copying data into the pod %v: %v\n", pod, err)
continue
} else {
// Leave a marker in the target container for pods to track the sync action
cpPodExecArgs[3] = injectionCompletionMarker
cpPodExecArgs[4] = pod + ":" + data.Target.Path
_, err = utils.ExecCommandWithContext(context.TODO(), true, "kubectl", cpPodExecArgs...)
_, _, err = utils.ExecCommandWithContext(context.TODO(), true, "kubectl", cpPodExecArgs...)
if err != nil {
message.Warnf("Error saving the zarf sync completion file after injection into pod %v\n", pod)
}
Expand Down
10 changes: 5 additions & 5 deletions src/internal/utils/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const colorCyan = "\x1b[36;1m"
const colorWhite = "\x1b[37;1m"

//nolint
func ExecCommandWithContext(ctx context.Context, showLogs bool, commandName string, args ...string) (string, error) {
func ExecCommandWithContext(ctx context.Context, showLogs bool, commandName string, args ...string) (string, string, error) {
if showLogs {
fmt.Println()
fmt.Printf(" %s", colorGreen)
Expand All @@ -44,7 +44,7 @@ func ExecCommandWithContext(ctx context.Context, showLogs bool, commandName stri
stderr := io.MultiWriter(os.Stderr, &stderrBuf)

if err := cmd.Start(); err != nil {
return "", err
return "", "", err
}

if showLogs {
Expand All @@ -61,14 +61,14 @@ func ExecCommandWithContext(ctx context.Context, showLogs bool, commandName stri
}

if err := cmd.Wait(); err != nil {
return "", err
return "", "", err
}

if showLogs {
if errStdout != nil || errStderr != nil {
return "", errors.New("unable to capture stdOut or stdErr")
return "", "", errors.New("unable to capture stdOut or stdErr")
}
}

return stdoutBuf.String(), nil
return stdoutBuf.String(), stderrBuf.String(), nil
}
2 changes: 1 addition & 1 deletion src/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ type ZarfManifest struct {
// ZarfComponentScripts are scripts that run before or after a component is deployed
type ZarfComponentScripts struct {
ShowOutput bool `yaml:"showOutput,omitempty"`
TimeoutSeconds int `yaml:"timeout,omitempty"`
TimeoutSeconds int `yaml:"timeoutSeconds,omitempty"`
Retry bool `yaml:"retry,omitempty"`
Before []string `yaml:"before,omitempty"`
After []string `yaml:"after,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion zarf.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
"showOutput": {
"type": "boolean"
},
"timeout": {
"timeoutSeconds": {
"type": "integer"
},
"retry": {
Expand Down

0 comments on commit 3b5fd4f

Please sign in to comment.