/
showcontroller.go
398 lines (350 loc) · 11.7 KB
/
showcontroller.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
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package controller
import (
"fmt"
"github.com/juju/cmd"
"github.com/juju/errors"
"github.com/juju/gnuflag"
"gopkg.in/juju/names.v2"
"github.com/juju/juju/api/base"
"github.com/juju/juju/api/controller"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/cmd/modelcmd"
"github.com/juju/juju/environs/bootstrap"
"github.com/juju/juju/jujuclient"
"github.com/juju/juju/permission"
"github.com/juju/juju/status"
)
var usageShowControllerSummary = `
Shows detailed information of a controller.`[1:]
var usageShowControllerDetails = `
Shows extended information about a controller(s) as well as related models
and user login details.
Examples:
juju show-controller
juju show-controller aws google
See also:
controllers`[1:]
type showControllerCommand struct {
modelcmd.JujuCommandBase
out cmd.Output
store jujuclient.ClientStore
api func(controllerName string) ControllerAccessAPI
controllerNames []string
showPasswords bool
}
// NewShowControllerCommand returns a command to show details of the desired controllers.
func NewShowControllerCommand() cmd.Command {
cmd := &showControllerCommand{
store: jujuclient.NewFileClientStore(),
}
return modelcmd.WrapBase(cmd)
}
// Init implements Command.Init.
func (c *showControllerCommand) Init(args []string) (err error) {
c.controllerNames = args
return nil
}
// Info implements Command.Info
func (c *showControllerCommand) Info() *cmd.Info {
return &cmd.Info{
Name: "show-controller",
Args: "[<controller name> ...]",
Purpose: usageShowControllerSummary,
Doc: usageShowControllerDetails,
}
}
// SetFlags implements Command.SetFlags.
func (c *showControllerCommand) SetFlags(f *gnuflag.FlagSet) {
c.JujuCommandBase.SetFlags(f)
f.BoolVar(&c.showPasswords, "show-password", false, "Show password for logged in user")
c.out.AddFlags(f, "yaml", map[string]cmd.Formatter{
"yaml": cmd.FormatYaml,
"json": cmd.FormatJson,
})
}
// ControllerAccessAPI defines a subset of the api/controller/Client API.
type ControllerAccessAPI interface {
GetControllerAccess(user string) (permission.Access, error)
ModelConfig() (map[string]interface{}, error)
ModelStatus(models ...names.ModelTag) ([]base.ModelStatus, error)
AllModels() ([]base.UserModel, error)
Close() error
}
func (c *showControllerCommand) 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 *showControllerCommand) Run(ctx *cmd.Context) error {
controllerNames := c.controllerNames
if len(controllerNames) == 0 {
currentController, err := c.store.CurrentController()
if errors.IsNotFound(err) {
return errors.New("there is no active controller")
} else if err != nil {
return errors.Trace(err)
}
controllerNames = []string{currentController}
}
controllers := make(map[string]ShowControllerDetails)
for _, controllerName := range controllerNames {
one, err := c.store.ControllerByName(controllerName)
if err != nil {
return err
}
var access string
client, err := c.getAPI(controllerName)
if err != nil {
return err
}
defer client.Close()
accountDetails, err := c.store.AccountDetails(controllerName)
if err != nil {
fmt.Fprintln(ctx.Stderr, err)
access = "(error)"
} else {
access = c.userAccess(client, ctx, accountDetails.User)
one.AgentVersion = c.agentVersion(client, ctx)
}
var details ShowControllerDetails
var modelStatus []base.ModelStatus
allModels, err := client.AllModels()
if err != nil {
details.Errors = append(details.Errors, err.Error())
continue
}
modelTags := make([]names.ModelTag, len(allModels))
for i, m := range allModels {
modelTags[i] = names.NewModelTag(m.UUID)
}
modelStatus, err = client.ModelStatus(modelTags...)
if err != nil {
details.Errors = append(details.Errors, err.Error())
continue
}
c.convertControllerForShow(&details, controllerName, one, access, allModels, modelStatus)
controllers[controllerName] = details
}
return c.out.Write(ctx, controllers)
}
func (c *showControllerCommand) userAccess(client ControllerAccessAPI, ctx *cmd.Context, user string) string {
var access string
userAccess, err := client.GetControllerAccess(user)
if err == nil {
access = string(userAccess)
} else {
code := params.ErrCode(err)
if code != "" {
access = fmt.Sprintf("(%s)", code)
} else {
fmt.Fprintln(ctx.Stderr, err)
access = "(error)"
}
}
return access
}
func (c *showControllerCommand) agentVersion(client ControllerAccessAPI, ctx *cmd.Context) string {
var ver string
mc, err := client.ModelConfig()
if err != nil {
code := params.ErrCode(err)
if code != "" {
ver = fmt.Sprintf("(%s)", code)
} else {
fmt.Fprintln(ctx.Stderr, err)
ver = "(error)"
}
return ver
}
return mc["agent-version"].(string)
}
type ShowControllerDetails struct {
// Details contains the same details that client store caches for this controller.
Details ControllerDetails `yaml:"details,omitempty" json:"details,omitempty"`
// Machines is a collection of all machines forming the controller cluster.
Machines map[string]MachineDetails `yaml:"controller-machines,omitempty" json:"controller-machines,omitempty"`
// Models is a collection of all models for this controller.
Models map[string]ModelDetails `yaml:"models,omitempty" json:"models,omitempty"`
// CurrentModel is the name of the current model for this controller
CurrentModel string `yaml:"current-model,omitempty" json:"current-model,omitempty"`
// Account is the account details for the user logged into this controller.
Account *AccountDetails `yaml:"account,omitempty" json:"account,omitempty"`
// Errors is a collection of errors related to accessing this controller details.
Errors []string `yaml:"errors,omitempty" json:"errors,omitempty"`
}
// ControllerDetails holds details of a controller to show.
type ControllerDetails struct {
// ControllerUUID is the unique ID for the controller.
ControllerUUID string `yaml:"uuid" json:"uuid"`
// APIEndpoints is the collection of API endpoints running in this controller.
APIEndpoints []string `yaml:"api-endpoints,flow" json:"api-endpoints"`
// CACert is a security certificate for this controller.
CACert string `yaml:"ca-cert" json:"ca-cert"`
// Cloud is the name of the cloud that this controller runs in.
Cloud string `yaml:"cloud" json:"cloud"`
// CloudRegion is the name of the cloud region that this controller runs in.
CloudRegion string `yaml:"region,omitempty" json:"region,omitempty"`
// AgentVersion is the version of the agent running on this controller.
// AgentVersion need not always exist so we omitempty here. This struct is
// used in both list-controller and show-controller. show-controller
// displays the agent version where list-controller does not.
AgentVersion string `yaml:"agent-version,omitempty" json:"agent-version,omitempty"`
}
// ModelDetails holds details of a model to show.
type MachineDetails struct {
// ID holds the id of the machine.
ID string `yaml:"id,omitempty" json:"id,omitempty"`
// InstanceID holds the cloud instance id of the machine.
InstanceID string `yaml:"instance-id,omitempty" json:"instance-id,omitempty"`
// HAStatus holds information informing of the HA status of the machine.
HAStatus string `yaml:"ha-status,omitempty" json:"ha-status,omitempty"`
}
// ModelDetails holds details of a model to show.
type ModelDetails struct {
// ModelUUID holds the details of a model.
ModelUUID string `yaml:"uuid" json:"uuid"`
// MachineCount holds the number of machines in the model.
MachineCount *int `yaml:"machine-count,omitempty" json:"machine-count,omitempty"`
// CoreCount holds the number of cores across the machines in the model.
CoreCount *int `yaml:"core-count,omitempty" json:"core-count,omitempty"`
}
// AccountDetails holds details of an account to show.
type AccountDetails struct {
// User is the username for the account.
User string `yaml:"user" json:"user"`
// Access is the level of access the user has on the controller.
Access string `yaml:"access,omitempty" json:"access,omitempty"`
// Password is the password for the account.
Password string `yaml:"password,omitempty" json:"password,omitempty"`
}
func (c *showControllerCommand) convertControllerForShow(
controller *ShowControllerDetails,
controllerName string,
details *jujuclient.ControllerDetails,
access string,
allModels []base.UserModel,
modelStatus []base.ModelStatus,
) {
controller.Details = ControllerDetails{
ControllerUUID: details.ControllerUUID,
APIEndpoints: details.APIEndpoints,
CACert: details.CACert,
Cloud: details.Cloud,
CloudRegion: details.CloudRegion,
AgentVersion: details.AgentVersion,
}
c.convertModelsForShow(controllerName, controller, allModels, modelStatus)
c.convertAccountsForShow(controllerName, controller, access)
var controllerModelUUID string
for _, m := range allModels {
if m.Name == bootstrap.ControllerModelName {
controllerModelUUID = m.UUID
break
}
}
if controllerModelUUID != "" {
var controllerModel base.ModelStatus
found := false
for _, m := range modelStatus {
if m.UUID == controllerModelUUID {
controllerModel = m
found = true
break
}
}
if found {
c.convertMachinesForShow(controllerName, controller, controllerModel)
}
}
}
func (c *showControllerCommand) convertAccountsForShow(controllerName string, controller *ShowControllerDetails, access string) {
storeDetails, err := c.store.AccountDetails(controllerName)
if err != nil && !errors.IsNotFound(err) {
controller.Errors = append(controller.Errors, err.Error())
}
if storeDetails == nil {
return
}
details := &AccountDetails{
User: storeDetails.User,
Access: access,
}
if c.showPasswords {
details.Password = storeDetails.Password
}
controller.Account = details
}
func (c *showControllerCommand) convertModelsForShow(
controllerName string,
controller *ShowControllerDetails,
models []base.UserModel,
modelStatus []base.ModelStatus,
) {
controller.Models = make(map[string]ModelDetails)
for i, model := range models {
modelDetails := ModelDetails{ModelUUID: model.UUID}
if modelStatus[i].TotalMachineCount > 0 {
modelDetails.MachineCount = new(int)
*modelDetails.MachineCount = modelStatus[i].TotalMachineCount
}
if modelStatus[i].CoreCount > 0 {
modelDetails.CoreCount = new(int)
*modelDetails.CoreCount = modelStatus[i].CoreCount
}
controller.Models[model.Name] = modelDetails
}
var err error
controller.CurrentModel, err = c.store.CurrentModel(controllerName)
if err != nil && !errors.IsNotFound(err) {
controller.Errors = append(controller.Errors, err.Error())
}
}
func (c *showControllerCommand) convertMachinesForShow(
controllerName string,
controller *ShowControllerDetails,
controllerModel base.ModelStatus,
) {
controller.Machines = make(map[string]MachineDetails)
numControllers := 0
for _, m := range controllerModel.Machines {
if !m.WantsVote {
continue
}
numControllers++
}
for _, m := range controllerModel.Machines {
if !m.WantsVote {
// Skip non controller machines.
continue
}
instId := m.InstanceId
if instId == "" {
instId = "(unprovisioned)"
}
details := MachineDetails{InstanceID: instId}
if numControllers > 1 {
details.HAStatus = haStatus(m.HasVote, m.WantsVote, m.Status)
}
controller.Machines[m.Id] = details
}
}
func haStatus(hasVote bool, wantsVote bool, statusStr string) string {
if statusStr == string(status.Down) {
return "down, lost connection"
}
if !wantsVote {
return ""
}
if hasVote {
return "ha-enabled"
}
return "ha-pending"
}