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

feat: send actions output to slogger #3164

Merged
merged 5 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
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
32 changes: 18 additions & 14 deletions src/pkg/packager/actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ package actions
import (
"context"
"fmt"
"github.com/zarf-dev/zarf/src/pkg/logger"
"regexp"
"runtime"
"strings"
"time"

"github.com/zarf-dev/zarf/src/pkg/logger"

"github.com/defenseunicorns/pkg/helpers/v2"
"github.com/zarf-dev/zarf/src/api/v1alpha1"
"github.com/zarf-dev/zarf/src/internal/packager/template"
Expand Down Expand Up @@ -40,7 +41,6 @@ func Run(ctx context.Context, defaultCfg v1alpha1.ZarfComponentActionDefaults, a
// Run commands that a component has provided.
func runAction(ctx context.Context, defaultCfg v1alpha1.ZarfComponentActionDefaults, action v1alpha1.ZarfComponentAction, variableConfig *variables.VariableConfig) error {
var cmdEscaped string
var out string
var err error
cmd := action.Cmd
l := logger.From(ctx)
Expand Down Expand Up @@ -103,15 +103,22 @@ retryCmd:
// Perform the action run.
tryCmd := func(ctx context.Context) error {
// Try running the command and continue the retry loop if it fails.
if out, err = actionRun(ctx, actionDefaults, cmd, actionDefaults.Shell, spinner); err != nil {
stdout, stderr, err := actionRun(ctx, actionDefaults, cmd, spinner)
if err != nil {
if !actionDefaults.Mute {
l.Warn("action failed", "cmd", cmdEscaped, "stdout", stdout, "stderr", stderr)
}
return err
}
if !actionDefaults.Mute {
l.Info("action succeeded", "cmd", cmdEscaped, "stdout", stdout, "stderr", stderr)
}

out = strings.TrimSpace(out)
outTrimmed := strings.TrimSpace(stdout)

// If an output variable is defined, set it.
for _, v := range action.SetVariables {
variableConfig.SetVariable(v.Name, out, v.Sensitive, v.AutoIndent, v.Type)
variableConfig.SetVariable(v.Name, outTrimmed, v.Sensitive, v.AutoIndent, v.Type)
if err := variableConfig.CheckVariablePattern(v.Name, v.Pattern); err != nil {
return err
}
Expand All @@ -135,8 +142,7 @@ retryCmd:
if actionDefaults.MaxTotalSeconds < 1 {
spinner.Updatef("Waiting for \"%s\" (no timeout)", cmdEscaped)
l.Info("waiting for action (no timeout)", "cmd", cmdEscaped)
//TODO (schristoff): Make it so tryCmd can take a normal ctx
AustinAbro321 marked this conversation as resolved.
Show resolved Hide resolved
if err := tryCmd(context.Background()); err != nil {
if err := tryCmd(ctx); err != nil {
continue retryCmd
}

Expand Down Expand Up @@ -286,9 +292,9 @@ func actionGetCfg(_ context.Context, cfg v1alpha1.ZarfComponentActionDefaults, a
return cfg
}

func actionRun(ctx context.Context, cfg v1alpha1.ZarfComponentActionDefaults, cmd string, shellPref v1alpha1.Shell, spinner *message.Spinner) (string, error) {
func actionRun(ctx context.Context, cfg v1alpha1.ZarfComponentActionDefaults, cmd string, spinner *message.Spinner) (string, string, error) {
l := logger.From(ctx)
shell, shellArgs := exec.GetOSShell(shellPref)
shell, shellArgs := exec.GetOSShell(cfg.Shell)

// TODO(mkcp): Remove message on logger release
message.Debugf("Running command in %s: %s", shell, cmd)
Expand All @@ -304,13 +310,11 @@ func actionRun(ctx context.Context, cfg v1alpha1.ZarfComponentActionDefaults, cm
execCfg.Stderr = spinner
}

out, errOut, err := exec.CmdWithContext(ctx, execCfg, shell, append(shellArgs, cmd)...)
stdout, stderr, err := exec.CmdWithContext(ctx, execCfg, shell, append(shellArgs, cmd)...)
// Dump final complete output (respect mute to prevent sensitive values from hitting the logs).
if !cfg.Mute {
// TODO(mkcp): Remove message on logger release
message.Debug(cmd, out, errOut)
l.Debug("action complete", "cmd", cmd, "out", out, "errOut", errOut)
message.Debug(cmd, stdout, stderr)
}

return out, err
return stdout, stderr, err
}
2 changes: 2 additions & 0 deletions src/pkg/utils/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ func CmdWithContext(ctx context.Context, config Config, command string, args ...
&stderrBuf,
}

// TODO (@austinabro321) remove config options for stdout/stderr once logger is released
// as these options seem to have been added specifically for the spinner
// Add the writers if requested.
if config.Stdout != nil {
stdoutWriters = append(stdoutWriters, config.Stdout)
Expand Down