From cb51e3255601756a82143387f656e7d4670b0cab Mon Sep 17 00:00:00 2001 From: Timofey Kirillov Date: Fri, 17 Jun 2022 18:45:48 +0300 Subject: [PATCH] fix(host-cleanup): host cleanup not working in buildah mode Signed-off-by: Timofey Kirillov --- cmd/werf/host/cleanup/cleanup.go | 2 +- cmd/werf/host/purge/purge.go | 2 +- pkg/container_backend/buildah_backend.go | 32 ++++++++++++- .../docker_server_backend.go | 19 ++++++++ pkg/container_backend/interface.go | 3 ++ .../perf_check_container_backend.go | 8 ++++ pkg/host_cleaning/host_cleanup.go | 45 +++++++++++-------- pkg/host_cleaning/host_purge.go | 12 ++--- pkg/tmp_manager/gc.go | 6 +-- pkg/tmp_manager/purge.go | 10 ++--- pkg/util/linux_container.go | 22 --------- 11 files changed, 102 insertions(+), 59 deletions(-) diff --git a/cmd/werf/host/cleanup/cleanup.go b/cmd/werf/host/cleanup/cleanup.go index 549a5f7ab1..28b73d434f 100644 --- a/cmd/werf/host/cleanup/cleanup.go +++ b/cmd/werf/host/cleanup/cleanup.go @@ -137,5 +137,5 @@ func runCleanup(ctx context.Context) error { DockerServerStoragePath: commonCmdData.DockerServerStoragePath, } - return host_cleaning.RunHostCleanup(ctx, hostCleanupOptions) + return host_cleaning.RunHostCleanup(ctx, containerBackend, hostCleanupOptions) } diff --git a/cmd/werf/host/purge/purge.go b/cmd/werf/host/purge/purge.go index c50c52dac2..9977ea2e75 100644 --- a/cmd/werf/host/purge/purge.go +++ b/cmd/werf/host/purge/purge.go @@ -102,7 +102,7 @@ func runReset(ctx context.Context) error { if projectName == "" { logboek.LogOptionalLn() hostPurgeOptions := host_cleaning.HostPurgeOptions{DryRun: *commonCmdData.DryRun, RmContainersThatUseWerfImages: cmdData.Force} - if err := host_cleaning.HostPurge(ctx, hostPurgeOptions); err != nil { + if err := host_cleaning.HostPurge(ctx, containerBackend, hostPurgeOptions); err != nil { return err } } else { diff --git a/pkg/container_backend/buildah_backend.go b/pkg/container_backend/buildah_backend.go index a7168a71f3..f00077a36c 100644 --- a/pkg/container_backend/buildah_backend.go +++ b/pkg/container_backend/buildah_backend.go @@ -59,7 +59,7 @@ func (runtime *BuildahBackend) createContainers(ctx context.Context, images []st var res []*containerDesc for _, img := range images { - containerID := fmt.Sprintf("werf-stage-build-%s", uuid.New().String()) + containerID := fmt.Sprintf("werf-%s", uuid.New().String()) _, err := runtime.buildah.FromCommand(ctx, containerID, img, buildah.FromCommandOpts(runtime.getBuildahCommonOpts(ctx, true))) if err != nil { @@ -176,7 +176,7 @@ func (runtime *BuildahBackend) CalculateDependencyImportChecksum(ctx context.Con } defer func() { if err := runtime.removeContainers(ctx, []*containerDesc{container}); err != nil { - logboek.Context(ctx).Error().LogF("ERROR: unable to remove temporal dependency container: %s\n", err) + logboek.Context(ctx).Error().LogF("ERROR: unable to remove temporal dependency container %q: %s\n", container.Name, err) } }() @@ -643,6 +643,34 @@ func (runtime *BuildahBackend) String() string { return "buildah-runtime" } +func (runtime *BuildahBackend) RemoveHostDirs(ctx context.Context, mountDir string, dirs []string) error { + var container *containerDesc + if c, err := runtime.createContainers(ctx, []string{"alpine"}); err != nil { + return fmt.Errorf("unable to create container based on alpine: %w", err) + } else { + container = c[0] + } + defer func() { + if err := runtime.removeContainers(ctx, []*containerDesc{container}); err != nil { + logboek.Context(ctx).Error().LogF("ERROR: unable to remove temporal container %q: %s\n", container.Name, err) + } + }() + var containerDirs []string + for _, dir := range dirs { + containerDirs = append(containerDirs, util.ToLinuxContainerPath(dir)) + } + + return runtime.buildah.RunCommand(ctx, container.Name, append([]string{"rm", "-rf"}, containerDirs...), buildah.RunCommandOpts{ + Mounts: []specs.Mount{ + { + Type: "bind", + Source: mountDir, + Destination: util.ToLinuxContainerPath(mountDir), + }, + }, + }) +} + func parseVolume(volume string) (string, string, error) { volumeParts := strings.SplitN(volume, ":", 2) if len(volumeParts) != 2 { diff --git a/pkg/container_backend/docker_server_backend.go b/pkg/container_backend/docker_server_backend.go index ffccd79873..aace6f9771 100644 --- a/pkg/container_backend/docker_server_backend.go +++ b/pkg/container_backend/docker_server_backend.go @@ -12,6 +12,7 @@ import ( "github.com/werf/logboek" "github.com/werf/werf/pkg/docker" "github.com/werf/werf/pkg/image" + "github.com/werf/werf/pkg/util" ) type DockerServerBackend struct{} @@ -240,3 +241,21 @@ func (runtime *DockerServerBackend) TagImageByName(ctx context.Context, img Lega func (runtime *DockerServerBackend) String() string { return "local-docker-server" } + +func (runtime *DockerServerBackend) RemoveHostDirs(ctx context.Context, mountDir string, dirs []string) error { + var containerDirs []string + for _, dir := range dirs { + containerDirs = append(containerDirs, util.ToLinuxContainerPath(dir)) + } + + args := []string{ + "--rm", + "--volume", fmt.Sprintf("%s:%s", mountDir, util.ToLinuxContainerPath(mountDir)), + "alpine", + "rm", "-rf", + } + + args = append(args, containerDirs...) + + return docker.CliRun(ctx, args...) +} diff --git a/pkg/container_backend/interface.go b/pkg/container_backend/interface.go index c8e60cac1e..4a44e6807b 100644 --- a/pkg/container_backend/interface.go +++ b/pkg/container_backend/interface.go @@ -57,6 +57,9 @@ type ContainerBackend interface { HasStapelBuildSupport() bool String() string + // TODO: Util method for cleanup, which possibly should be avoided in the future + RemoveHostDirs(ctx context.Context, mountDir string, dirs []string) error + // Legacy ShouldCleanupDockerfileImage() bool RefreshImageObject(ctx context.Context, img LegacyImageInterface) error diff --git a/pkg/container_backend/perf_check_container_backend.go b/pkg/container_backend/perf_check_container_backend.go index 929bec0716..1f00639190 100644 --- a/pkg/container_backend/perf_check_container_backend.go +++ b/pkg/container_backend/perf_check_container_backend.go @@ -122,3 +122,11 @@ func (runtime *PerfCheckContainerBackend) RemoveImage(ctx context.Context, img L func (runtime *PerfCheckContainerBackend) String() string { return runtime.ContainerBackend.String() } + +func (runtime *PerfCheckContainerBackend) RemoveHostDirs(ctx context.Context, mountDir string, dirs []string) (resErr error) { + logboek.Context(ctx).Default().LogProcess("ContainerBackend.RemoveHostDirs %q %v", mountDir, dirs). + Do(func() { + resErr = runtime.ContainerBackend.RemoveHostDirs(ctx, mountDir, dirs) + }) + return +} diff --git a/pkg/host_cleaning/host_cleanup.go b/pkg/host_cleaning/host_cleanup.go index 18cc816642..0cc85d5c62 100644 --- a/pkg/host_cleaning/host_cleanup.go +++ b/pkg/host_cleaning/host_cleanup.go @@ -8,6 +8,7 @@ import ( "strings" "github.com/werf/logboek" + "github.com/werf/werf/pkg/container_backend" "github.com/werf/werf/pkg/git_repo/gitdata" "github.com/werf/werf/pkg/tmp_manager" ) @@ -113,9 +114,9 @@ func RunAutoHostCleanup(ctx context.Context, options AutoHostCleanupOptions) err return nil } -func RunHostCleanup(ctx context.Context, options HostCleanupOptions) error { +func RunHostCleanup(ctx context.Context, containerBackend container_backend.ContainerBackend, options HostCleanupOptions) error { if err := logboek.Context(ctx).LogProcess("Running GC for tmp data").DoError(func() error { - if err := tmp_manager.RunGC(ctx, options.DryRun); err != nil { + if err := tmp_manager.RunGC(ctx, options.DryRun, containerBackend); err != nil { return fmt.Errorf("tmp files GC failed: %w", err) } return nil @@ -164,25 +165,31 @@ func ShouldRunAutoHostCleanup(ctx context.Context, options HostCleanupOptions) ( return true, nil } - allowedLocalCacheVolumeUsagePercentage := getOptionValueOrDefault(options.AllowedLocalCacheVolumeUsagePercentage, DefaultAllowedLocalCacheVolumeUsagePercentage) - allowedDockerStorageVolumeUsagePercentage := getOptionValueOrDefault(options.AllowedDockerStorageVolumeUsagePercentage, DefaultAllowedDockerStorageVolumeUsagePercentage) + if options.CleanupDockerServer { + allowedLocalCacheVolumeUsagePercentage := getOptionValueOrDefault(options.AllowedLocalCacheVolumeUsagePercentage, DefaultAllowedLocalCacheVolumeUsagePercentage) + allowedDockerStorageVolumeUsagePercentage := getOptionValueOrDefault(options.AllowedDockerStorageVolumeUsagePercentage, DefaultAllowedDockerStorageVolumeUsagePercentage) - shouldRun, err = gitdata.ShouldRunAutoGC(ctx, allowedLocalCacheVolumeUsagePercentage) - if err != nil { - return false, fmt.Errorf("failed to check git repo GC: %w", err) - } - if shouldRun { - return true, nil - } + shouldRun, err = gitdata.ShouldRunAutoGC(ctx, allowedLocalCacheVolumeUsagePercentage) + if err != nil { + return false, fmt.Errorf("failed to check git repo GC: %w", err) + } + if shouldRun { + return true, nil + } - dockerServerStoragePath, err := getDockerServerStoragePath(ctx, options.DockerServerStoragePath) - if err != nil { - return false, fmt.Errorf("error getting local docker server storage path: %w", err) - } + dockerServerStoragePath, err := getDockerServerStoragePath(ctx, options.DockerServerStoragePath) + if err != nil { + return false, fmt.Errorf("error getting local docker server storage path: %w", err) + } - shouldRun, err = ShouldRunAutoGCForLocalDockerServer(ctx, allowedDockerStorageVolumeUsagePercentage, dockerServerStoragePath) - if err != nil { - return false, fmt.Errorf("failed to check local docker server host cleaner GC: %w", err) + shouldRun, err = ShouldRunAutoGCForLocalDockerServer(ctx, allowedDockerStorageVolumeUsagePercentage, dockerServerStoragePath) + if err != nil { + return false, fmt.Errorf("failed to check local docker server host cleaner GC: %w", err) + } + if shouldRun { + return true, nil + } } - return shouldRun, nil + + return false, nil } diff --git a/pkg/host_cleaning/host_purge.go b/pkg/host_cleaning/host_purge.go index c8baa41196..c1d70f02f1 100644 --- a/pkg/host_cleaning/host_purge.go +++ b/pkg/host_cleaning/host_purge.go @@ -9,10 +9,10 @@ import ( "github.com/docker/docker/api/types/filters" "github.com/werf/logboek" + "github.com/werf/werf/pkg/container_backend" "github.com/werf/werf/pkg/image" "github.com/werf/werf/pkg/stapel" "github.com/werf/werf/pkg/tmp_manager" - "github.com/werf/werf/pkg/util" "github.com/werf/werf/pkg/werf" ) @@ -21,7 +21,7 @@ type HostPurgeOptions struct { RmContainersThatUseWerfImages bool } -func HostPurge(ctx context.Context, options HostPurgeOptions) error { +func HostPurge(ctx context.Context, containerBackend container_backend.ContainerBackend, options HostPurgeOptions) error { commonOptions := CommonOptions{ RmiForce: true, RmForce: true, @@ -68,12 +68,12 @@ func HostPurge(ctx context.Context, options HostPurgeOptions) error { return err } - if err := tmp_manager.Purge(ctx, commonOptions.DryRun); err != nil { + if err := tmp_manager.Purge(ctx, commonOptions.DryRun, containerBackend); err != nil { return fmt.Errorf("tmp files purge failed: %w", err) } if err := logboek.Context(ctx).LogProcess("Running werf home data purge").DoError(func() error { - return purgeHomeWerfFiles(ctx, commonOptions.DryRun) + return purgeHomeWerfFiles(ctx, commonOptions.DryRun, containerBackend) }); err != nil { return err } @@ -99,7 +99,7 @@ func deleteStapel(ctx context.Context, dryRun bool) error { return nil } -func purgeHomeWerfFiles(ctx context.Context, dryRun bool) error { +func purgeHomeWerfFiles(ctx context.Context, dryRun bool, containerBackend container_backend.ContainerBackend) error { pathsToRemove := []string{werf.GetServiceDir(), werf.GetLocalCacheDir(), werf.GetSharedContextDir()} for _, path := range pathsToRemove { @@ -119,6 +119,6 @@ func purgeHomeWerfFiles(ctx context.Context, dryRun bool) error { return nil } else { - return util.RemoveHostDirsWithLinuxContainer(ctx, werf.GetHomeDir(), pathsToRemove) + return containerBackend.RemoveHostDirs(ctx, werf.GetHomeDir(), pathsToRemove) } } diff --git a/pkg/tmp_manager/gc.go b/pkg/tmp_manager/gc.go index a0ab165044..de24de60c9 100644 --- a/pkg/tmp_manager/gc.go +++ b/pkg/tmp_manager/gc.go @@ -11,7 +11,7 @@ import ( "time" "github.com/werf/logboek" - "github.com/werf/werf/pkg/util" + "github.com/werf/werf/pkg/container_backend" "github.com/werf/werf/pkg/werf" ) @@ -79,7 +79,7 @@ func ShouldRunAutoGC() (bool, error) { return false, nil } -func RunGC(ctx context.Context, dryRun bool) error { +func RunGC(ctx context.Context, dryRun bool, containerBackend container_backend.ContainerBackend) error { projectDirsToRemove := []string{} pathsToRemove := []string{} @@ -118,7 +118,7 @@ func RunGC(ctx context.Context, dryRun bool) error { } } } else { - if err := util.RemoveHostDirsWithLinuxContainer(ctx, werf.GetTmpDir(), projectDirsToRemove); err != nil { + if err := containerBackend.RemoveHostDirs(ctx, werf.GetTmpDir(), projectDirsToRemove); err != nil { removeErrors = append(removeErrors, fmt.Errorf("unable to remove tmp projects dirs %s: %w", strings.Join(projectDirsToRemove, ", "), err)) } } diff --git a/pkg/tmp_manager/purge.go b/pkg/tmp_manager/purge.go index 3468bdf583..678738f0af 100644 --- a/pkg/tmp_manager/purge.go +++ b/pkg/tmp_manager/purge.go @@ -10,15 +10,15 @@ import ( "strings" "github.com/werf/logboek" - "github.com/werf/werf/pkg/util" + "github.com/werf/werf/pkg/container_backend" "github.com/werf/werf/pkg/werf" ) -func Purge(ctx context.Context, dryRun bool) error { - return logboek.Context(ctx).LogProcess("Running purge for tmp data").DoError(func() error { return purge(ctx, dryRun) }) +func Purge(ctx context.Context, dryRun bool, containerBackend container_backend.ContainerBackend) error { + return logboek.Context(ctx).LogProcess("Running purge for tmp data").DoError(func() error { return purge(ctx, dryRun, containerBackend) }) } -func purge(ctx context.Context, dryRun bool) error { +func purge(ctx context.Context, dryRun bool, containerBackend container_backend.ContainerBackend) error { tmpFiles, err := ioutil.ReadDir(werf.GetTmpDir()) if err != nil { return fmt.Errorf("unable to list tmp files in %s: %w", werf.GetTmpDir(), err) @@ -50,7 +50,7 @@ func purge(ctx context.Context, dryRun bool) error { } } } else { - if err := util.RemoveHostDirsWithLinuxContainer(ctx, werf.GetTmpDir(), projectDirsToRemove); err != nil { + if err := containerBackend.RemoveHostDirs(ctx, werf.GetTmpDir(), projectDirsToRemove); err != nil { errors = append(errors, fmt.Errorf("unable to remove tmp projects dirs %s: %w", strings.Join(projectDirsToRemove, ", "), err)) } } diff --git a/pkg/util/linux_container.go b/pkg/util/linux_container.go index 1e73b7fbe0..a12fa8eb03 100644 --- a/pkg/util/linux_container.go +++ b/pkg/util/linux_container.go @@ -1,33 +1,11 @@ package util import ( - "context" - "fmt" "os" "path/filepath" "strings" - - "github.com/werf/werf/pkg/docker" ) -func RemoveHostDirsWithLinuxContainer(ctx context.Context, mountDir string, dirs []string) error { - var containerDirs []string - for _, dir := range dirs { - containerDirs = append(containerDirs, ToLinuxContainerPath(dir)) - } - - args := []string{ - "--rm", - "--volume", fmt.Sprintf("%s:%s", mountDir, ToLinuxContainerPath(mountDir)), - "alpine", - "rm", "-rf", - } - - args = append(args, containerDirs...) - - return docker.CliRun(ctx, args...) -} - func ToLinuxContainerPath(path string) string { return filepath.ToSlash( strings.TrimPrefix(