/
host_purge.go
124 lines (98 loc) · 3.26 KB
/
host_purge.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
package host_cleaning
import (
"context"
"fmt"
"os"
"runtime"
"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/werf"
)
type HostPurgeOptions struct {
DryRun bool
RmContainersThatUseWerfImages bool
}
func HostPurge(ctx context.Context, containerBackend container_backend.ContainerBackend, options HostPurgeOptions) error {
commonOptions := CommonOptions{
RmiForce: true,
RmForce: true,
RmContainersThatUseWerfImages: options.RmContainersThatUseWerfImages,
DryRun: options.DryRun,
}
if err := logboek.Context(ctx).LogProcess("Running werf docker containers purge").DoError(func() error {
if err := werfContainersFlushByFilterSet(ctx, filters.NewArgs(), commonOptions); err != nil {
return err
}
return nil
}); err != nil {
return err
}
if err := logboek.Context(ctx).LogProcess("Running werf docker images purge").DoError(func() error {
filterSet := filters.NewArgs()
filterSet.Add("label", image.WerfLabel)
if err := werfImagesFlushByFilterSet(ctx, filterSet, commonOptions); err != nil {
return err
}
localManagedImageRecordImageNameFormat := "werf-managed-images/%s" // legacy
filterSet = filters.NewArgs()
filterSet.Add("reference", fmt.Sprintf(localManagedImageRecordImageNameFormat, "*"))
if err := werfImagesFlushByFilterSet(ctx, filterSet, commonOptions); err != nil {
return err
}
localMetadataImageRecordImageNameFormat := "werf-images-metadata-by-commit/%s" // legacy
filterSet = filters.NewArgs()
filterSet.Add("reference", fmt.Sprintf(localMetadataImageRecordImageNameFormat, "*"))
if err := werfImagesFlushByFilterSet(ctx, filterSet, commonOptions); err != nil {
return err
}
return nil
}); err != nil {
return err
}
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, containerBackend)
}); err != nil {
return err
}
if err := logboek.Context(ctx).LogProcess("Deleting stapel").DoError(func() error {
return deleteStapel(ctx, commonOptions.DryRun)
}); err != nil {
return fmt.Errorf("stapel delete failed: %w", err)
}
return nil
}
func deleteStapel(ctx context.Context, dryRun bool) error {
if dryRun {
return nil
}
if err := stapel.Purge(ctx); err != nil {
return err
}
return nil
}
func purgeHomeWerfFiles(ctx context.Context, dryRun bool, containerBackend container_backend.ContainerBackend) error {
pathsToRemove := []string{werf.GetServiceDir(), werf.GetLocalCacheDir(), werf.GetSharedContextDir()}
for _, path := range pathsToRemove {
logboek.Context(ctx).LogLn(path)
}
if dryRun {
return nil
}
if runtime.GOOS == "windows" {
for _, path := range pathsToRemove {
if err := os.RemoveAll(path); err != nil {
return err
}
}
return nil
} else {
return containerBackend.RemoveHostDirs(ctx, werf.GetHomeDir(), pathsToRemove)
}
}