-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
411 lines (330 loc) · 9.15 KB
/
main.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package main
import (
"errors"
"fmt"
"log"
"net"
"net/url"
"strings"
"github.com/idb-project/idbclient"
"github.com/idb-project/idbclient/machine"
"github.com/vmware/govmomi"
"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/property"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/types"
"context"
)
var debug = false
// Properties to retrieve for vms
var props = []string{"name", "capability", "config", "datastore", "environmentBrowser", "guest", "guestHeartbeatStatus", "layout", "layoutEx", "network", "parentVApp", "resourceConfig", "resourcePool", "rootSnapshot", "runtime", "snapshot", "storage", "summary"}
// machineFromVM tries to fill a IDB-machine with values from
// a vsphere virtual machine. The resulting machine should always
// be usable in the IDB, with missing values replaced by sane defaults.
func machineFromVM(ctx context.Context, c *govmomi.Client, vm mo.VirtualMachine, lookup bool, unknownSuffix string, fqdnStrip bool) (*machine.Machine, error) {
m := new(machine.Machine)
m.Os, m.OsRelease = osFromVM(vm)
m.Fqdn = fqdnFromVM(vm, lookup, unknownSuffix, fqdnStrip)
m.Cores = int(vm.Summary.Config.NumCpu)
m.RAM = int(vm.Summary.Config.MemorySizeMB)
m.Diskspace = diskspaceFromVM(vm)
if vm.Guest != nil {
m.Nics = nicsFromVM(vm)
}
pc := property.DefaultCollector(c.Client)
host := mo.HostSystem{}
err := pc.RetrieveOne(ctx, *vm.Summary.Runtime.Host, []string{"name"}, &host)
if err != nil {
return nil, err
}
if debug {
log.Println("Found vm host name:", host.Name)
}
m.Vmhost = host.Name
m.DeviceTypeID = machine.DeviceTypeVirtual
return m, nil
}
// diskspaceFromVM returns the committed space of the guest in bytes.
func diskspaceFromVM(vm mo.VirtualMachine) int {
if vm.Summary.Storage == nil {
return 0
}
return int((vm.Summary.Storage.Committed))
}
// osFromVM tries to extract os and os release information.
// os is selected from guestId and guestFamily in this order.
// os release is guestFullName.
// See: https://pubs.vmware.com/vsphere-60/topic/com.vmware.wssdk.apiref.doc/vim.vm.GuestInfo.html
func osFromVM(vm mo.VirtualMachine) (string, string) {
var os string
var osRelease string
if vm.Guest == nil {
if debug {
log.Println("vm.Guest is nil")
}
return "", ""
}
switch {
case vm.Guest.GuestId != "":
if debug {
log.Println("using vm.Guest.GuestId as os")
}
os = vm.Guest.GuestId
case vm.Guest.GuestFamily != "":
if debug {
log.Println("using vm.Guest.GuestFamily as os")
}
os = vm.Guest.GuestFamily
}
switch {
case vm.Guest.GuestFullName != "":
if debug {
log.Println("using vm.Guest.GuestFullName as os release")
}
osRelease = vm.Guest.GuestFullName
}
return os, osRelease
}
// fqdnFromVM tries to extract a hostname from the VM.
// If the VM guest has a hostname set, it is used.
// When no hostname is set, and lookups are enabled,
// a reverse lookup is performed for each IP of the guest.
// If none of this works, an fqdn in the format
// is returned with the prefix "vsphere.unknown." and name of
// the vm concatenated, eg. "vsphere.unknown.vmname".
func fqdnFromVM(vm mo.VirtualMachine, lookup bool, unknownSuffix string, fqdnStrip bool) string {
var hostName string
if debug {
log.Println("trying to find fqdn")
}
// This shouldn't happen.
if vm.Guest == nil {
if debug {
log.Println("vm.Guest is nil")
}
return fmt.Sprintf("noguest%v", unknownSuffix)
}
switch {
case vm.Guest.HostName != "":
if debug {
log.Println("using vm.Guest.HostName as fqdn")
}
hostName = vm.Guest.HostName
case lookup:
if debug {
log.Println("trying to reverse-lookup fqdn")
}
nics := nicsFromVM(vm)
for _, nic := range nics {
if nic.IPAddress.Addr != "" {
if debug {
log.Printf("reverse lookup for: %v\n", nic.IPAddress.Addr)
}
nicHostName, err := net.LookupAddr(nic.IPAddress.Addr)
if err != nil || len(nicHostName) < 1 {
if debug {
log.Printf("no hostname found for %v", nic.IPAddress.Addr)
}
continue
}
if debug {
log.Printf("found hostname %v for %v\n", nicHostName[0], nic.IPAddress.Addr)
}
hostName = nicHostName[0]
}
}
}
if hostName == "" {
if debug {
log.Printf("no fqdn found, falling back to: %v\n", vm.Name)
}
hostName = vm.Name
}
// check for valid fqdn (must contain a dot) and append unknownSuffix if invalid
if strings.IndexRune(hostName, '.') == -1 {
if debug {
log.Println("fqdn doesn't contain '.', appending unknownSuffix")
}
hostName = fmt.Sprintf("%v%v", hostName, unknownSuffix)
}
if debug {
log.Printf("using fqdn: %v", hostName)
}
// remove all characters which are invalid for fqdns.
if fqdnStrip {
hostName = strings.Map(hostNameMap, hostName)
if debug {
log.Printf("removed invalid characters from fqdn: %v", hostName)
}
}
return hostName
}
// hostNameMap converts to lowercase and removes everything except
// 'a'-'z', '0'-'9', '.' and '-'
func hostNameMap(r rune) rune {
switch {
case r >= 'A' && r <= 'Z':
r += 'a' - 'A'
return r
case r >= 'a' && r <= 'z':
return r
case r >= '0' && r <= '9':
return r
case r == '.':
return r
case r == '-':
return r
default:
return -1
}
}
// nicsFromVM tries to extract configured network interfaces and their
// ip address and netmask.
func nicsFromVM(vm mo.VirtualMachine) []machine.Nic {
nics := make([]machine.Nic, 0)
if vm.Guest == nil {
if debug {
log.Println("vm.Guest is nil")
}
return nil
}
if vm.Guest.Net == nil {
if debug {
log.Println("vm.Guest.Net is nil")
}
return nil
}
for i, vmNic := range vm.Guest.Net {
if vmNic.IpConfig == nil {
if debug {
log.Println("vmNic.IpConfig is nil")
}
continue
}
if vmNic.IpConfig.IpAddress == nil {
if debug {
log.Println("vmNic.IpConfig.IpAddress is nil")
}
continue
}
for _, addr := range vmNic.IpConfig.IpAddress {
ipString := addr.IpAddress
ip := net.ParseIP(ipString)
if ip == nil {
if debug {
log.Printf("%v is not a valid ip address\n", ipString)
}
continue
}
switch {
case ip.To4() != nil:
net, err := suffixToNetmask(addr.PrefixLength)
if err != nil {
net = "unknown"
}
if debug {
log.Printf("found nic with v4 address: %v network: %v\n", ipString, net)
}
nics = append(nics, machine.Nic{Name: fmt.Sprintf("unknown%v", i), IPAddress: machine.IPAddress{Addr: ipString, Netmask: net}})
case ip.To16() != nil:
net := fmt.Sprintf("%d", addr.PrefixLength)
if debug {
log.Printf("found nic with v6 address: %v/%v\n", ipString, net)
}
nics = append(nics, machine.Nic{Name: fmt.Sprintf("unknown%v", i), IPAddress: machine.IPAddress{AddrV6: ipString, NetmaskV6: net}})
default:
continue
}
}
}
return nics
}
// prefixToNetmask converts a IPv4 suffix to a netmask.
// Eg. 24 will be converted to 255.255.255.0
// An error is returned if the suffix is lower than 0 or larger than 32.
func suffixToNetmask(suffix int32) (string, error) {
if suffix < 0 || suffix > 32 {
return "", errors.New(fmt.Sprintf("Invalid suffix: %v", suffix))
}
mask := uint32(0xffffffff) >> uint32(32-suffix) << uint32(32-suffix)
return fmt.Sprintf("%v.%v.%v.%v", mask&0xFF000000>>24, mask&0x00FF0000>>16, mask&0x0000FF00>>8, mask&0xFF), nil
}
func getVMs(ctx context.Context, c *govmomi.Client, props []string) ([]mo.VirtualMachine, error) {
/* ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Connect and log in to ESX or vCenter
c, err := govmomi.NewClient(ctx, u, true)
if err != nil {
return nil, err
}
*/
f := find.NewFinder(c.Client, true)
// Find one and only datacenter
dc, err := f.DefaultDatacenter(ctx)
if err != nil {
return nil, err
}
// Make future calls local to this datacenter
f.SetDatacenter(dc)
// Find virtual machines in datacenter
vmList, err := f.VirtualMachineList(ctx, "*")
if err != nil {
return nil, err
}
pc := property.DefaultCollector(c.Client)
// Convert datastores into list of references
var refs []types.ManagedObjectReference
for _, vm := range vmList {
refs = append(refs, vm.Reference())
}
// Retrieve name property for all vms
var vms []mo.VirtualMachine
err = pc.Retrieve(ctx, refs, props, &vms)
if err != nil {
return nil, err
}
return vms, nil
}
func main() {
c, err := loadConfig(*configFile)
if err != nil {
log.Fatal(err)
}
debug = c.Debug
i, err := idbclient.NewIdb(c.IdbUrl, c.IdbToken, c.InsecureSkipVerify)
if err != nil {
log.Fatal(err)
}
i.Debug = c.Debug
vmwareUrl, err := url.Parse(c.VmwareUrl)
if err != nil {
log.Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Connect and log in to ESX or vCenter
vsc, err := govmomi.NewClient(ctx, vmwareUrl, true)
if err != nil {
log.Fatal(err)
}
vms, err := getVMs(ctx, vsc, props)
if err != nil {
log.Fatal(err)
}
for _, vm := range vms {
x, err := machineFromVM(ctx, vsc, vm, c.Lookup, c.UnknownSuffix, c.FqdnStrip)
if err != nil {
log.Fatal(err)
}
if !*dryrun {
_, err := i.UpdateMachine(x, c.Create)
if err != nil {
log.Fatal(err)
}
}
if c.Debug {
log.Printf("VMware machine:\n%#v\n", vm)
log.Printf("IDB machine:\n%#v\n", x)
}
}
}