Skip to content

Commit

Permalink
feat(gc): run host garbage collection in background
Browse files Browse the repository at this point in the history
  • Loading branch information
distorhead committed Jan 21, 2022
1 parent 75fc500 commit 29a1ea5
Show file tree
Hide file tree
Showing 49 changed files with 209 additions and 119 deletions.
2 changes: 1 addition & 1 deletion cmd/werf/build/main.go
Expand Up @@ -51,7 +51,7 @@ If one or more IMAGE_NAME parameters specified, werf will build only these image
common.CmdEnvAnno: common.EnvsDescription(common.WerfDebugAnsibleArgs),
},
RunE: func(cmd *cobra.Command, args []string) error {
ctx := common.BackgroundContext()
ctx := common.GetContext()

defer global_warnings.PrintGlobalWarnings(ctx)

Expand Down
4 changes: 2 additions & 2 deletions cmd/werf/bundle/apply/apply.go
Expand Up @@ -44,7 +44,7 @@ func NewCmd() *cobra.Command {
common.CmdEnvAnno: common.EnvsDescription(),
},
RunE: func(cmd *cobra.Command, args []string) error {
defer global_warnings.PrintGlobalWarnings(common.BackgroundContext())
defer global_warnings.PrintGlobalWarnings(common.GetContext())

if err := common.ProcessLogOptions(&commonCmdData); err != nil {
common.PrintHelp(cmd)
Expand Down Expand Up @@ -101,7 +101,7 @@ func NewCmd() *cobra.Command {
}

func runApply() error {
ctx := common.BackgroundContext()
ctx := common.GetContext()

global_warnings.PostponeMultiwerfNotUpToDateWarning()

Expand Down
4 changes: 2 additions & 2 deletions cmd/werf/bundle/download/download.go
Expand Up @@ -30,7 +30,7 @@ func NewCmd() *cobra.Command {
common.CmdEnvAnno: common.EnvsDescription(),
},
RunE: func(cmd *cobra.Command, args []string) error {
defer global_warnings.PrintGlobalWarnings(common.BackgroundContext())
defer global_warnings.PrintGlobalWarnings(common.GetContext())

if err := common.ProcessLogOptions(&commonCmdData); err != nil {
common.PrintHelp(cmd)
Expand Down Expand Up @@ -67,7 +67,7 @@ func NewCmd() *cobra.Command {
}

func runDownload() error {
ctx := common.BackgroundContext()
ctx := common.GetContext()

if err := werf.Init(*commonCmdData.TmpDir, *commonCmdData.HomeDir); err != nil {
return fmt.Errorf("initialization error: %s", err)
Expand Down
4 changes: 2 additions & 2 deletions cmd/werf/bundle/export/export.go
Expand Up @@ -7,7 +7,7 @@ import (
"time"

"github.com/spf13/cobra"
helm_v3 "helm.sh/helm/v3/cmd/helm"
"helm.sh/helm/v3/cmd/helm"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/cli/values"
Expand Down Expand Up @@ -47,7 +47,7 @@ func NewCmd() *cobra.Command {
common.CmdEnvAnno: common.EnvsDescription(),
},
RunE: func(cmd *cobra.Command, args []string) error {
ctx := common.BackgroundContext()
ctx := common.GetContext()

defer global_warnings.PrintGlobalWarnings(ctx)

Expand Down
4 changes: 2 additions & 2 deletions cmd/werf/bundle/publish/publish.go
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/Masterminds/semver"
uuid "github.com/satori/go.uuid"
"github.com/spf13/cobra"
helm_v3 "helm.sh/helm/v3/cmd/helm"
"helm.sh/helm/v3/cmd/helm"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/cli/values"
Expand Down Expand Up @@ -55,7 +55,7 @@ Published into container registry bundle can be rolled out by the "werf bundle"
common.CmdEnvAnno: common.EnvsDescription(),
},
RunE: func(cmd *cobra.Command, args []string) error {
ctx := common.BackgroundContext()
ctx := common.GetContext()

defer global_warnings.PrintGlobalWarnings(ctx)

Expand Down
2 changes: 1 addition & 1 deletion cmd/werf/ci_env/ci_env.go
Expand Up @@ -87,7 +87,7 @@ Currently supported only GitLab (gitlab) and GitHub (github) CI systems`,
func runCIEnv(cmd *cobra.Command, args []string) error {
logboek.SetAcceptedLevel(level.Error)

ctx := common.BackgroundContext()
ctx := common.GetContext()

if err := werf.Init(*commonCmdData.TmpDir, *commonCmdData.HomeDir); err != nil {
return fmt.Errorf("initialization error: %s", err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/werf/cleanup/cleanup.go
Expand Up @@ -34,7 +34,7 @@ The command works according to special rules called cleanup policies, which the
It is safe to run this command periodically (daily is enough) by automated cleanup job in parallel with other werf commands such as build, converge and host cleanup.`),
Example: ` $ werf cleanup --repo registry.mydomain.com/myproject/werf`,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := common.BackgroundContext()
ctx := common.GetContext()

defer global_warnings.PrintGlobalWarnings(ctx)

Expand Down
73 changes: 69 additions & 4 deletions cmd/werf/common/common.go
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/werf/werf/pkg/true_git"
"github.com/werf/werf/pkg/util"
"github.com/werf/werf/pkg/werf"
"github.com/werf/werf/pkg/werf/global_warnings"
)

type CmdData struct {
Expand Down Expand Up @@ -1182,17 +1183,17 @@ func GetGiterminismManager(ctx context.Context, cmdData *CmdData) (giterminism_m
openLocalRepoOptions.ServiceBranchOptions.GlobExcludeList = GetDevIgnore(cmdData)
}

localGitRepo, err := git_repo.OpenLocalRepo(BackgroundContext(), "own", gitWorkTree, openLocalRepoOptions)
localGitRepo, err := git_repo.OpenLocalRepo(GetContext(), "own", gitWorkTree, openLocalRepoOptions)
if err != nil {
return nil, err
}

headCommit, err := localGitRepo.HeadCommitHash(BackgroundContext())
headCommit, err := localGitRepo.HeadCommitHash(GetContext())
if err != nil {
return nil, err
}

return giterminism_manager.NewManager(BackgroundContext(), workingDir, localGitRepo, headCommit, giterminism_manager.NewManagerOptions{
return giterminism_manager.NewManager(GetContext(), workingDir, localGitRepo, headCommit, giterminism_manager.NewManagerOptions{
LooseGiterminism: *cmdData.LooseGiterminism,
Dev: *cmdData.Dev,
})
Expand Down Expand Up @@ -1520,6 +1521,70 @@ func SetupPlatform(cmdData *CmdData, cmd *cobra.Command) {
cmd.Flags().StringVarP(cmdData.Platform, "platform", "", defaultValue, "Enable platform emulation when building images with werf. The only supported option for now is linux/amd64.")
}

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

func WithContext(allowBackgroundMode bool, f func(ctx context.Context) error) error {
var ctx context.Context

if allowBackgroundMode && IsBackgroundModeEnabled() {
out, err := os.OpenFile(GetBackgroundOutputFile(), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0o644)
if err != nil {
return fmt.Errorf("unable to open background output file %q: %s", GetBackgroundOutputFile(), err)
}
defer out.Close()

ctx = logboek.NewContext(context.Background(), logboek.NewLogger(out, out))

if err := f(ctx); err != nil {
if err := os.WriteFile(GetLastBackgroundErrorFile(), []byte(err.Error()+"\n"), 0o644); err != nil {
logboek.Context(ctx).Warn().LogF("ERROR: unable to write %q: %s\n", GetLastBackgroundErrorFile(), err)
}
return err
}

return nil
} else {
ctx = logboek.NewContext(context.Background(), logboek.DefaultLogger())

if allowBackgroundMode {
if backgroundErr, err := GetAndRemoveLastBackgroundError(); err != nil {
return fmt.Errorf("unable to get last background error: %s", err)
} else if backgroundErr != nil {
global_warnings.GlobalWarningLn(ctx, fmt.Sprintf("last background error: %s", backgroundErr))
}
}

return f(ctx)
}
}

func GetAndRemoveLastBackgroundError() (error, error) {
data, err := os.ReadFile(GetLastBackgroundErrorFile())
if err != nil {
return nil, nil
}

if err := os.RemoveAll(GetLastBackgroundErrorFile()); err != nil {
return nil, fmt.Errorf("unable to remove %q: %s", GetLastBackgroundErrorFile(), err)
}

if len(data) != 0 {
return fmt.Errorf("%s", string(data)), nil
}

return nil, nil
}

func IsBackgroundModeEnabled() bool {
return os.Getenv("_WERF_BACKGROUND_MODE_ENABLED") == "1"
}

func GetBackgroundOutputFile() string {
return filepath.Join(werf.GetServiceDir(), "background_output.log")
}

func GetLastBackgroundErrorFile() string {
return filepath.Join(werf.GetServiceDir(), "last_background_error")
}
18 changes: 10 additions & 8 deletions cmd/werf/common/host_cleanup.go
Expand Up @@ -23,14 +23,16 @@ func RunAutoHostCleanup(ctx context.Context, cmdData *CmdData) error {
return fmt.Errorf("incompatible params --allowed-local-cache-volume-usage=%d and --allowed-local-cache-volume-usage-margin=%d: margin percentage should be less than allowed volume usage level percentage", *cmdData.AllowedLocalCacheVolumeUsage, *cmdData.AllowedLocalCacheVolumeUsageMargin)
}

return host_cleaning.RunAutoHostCleanup(ctx, host_cleaning.HostCleanupOptions{
DryRun: false,
Force: false,
AllowedDockerStorageVolumeUsagePercentage: cmdData.AllowedDockerStorageVolumeUsage,
AllowedDockerStorageVolumeUsageMarginPercentage: cmdData.AllowedDockerStorageVolumeUsageMargin,
AllowedLocalCacheVolumeUsagePercentage: cmdData.AllowedLocalCacheVolumeUsage,
AllowedLocalCacheVolumeUsageMarginPercentage: cmdData.AllowedLocalCacheVolumeUsageMargin,
DockerServerStoragePath: *cmdData.DockerServerStoragePath,
return host_cleaning.RunAutoHostCleanup(ctx, host_cleaning.AutoHostCleanupOptions{
HostCleanupOptions: host_cleaning.HostCleanupOptions{
DryRun: false,
Force: false,
AllowedDockerStorageVolumeUsagePercentage: cmdData.AllowedDockerStorageVolumeUsage,
AllowedDockerStorageVolumeUsageMarginPercentage: cmdData.AllowedDockerStorageVolumeUsageMargin,
AllowedLocalCacheVolumeUsagePercentage: cmdData.AllowedLocalCacheVolumeUsage,
AllowedLocalCacheVolumeUsageMarginPercentage: cmdData.AllowedLocalCacheVolumeUsageMargin,
DockerServerStoragePath: cmdData.DockerServerStoragePath,
},
})
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/werf/common/storage_repo_data.go
Expand Up @@ -31,7 +31,7 @@ func (d *RepoData) GetContainerRegistry() string {
case *d.ContainerRegistry != "":
return *d.ContainerRegistry
case *d.Implementation != "":
logboek.Context(BackgroundContext()).Warn().LogLn("DEPRECATION WARNING: The option --repo-implementation ($WERF_REPO_IMPLEMENTATION) is renamed to --repo-container-registry ($WERF_REPO_CONTAINER_REGISTRY) and will be removed in v1.3!")
logboek.Context(GetContext()).Warn().LogLn("DEPRECATION WARNING: The option --repo-implementation ($WERF_REPO_IMPLEMENTATION) is renamed to --repo-container-registry ($WERF_REPO_CONTAINER_REGISTRY) and will be removed in v1.3!")
return *d.Implementation
default:
return ""
Expand Down
2 changes: 1 addition & 1 deletion cmd/werf/common/synchronization.go
Expand Up @@ -54,7 +54,7 @@ func checkSynchronizationKubernetesParamsForWarnings(cmdData *CmdData) {
return
}

ctx := BackgroundContext()
ctx := GetContext()
doPrintWarning := false
kubeConfigEnv := os.Getenv("KUBECONFIG")
switch {
Expand Down
2 changes: 1 addition & 1 deletion cmd/werf/compose/main.go
Expand Up @@ -164,7 +164,7 @@ services:
},
Example: options.Example,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := common.BackgroundContext()
ctx := common.GetContext()
defer global_warnings.PrintGlobalWarnings(ctx)

if err := common.ProcessLogOptions(&commonCmdData); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cmd/werf/config/list/list.go
Expand Up @@ -53,7 +53,7 @@ func NewCmd() *cobra.Command {
}

func run() error {
ctx := common.BackgroundContext()
ctx := common.GetContext()

if err := werf.Init(*commonCmdData.TmpDir, *commonCmdData.HomeDir); err != nil {
return fmt.Errorf("initialization error: %s", err)
Expand All @@ -77,7 +77,7 @@ func run() error {
return err
}

_, werfConfig, err := common.GetRequiredWerfConfig(common.BackgroundContext(), &commonCmdData, giterminismManager, common.GetWerfConfigOptions(&commonCmdData, false))
_, werfConfig, err := common.GetRequiredWerfConfig(common.GetContext(), &commonCmdData, giterminismManager, common.GetWerfConfigOptions(&commonCmdData, false))
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/werf/config/render/render.go
Expand Up @@ -21,7 +21,7 @@ func NewCmd() *cobra.Command {
DisableFlagsInUseLine: true,
Short: "Render werf.yaml",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := common.BackgroundContext()
ctx := common.GetContext()

if err := common.ProcessLogOptions(&commonCmdData); err != nil {
common.PrintHelp(cmd)
Expand Down Expand Up @@ -62,7 +62,7 @@ func NewCmd() *cobra.Command {
return err
}

return config.RenderWerfConfig(common.BackgroundContext(), customWerfConfigRelPath, customWerfConfigTemplatesDirRelPath, args, giterminismManager, configOpts)
return config.RenderWerfConfig(common.GetContext(), customWerfConfigRelPath, customWerfConfigTemplatesDirRelPath, args, giterminismManager, configOpts)
},
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/werf/converge/converge.go
Expand Up @@ -9,7 +9,7 @@ import (
"time"

"github.com/spf13/cobra"
helm_v3 "helm.sh/helm/v3/cmd/helm"
"helm.sh/helm/v3/cmd/helm"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
Expand Down Expand Up @@ -65,7 +65,7 @@ werf converge --repo registry.mydomain.com/web --env production`,
common.CmdEnvAnno: common.EnvsDescription(common.WerfDebugAnsibleArgs, common.WerfSecretKey),
},
RunE: func(cmd *cobra.Command, args []string) error {
ctx := common.BackgroundContext()
ctx := common.GetContext()

defer global_warnings.PrintGlobalWarnings(ctx)

Expand Down
2 changes: 1 addition & 1 deletion cmd/werf/cr/login/login.go
Expand Up @@ -41,7 +41,7 @@ werf cr login -p token registry.example.com
werf cr login --insecure-registry registry.example.com`,
DisableFlagsInUseLine: true,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := common.BackgroundContext()
ctx := common.GetContext()

defer global_warnings.PrintGlobalWarnings(ctx)

Expand Down
2 changes: 1 addition & 1 deletion cmd/werf/cr/logout/logout.go
Expand Up @@ -23,7 +23,7 @@ func NewCmd() *cobra.Command {
Long: common.GetLongCommandDescription(`Logout from a remote registry`),
DisableFlagsInUseLine: true,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := common.BackgroundContext()
ctx := common.GetContext()

defer global_warnings.PrintGlobalWarnings(ctx)

Expand Down
2 changes: 1 addition & 1 deletion cmd/werf/dismiss/dismiss.go
Expand Up @@ -53,7 +53,7 @@ Read more info about Helm Release name, Kubernetes Namespace and how to change i
$ werf dismiss --release myrelease --namespace myns`,
DisableFlagsInUseLine: true,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := common.BackgroundContext()
ctx := common.GetContext()

defer global_warnings.PrintGlobalWarnings(ctx)

Expand Down
2 changes: 1 addition & 1 deletion cmd/werf/export/export.go
Expand Up @@ -45,7 +45,7 @@ All meta-information related to werf is removed from the exported images, and th
common.DisableOptionsInUseLineAnno: "1",
},
RunE: func(cmd *cobra.Command, args []string) error {
ctx := common.BackgroundContext()
ctx := common.GetContext()
defer global_warnings.PrintGlobalWarnings(ctx)

if err := common.ProcessLogOptions(&commonCmdData); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/werf/helm/get_autogenerated_values.go
Expand Up @@ -37,7 +37,7 @@ These values includes project name, docker images ids and other`),
common.CmdEnvAnno: common.EnvsDescription(common.WerfSecretKey),
},
RunE: func(cmd *cobra.Command, args []string) error {
ctx := common.BackgroundContext()
ctx := common.GetContext()

if err := common.ProcessLogOptions(&getAutogeneratedValuedCmdData); err != nil {
common.PrintHelp(cmd)
Expand Down
4 changes: 2 additions & 2 deletions cmd/werf/helm/get_namespace.go
Expand Up @@ -47,7 +47,7 @@ func NewGetNamespaceCmd() *cobra.Command {
}

func runGetNamespace() error {
ctx := common.BackgroundContext()
ctx := common.GetContext()

if err := werf.Init(*getNamespaceCmdData.TmpDir, *getNamespaceCmdData.HomeDir); err != nil {
return fmt.Errorf("initialization error: %s", err)
Expand All @@ -71,7 +71,7 @@ func runGetNamespace() error {
return err
}

_, werfConfig, err := common.GetRequiredWerfConfig(common.BackgroundContext(), &getNamespaceCmdData, giterminismManager, common.GetWerfConfigOptions(&getNamespaceCmdData, false))
_, werfConfig, err := common.GetRequiredWerfConfig(common.GetContext(), &getNamespaceCmdData, giterminismManager, common.GetWerfConfigOptions(&getNamespaceCmdData, false))
if err != nil {
return fmt.Errorf("unable to load werf config: %s", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/werf/helm/get_release.go
Expand Up @@ -47,7 +47,7 @@ func NewGetReleaseCmd() *cobra.Command {
}

func runGetRelease() error {
ctx := common.BackgroundContext()
ctx := common.GetContext()

if err := werf.Init(*getReleaseCmdData.TmpDir, *getReleaseCmdData.HomeDir); err != nil {
return fmt.Errorf("initialization error: %s", err)
Expand All @@ -71,7 +71,7 @@ func runGetRelease() error {
return err
}

_, werfConfig, err := common.GetRequiredWerfConfig(common.BackgroundContext(), &getReleaseCmdData, giterminismManager, common.GetWerfConfigOptions(&getReleaseCmdData, false))
_, werfConfig, err := common.GetRequiredWerfConfig(common.GetContext(), &getReleaseCmdData, giterminismManager, common.GetWerfConfigOptions(&getReleaseCmdData, false))
if err != nil {
return fmt.Errorf("unable to load werf config: %s", err)
}
Expand Down

0 comments on commit 29a1ea5

Please sign in to comment.