Skip to content

Commit

Permalink
Merge pull request #2613 from werf/logboek_v0.4.0
Browse files Browse the repository at this point in the history
logboek@v0.4
  • Loading branch information
alexey-igrychev committed Aug 19, 2020
2 parents 26f4792 + 631fdc5 commit 09a8b5c
Show file tree
Hide file tree
Showing 198 changed files with 3,493 additions and 3,231 deletions.
18 changes: 10 additions & 8 deletions cmd/werf/build_and_publish/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ If one or more IMAGE_NAME parameters specified, werf will build images stages an
common.CmdEnvAnno: common.EnvsDescription(common.WerfDebugAnsibleArgs),
},
RunE: func(cmd *cobra.Command, args []string) error {
defer werf.PrintGlobalWarnings()
defer werf.PrintGlobalWarnings(common.BackgroundContext())

if err := common.ProcessLogOptions(&commonCmdData); err != nil {
common.PrintHelp(cmd)
Expand Down Expand Up @@ -118,6 +118,8 @@ If one or more IMAGE_NAME parameters specified, werf will build images stages an
}

func runBuildAndPublish(imagesToProcess []string) error {
ctx := common.BackgroundContext()

if err := werf.Init(*commonCmdData.TmpDir, *commonCmdData.HomeDir); err != nil {
return fmt.Errorf("initialization error: %s", err)
}
Expand All @@ -126,7 +128,7 @@ func runBuildAndPublish(imagesToProcess []string) error {
return err
}

if err := true_git.Init(true_git.Options{Out: logboek.GetOutStream(), Err: logboek.GetErrStream(), LiveGitOutput: *commonCmdData.LogVerbose || *commonCmdData.LogDebug}); err != nil {
if err := true_git.Init(true_git.Options{LiveGitOutput: *commonCmdData.LogVerbose || *commonCmdData.LogDebug}); err != nil {
return err
}

Expand Down Expand Up @@ -158,7 +160,7 @@ func runBuildAndPublish(imagesToProcess []string) error {

projectName := werfConfig.Meta.Project

projectTmpDir, err := tmp_manager.CreateProjectDir()
projectTmpDir, err := tmp_manager.CreateProjectDir(ctx)
if err != nil {
return fmt.Errorf("getting project tmp dir failed: %s", err)
}
Expand All @@ -185,7 +187,7 @@ func runBuildAndPublish(imagesToProcess []string) error {
}

stagesManager := stages_manager.NewStagesManager(projectName, storageLockManager, stagesStorageCache)
if err := stagesManager.UseStagesStorage(stagesStorage); err != nil {
if err := stagesManager.UseStagesStorage(ctx, stagesStorage); err != nil {
return err
}

Expand All @@ -199,13 +201,13 @@ func runBuildAndPublish(imagesToProcess []string) error {
return err
}

if err := ssh_agent.Init(*commonCmdData.SSHKeys); err != nil {
if err := ssh_agent.Init(ctx, *commonCmdData.SSHKeys); err != nil {
return fmt.Errorf("cannot initialize ssh agent: %s", err)
}
defer func() {
err := ssh_agent.Terminate()
if err != nil {
logboek.LogWarnF("WARNING: ssh agent termination failed: %s\n", err)
logboek.Warn().LogF("WARNING: ssh agent termination failed: %s\n", err)
}
}()

Expand Down Expand Up @@ -240,8 +242,8 @@ func runBuildAndPublish(imagesToProcess []string) error {
conveyorWithRetry := build.NewConveyorWithRetryWrapper(werfConfig, imagesToProcess, projectDir, projectTmpDir, ssh_agent.SSHAuthSock, containerRuntime, stagesManager, imagesRepo, storageLockManager, common.GetConveyorOptions(&commonCmdData))
defer conveyorWithRetry.Terminate()

if err := conveyorWithRetry.WithRetryBlock(func(c *build.Conveyor) error {
return c.BuildAndPublish(opts)
if err := conveyorWithRetry.WithRetryBlock(ctx, func(c *build.Conveyor) error {
return c.BuildAndPublish(ctx, opts)
}); err != nil {
return err
}
Expand Down
17 changes: 8 additions & 9 deletions cmd/werf/ci_env/ci_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ import (
"github.com/ghodss/yaml"
"github.com/spf13/cobra"

"github.com/werf/logboek"

"github.com/werf/werf/cmd/werf/common"
"github.com/werf/werf/pkg/docker"
"github.com/werf/werf/pkg/docker_registry"
"github.com/werf/werf/pkg/logging"
"github.com/werf/werf/pkg/slug"
"github.com/werf/werf/pkg/tmp_manager"
"github.com/werf/werf/pkg/werf"
Expand Down Expand Up @@ -74,11 +75,7 @@ Currently supported only GitLab (gitlab) and GitHub (github) CI systems`,
}

func runCIEnv(cmd *cobra.Command, args []string) error {
//if err := common.ProcessLogOptions(&commonCmdData); err != nil {
// common.PrintHelp(cmd)
// return err
//}
logging.EnableLogQuiet()
logboek.Streams().Mute()

if err := werf.Init(*commonCmdData.TmpDir, *commonCmdData.HomeDir); err != nil {
return fmt.Errorf("initialization error: %s", err)
Expand Down Expand Up @@ -180,7 +177,7 @@ func generateGitlabEnvs(w io.Writer, taggingStrategy string) error {
}

if doLogin {
err := docker.Login(imagesUsername, imagesPassword, ciRegistryImageEnv)
err := docker.Login(common.BackgroundContext(), imagesUsername, imagesPassword, ciRegistryImageEnv)
if err != nil {
return fmt.Errorf("unable to login into docker repo %s: %s", ciRegistryImageEnv, err)
}
Expand Down Expand Up @@ -301,7 +298,7 @@ func generateGithubEnvs(w io.Writer, taggingStrategy string) error {
ciGithubToken := os.Getenv("GITHUB_TOKEN")
ciGithubActor := os.Getenv("GITHUB_ACTOR")
if ciGithubActor != "" && ciGithubToken != "" {
err := docker.Login(ciGithubActor, ciGithubToken, githubRegistry)
err := docker.Login(common.BackgroundContext(), ciGithubActor, ciGithubToken, githubRegistry)
if err != nil {
return fmt.Errorf("unable to login into docker repo %s: %s", githubRegistry, err)
}
Expand Down Expand Up @@ -389,14 +386,16 @@ func generateGithubEnvs(w io.Writer, taggingStrategy string) error {
}

func generateSessionDockerConfigDir() (string, error) {
ctx := common.BackgroundContext()

dockerConfigPath := *commonCmdData.DockerConfig
if *commonCmdData.DockerConfig == "" {
dockerConfigPath = filepath.Join(os.Getenv("HOME"), ".docker")
}

tmp_manager.AutoGCEnabled = false

dockerConfigDir, err := tmp_manager.CreateDockerConfigDir(dockerConfigPath)
dockerConfigDir, err := tmp_manager.CreateDockerConfigDir(ctx, dockerConfigPath)
if err != nil {
return "", fmt.Errorf("unable to create tmp docker config: %s", err)
}
Expand Down
18 changes: 10 additions & 8 deletions cmd/werf/cleanup/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ First step is 'werf images cleanup' command, which will delete unused images fro
It is safe to run this command periodically (daily is enough) by automated cleanup job in parallel with other werf commands such as build, deploy and host cleanup.`),
Example: ` $ werf cleanup --stages-storage :local --images-repo registry.mydomain.com/myproject`,
RunE: func(cmd *cobra.Command, args []string) error {
defer werf.PrintGlobalWarnings()
defer werf.PrintGlobalWarnings(common.BackgroundContext())

if err := common.ProcessLogOptions(&commonCmdData); err != nil {
common.PrintHelp(cmd)
Expand Down Expand Up @@ -88,11 +88,13 @@ It is safe to run this command periodically (daily is enough) by automated clean
}

func runCleanup() error {
ctx := common.BackgroundContext()

if err := werf.Init(*commonCmdData.TmpDir, *commonCmdData.HomeDir); err != nil {
return fmt.Errorf("initialization error: %s", err)
}

if err := true_git.Init(true_git.Options{Out: logboek.GetOutStream(), Err: logboek.GetErrStream(), LiveGitOutput: *commonCmdData.LogVerbose || *commonCmdData.LogDebug}); err != nil {
if err := true_git.Init(true_git.Options{LiveGitOutput: *commonCmdData.LogVerbose || *commonCmdData.LogDebug}); err != nil {
return err
}

Expand All @@ -116,7 +118,7 @@ func runCleanup() error {
return fmt.Errorf("cannot initialize kube: %s", err)
}

if err := common.InitKubedog(); err != nil {
if err := common.InitKubedog(ctx); err != nil {
return fmt.Errorf("cannot init kubedog: %s", err)
}

Expand All @@ -127,7 +129,7 @@ func runCleanup() error {

common.ProcessLogProjectDir(&commonCmdData, projectDir)

projectTmpDir, err := tmp_manager.CreateProjectDir()
projectTmpDir, err := tmp_manager.CreateProjectDir(ctx)
if err != nil {
return fmt.Errorf("getting project tmp dir failed: %s", err)
}
Expand Down Expand Up @@ -161,7 +163,7 @@ func runCleanup() error {
}

stagesManager := stages_manager.NewStagesManager(projectName, storageLockManager, stagesStorageCache)
if err := stagesManager.UseStagesStorage(stagesStorage); err != nil {
if err := stagesManager.UseStagesStorage(ctx, stagesStorage); err != nil {
return err
}

Expand All @@ -170,11 +172,11 @@ func runCleanup() error {
return err
}

imagesNames, err := common.GetManagedImagesNames(projectName, stagesStorage, werfConfig)
imagesNames, err := common.GetManagedImagesNames(ctx, projectName, stagesStorage, werfConfig)
if err != nil {
return err
}
logboek.Debug.LogF("Managed images names: %v\n", imagesNames)
logboek.Debug().LogF("Managed images names: %v\n", imagesNames)

localGitRepo, err := common.GetLocalGitRepoForImagesCleanup(projectDir, &commonCmdData)
if err != nil {
Expand Down Expand Up @@ -215,7 +217,7 @@ func runCleanup() error {
}

logboek.LogOptionalLn()
if err := cleaning.Cleanup(projectName, imagesRepo, storageLockManager, stagesManager, cleanupOptions); err != nil {
if err := cleaning.Cleanup(ctx, projectName, imagesRepo, storageLockManager, stagesManager, cleanupOptions); err != nil {
return err
}

Expand Down
49 changes: 31 additions & 18 deletions cmd/werf/common/common.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package common

import (
"context"
"fmt"
"os"
"path/filepath"
Expand All @@ -11,6 +12,9 @@ import (
"github.com/spf13/cobra"

"github.com/werf/logboek"
"github.com/werf/logboek/pkg/level"
"github.com/werf/logboek/pkg/style"
"github.com/werf/logboek/pkg/types"

"github.com/werf/werf/pkg/build"
"github.com/werf/werf/pkg/build/stage"
Expand Down Expand Up @@ -124,7 +128,7 @@ const (
)

func GetLongCommandDescription(text string) string {
return logboek.FitText(text, logboek.FitTextOptions{MaxWidth: 100})
return logboek.FitText(text, types.FitTextOptions{MaxWidth: 100})
}

func SetupProjectName(cmdData *CmdData, cmd *cobra.Command) {
Expand Down Expand Up @@ -682,7 +686,7 @@ func setupTerminalWidth(cmdData *CmdData, cmd *cobra.Command) {
cmd.Flags().Int64VarP(cmdData.LogTerminalWidth, "log-terminal-width", "", -1, fmt.Sprintf(`Set log terminal width.
Defaults to:
* $WERF_LOG_TERMINAL_WIDTH
* interactive terminal width or %d`, logboek.DefaultWidth))
* interactive terminal width or %d`, 140))
}

func SetupSet(cmdData *CmdData, cmd *cobra.Command) {
Expand Down Expand Up @@ -955,7 +959,7 @@ func GetImagesCleanupPolicies(cmdData *CmdData) (cleanup.ImagesCleanupPolicies,
commitDays != CiEnvGitCommitStrategyExpiryDaysDefault && commitDays != -1 ||
stagesSignatureLimit != CiEnvStagesSignatureStrategyLimitDefault ||
stagesSignatureDays != CiEnvStagesSignatureStrategyExpiryDaysDefault {
logboek.Warn.LogLn(`WARNING: Detected custom settings for cleanup based on tagging schemes (https://werf.io/documentation/reference/cleaning_process.html#tagging-scheme-based-cleanup-algorithm) which is not used by default anymore and will not be supported since version v1.2. However, you can switch to old algorithm with option --git-history-based-cleanup=false.
logboek.Warn().LogLn(`WARNING: Detected custom settings for cleanup based on tagging schemes (https://werf.io/documentation/reference/cleaning_process.html#tagging-scheme-based-cleanup-algorithm) which is not used by default anymore and will not be supported since version v1.2. However, you can switch to old algorithm with option --git-history-based-cleanup=false.
Now werf uses the git history-based cleanup algorithm (https://werf.io/documentation/reference/cleaning_process.html#git-history-based-cleanup-algorithm) with the following default policies (https://werf.io/documentation/configuration/cleanup.html#default-policies).`)
}
Expand Down Expand Up @@ -1036,6 +1040,7 @@ func getImagesRepo(projectName string, cmdData *CmdData, optionalStubRepoAddress
}

return storage.NewImagesRepo(
BackgroundContext(),
projectName,
imagesRepoAddress,
imagesRepoMode,
Expand Down Expand Up @@ -1138,7 +1143,7 @@ func GetOptionalWerfConfig(projectDir string, cmdData *CmdData, logRenderedFileP

if werfConfigPath != "" {
werfConfigTemplatesDir := GetWerfConfigTemplatesDir(projectDir, cmdData)
return config.GetWerfConfig(werfConfigPath, werfConfigTemplatesDir, logRenderedFilePath)
return config.GetWerfConfig(BackgroundContext(), werfConfigPath, werfConfigTemplatesDir, logRenderedFilePath)
}

return nil, nil
Expand All @@ -1152,7 +1157,7 @@ func GetRequiredWerfConfig(projectDir string, cmdData *CmdData, logRenderedFileP

werfConfigTemplatesDir := GetWerfConfigTemplatesDir(projectDir, cmdData)

return config.GetWerfConfig(werfConfigPath, werfConfigTemplatesDir, logRenderedFilePath)
return config.GetWerfConfig(BackgroundContext(), werfConfigPath, werfConfigTemplatesDir, logRenderedFilePath)
}

func GetWerfConfigPath(projectDir string, cmdData *CmdData, required bool) (string, error) {
Expand Down Expand Up @@ -1297,14 +1302,17 @@ func ProcessLogOptions(cmdData *CmdData) error {
}

if *cmdData.LogQuiet {
logging.EnableLogQuiet()
logboek.Streams().Mute()
} else if *cmdData.LogDebug {
logging.EnableLogDebug()
logboek.SetAcceptedLevel(level.Debug)
logboek.Streams().EnablePrefixWithTime()
logboek.Streams().SetPrefixStyle(style.Details())
} else if *cmdData.LogVerbose {
logging.EnableLogVerbose()
logboek.SetAcceptedLevel(level.Info)
}

if !*cmdData.LogPretty {
logboek.Streams().DisablePrettyLog()
logging.DisablePrettyLog()
}

Expand All @@ -1321,9 +1329,9 @@ func ProcessLogColorMode(cmdData *CmdData) error {
switch logColorMode {
case "auto":
case "on":
logging.EnableLogColor()
logboek.Streams().EnableStyle()
case "off":
logging.DisableLogColor()
logboek.Streams().DisableStyle()
default:
return fmt.Errorf("bad log color mode '%s': on, off and auto modes are supported", logColorMode)
}
Expand All @@ -1339,7 +1347,7 @@ func ProcessLogTerminalWidth(cmdData *CmdData) error {
return fmt.Errorf("--log-terminal-width parameter (%d) can not be negative", value)
}

logging.SetWidth(int(value))
logboek.Streams().SetWidth(int(value))
} else {
pInt64, err := getInt64EnvVar("WERF_LOG_TERMINAL_WIDTH")
if err != nil {
Expand All @@ -1354,14 +1362,14 @@ func ProcessLogTerminalWidth(cmdData *CmdData) error {
return fmt.Errorf("WERF_LOG_TERMINAL_WIDTH value (%s) can not be negative", os.Getenv("WERF_LOG_TERMINAL_WIDTH"))
}

logging.SetWidth(int(*pInt64))
logboek.Streams().SetWidth(int(*pInt64))
}

return nil
}

func DockerRegistryInit(cmdData *CmdData) error {
return docker_registry.Init(*cmdData.InsecureRegistry, *cmdData.SkipTlsVerifyRegistry)
return docker_registry.Init(BackgroundContext(), *cmdData.InsecureRegistry, *cmdData.SkipTlsVerifyRegistry)
}

func ValidateRepoImplementation(implementation string) error {
Expand Down Expand Up @@ -1413,7 +1421,7 @@ func LogRunningTime(f func() error) error {
t := time.Now()
err := f()

logboek.Default.LogFHighlight("Running time %0.2f seconds\n", time.Now().Sub(t).Seconds())
logboek.Default().LogFHighlight("Running time %0.2f seconds\n", time.Now().Sub(t).Seconds())

return err
}
Expand All @@ -1426,7 +1434,8 @@ func TerminateWithError(errMsg string, exitCode int) {
msg := fmt.Sprintf("Error: %s", errMsg)
msg = strings.TrimSuffix(msg, "\n")

logboek.LogErrorLn(msg)
logboek.Streams().DisableLineWrapping()
logboek.Error().LogLn(msg)
os.Exit(exitCode)
}

Expand Down Expand Up @@ -1463,15 +1472,15 @@ func GetLocalGitRepoForImagesCleanup(projectDir string, cmdData *CmdData) (clean
}

if isShallow {
logboek.Warn.LogLn("Git shallow clone should not be used with images cleanup commands due to incompleteness of the repository history that is extremely essential for proper work.")
logboek.Warn.LogLn("If you still want to use shallow clone, add --allow-git-shallow-clone option (WERF_ALLOW_GIT_SHALLOW_CLONE=1).")
logboek.Warn().LogLn("Git shallow clone should not be used with images cleanup commands due to incompleteness of the repository history that is extremely essential for proper work.")
logboek.Warn().LogLn("If you still want to use shallow clone, add --allow-git-shallow-clone option (WERF_ALLOW_GIT_SHALLOW_CLONE=1).")

return nil, fmt.Errorf("git shallow clone is not allowed")
}
}

if *cmdData.GitHistorySynchronization {
if err := localGitRepo.SyncWithOrigin(); err != nil {
if err := localGitRepo.SyncWithOrigin(BackgroundContext()); err != nil {
return nil, fmt.Errorf("synchronization failed: %s", err)
}
}
Expand All @@ -1481,3 +1490,7 @@ func GetLocalGitRepoForImagesCleanup(projectDir string, cmdData *CmdData) (clean
return nil, nil
}
}

func BackgroundContext() context.Context {
return logboek.NewContext(context.Background(), logboek.DefaultLogger())
}

0 comments on commit 09a8b5c

Please sign in to comment.