forked from hashicorp/nomad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.go
222 lines (182 loc) · 5.12 KB
/
ui.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
package command
import (
"fmt"
"net/url"
"strings"
"github.com/hashicorp/nomad/api/contexts"
"github.com/posener/complete"
"github.com/skratchdot/open-golang/open"
)
var (
// uiContexts is the contexts the ui can open automatically.
uiContexts = []contexts.Context{contexts.Jobs, contexts.Allocs, contexts.Nodes}
)
type UiCommand struct {
Meta
}
func (c *UiCommand) Help() string {
helpText := `
Usage: nomad ui [options] <identifier>
Open the Nomad Web UI in the default browser. An optional identifier may be
provided, in which case the UI will be opened to view the details for that
object. Supported identifiers are jobs, allocations and nodes.
General Options:
` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace) + `
UI Options
-authenticate: Exchange your Nomad ACL token for a one-time token in the
web UI, if ACLs are enabled.
-show-url: Show the Nomad UI URL instead of opening with the default browser.
`
return strings.TrimSpace(helpText)
}
func (c *UiCommand) AutocompleteFlags() complete.Flags {
return c.Meta.AutocompleteFlags(FlagSetClient)
}
func (c *UiCommand) AutocompleteArgs() complete.Predictor {
return complete.PredictFunc(func(a complete.Args) []string {
client, err := c.Meta.Client()
if err != nil {
return nil
}
resp, _, err := client.Search().PrefixSearch(a.Last, contexts.All, nil)
if err != nil {
return []string{}
}
final := make([]string, 0)
for _, allowed := range uiContexts {
matches, ok := resp.Matches[allowed]
if !ok {
continue
}
if len(matches) == 0 {
continue
}
final = append(final, matches...)
}
return final
})
}
func (c *UiCommand) Synopsis() string {
return "Open the Nomad Web UI"
}
func (c *UiCommand) Name() string { return "ui" }
func (c *UiCommand) Run(args []string) int {
var authenticate bool
var showUrl bool
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
flags.BoolVar(&authenticate, "authenticate", false, "")
flags.BoolVar(&showUrl, "show-url", false, "")
if err := flags.Parse(args); err != nil {
return 1
}
// Check that we got no more than one argument
args = flags.Args()
if l := len(args); l > 1 {
c.Ui.Error("This command takes no or one optional argument, [<identifier>]")
c.Ui.Error(commandErrorText(c))
return 1
}
// Get the HTTP client
client, err := c.Meta.Client()
if err != nil {
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
return 1
}
url, err := url.Parse(client.Address())
if err != nil {
c.Ui.Error(fmt.Sprintf("Error parsing Nomad address %q: %s", client.Address(), err))
return 1
}
var ottSecret string
if authenticate {
ott, _, err := client.ACLTokens().UpsertOneTimeToken(nil)
if err != nil {
c.Ui.Error(fmt.Sprintf("Could not get one-time token: %s", err))
return 1
}
ottSecret = ott.OneTimeSecretID
}
// We were given an id so look it up
if len(args) == 1 {
id := args[0]
// Query for the context associated with the id
res, _, err := client.Search().PrefixSearch(id, contexts.All, nil)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error querying search with id: %q", err))
return 1
}
if res.Matches == nil {
c.Ui.Error(fmt.Sprintf("No matches returned for query: %q", err))
return 1
}
var match contexts.Context
var fullID string
matchCount := 0
for _, ctx := range uiContexts {
vers, ok := res.Matches[ctx]
if !ok {
continue
}
if l := len(vers); l == 1 {
match = ctx
fullID = vers[0]
matchCount++
} else if l > 0 && vers[0] == id {
// Exact match
match = ctx
fullID = vers[0]
break
}
// Only a single result should return, as this is a match against a full id
if matchCount > 1 || len(vers) > 1 {
c.logMultiMatchError(id, res.Matches)
return 1
}
}
switch match {
case contexts.Nodes:
url.Path = fmt.Sprintf("ui/clients/%s", fullID)
case contexts.Allocs:
url.Path = fmt.Sprintf("ui/allocations/%s", fullID)
case contexts.Jobs:
url.Path = fmt.Sprintf("ui/jobs/%s", fullID)
default:
c.Ui.Error(fmt.Sprintf("Unable to resolve ID: %q", id))
return 1
}
}
var output string
if authenticate && ottSecret != "" {
output = fmt.Sprintf("Opening URL %q with one-time token", url.String())
url.RawQuery = fmt.Sprintf("ott=%s", ottSecret)
} else {
output = fmt.Sprintf("Opening URL %q", url.String())
}
if showUrl {
c.Ui.Output(fmt.Sprintf("URL for web UI: %s", url.String()))
return 0
}
c.Ui.Output(output)
if err := open.Start(url.String()); err != nil {
c.Ui.Error(fmt.Sprintf("Error opening URL: %s", err))
return 1
}
return 0
}
// logMultiMatchError is used to log an error message when multiple matches are
// found. The error message logged displays the matched IDs per context.
func (c *UiCommand) logMultiMatchError(id string, matches map[contexts.Context][]string) {
c.Ui.Error(fmt.Sprintf("Multiple matches found for id %q", id))
for _, ctx := range uiContexts {
vers, ok := matches[ctx]
if !ok {
continue
}
if len(vers) == 0 {
continue
}
c.Ui.Error(fmt.Sprintf("\n%s:", strings.Title(string(ctx))))
c.Ui.Error(strings.Join(vers, ", "))
}
}