forked from vmware/govmomi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
info.go
142 lines (113 loc) · 3.31 KB
/
info.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
/*
Copyright (c) 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 override
import (
"context"
"flag"
"fmt"
"io"
"strings"
"text/tabwriter"
"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/govc/flags"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/vim25/types"
)
type info struct {
*flags.ClusterFlag
}
func init() {
cli.Register("cluster.override.info", &info{})
}
func (cmd *info) Register(ctx context.Context, f *flag.FlagSet) {
cmd.ClusterFlag, ctx = flags.NewClusterFlag(ctx)
cmd.ClusterFlag.Register(ctx, f)
}
func (cmd *info) Description() string {
return `Cluster VM overrides info.
Examples:
govc cluster.override.info
govc cluster.override.info -json`
}
func (cmd *info) Process(ctx context.Context) error {
return cmd.ClusterFlag.Process(ctx)
}
type Override struct {
id types.ManagedObjectReference
Name string
Host string `json:",omitempty"`
DRS *types.ClusterDrsVmConfigInfo `json:",omitempty"`
DAS *types.ClusterDasVmConfigInfo `json:",omitempty"`
}
type infoResult struct {
Overrides map[string]*Override
}
func (r *infoResult) Write(w io.Writer) error {
tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
for _, entry := range r.Overrides {
behavior := fmt.Sprintf("Default (%s)", types.DrsBehaviorFullyAutomated)
if entry.DRS != nil {
if *entry.DRS.Enabled {
behavior = string(entry.DRS.Behavior)
}
}
priority := fmt.Sprintf("Default (%s)", types.DasVmPriorityMedium)
if entry.DAS != nil {
priority = entry.DAS.DasSettings.RestartPriority
}
fmt.Fprintf(tw, "Name:\t%s\n", entry.Name)
fmt.Fprintf(tw, " DRS Automation Level:\t%s\n", strings.Title(behavior))
fmt.Fprintf(tw, " HA Restart Priority:\t%s\n", strings.Title(priority))
fmt.Fprintf(tw, " Host:\t%s\n", entry.Host)
}
return tw.Flush()
}
func (r *infoResult) entry(id types.ManagedObjectReference) *Override {
key := id.String()
vm, ok := r.Overrides[key]
if !ok {
r.Overrides[key] = &Override{id: id}
vm = r.Overrides[key]
}
return vm
}
func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error {
cluster, err := cmd.Cluster()
if err != nil {
return err
}
config, err := cluster.Configuration(ctx)
if err != nil {
return err
}
res := &infoResult{
Overrides: make(map[string]*Override),
}
for i := range config.DasVmConfig {
vm := res.entry(config.DasVmConfig[i].Key)
vm.DAS = &config.DasVmConfig[i]
}
for i := range config.DrsVmConfig {
vm := res.entry(config.DrsVmConfig[i].Key)
vm.DRS = &config.DrsVmConfig[i]
}
for _, o := range res.Overrides {
// TODO: can optimize to reduce round trips
vm := object.NewVirtualMachine(cluster.Client(), o.id)
o.Name, _ = vm.ObjectName(ctx)
if h, herr := vm.HostSystem(ctx); herr == nil {
o.Host, _ = h.ObjectName(ctx)
}
}
return cmd.WriteResult(res)
}