forked from xorpaul/g10k
-
Notifications
You must be signed in to change notification settings - Fork 0
/
g10k.go
163 lines (145 loc) · 4.75 KB
/
g10k.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
package main
import (
"flag"
"fmt"
"log"
"os"
"strconv"
"sync"
"time"
)
var (
debug bool
verbose bool
info bool
force bool
pfMode bool
config ConfigSettings
wg sync.WaitGroup
mutex sync.Mutex
empty struct{}
syncGitCount int
syncForgeCount int
syncGitTime float64
syncForgeTime float64
cpGitTime float64
cpForgeTime float64
buildtime string
uniqueForgeModules map[string]struct{}
latestForgeModules map[string]string
)
// ConfigSettings contains the key value pairs from the g10k config file
type ConfigSettings struct {
CacheDir string `yaml:"cachedir"`
ForgeCacheDir string
ModulesCacheDir string
EnvCacheDir string
Git struct {
privateKey string `yaml:"private_key"`
username string
}
Sources map[string]Source
Timeout int `yaml:"timeout"`
}
type Source struct {
Remote string
Basedir string
Prefix bool
PrivateKey string `yaml:"private_key"`
}
// Puppetfile contains the key value pairs from the Puppetfile
type Puppetfile struct {
moduleDir string
forgeModules map[string]ForgeModule
gitModules map[string]GitModule
privateKey string
}
type ForgeModule struct {
version string
name string
author string
}
type GitModule struct {
git string
branch string
tag string
commit string
ref string
link string
}
type ForgeResult struct {
needToGet bool
versionNumber string
}
type ExecResult struct {
returnCode int
output string
}
func main() {
var (
configFile = flag.String("config", "", "which config file to use")
envBranchFlag = flag.String("branch", "", "which git branch of the Puppet environment to update, e.g. core_foobar")
pfFlag = flag.Bool("puppetfile", false, "install all modules from Puppetfile in cwd")
forceFlag = flag.Bool("force", false, "purge the Puppet environment directory and do a full sync")
debugFlag = flag.Bool("debug", false, "log debug output, defaults to false")
verboseFlag = flag.Bool("verbose", false, "log verbose output, defaults to false")
infoFlag = flag.Bool("info", false, "log info output, defaults to false")
versionFlag = flag.Bool("version", false, "show build time and version number")
)
flag.Parse()
debug = *debugFlag
verbose = *verboseFlag
info = *infoFlag
force = *forceFlag
pfMode = *pfFlag
if *versionFlag {
fmt.Println("g10k Version 1.0 Build time:", buildtime, "UTC")
os.Exit(0)
}
if len(os.Getenv("VIMRUNTIME")) > 0 {
*configFile = "/home/andpaul/dev/go/src/github.com/xorpaul/g10k/test.yaml"
*envBranchFlag = "invalid_modulename"
debug = true
}
target := ""
if len(*configFile) > 0 {
Debugf("Using as config file: " + *configFile)
config = readConfigfile(*configFile)
target = *configFile
if len(*envBranchFlag) > 0 {
resolvePuppetEnvironment(*envBranchFlag)
target += " with branch " + *envBranchFlag
} else {
resolvePuppetEnvironment("")
}
} else {
if pfMode {
Debugf("Trying to use as Puppetfile: ./Puppetfile")
sm := make(map[string]Source)
sm["cmdlineparam"] = Source{Basedir: "."}
config = ConfigSettings{CacheDir: "/tmp/", ForgeCacheDir: "/tmp/", ModulesCacheDir: "/tmp/", EnvCacheDir: "/tmp/", Sources: sm}
target = "./Puppetfile"
puppetfile := readPuppetfile("./Puppetfile", "")
pfm := make(map[string]Puppetfile)
pfm["cmdlineparam"] = puppetfile
resolvePuppetfile(pfm)
} else {
log.Println("Error: no config file set")
log.Printf("Example call: %s -config test.yaml\n", os.Args[0])
log.Printf("or: %s -puppetfile\n", os.Args[0])
os.Exit(1)
}
}
before := time.Now()
// DEBUG
//pf := make(map[string]Puppetfile)
//pf["core_fullmanaged"] = readPuppetfile("/tmp/core/core_fullmanaged/", "/home/andpaul/dev/go/src/github.com/xorpaul/g10k/portal_envs")
//pf["itodsi_corosync"] = readPuppetfile("/tmp/itodsi/itodsi_corosync/", "/home/andpaul/dev/go/src/github.com/xorpaul/g10k/portal_envs")
//resolvePuppetfile(pf)
//resolveGitRepositories(config)
//resolveForgeModules(configSettings.forge)
//doModuleInstallOrNothing("camptocamp-postfix-1.2.2", "/tmp/g10k/camptocamp-postfix-1.2.2")
//doModuleInstallOrNothing("saz-resolv_conf-latest")
//readModuleMetadata("/tmp/g10k/forge/camptocamp-postfix-1.2.2/metadata.json")
fmt.Println("Synced", target, "with", syncGitCount, "git repositories and", syncForgeCount, "Forge modules in "+strconv.FormatFloat(time.Since(before).Seconds(), 'f', 1, 64)+"s with git ("+strconv.FormatFloat(syncGitTime, 'f', 1, 64)+"s sync, I/O", strconv.FormatFloat(cpGitTime, 'f', 1, 64)+"s) and Forge ("+strconv.FormatFloat(syncForgeTime, 'f', 1, 64)+"s query+download, I/O", strconv.FormatFloat(cpForgeTime, 'f', 1, 64)+"s)")
}