forked from vmware/vic
/
list.go
189 lines (163 loc) · 5.59 KB
/
list.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
// Copyright 2016-2018 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 list
import (
"context"
"path"
"text/tabwriter"
"text/template"
"time"
"gopkg.in/urfave/cli.v1"
"github.com/vmware/vic/cmd/vic-machine/common"
"github.com/vmware/vic/lib/install/data"
"github.com/vmware/vic/lib/install/interaction"
"github.com/vmware/vic/lib/install/management"
"github.com/vmware/vic/lib/install/validate"
"github.com/vmware/vic/pkg/errors"
"github.com/vmware/vic/pkg/trace"
"github.com/vmware/vic/pkg/version"
"github.com/vmware/vic/pkg/vsphere/vm"
)
type items struct {
ID string
Path string
Name string
Version string
UpgradeStatus string
}
// templ is parsed by text/template package
const templ = `{{range .}}
{{.ID}} {{.Path}} {{.Name}} {{.Version}} {{.UpgradeStatus}}{{end}}
`
// List has all input parameters for vic-machine ls command
type List struct {
*data.Data
executor *management.Dispatcher
}
func NewList() *List {
d := &List{}
d.Data = data.NewData()
return d
}
// Flags return all cli flags for ls
func (l *List) Flags() []cli.Flag {
util := []cli.Flag{
cli.DurationFlag{
Name: "timeout",
Value: 3 * time.Minute,
Usage: "Time to wait for list",
Destination: &l.Timeout,
},
}
target := l.TargetFlags()
// TODO: why not allow name as a filter, like most list operations
compute := l.ComputeFlagsNoName()
debug := l.DebugFlags(true)
// flag arrays are declared, now combined
var flags []cli.Flag
for _, f := range [][]cli.Flag{target, compute, util, debug} {
flags = append(flags, f...)
}
return flags
}
func (l *List) processParams(op trace.Operation) error {
defer trace.End(trace.Begin("", op))
if err := l.HasCredentials(op); err != nil {
return err
}
return nil
}
func (l *List) prettyPrint(op trace.Operation, cli *cli.Context, vchs []*vm.VirtualMachine, executor *management.Dispatcher) {
data := []items{
{"ID", "PATH", "NAME", "VERSION", "UPGRADE STATUS"},
}
installerVer := version.GetBuild()
for _, vch := range vchs {
vchConfig, err := executor.GetNoSecretVCHConfig(vch)
var version string
var upgradeStatus string
if err != nil {
op.Warnf("Failed to get Virtual Container Host configuration for VCH %q: %s", vch.Reference().Value, err)
op.Warnf("Skip listing VCH %q", vch.Reference().Value)
version = "unknown"
upgradeStatus = "unknown"
} else {
version = vchConfig.Version.ShortVersion()
upgradeStatus = l.upgradeStatusMessage(op, vch, installerVer, vchConfig.Version)
}
// When the VCH was found the inventory path was overwritten with the resource pool path, so
// to print the path to the pool we need to call Dir twice.
data = append(data,
items{vch.Reference().Value, path.Dir(path.Dir(vch.InventoryPath)), vch.Name(), version, upgradeStatus})
}
t := template.New("vic-machine ls")
// #nosec: Errors unhandled.
t, _ = t.Parse(templ)
w := tabwriter.NewWriter(cli.App.Writer, 8, 8, 8, ' ', 0)
if err := t.Execute(w, data); err != nil {
op.Fatal(err)
}
// #nosec: Errors unhandled.
w.Flush()
}
func (l *List) Run(clic *cli.Context) (err error) {
op := common.NewOperation(clic, l.Debug.Debug)
defer func() {
// urfave/cli will print out exit in error handling, so no more information in main method can be printed out.
err = common.LogErrorIfAny(op, clic, err)
}()
op, cancel := trace.WithTimeout(&op, l.Timeout, clic.App.Name)
defer cancel()
defer func() {
if op.Err() != nil && op.Err() == context.DeadlineExceeded {
//context deadline exceeded, replace returned error message
err = errors.Errorf("List timed out: use --timeout to add more time")
}
}()
if err = l.processParams(op); err != nil {
return err
}
if len(clic.Args()) > 0 {
op.Errorf("Unknown argument: %s", clic.Args()[0])
return errors.New("invalid CLI arguments")
}
op.Infof("### Listing VCHs ####")
op.Debugf("vic-machine version %s", version.GetBuild().ShortVersion())
var validator *validate.Validator
if validator, err = validate.NewValidator(op, l.Data); err != nil {
op.Errorf("List cannot continue - failed to create validator: %s", err)
return errors.New("list failed")
}
defer validator.Session().Logout(op)
_, err = validator.ValidateTarget(op, l.Data, true)
if err != nil {
op.Errorf("List cannot continue - target validation failed: %s", err)
return errors.New("list failed")
}
_, err = validator.ValidateCompute(op, l.Data, false)
if err != nil {
op.Errorf("List cannot continue - compute resource validation failed: %s", err)
return errors.New("list failed")
}
executor := management.NewDispatcher(op, validator.Session(), management.ActionList, false)
vchs, err := executor.SearchVCHs(validator.Session().ClusterPath)
if err != nil {
op.Errorf("List cannot continue - failed to search VCHs in %s: %s", validator.Session().PoolPath, err)
}
l.prettyPrint(op, clic, vchs, executor)
return nil
}
func (l *List) upgradeStatusMessage(op trace.Operation, vch *vm.VirtualMachine, installerVer *version.Build, vchVer *version.Build) string {
return interaction.GetUpgradeStatusShortMessage(op, vch, installerVer, vchVer)
}