This repository has been archived by the owner on Sep 7, 2022. It is now read-only.
/
inventory.go
259 lines (234 loc) · 6.72 KB
/
inventory.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
251
252
253
254
255
256
257
258
259
package henchman
import (
"fmt"
"io/ioutil"
"strings"
"gopkg.in/yaml.v2"
)
type InventoryConfig map[string]string
type InventoryInterface interface {
Load(ic InventoryConfig) (Inventory, error)
}
//type Inventory map[string][]*Machine
type Inventory struct {
//GroupHosts map[string][]*Machine
Groups map[string]HostGroup `yaml:"groups"`
HostVars map[string]VarsMap `yaml:"host_vars"`
GlobalVars VarsMap `yaml:"global_vars"`
}
type HostGroup struct {
Hosts []string `yaml:"hosts"`
Vars VarsMap `yaml:"vars"`
}
func (inv Inventory) Count() int {
count := 0
for _, hostGroup := range inv.Groups {
count += len(hostGroup.Hosts)
}
return count
}
// FIXME: Have a way to provide specifics
type YAMLInventory struct {
Groups map[string]HostGroup `yaml:"groups"`
HostVars map[string]VarsMap `yaml:"host_vars"`
GlobalVars VarsMap `yaml:"global_vars"`
}
func (yi *YAMLInventory) Load(ic InventoryConfig) (Inventory, error) {
fname, present := ic["path"]
if !present {
return Inventory{}, fmt.Errorf("Missing 'path' in the config")
}
buf, err := ioutil.ReadFile(fname)
if err != nil {
return Inventory{}, HenchErr(err, map[string]interface{}{
"file": fname,
"solution": "make sure directory exists, correct permissions, or is not corrupted",
}, "While Reading File")
}
err = yaml.Unmarshal(buf, &yi)
if err != nil {
return Inventory{}, HenchErr(err, map[string]interface{}{
"file": fname,
"solution": "Make sure inventory follows proper formatting. Also check for tabs when there should be spaces.",
}, "While unmarshalling inventory")
}
if yi.Groups == nil {
return Inventory{}, HenchErr(fmt.Errorf("Groups field is required."), map[string]interface{}{
"file": fname,
"solution": "Refer to the wiki for proper formatting.",
}, "")
}
for key, val := range yi.Groups {
if key == "hosts" {
return Inventory{}, HenchErr(fmt.Errorf("'hosts' is not a valid group name"), map[string]interface{}{
"file": fname,
"solution": "Change a group name away from hosts",
}, "")
}
if val.Hosts == nil {
return Inventory{}, HenchErr(fmt.Errorf("%v requires a hosts field.", key), map[string]interface{}{
"file": fname,
"group": key,
"solution": "Refet to the wiki for proper formatting.",
}, "")
}
}
iv := &Inventory{}
iv.HostVars = yi.HostVars
iv.Groups = yi.Groups
iv.GlobalVars = yi.GlobalVars
if iv.GlobalVars == nil {
iv.GlobalVars = make(VarsMap)
}
return *iv, nil
}
/**
* gets the groups under hosts section from plan file
*/
func GetInventoryGroups(planBuf []byte) ([]string, error) {
hostsProxy := struct {
Groups []string `yaml:"hosts"`
}{}
err := yaml.Unmarshal(planBuf, &hostsProxy)
if err != nil {
return nil, HenchErr(err, map[string]interface{}{
"solution": "Check if hosts section exists",
}, "While unmarshalling hosts section")
}
return hostsProxy.Groups, nil
}
/**
* Returns a filtered inventory depending on which groups are specified
* Keeps the groups specified in the filtered inventory
*/
func (inv *Inventory) GetInventoryForGroups(groups []string) Inventory {
// FIXME: Support globbing in the groups
// No groups? No problem. Just return the full inventory
// return fullInventory
if len(groups) == 0 {
return *inv
}
filtered := Inventory{}
filtered.Groups = make(map[string]HostGroup)
//filtered.HostVars = fullInventory.HostVars
if len(groups) == 1 &&
(groups[0] == "localhost" || groups[0] == "127.0.0.1") {
filtered.Groups["localhost"] = HostGroup{Hosts: []string{"localhost"},
Vars: nil}
} else {
for _, group := range groups {
machines, present := inv.Groups[group]
if present {
filtered.Groups[group] = machines
}
}
}
filtered.HostVars = inv.HostVars
filtered.GlobalVars = inv.GlobalVars
return filtered
}
/**
* Sets all_hosts, and groups with attached hosts lists
* These can be accessed in vars.inv.whatevs
*/
func (inv *Inventory) SetGlobalVarsFromInventoryGroups(groups map[string]HostGroup) {
var all_hosts []string
globalVars := make(map[string]interface{})
duplicates := make(map[string]bool)
for group, hostGroup := range groups {
globalVars[group] = hostGroup.Hosts
for _, host := range hostGroup.Hosts {
if _, present := duplicates[host]; !present {
duplicates[host] = true
all_hosts = append(all_hosts, host)
}
}
}
globalVars["all_hosts"] = all_hosts
inv.GlobalVars["inv"] = globalVars
}
func (inv *Inventory) GetMachines(tc TransportConfig) ([]*Machine, error) {
var machines []*Machine
machineSet := make(map[string]bool)
for _, hostGroup := range inv.Groups {
for _, hostname := range hostGroup.Hosts {
if _, present := machineSet[hostname]; !present {
machine := &Machine{}
machineSet[hostname] = true
machine.Hostname = hostname
machine.Vars = hostGroup.Vars
if machine.Vars == nil {
machine.Vars = make(VarsMap)
}
machines = append(machines, machine)
} else {
//machine part of multiple groups
//update vars if any
//latter group's vars overrides prev. groups vars
for _, machine := range machines {
if machine.Hostname == hostname {
MergeMap(hostGroup.Vars, machine.Vars, true)
}
//get second groups henchman tc vars
}
}
}
}
// gets henchman specific vars from global_vars
globalInvHenchmanVars := GetHenchmanVars(inv.GlobalVars)
// update hostvars
for _, machine := range machines {
for hostname, vars := range inv.HostVars {
if machine.Hostname == hostname {
MergeMap(vars, machine.Vars, true)
}
}
// now open ssh connection for each machine
tcCurr := make(TransportConfig)
tcCurr["hostname"] = machine.Hostname
for k, v := range tc {
tcCurr[k] = v
}
// gets henchman specific vars from each machine and merges with global
henchmanVars := GetHenchmanVars(machine.Vars)
MergeMap(globalInvHenchmanVars, henchmanVars, false)
for k, v := range henchmanVars {
tcCurr[k] = v.(string)
}
Debug(map[string]interface{}{
"host": machine.Hostname,
"config": tcCurr,
}, "Transport Config for machine")
// FIXME: This is frigging wrong
// See #47
if machine.Hostname != "localhost" {
ssht, err := NewSSH(&tcCurr)
if err != nil {
return nil, err
}
machine.Transport = ssht
} else if machine.Hostname == "localhost" {
loct, err := NewLocal(&tcCurr)
if err != nil {
return nil, err
}
machine.Transport = loct
} else {
machine.Transport = nil
}
}
return machines, nil
}
/**
* Gets any vars with the prefix henchman
*/
func GetHenchmanVars(vars VarsMap) VarsMap {
henchmanVars := VarsMap{}
for k, v := range vars {
if strings.Contains(k, HENCHMAN_PREFIX) {
parts := strings.Split(k, HENCHMAN_PREFIX)
henchmanVars[parts[len(parts)-1]] = v
}
}
return henchmanVars
}