forked from vmware/vic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.go
440 lines (376 loc) · 12.4 KB
/
validate.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// Copyright 2016-2017 VMware, Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package vicadmin
import (
"context"
"fmt"
"html"
"html/template"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"path"
"sort"
"strings"
"sync"
"time"
log "github.com/Sirupsen/logrus"
"github.com/docker/docker/opts"
"github.com/vishvananda/netlink"
"github.com/vmware/govmomi/property"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/types"
"github.com/vmware/vic/lib/config"
"github.com/vmware/vic/lib/guest"
"github.com/vmware/vic/lib/install/validate"
"github.com/vmware/vic/lib/tether/shared"
"github.com/vmware/vic/pkg/trace"
"github.com/vmware/vic/pkg/version"
"github.com/vmware/vic/pkg/vsphere/session"
"github.com/vmware/vic/pkg/vsphere/vm"
)
type Validator struct {
Hostname string
Version string
FirewallStatus template.HTML
FirewallIssues template.HTML
LicenseStatus template.HTML
LicenseIssues template.HTML
NetworkStatus template.HTML
NetworkIssues template.HTML
StorageRemaining template.HTML
HostIP string
DockerPort string
VCHStatus template.HTML
VCHIssues template.HTML
VCHReachable bool
SystemTime string
}
const (
GoodStatus = template.HTML(`<i class="icon-ok"></i>`)
BadStatus = template.HTML(`<i class="icon-attention"></i>`)
DefaultVCHName = ` `
)
func GetMgmtIP() net.IPNet {
var mgmtIP net.IPNet
// management alias may not be present, try others if not found
link := LinkByOneOfNameOrAlias(config.ManagementNetworkName, "public", "client")
if link == nil {
log.Error("unable to find any interfaces when searching for mgmt IP")
return mgmtIP
}
addrs, err := netlink.AddrList(link, netlink.FAMILY_V4)
if err != nil {
log.Errorf("error getting address list: %s", err)
return mgmtIP
}
if len(addrs) == 0 {
log.Warnf("no addresses on interface when searching for mgmt IP")
return mgmtIP
}
if len(addrs) > 1 {
log.Warnf("multiple addresses on interface when searching for mgmt IP, using first")
}
return *addrs[0].IPNet
}
func LinkByOneOfNameOrAlias(name ...string) netlink.Link {
for _, n := range name {
// #nosec: Errors unhandled.
if l, _ := netlink.LinkByName(n); l != nil {
return l
}
// #nosec: Errors unhandled.
if l, _ := netlink.LinkByAlias(n); l != nil {
return l
}
}
return nil
}
func NewValidator(ctx context.Context, vch *config.VirtualContainerHostConfigSpec, sess *session.Session) *Validator {
defer trace.End(trace.Begin(""))
log.Infof("Creating new validator")
v := &Validator{}
if version.Version != "" {
v.Version = version.Version
}
log.Infof("Setting version to %s", v.Version)
if err := v.GetVCHName(ctx, sess); err != nil {
log.Errorf("Failed to obtain the VCH name: %s", err)
}
// System time
v.SystemTime = time.Now().Format(time.UnixDate)
if sess == nil {
// We can't connect to vSphere
v.VCHReachable = false
v.FirewallStatus = BadStatus
v.FirewallIssues = template.HTML("")
v.LicenseStatus = BadStatus
v.LicenseIssues = template.HTML("")
} else {
v.VCHReachable = true
// Firewall status check
// #nosec: Errors unhandled.
v2, _ := validate.CreateFromVCHConfig(ctx, vch, sess)
mgmtIP := GetMgmtIP()
log.Infof("Using management IP %s for firewall check", mgmtIP)
fwStatus := v2.CheckFirewallForTether(ctx, mgmtIP)
v2.FirewallCheckOutput(ctx, fwStatus)
firewallIssues := v2.GetIssues()
if len(firewallIssues) == 0 {
v.FirewallStatus = GoodStatus
v.FirewallIssues = template.HTML("")
} else {
v.FirewallStatus = BadStatus
for _, err := range firewallIssues {
// #nosec: this method will not auto-escape HTML. Verify data is well formed.
v.FirewallIssues = template.HTML(fmt.Sprintf("%s<span class=\"error-message\">%s</span>\n", v.FirewallIssues, err))
}
}
// License status check
v2.ClearIssues()
v2.CheckLicense(ctx)
licenseIssues := v2.GetIssues()
if len(licenseIssues) == 0 {
v.LicenseStatus = GoodStatus
v.LicenseIssues = template.HTML("")
} else {
v.LicenseStatus = BadStatus
for _, err := range licenseIssues {
// #nosec: this method will not auto-escape HTML. Verify data is well formed.
v.LicenseIssues = template.HTML(fmt.Sprintf("%s<span class=\"error-message\">%s</span>\n", v.LicenseIssues, err))
}
}
}
log.Infof("FirewallStatus set to: %s", v.FirewallStatus)
log.Infof("FirewallIssues set to: %s", v.FirewallIssues)
log.Infof("LicenseStatus set to: %s", v.LicenseStatus)
log.Infof("LicenseIssues set to: %s", v.LicenseIssues)
// Network Connection Check
hosts := []string{
"http://google.com",
"https://docker.io",
}
nwErrors := []error{}
// create a http client with a custom transport using the proxy from env vars
client := &http.Client{Timeout: 10 * time.Second}
// priority given to https proxies
proxy := os.Getenv("VICADMIN_HTTPS_PROXY")
if proxy == "" {
proxy = os.Getenv("VICADMIN_HTTP_PROXY")
}
if proxy != "" {
url, err := url.Parse(proxy)
if err != nil {
nwErrors = append(nwErrors, err)
} else {
client.Transport = &http.Transport{Proxy: http.ProxyURL(url)}
}
}
// perform the wan check
var wg sync.WaitGroup
wg.Add(len(hosts))
errs := make(chan error, len(hosts))
for _, host := range hosts {
go func(host string) {
defer wg.Done()
log.Infof("Getting %s", host)
_, err := client.Get(host)
if err != nil {
errs <- err
}
}(host)
}
wg.Wait()
close(errs)
for err := range errs {
nwErrors = append(nwErrors, err)
}
if len(nwErrors) > 0 {
v.NetworkStatus = BadStatus
for _, err := range nwErrors {
// #nosec: this method will not auto-escape HTML. Verify data is well formed.
v.NetworkIssues = template.HTML(fmt.Sprintf("%s<span class=\"error-message\">%s</span>\n", v.NetworkIssues, err))
}
} else {
v.NetworkStatus = GoodStatus
v.NetworkIssues = template.HTML("")
}
log.Infof("NetworkStatus set to: %s", v.NetworkStatus)
log.Infof("NetworkIssues set to: %s", v.NetworkIssues)
// Retrieve Host IP Information and Set Docker Endpoint
v.HostIP = vch.ExecutorConfig.Networks["client"].Assigned.IP.String()
if vch.HostCertificate.IsNil() {
v.DockerPort = fmt.Sprintf("%d", opts.DefaultHTTPPort)
} else {
v.DockerPort = fmt.Sprintf("%d", opts.DefaultTLSHTTPPort)
}
err := v.QueryDatastore(ctx, vch, sess)
if err != nil {
log.Errorf("Had a problem querying the datastores: %s", err.Error())
}
v.QueryVCHStatus(ctx, vch, sess)
return v
}
type dsList []mo.Datastore
func (d dsList) Len() int { return len(d) }
func (d dsList) Swap(i, j int) { d[i], d[j] = d[j], d[i] }
func (d dsList) Less(i, j int) bool { return d[i].Name < d[j].Name }
// Obtain the VCH name from vsphere
func (v *Validator) GetVCHName(ctx context.Context, sess *session.Session) error {
defer trace.End(trace.Begin(""))
var err error
if sess == nil {
v.Hostname = DefaultVCHName
return fmt.Errorf("session is nil")
}
self, err := guest.GetSelf(ctx, sess)
if err != nil {
v.Hostname = DefaultVCHName
return fmt.Errorf("unknown self-reference: %s", err)
}
newVM := vm.NewVirtualMachineFromVM(ctx, sess, self)
vchName, err := newVM.ObjectName(ctx)
if err != nil {
v.Hostname = DefaultVCHName
return err
}
v.Hostname = vchName
log.Infof("Setting the VCH name to %s", vchName)
return nil
}
func (v *Validator) QueryDatastore(ctx context.Context, vch *config.VirtualContainerHostConfigSpec, sess *session.Session) error {
if sess == nil {
// If we can't connect to vSphere, don't display datastore info
v.StorageRemaining = template.HTML("")
return nil
}
var dataStores dsList
dsNames := make(map[string]bool)
for _, url := range vch.ImageStores {
dsNames[url.Host] = true
}
for _, url := range vch.VolumeLocations {
dsNames[url.Host] = true
}
for _, url := range vch.ContainerStores {
dsNames[url.Host] = true
}
refs := []types.ManagedObjectReference{}
for dsName := range dsNames {
ds, err := sess.Finder.DatastoreOrDefault(ctx, dsName)
if err != nil {
log.Errorf("Unable to collect information for datastore %s: %s", dsName, err)
} else {
refs = append(refs, ds.Reference())
}
}
if len(refs) == 0 {
return fmt.Errorf("No datastore references found")
}
pc := property.DefaultCollector(sess.Client.Client)
if pc == nil {
return fmt.Errorf("Could not get default propery collector; prop-collector came back nil")
}
err := pc.Retrieve(ctx, refs, nil, &dataStores)
if err != nil {
log.Errorf("Error while accessing datastore: %s", err)
}
sort.Sort(dataStores)
for _, ds := range dataStores {
log.Infof("Datastore %s Status: %s", ds.Name, ds.OverallStatus)
log.Infof("Datastore %s Free Space: %.1fGB", ds.Name, float64(ds.Summary.FreeSpace)/(1<<30))
log.Infof("Datastore %s Capacity: %.1fGB", ds.Name, float64(ds.Summary.Capacity)/(1<<30))
// #nosec: this method will not auto-escape HTML. Verify data is well formed.
v.StorageRemaining = template.HTML(fmt.Sprintf(`%s
<p class="card-text">
%s:
%.1f GB remaining
</p>`, v.StorageRemaining, ds.Name, float64(ds.Summary.FreeSpace)/(1<<30)))
}
return nil
}
func (v *Validator) QueryVCHStatus(ctx context.Context, vch *config.VirtualContainerHostConfigSpec, sess *session.Session) {
defer trace.End(trace.Begin(""))
if sess == nil {
// We can't connect to vSphere
v.VCHStatus = BadStatus
return
}
v.VCHIssues = template.HTML("")
v.VCHStatus = GoodStatus
procs := map[string]string{"vic-init": "vic-init"}
// Extract required components from vchConfig
// Only report on components with Restart set to true
for service, sess := range vch.ExecutorConfig.Sessions {
if !sess.Restart {
continue
}
cmd := path.Base(sess.Cmd.Path)
procs[service] = cmd
}
log.Infof("Processes to check: %+v", procs)
for service, proc := range procs {
log.Infof("Checking status of %s", proc)
pid, err := ioutil.ReadFile(fmt.Sprintf("%s.pid", path.Join(shared.PIDFileDir(), proc)))
if err != nil {
// #nosec: this method will not auto-escape HTML. Verify data is well formed.
v.VCHIssues = template.HTML(fmt.Sprintf("%s<span class=\"error-message\">%s service is not running</span>\n",
v.VCHIssues, strings.Title(service)))
log.Errorf("Process %s not running: %s", proc, err)
continue
}
status, err := ioutil.ReadFile(path.Join("/proc", string(pid), "stat"))
if err != nil {
// #nosec: this method will not auto-escape HTML. Verify data is well formed.
v.VCHIssues = template.HTML(fmt.Sprintf("%s<span class=\"error-message\">Unable to query service %s</span>\n",
v.VCHIssues, strings.Title(service)))
continue
}
fields := strings.Split(string(status), " ")
// Field 3 is the current state as reported by the kernel
switch fields[2][0] {
// We're good
case 'R', 'S', 'D':
log.Infof("Process %s running as PID %s", proc, pid)
break
// Process has been killed, is dying, or a zombie
case 'K', 'X', 'x', 'Z':
// #nosec: this method will not auto-escape HTML. Verify data is well formed.
v.VCHIssues = template.HTML(fmt.Sprintf("%s<span class=\"error-message\">%s has failed</span>\n",
v.VCHIssues, strings.Title(service)))
}
}
v.QueryVMGroupStatus(ctx, vch, sess)
if v.VCHIssues != template.HTML("") {
v.VCHStatus = BadStatus
}
}
func (v *Validator) QueryVMGroupStatus(ctx context.Context, vch *config.VirtualContainerHostConfigSpec, sess *session.Session) {
if !vch.UseVMGroup {
return
}
exists, err := validate.VMGroupExists(trace.FromContext(ctx, ""), sess.Cluster, vch.VMGroupName)
if err != nil {
// #nosec: this method will not auto-escape HTML. Verify data is well formed.
v.VCHIssues = template.HTML(fmt.Sprintf("%s<span class=\"error-message\">%s</span>\n", v.VCHIssues, html.EscapeString(err.Error())))
return
}
if !exists {
// #nosec: this method will not auto-escape HTML. Verify data is well formed.
v.VCHIssues = template.HTML(fmt.Sprintf("%s<span class=\"error-message\">VCH is configured to use DRS VM Group %q, which cannot be found</span>\n", v.VCHIssues, html.EscapeString(vch.VMGroupName)))
}
return
}