Skip to content

Commit

Permalink
fix(host-cleanup): host cleanup not working in buildah mode
Browse files Browse the repository at this point in the history
Signed-off-by: Timofey Kirillov <timofey.kirillov@flant.com>
  • Loading branch information
distorhead committed Jun 17, 2022
1 parent 71b152e commit cb51e32
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 59 deletions.
2 changes: 1 addition & 1 deletion cmd/werf/host/cleanup/cleanup.go
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion cmd/werf/host/purge/purge.go
Expand Up @@ -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 {
Expand Down
32 changes: 30 additions & 2 deletions pkg/container_backend/buildah_backend.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}()

Expand Down Expand Up @@ -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 {
Expand Down
19 changes: 19 additions & 0 deletions pkg/container_backend/docker_server_backend.go
Expand Up @@ -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{}
Expand Down Expand Up @@ -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...)
}
3 changes: 3 additions & 0 deletions pkg/container_backend/interface.go
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions pkg/container_backend/perf_check_container_backend.go
Expand Up @@ -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
}
45 changes: 26 additions & 19 deletions pkg/host_cleaning/host_cleanup.go
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
12 changes: 6 additions & 6 deletions pkg/host_cleaning/host_purge.go
Expand Up @@ -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"
)

Expand All @@ -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,
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -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)
}
}
6 changes: 3 additions & 3 deletions pkg/tmp_manager/gc.go
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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{}

Expand Down Expand Up @@ -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))
}
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/tmp_manager/purge.go
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
}
}
Expand Down
22 changes: 0 additions & 22 deletions 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(
Expand Down

0 comments on commit cb51e32

Please sign in to comment.