forked from xorpaul/g10k
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuppetfile.go
250 lines (233 loc) · 8.26 KB
/
puppetfile.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package main
import (
"io/ioutil"
"os"
"strings"
"sync"
"github.com/xorpaul/uiprogress"
)
func resolvePuppetEnvironment(envBranch string) {
allPuppetfiles := make(map[string]Puppetfile)
for source, sa := range config.Sources {
wg.Add(1)
go func(source string, sa Source) {
defer wg.Done()
if force {
createOrPurgeDir(sa.Basedir, "resolvePuppetEnvironment()")
}
sa.Basedir = checkDirAndCreate(sa.Basedir, "basedir for source "+source)
Debugf("Puppet environment: " + source + " (remote=" + sa.Remote + ", basedir=" + sa.Basedir + ", private_key=" + sa.PrivateKey + ", prefix=" + sa.Prefix + ")")
if len(sa.PrivateKey) > 0 {
if _, err := os.Stat(sa.PrivateKey); err != nil {
Fatalf("resolvePuppetEnvironment(): could not find SSH private key " + sa.PrivateKey + "error: " + err.Error())
}
}
workDir := config.EnvCacheDir + source + ".git"
// check if sa.Basedir exists
checkDirAndCreate(sa.Basedir, "basedir")
//if !strings.Contains(source, "hiera") && !strings.Contains(source, "files") {
// gitKey = sa.PrivateKey
//}
if success := doMirrorOrUpdate(sa.Remote, workDir, sa.PrivateKey, true); success {
// get all branches
er := executeCommand("git --git-dir "+workDir+" branch", config.Timeout, false)
branches := strings.Split(strings.TrimSpace(er.output), "\n")
for _, branch := range branches {
branch = strings.TrimLeft(branch, "* ")
if strings.HasPrefix(branch, "tmp/") && strings.HasSuffix(branch, "/head") || (len(envBranch) > 0 && branch != envBranch) {
Debugf("Skipping branch " + branch)
continue
}
wg.Add(1)
go func(branch string) {
defer wg.Done()
if len(branch) != 0 {
Debugf("Resolving branch: " + branch)
targetDir := sa.Basedir + sa.Prefix + "_" + strings.Replace(branch, "/", "_", -1) + "/"
if sa.Prefix == "false" {
targetDir = sa.Basedir + strings.Replace(branch, "/", "_", -1) + "/"
} else if sa.Prefix == "true" {
targetDir = sa.Basedir + source + "_" + strings.Replace(branch, "/", "_", -1) + "/"
}
syncToModuleDir(workDir, targetDir, branch, false, false)
if !fileExists(targetDir + "Puppetfile") {
Debugf("Skipping branch " + source + "_" + branch + " because " + targetDir + "Puppetfile does not exitst")
} else {
puppetfile := readPuppetfile(targetDir+"Puppetfile", sa.PrivateKey, source, sa.ForceForgeVersions)
puppetfile.workDir = targetDir
mutex.Lock()
allPuppetfiles[source+"_"+branch] = puppetfile
mutex.Unlock()
}
}
}(branch)
}
}
}(source, sa)
}
wg.Wait()
//fmt.Println("allPuppetfiles: ", allPuppetfiles, len(allPuppetfiles))
//fmt.Println("allPuppetfiles[0]: ", allPuppetfiles["postinstall"])
resolvePuppetfile(allPuppetfiles)
//// sync to basedir
//for _, branch := range branches {
// if len(branch) != 0 {
// Debugf("Syncing branch: " + branch)
// // TODO if sa.Prefix != true
// if !strings.Contains(branch, "hiera") && !strings.Contains(branch, "files") {
// //puppetfile := readPuppetfile(targetDir)
// }
// }
//}
}
func resolvePuppetfile(allPuppetfiles map[string]Puppetfile) {
var wg sync.WaitGroup
exisitingModuleDirs := make(map[string]struct{})
uniqueGitModules := make(map[string]GitModule)
uniqueForgeModules := make(map[string]ForgeModule)
// if we made it this far initialize the global maps
latestForgeModules.m = make(map[string]string)
for env, pf := range allPuppetfiles {
Debugf("Resolving " + env)
//fmt.Println(pf)
for _, gitModule := range pf.gitModules {
gitModule.privateKey = pf.privateKey
if _, ok := uniqueGitModules[gitModule.git]; !ok {
uniqueGitModules[gitModule.git] = gitModule
}
}
for forgeModuleName, fm := range pf.forgeModules {
//fmt.Println("Found Forge module ", forgeModuleName, " with version", fm.version)
fm.baseUrl = pf.forgeBaseURL
fm.cacheTtl = pf.forgeCacheTtl
forgeModuleName = strings.Replace(forgeModuleName, "/", "-", -1)
if _, ok := uniqueForgeModules[forgeModuleName+"-"+fm.version]; !ok {
uniqueForgeModules[forgeModuleName+"-"+fm.version] = fm
}
}
}
if !debug && !verbose && !info {
uiprogress.Start()
}
//fmt.Println(uniqueGitModules)
var wgResolve sync.WaitGroup
wgResolve.Add(2)
go func() {
defer wgResolve.Done()
resolveGitRepositories(uniqueGitModules)
}()
go func() {
defer wgResolve.Done()
resolveForgeModules(uniqueForgeModules)
}()
wgResolve.Wait()
//log.Println(config.Sources["cmdlineparam"])
for env, pf := range allPuppetfiles {
Debugf("Syncing " + env + " with workDir " + pf.workDir)
basedir := checkDirAndCreate(pf.workDir, "basedir 2 for source "+pf.source)
moduleDir := pf.workDir + pf.moduleDir
var envBranch string
if pfMode {
moduleDir = basedir + "/" + pf.moduleDir
} else {
envBranch = strings.Replace(env, pf.source+"_", "", 1)
}
if strings.HasPrefix(pf.moduleDir, "/") {
moduleDir = pf.moduleDir
}
if force {
createOrPurgeDir(moduleDir, "resolvePuppetfile()")
moduleDir = checkDirAndCreate(moduleDir, "moduleDir for source "+pf.source)
} else {
moduleDir = checkDirAndCreate(moduleDir, "moduleDir for "+pf.source)
exisitingModuleDirsFI, _ := ioutil.ReadDir(moduleDir)
mutex.Lock()
for _, exisitingModuleDir := range exisitingModuleDirsFI {
exisitingModuleDirs[moduleDir+exisitingModuleDir.Name()] = empty
}
mutex.Unlock()
}
for gitName, gitModule := range pf.gitModules {
wg.Add(1)
go func(gitName string, gitModule GitModule) {
defer wg.Done()
//fmt.Println(gitModule)
//fmt.Println("source: " + source)
targetDir := moduleDir + "/" + gitName
//fmt.Println("targetDir: " + targetDir)
tree := "master"
if len(gitModule.branch) > 0 {
tree = gitModule.branch
} else if len(gitModule.commit) > 0 {
tree = gitModule.commit
} else if len(gitModule.tag) > 0 {
tree = gitModule.tag
} else if len(gitModule.ref) > 0 {
tree = gitModule.ref
} else if gitModule.link {
if pfMode {
if len(os.Getenv("g10k_branch")) > 0 {
tree = os.Getenv("g10k_branch")
} else {
Fatalf("resolvePuppetfile(): found module " + gitName + " with module link mode enabled and g10k in Puppetfile mode which is not supported, as I can not detect the environment branch of the Puppetfile. You can explicitly set the module link branch you want to use in Puppetfile mode by setting the environment variable 'g10k_branch'")
}
} else {
tree = envBranch
}
}
success := false
//fmt.Println(gitModule.fallback)
moduleCacheDir := config.ModulesCacheDir + strings.Replace(strings.Replace(gitModule.git, "/", "_", -1), ":", "-", -1)
if len(gitModule.fallback) > 0 {
success = syncToModuleDir(moduleCacheDir, targetDir, tree, true, gitModule.ignoreUnreachable)
if !success {
for i, fallbackBranch := range gitModule.fallback {
if i == len(gitModule.fallback)-1 {
// last try
gitModule.ignoreUnreachable = true
}
success = syncToModuleDir(moduleCacheDir, targetDir, fallbackBranch, true, gitModule.ignoreUnreachable)
if success {
break
}
}
}
} else {
success = syncToModuleDir(moduleCacheDir, targetDir, tree, gitModule.ignoreUnreachable, gitModule.ignoreUnreachable)
}
// remove this module from the exisitingModuleDirs map
mutex.Lock()
if _, ok := exisitingModuleDirs[moduleDir+gitName]; ok {
delete(exisitingModuleDirs, moduleDir+gitName)
}
mutex.Unlock()
}(gitName, gitModule)
}
for forgeModuleName, fm := range pf.forgeModules {
wg.Add(1)
go func(forgeModuleName string, fm ForgeModule) {
defer wg.Done()
syncForgeToModuleDir(forgeModuleName, fm, moduleDir)
// remove this module from the exisitingModuleDirs map
mutex.Lock()
if _, ok := exisitingModuleDirs[moduleDir+fm.name]; ok {
delete(exisitingModuleDirs, moduleDir+fm.name)
}
mutex.Unlock()
}(forgeModuleName, fm)
}
}
wg.Wait()
//fmt.Println(uniqueForgeModules)
if len(exisitingModuleDirs) > 0 {
for d := range exisitingModuleDirs {
Debugf("Removing unmanaged file " + d)
if err := os.RemoveAll(d); err != nil {
Debugf("Error while trying to remove unmanaged file " + d)
}
}
}
if !debug && !verbose && !info {
uiprogress.Stop()
}
}