forked from xorpaul/g10k
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stale.go
124 lines (112 loc) · 3.89 KB
/
stale.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 main
import (
"os"
"path/filepath"
"strings"
"github.com/yargevad/filepathx"
)
func purgeUnmanagedContent(allBasedirs map[string]bool, allEnvironments map[string]bool) {
if !stringSliceContains(config.PurgeLevels, "deployment") {
if !stringSliceContains(config.PurgeLevels, "environment") {
// nothing allowed to purge
return
}
}
for source, sa := range config.Sources {
prefix := resolveSourcePrefix(source, sa)
if len(environmentParam) > 0 {
if !strings.HasPrefix(environmentParam, prefix) {
Debugf("Skipping purging unmanaged content for source '" + source + "', because -environment parameter is set to " + environmentParam)
continue
}
}
// Clean up unknown environment directories
if len(branchParam) == 0 {
for basedir := range allBasedirs {
globPath := filepath.Join(basedir, prefix+"*")
Debugf("Glob'ing with path " + globPath)
environments, _ := filepath.Glob(globPath)
whitelistEnvironments := []string{}
if len(config.DeploymentPurgeWhitelist) > 0 {
for _, wlpattern := range config.DeploymentPurgeWhitelist {
whitelistGlobPath := filepath.Join(basedir, wlpattern)
Debugf("deployment_purge_whitelist Glob'ing with path " + whitelistGlobPath)
we, _ := filepath.Glob(whitelistGlobPath)
whitelistEnvironments = append(whitelistEnvironments, we...)
}
}
for _, env := range environments {
envPath := strings.Split(env, "/")
envName := envPath[len(envPath)-1]
if len(environmentParam) > 0 {
if envName != environmentParam {
Debugf("Skipping purging unmanaged content for Puppet environment '" + envName + "', because -environment parameter is set to " + environmentParam)
continue
}
}
if stringSliceContains(config.PurgeLevels, "environment") {
if allEnvironments[envName] {
checkForStaleContent(env)
}
}
if stringSliceContains(config.PurgeLevels, "deployment") {
Debugf("Checking if environment should exist: " + envName)
if allEnvironments[envName] {
Debugf("Not purging environment " + envName)
} else if stringSliceContains(whitelistEnvironments, filepath.Join(basedir, envName)) {
Debugf("Not purging environment " + envName + " due to deployment_purge_whitelist match")
} else {
Infof("Removing unmanaged environment " + envName)
if !dryRun {
purgeDir(filepath.Join(basedir, envName), "purgeStaleContent()")
}
}
}
}
}
} else {
if stringSliceContains(config.PurgeLevels, "environment") {
// check for purgeable content inside -branch folder
checkForStaleContent(filepath.Join(sa.Basedir, prefix+branchParam))
}
}
}
}
func checkForStaleContent(workDir string) {
// add purge whitelist
if len(config.PurgeWhitelist) > 0 {
Debugf("interpreting purge whitelist globs: " + strings.Join(config.PurgeWhitelist, " "))
for _, wlItem := range config.PurgeWhitelist {
globPath := filepath.Join(workDir, wlItem)
Debugf("Glob'ing with purge whitelist glob " + globPath)
wlPaths, _ := filepathx.Glob(globPath)
Debugf("additional purge whitelist items: " + strings.Join(wlPaths, " "))
desiredContent = append(desiredContent, wlPaths...)
}
}
checkForStaleContent := func(path string, info os.FileInfo, err error) error {
//Debugf("filepath.Walk'ing found path: " + path)
stale := true
for _, desiredFile := range desiredContent {
for _, unchangedModuleDir := range unchangedModuleDirs {
if strings.HasPrefix(path, unchangedModuleDir) {
stale = false
break
}
}
if path == desiredFile || path == workDir {
stale = false
break
}
}
if stale {
Infof("Removing unmanaged path " + path)
purgeDir(path, "checkForStaleContent()")
}
return nil
}
c := make(chan error)
Debugf("filepath.Walk'ing directory " + workDir)
go func() { c <- filepath.Walk(workDir, checkForStaleContent) }()
<-c // Walk done
}