-
Notifications
You must be signed in to change notification settings - Fork 51
/
stale.go
87 lines (77 loc) · 2.69 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
package main
import (
"path/filepath"
"strings"
)
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 {
// fmt.Printf("source: %+v\n", sa)
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)
allowlistEnvironments := []string{}
if len(config.DeploymentPurgeAllowList) > 0 {
for _, wlpattern := range config.DeploymentPurgeAllowList {
allowlistGlobPath := filepath.Join(basedir, wlpattern)
Debugf("deployment_purge_allowlist Glob'ing with path " + allowlistGlobPath)
we, _ := filepath.Glob(allowlistGlobPath)
allowlistEnvironments = append(allowlistEnvironments, 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, "deployment") {
Debugf("Checking if environment should exist: " + env)
if allEnvironments[env] {
Debugf("Not purging environment " + env)
} else if stringSliceContains(allowlistEnvironments, env) {
Debugf("Not purging environment " + env + " due to deployment_purge_allowlist match")
} else {
Infof("Removing unmanaged environment " + env)
if !dryRun {
purgeDir(env, "purgeStaleContent()")
}
}
}
}
}
}
}
}
func purgeControlRepoExceptModuledir(dir string, moduleDir string) {
moduleDir = filepath.Join(dir, moduleDir)
globPath := filepath.Join(dir, "*")
Debugf("Glob'ing with path " + globPath)
folders, _ := filepath.Glob(globPath)
for _, folder := range folders {
if folder == moduleDir || strings.HasPrefix(folder, moduleDir) {
continue
} else {
Debugf("deleting " + folder)
purgeDir(folder, "purgeControlRepoExceptModuledir")
}
}
}