Skip to content

Commit

Permalink
fix: hide build log in export command (#5658)
Browse files Browse the repository at this point in the history
fix: hide build log in export command

- always print grouping frame for images
- move log catching logic into Conveyor
- add DeferBuildLog to ConveyorOptions
- refactor ExportPhase into standalone Exporter
- print log using logboek OutStream instead of fmt.Print

Signed-off-by: Ivan Mikheykin <ivan.mikheykin@flant.com>
  • Loading branch information
diafour committed May 29, 2023
1 parent d726558 commit 88bc502
Show file tree
Hide file tree
Showing 16 changed files with 184 additions and 231 deletions.
5 changes: 4 additions & 1 deletion cmd/werf/build/main.go
Expand Up @@ -243,11 +243,14 @@ func run(ctx context.Context, containerBackend container_backend.ContainerBacken
return err
}

conveyorOptions, err := common.GetConveyorOptionsWithParallel(&commonCmdData, imagesToProcess, buildOptions)
conveyorOptions, err := common.GetConveyorOptionsWithParallel(ctx, &commonCmdData, imagesToProcess, buildOptions)
if err != nil {
return err
}

// Always print logs.
conveyorOptions.DeferBuildLog = false

logboek.LogOptionalLn()

conveyorWithRetry := build.NewConveyorWithRetryWrapper(werfConfig, giterminismManager, giterminismManager.ProjectDir(), projectTmpDir, ssh_agent.SSHAuthSock, containerBackend, storageManager, storageLockManager, conveyorOptions)
Expand Down
32 changes: 12 additions & 20 deletions cmd/werf/bundle/export/export.go
Expand Up @@ -13,7 +13,6 @@ import (
"helm.sh/helm/v3/pkg/cli/values"

"github.com/werf/logboek"
"github.com/werf/logboek/pkg/level"
"github.com/werf/werf/cmd/werf/common"
"github.com/werf/werf/pkg/build"
"github.com/werf/werf/pkg/config"
Expand All @@ -24,7 +23,6 @@ import (
"github.com/werf/werf/pkg/git_repo"
"github.com/werf/werf/pkg/git_repo/gitdata"
"github.com/werf/werf/pkg/image"
"github.com/werf/werf/pkg/logging"
"github.com/werf/werf/pkg/ssh_agent"
"github.com/werf/werf/pkg/storage/lrumeta"
"github.com/werf/werf/pkg/storage/manager"
Expand Down Expand Up @@ -290,7 +288,7 @@ func runExport(ctx context.Context, imagesToProcess build.ImagesToProcess) error

imagesRepo = storageManager.GetServiceValuesRepo()

conveyorOptions, err := common.GetConveyorOptionsWithParallel(&commonCmdData, imagesToProcess, buildOptions)
conveyorOptions, err := common.GetConveyorOptionsWithParallel(ctx, &commonCmdData, imagesToProcess, buildOptions)
if err != nil {
return err
}
Expand All @@ -299,25 +297,19 @@ func runExport(ctx context.Context, imagesToProcess build.ImagesToProcess) error
defer conveyorWithRetry.Terminate()

if err := conveyorWithRetry.WithRetryBlock(ctx, func(c *build.Conveyor) error {
buildFunc := func(ctx context.Context) error {
if common.GetRequireBuiltImages(ctx, &commonCmdData) {
shouldBeBuiltOptions, err := common.GetShouldBeBuiltOptions(&commonCmdData, giterminismManager, werfConfig)
if err != nil {
return err
}

return c.ShouldBeBuilt(ctx, shouldBeBuiltOptions)
if common.GetRequireBuiltImages(ctx, &commonCmdData) {
shouldBeBuiltOptions, err := common.GetShouldBeBuiltOptions(&commonCmdData, giterminismManager, werfConfig)
if err != nil {
return err
}
return c.Build(ctx, buildOptions)
}

// Print build logs on error if --require-built-images is specified.
// Always print logs by default or if --log-verbose is specified.
requireBuiltImage := common.GetRequireBuiltImages(ctx, &commonCmdData)
isVerbose := logboek.Context(ctx).IsAcceptedLevel(level.Default)
deferLog := requireBuiltImage || !isVerbose
if err := logging.RunWithDeferredLog(ctx, deferLog, buildFunc); err != nil {
return err
if err := c.ShouldBeBuilt(ctx, shouldBeBuiltOptions); err != nil {
return err
}
} else {
if err := c.Build(ctx, buildOptions); err != nil {
return err
}
}

imagesInfoGetters, err = c.GetImageInfoGetters(image.InfoGetterOptions{CustomTagFunc: useCustomTagFunc})
Expand Down
32 changes: 12 additions & 20 deletions cmd/werf/bundle/publish/publish.go
Expand Up @@ -15,7 +15,6 @@ import (
"helm.sh/helm/v3/pkg/cli/values"

"github.com/werf/logboek"
"github.com/werf/logboek/pkg/level"
"github.com/werf/werf/cmd/werf/common"
"github.com/werf/werf/pkg/build"
"github.com/werf/werf/pkg/config"
Expand All @@ -27,7 +26,6 @@ import (
"github.com/werf/werf/pkg/git_repo"
"github.com/werf/werf/pkg/git_repo/gitdata"
"github.com/werf/werf/pkg/image"
"github.com/werf/werf/pkg/logging"
"github.com/werf/werf/pkg/ssh_agent"
"github.com/werf/werf/pkg/storage/lrumeta"
"github.com/werf/werf/pkg/storage/manager"
Expand Down Expand Up @@ -296,7 +294,7 @@ func runPublish(ctx context.Context, imagesToProcess build.ImagesToProcess) erro

imagesRepo = storageManager.GetServiceValuesRepo()

conveyorOptions, err := common.GetConveyorOptionsWithParallel(&commonCmdData, imagesToProcess, buildOptions)
conveyorOptions, err := common.GetConveyorOptionsWithParallel(ctx, &commonCmdData, imagesToProcess, buildOptions)
if err != nil {
return err
}
Expand All @@ -305,25 +303,19 @@ func runPublish(ctx context.Context, imagesToProcess build.ImagesToProcess) erro
defer conveyorWithRetry.Terminate()

if err := conveyorWithRetry.WithRetryBlock(ctx, func(c *build.Conveyor) error {
buildFunc := func(ctx context.Context) error {
if common.GetRequireBuiltImages(ctx, &commonCmdData) {
shouldBeBuiltOptions, err := common.GetShouldBeBuiltOptions(&commonCmdData, giterminismManager, werfConfig)
if err != nil {
return err
}

return c.ShouldBeBuilt(ctx, shouldBeBuiltOptions)
if common.GetRequireBuiltImages(ctx, &commonCmdData) {
shouldBeBuiltOptions, err := common.GetShouldBeBuiltOptions(&commonCmdData, giterminismManager, werfConfig)
if err != nil {
return err
}
return c.Build(ctx, buildOptions)
}

// Print build logs on error if --require-built-images is specified.
// Always print logs by default or if --log-verbose is specified.
requireBuiltImage := common.GetRequireBuiltImages(ctx, &commonCmdData)
isVerbose := logboek.Context(ctx).IsAcceptedLevel(level.Default)
deferLog := requireBuiltImage || !isVerbose
if err := logging.RunWithDeferredLog(ctx, deferLog, buildFunc); err != nil {
return err
if err := c.ShouldBeBuilt(ctx, shouldBeBuiltOptions); err != nil {
return err
}
} else {
if err := c.Build(ctx, buildOptions); err != nil {
return err
}
}

imagesInfoGetters, err = c.GetImageInfoGetters(image.InfoGetterOptions{CustomTagFunc: useCustomTagFunc})
Expand Down
21 changes: 18 additions & 3 deletions cmd/werf/common/conveyor_options.go
Expand Up @@ -7,6 +7,8 @@ import (
"strings"
"text/template"

"github.com/werf/logboek"
"github.com/werf/logboek/pkg/level"
"github.com/werf/werf/pkg/build"
"github.com/werf/werf/pkg/build/stage"
"github.com/werf/werf/pkg/config"
Expand All @@ -18,7 +20,7 @@ import (
"github.com/werf/werf/pkg/storage"
)

func GetConveyorOptions(commonCmdData *CmdData, imagesToProcess build.ImagesToProcess) (build.ConveyorOptions, error) {
func GetConveyorOptions(ctx context.Context, commonCmdData *CmdData, imagesToProcess build.ImagesToProcess) (build.ConveyorOptions, error) {
conveyorOptions := build.ConveyorOptions{
LocalGitRepoVirtualMergeOptions: stage.VirtualMergeOptions{
VirtualMerge: *commonCmdData.VirtualMerge,
Expand All @@ -34,11 +36,24 @@ func GetConveyorOptions(commonCmdData *CmdData, imagesToProcess build.ImagesToPr
conveyorOptions.TargetPlatforms = platforms
}

conveyorOptions.DeferBuildLog = GetDeferredBuildLog(ctx, commonCmdData)

return conveyorOptions, nil
}

func GetConveyorOptionsWithParallel(commonCmdData *CmdData, imagesToProcess build.ImagesToProcess, buildStagesOptions build.BuildOptions) (build.ConveyorOptions, error) {
conveyorOptions, err := GetConveyorOptions(commonCmdData, imagesToProcess)
// GetDeferredBuildLog returns true if build log should be catched and printed on error.
// Default rules are follows:
// - If --require-built-images is specified catch log and print on error.
// - Hide log messages if --log-quiet is specified.
// - Print "live" logs by default or if --log-verbose is specified.
func GetDeferredBuildLog(ctx context.Context, commonCmdData *CmdData) bool {
requireBuiltImage := GetRequireBuiltImages(ctx, commonCmdData)
isVerbose := logboek.Context(ctx).IsAcceptedLevel(level.Default)
return requireBuiltImage || !isVerbose
}

func GetConveyorOptionsWithParallel(ctx context.Context, commonCmdData *CmdData, imagesToProcess build.ImagesToProcess, buildStagesOptions build.BuildOptions) (build.ConveyorOptions, error) {
conveyorOptions, err := GetConveyorOptions(ctx, commonCmdData, imagesToProcess)
if err != nil {
return conveyorOptions, err
}
Expand Down
24 changes: 8 additions & 16 deletions cmd/werf/compose/main.go
Expand Up @@ -11,15 +11,13 @@ import (
"github.com/spf13/cobra"

"github.com/werf/logboek"
"github.com/werf/logboek/pkg/level"
"github.com/werf/werf/cmd/werf/common"
"github.com/werf/werf/pkg/build"
"github.com/werf/werf/pkg/container_backend"
"github.com/werf/werf/pkg/git_repo"
"github.com/werf/werf/pkg/git_repo/gitdata"
"github.com/werf/werf/pkg/giterminism_manager"
"github.com/werf/werf/pkg/image"
"github.com/werf/werf/pkg/logging"
"github.com/werf/werf/pkg/ssh_agent"
"github.com/werf/werf/pkg/storage/lrumeta"
"github.com/werf/werf/pkg/storage/manager"
Expand Down Expand Up @@ -402,7 +400,7 @@ func run(ctx context.Context, containerBackend container_backend.ContainerBacken

logboek.Context(ctx).Info().LogOptionalLn()

conveyorOptions, err := common.GetConveyorOptions(&commonCmdData, imagesToProcess)
conveyorOptions, err := common.GetConveyorOptions(ctx, &commonCmdData, imagesToProcess)
if err != nil {
return err
}
Expand All @@ -411,20 +409,14 @@ func run(ctx context.Context, containerBackend container_backend.ContainerBacken
defer conveyorWithRetry.Terminate()

if err := conveyorWithRetry.WithRetryBlock(ctx, func(c *build.Conveyor) error {
buildFunc := func(ctx context.Context) error {
if common.GetRequireBuiltImages(ctx, &commonCmdData) {
return c.ShouldBeBuilt(ctx, build.ShouldBeBuiltOptions{})
if common.GetRequireBuiltImages(ctx, &commonCmdData) {
if err := c.ShouldBeBuilt(ctx, build.ShouldBeBuiltOptions{}); err != nil {
return err
}
} else {
if err := c.Build(ctx, build.BuildOptions{SkipImageMetadataPublication: *commonCmdData.Dev}); err != nil {
return err
}
return c.Build(ctx, build.BuildOptions{SkipImageMetadataPublication: *commonCmdData.Dev})
}

// Print build logs on error if --require-built-images is specified.
// Always print logs by default or if --log-verbose is specified.
requireBuiltImage := common.GetRequireBuiltImages(ctx, &commonCmdData)
isVerbose := logboek.Context(ctx).IsAcceptedLevel(level.Default)
deferLog := requireBuiltImage || !isVerbose
if err := logging.RunWithDeferredLog(ctx, deferLog, buildFunc); err != nil {
return err
}

for _, img := range c.GetExportedImages() {
Expand Down
32 changes: 12 additions & 20 deletions cmd/werf/converge/converge.go
Expand Up @@ -20,7 +20,6 @@ import (

"github.com/werf/kubedog/pkg/kube"
"github.com/werf/logboek"
"github.com/werf/logboek/pkg/level"
"github.com/werf/werf/cmd/werf/common"
"github.com/werf/werf/pkg/build"
"github.com/werf/werf/pkg/config/deploy_params"
Expand All @@ -36,7 +35,6 @@ import (
"github.com/werf/werf/pkg/git_repo/gitdata"
"github.com/werf/werf/pkg/giterminism_manager"
"github.com/werf/werf/pkg/image"
"github.com/werf/werf/pkg/logging"
"github.com/werf/werf/pkg/ssh_agent"
"github.com/werf/werf/pkg/storage/lrumeta"
"github.com/werf/werf/pkg/storage/manager"
Expand Down Expand Up @@ -339,7 +337,7 @@ func run(ctx context.Context, containerBackend container_backend.ContainerBacken

imagesRepo = storageManager.GetServiceValuesRepo()

conveyorOptions, err := common.GetConveyorOptionsWithParallel(&commonCmdData, imagesToProcess, buildOptions)
conveyorOptions, err := common.GetConveyorOptionsWithParallel(ctx, &commonCmdData, imagesToProcess, buildOptions)
if err != nil {
return err
}
Expand All @@ -348,25 +346,19 @@ func run(ctx context.Context, containerBackend container_backend.ContainerBacken
defer conveyorWithRetry.Terminate()

if err := conveyorWithRetry.WithRetryBlock(ctx, func(c *build.Conveyor) error {
buildFunc := func(ctx context.Context) error {
if common.GetRequireBuiltImages(ctx, &commonCmdData) {
shouldBeBuiltOptions, err := common.GetShouldBeBuiltOptions(&commonCmdData, giterminismManager, werfConfig)
if err != nil {
return err
}

return c.ShouldBeBuilt(ctx, shouldBeBuiltOptions)
if common.GetRequireBuiltImages(ctx, &commonCmdData) {
shouldBeBuiltOptions, err := common.GetShouldBeBuiltOptions(&commonCmdData, giterminismManager, werfConfig)
if err != nil {
return err
}
return c.Build(ctx, buildOptions)
}

// Print build logs on error if --require-built-images is specified.
// Always print logs by default or if --log-verbose is specified.
requireBuiltImage := common.GetRequireBuiltImages(ctx, &commonCmdData)
isVerbose := logboek.Context(ctx).IsAcceptedLevel(level.Default)
deferLog := requireBuiltImage || !isVerbose
if err := logging.RunWithDeferredLog(ctx, deferLog, buildFunc); err != nil {
return err
if err := c.ShouldBeBuilt(ctx, shouldBeBuiltOptions); err != nil {
return err
}
} else {
if err := c.Build(ctx, buildOptions); err != nil {
return err
}
}

imagesInfoGetters, err = c.GetImageInfoGetters(image.InfoGetterOptions{CustomTagFunc: useCustomTagFunc})
Expand Down
33 changes: 18 additions & 15 deletions cmd/werf/export/export.go
Expand Up @@ -237,7 +237,7 @@ func run(ctx context.Context, imagesToProcess build.ImagesToProcess, tagTemplate

logboek.Context(ctx).Info().LogOptionalLn()

conveyorOptions, err := common.GetConveyorOptions(&commonCmdData, imagesToProcess)
conveyorOptions, err := common.GetConveyorOptions(ctx, &commonCmdData, imagesToProcess)
if err != nil {
return err
}
Expand All @@ -263,21 +263,24 @@ func run(ctx context.Context, imagesToProcess build.ImagesToProcess, tagTemplate
return err
}

if common.GetRequireBuiltImages(ctx, &commonCmdData) {
if err := c.ShouldBeBuilt(ctx, build.ShouldBeBuiltOptions{}); err != nil {
return err
}
} else {
if err := c.Build(ctx, build.BuildOptions{SkipImageMetadataPublication: *commonCmdData.Dev}); err != nil {
return err
}
}

return c.Export(ctx, build.ExportOptions{
BuildPhaseOptions: build.BuildPhaseOptions{
BuildOptions: build.BuildOptions{SkipImageMetadataPublication: *commonCmdData.Dev},
ShouldBeBuiltMode: common.GetRequireBuiltImages(ctx, &commonCmdData),
},
ExportPhaseOptions: build.ExportPhaseOptions{
ExportImageNameList: imageNameList,
ExportTagFuncList: tagFuncList,
MutateConfigFunc: func(config v1.Config) (v1.Config, error) {
for k, v := range extraLabels {
config.Labels[k] = v
}

return config, nil
},
ExportImageNameList: imageNameList,
ExportTagFuncList: tagFuncList,
MutateConfigFunc: func(config v1.Config) (v1.Config, error) {
for k, v := range extraLabels {
config.Labels[k] = v
}
return config, nil
},
})
})
Expand Down
2 changes: 1 addition & 1 deletion cmd/werf/helm/get_autogenerated_values.go
Expand Up @@ -208,7 +208,7 @@ func runGetServiceValues(ctx context.Context, imagesToProcess build.ImagesToProc

storageManager := manager.NewStorageManager(projectName, stagesStorage, finalStagesStorage, secondaryStagesStorageList, cacheStagesStorageList, storageLockManager)

conveyorOptions, err := common.GetConveyorOptions(&getAutogeneratedValuedCmdData, imagesToProcess)
conveyorOptions, err := common.GetConveyorOptions(ctx, &getAutogeneratedValuedCmdData, imagesToProcess)
if err != nil {
return err
}
Expand Down

0 comments on commit 88bc502

Please sign in to comment.