/
listcontrollers.go
216 lines (195 loc) · 5.92 KB
/
listcontrollers.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
// Copyright 2015,2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package controller
import (
"fmt"
"strings"
"sync"
"github.com/juju/cmd"
"github.com/juju/errors"
"github.com/juju/gnuflag"
"github.com/juju/names/v4"
"github.com/juju/juju/api/base"
"github.com/juju/juju/api/controller"
jujucmd "github.com/juju/juju/cmd"
"github.com/juju/juju/cmd/modelcmd"
"github.com/juju/juju/core/status"
"github.com/juju/juju/environs/bootstrap"
"github.com/juju/juju/jujuclient"
)
var helpControllersSummary = `
Lists all controllers.`[1:]
var helpControllersDetails = `
The output format may be selected with the '--format' option. In the
default tabular output, the current controller is marked with an asterisk.
Examples:
juju controllers
juju controllers --format json --output ~/tmp/controllers.json
See also:
models
show-controller`[1:]
// NewListControllersCommand returns a command to list registered controllers.
func NewListControllersCommand() cmd.Command {
cmd := &listControllersCommand{
store: jujuclient.NewFileClientStore(),
}
return modelcmd.WrapBase(cmd)
}
// Info implements Command.Info
func (c *listControllersCommand) Info() *cmd.Info {
return jujucmd.Info(&cmd.Info{
Name: "controllers",
Purpose: helpControllersSummary,
Doc: helpControllersDetails,
Aliases: []string{"list-controllers"},
})
}
// SetFlags implements Command.SetFlags.
func (c *listControllersCommand) SetFlags(f *gnuflag.FlagSet) {
c.CommandBase.SetFlags(f)
f.BoolVar(&c.refresh, "refresh", false, "Connect to each controller to download the latest details")
c.out.AddFlags(f, "tabular", map[string]cmd.Formatter{
"yaml": cmd.FormatYaml,
"json": cmd.FormatJson,
"tabular": c.formatControllersListTabular,
})
}
// SetClientStore implements Command.SetClientStore.
func (c *listControllersCommand) SetClientStore(store jujuclient.ClientStore) {
c.store = store
}
func (c *listControllersCommand) getAPI(controllerName string) (ControllerAccessAPI, error) {
if c.api != nil {
return c.api(controllerName), nil
}
api, err := c.NewAPIRoot(c.store, controllerName, "")
if err != nil {
return nil, errors.Annotate(err, "opening API connection")
}
return controller.NewClient(api), nil
}
// Run implements Command.Run
func (c *listControllersCommand) Run(ctx *cmd.Context) error {
controllers, err := c.store.AllControllers()
if err != nil {
return errors.Annotate(err, "failed to list controllers")
}
if len(controllers) == 0 && c.out.Name() == "tabular" {
return errors.Trace(modelcmd.ErrNoControllersDefined)
}
if c.refresh && len(controllers) > 0 {
var wg sync.WaitGroup
wg.Add(len(controllers))
for controllerName := range controllers {
name := controllerName
go func() {
defer wg.Done()
client, err := c.getAPI(name)
if err != nil {
fmt.Fprintf(ctx.GetStderr(), "error connecting to api for %q: %v\n", name, err)
return
}
defer client.Close()
if err := c.refreshControllerDetails(client, name); err != nil {
fmt.Fprintf(ctx.GetStderr(), "error updating cached details for %q: %v\n", name, err)
}
}()
}
wg.Wait()
// Reload controller details
controllers, err = c.store.AllControllers()
if err != nil {
return errors.Annotate(err, "failed to list controllers")
}
}
details, errs := c.convertControllerDetails(controllers)
if len(errs) > 0 {
fmt.Fprintln(ctx.Stderr, strings.Join(errs, "\n"))
}
currentController, err := modelcmd.DetermineCurrentController(c.store)
if errors.IsNotFound(err) {
currentController = ""
} else if err != nil {
return errors.Annotate(err, "getting current controller")
}
controllerSet := ControllerSet{
Controllers: details,
CurrentController: currentController,
}
return c.out.Write(ctx, controllerSet)
}
func (c *listControllersCommand) refreshControllerDetails(client ControllerAccessAPI, controllerName string) error {
// First, get all the models the user can see, and their details.
allModels, err := client.AllModels()
if err != nil {
return err
}
// Update client store.
if err := c.SetControllerModels(c.store, controllerName, allModels); err != nil {
return errors.Trace(err)
}
var controllerModelUUID string
modelTags := make([]names.ModelTag, len(allModels))
for i, m := range allModels {
modelTags[i] = names.NewModelTag(m.UUID)
if m.Name == bootstrap.ControllerModelName {
controllerModelUUID = m.UUID
}
}
modelStatus, err := client.ModelStatus(modelTags...)
if err != nil {
return err
}
c.mu.Lock()
defer c.mu.Unlock()
// Use the model information to update the cached controller details.
details, err := c.store.ControllerByName(controllerName)
if err != nil {
return err
}
machineCount := 0
for _, s := range modelStatus {
if s.Error != nil {
if errors.IsNotFound(s.Error) {
// This most likely occurred because a model was
// destroyed half-way through the call.
continue
}
return errors.Trace(s.Error)
}
machineCount += s.TotalMachineCount
}
details.MachineCount = &machineCount
details.ActiveControllerMachineCount, details.ControllerMachineCount = ControllerMachineCounts(controllerModelUUID, modelStatus)
return c.store.UpdateController(controllerName, *details)
}
func ControllerMachineCounts(controllerModelUUID string, modelStatusResults []base.ModelStatus) (activeCount, totalCount int) {
for _, s := range modelStatusResults {
if s.Error != nil {
// This most likely occurred because a model was
// destroyed half-way through the call.
continue
}
if s.UUID != controllerModelUUID {
continue
}
for _, m := range s.Machines {
if !m.WantsVote {
continue
}
totalCount++
if m.Status != string(status.Down) && m.HasVote {
activeCount++
}
}
}
return activeCount, totalCount
}
type listControllersCommand struct {
modelcmd.CommandBase
out cmd.Output
store jujuclient.ClientStore
api func(controllerName string) ControllerAccessAPI
refresh bool
mu sync.Mutex
}