/
cleanup.go
141 lines (112 loc) · 4.66 KB
/
cleanup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package cleanup
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/werf/logboek"
"github.com/werf/werf/cmd/werf/common"
"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/host_cleaning"
"github.com/werf/werf/pkg/image"
"github.com/werf/werf/pkg/storage/lrumeta"
"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"
)
var commonCmdData common.CmdData
var cmdData struct {
Force bool
}
func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "cleanup",
Short: "Cleanup old unused werf cache and data of all projects on host machine",
Long: common.GetLongCommandDescription(`Cleanup old unused werf cache and data of all projects on host machine.
The data include:
* Lost docker containers and images from interrupted builds.
* Old service tmp dirs, which werf creates during every build, converge and other commands.
* Local cache:
* Remote git clones cache.
* Git worktree cache.
It is safe to run this command periodically by automated cleanup job in parallel with other werf commands such as build, converge and cleanup.`),
DisableFlagsInUseLine: true,
RunE: func(cmd *cobra.Command, args []string) error {
if err := werf.Init(*commonCmdData.TmpDir, *commonCmdData.HomeDir); err != nil {
return fmt.Errorf("initialization error: %w", err)
}
return common.WithContext(true, func(ctx context.Context) error {
defer global_warnings.PrintGlobalWarnings(ctx)
if err := common.ProcessLogOptions(&commonCmdData); err != nil {
common.PrintHelp(cmd)
return err
}
common.LogVersion()
return common.LogRunningTime(func() error { return runCleanup(ctx) })
})
},
}
common.SetupTmpDir(&commonCmdData, cmd, common.SetupTmpDirOptions{})
common.SetupHomeDir(&commonCmdData, cmd, common.SetupHomeDirOptions{})
common.SetupDockerConfig(&commonCmdData, cmd, "")
common.SetupProjectName(&commonCmdData, cmd)
common.SetupGiterminismOptions(&commonCmdData, cmd)
common.SetupLogOptions(&commonCmdData, cmd)
common.SetupDryRun(&commonCmdData, cmd)
common.SetupDisableAutoHostCleanup(&commonCmdData, cmd)
common.SetupAllowedDockerStorageVolumeUsage(&commonCmdData, cmd)
common.SetupAllowedDockerStorageVolumeUsageMargin(&commonCmdData, cmd)
common.SetupAllowedLocalCacheVolumeUsage(&commonCmdData, cmd)
common.SetupAllowedLocalCacheVolumeUsageMargin(&commonCmdData, cmd)
common.SetupDockerServerStoragePath(&commonCmdData, cmd)
common.SetupPlatform(&commonCmdData, cmd)
common.SetupInsecureRegistry(&commonCmdData, cmd)
common.SetupSkipTlsVerifyRegistry(&commonCmdData, cmd)
cmd.Flags().BoolVarP(&cmdData.Force, "force", "", util.GetBoolEnvironmentDefaultFalse("WERF_FORCE"), "Force deletion of images which are being used by some containers (default $WERF_FORCE)")
return cmd
}
func runCleanup(ctx context.Context) error {
containerBackend, processCtx, err := common.InitProcessContainerBackend(ctx, &commonCmdData)
if err != nil {
return err
}
ctx = processCtx
projectName := *commonCmdData.ProjectName
if projectName != "" {
return fmt.Errorf("no functionality for cleaning a certain project is implemented (--project-name=%s)", projectName)
}
gitDataManager, err := gitdata.GetHostGitDataManager(ctx)
if err != nil {
return fmt.Errorf("error getting host git data manager: %w", err)
}
if err := git_repo.Init(gitDataManager); err != nil {
return err
}
if err := image.Init(); err != nil {
return err
}
if err := lrumeta.Init(); err != nil {
return err
}
if err := true_git.Init(ctx, true_git.Options{LiveGitOutput: *commonCmdData.LogVerbose || *commonCmdData.LogDebug}); err != nil {
return err
}
logboek.LogOptionalLn()
cleanupDockerServer := false
if _, match := containerBackend.(*container_backend.DockerServerBackend); match {
cleanupDockerServer = true
}
hostCleanupOptions := host_cleaning.HostCleanupOptions{
DryRun: *commonCmdData.DryRun,
Force: cmdData.Force,
CleanupDockerServer: cleanupDockerServer,
AllowedDockerStorageVolumeUsagePercentage: commonCmdData.AllowedDockerStorageVolumeUsage,
AllowedDockerStorageVolumeUsageMarginPercentage: commonCmdData.AllowedDockerStorageVolumeUsageMargin,
AllowedLocalCacheVolumeUsagePercentage: commonCmdData.AllowedLocalCacheVolumeUsage,
AllowedLocalCacheVolumeUsageMarginPercentage: commonCmdData.AllowedLocalCacheVolumeUsageMargin,
DockerServerStoragePath: commonCmdData.DockerServerStoragePath,
}
return host_cleaning.RunHostCleanup(ctx, containerBackend, hostCleanupOptions)
}