/
proxy.go
403 lines (346 loc) · 10.7 KB
/
proxy.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
399
400
401
402
403
/*
* Copyright (c) 2021, 2024 Oracle and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at
* https://oss.oracle.com/licenses/upl.
*/
package cmd
import (
"encoding/json"
"fmt"
"github.com/oracle/coherence-cli/pkg/config"
"github.com/oracle/coherence-cli/pkg/constants"
"github.com/oracle/coherence-cli/pkg/fetcher"
"github.com/oracle/coherence-cli/pkg/utils"
"github.com/spf13/cobra"
"strings"
"sync"
"time"
)
const (
provideProxyService = "you must provide a proxy service name"
proxyErrorMsg = "unable to find proxy service with service name"
tcpString = "tcp"
)
// var getProxiesCmd = &cobra.Command{ represents the getProxies command.
var getProxiesCmd = &cobra.Command{
Use: "proxies",
Short: "display Coherence*Extend proxy services for a cluster",
Long: `The 'get proxies' command displays the list of Coherence*Extend proxy
servers for a cluster. You can specify '-o wide' to display addition information.`,
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
var (
err error
dataFetcher fetcher.Fetcher
connection string
)
connection, dataFetcher, err = GetConnectionAndDataFetcher()
if err != nil {
return err
}
details, err := returnGetProxiesDetails(cmd, tcpString, dataFetcher, connection)
if err != nil {
return err
}
if !isWatchEnabled() {
// don't display the value if watch was enabled as it was already output
cmd.Println(details)
}
return nil
},
}
// describeProxyCmd represents the describe proxy command.
var describeProxyCmd = &cobra.Command{
Use: "proxy service-name",
Short: "describe a proxy server",
Long: `The 'describe proxy' command shows information related to proxy servers including
all nodes running the proxy service as well as detailed connection information.`,
ValidArgsFunction: completionProxies,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
displayErrorAndExit(cmd, provideProxyService)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
var (
serviceResult []byte
proxiesSummary = config.ProxiesSummary{}
err error
dataFetcher fetcher.Fetcher
connection string
)
serviceName := args[0]
// retrieve the current context or the value from "-c"
connection, dataFetcher, err = GetConnectionAndDataFetcher()
if err != nil {
return err
}
proxyResults, err := dataFetcher.GetProxySummaryJSON()
if err != nil {
return err
}
if len(proxyResults) == 0 {
return fmt.Errorf("%s '%s'", proxyErrorMsg, serviceName)
}
err = json.Unmarshal(proxyResults, &proxiesSummary)
if err != nil {
return err
}
// get a list of node Id's while we search for the service name
nodeIds := getProxyNodeIDs(serviceName, proxiesSummary)
if len(nodeIds) == 0 {
return fmt.Errorf("%s '%s'", proxyErrorMsg, serviceName)
}
// we have valid service name so issue queries
serviceResult, err = dataFetcher.GetSingleServiceDetailsJSON(serviceName)
if err != nil {
return err
}
// retrieve all connection details from JSON
connectionDetails := make([]config.GenericDetails, len(nodeIds))
for i := range nodeIds {
connectionDetails[i] = config.GenericDetails{}
data, err := dataFetcher.GetProxyConnectionsJSON(serviceName, nodeIds[i])
if err != nil {
return err
}
err = json.Unmarshal(data, &connectionDetails[i])
if err != nil {
return err
}
}
err = displayProxyDetails(cmd, dataFetcher, connection, "tcp", serviceResult, proxyResults)
if err != nil {
return err
}
cmd.Print("PROXY CONNECTIONS\n")
cmd.Print("-----------------\n")
for _, value := range connectionDetails {
for _, detail := range value.Details {
// deserialize the map into json for the format function
jsonData, err := json.Marshal(detail)
if err != nil {
return err
}
value, err := FormatJSONForDescribe(jsonData, true, "Node Id", "Remote Address", "Remote Port")
if err != nil {
return err
}
cmd.Println(value)
}
}
return nil
},
}
// getProxyConnectionsCmd represents the get proxy-connections command.
var getProxyConnectionsCmd = &cobra.Command{
Use: "proxy-connections service-name",
Short: "display proxy server connections for a specific proxy server",
Long: `The 'get proxy-connections' command displays proxy server connections for a specific proxy server.`,
ValidArgsFunction: completionProxies,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
displayErrorAndExit(cmd, provideProxyService)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
var (
err error
dataFetcher fetcher.Fetcher
connection string
)
serviceName := args[0]
// retrieve the current context or the value from "-c"
connection, dataFetcher, err = GetConnectionAndDataFetcher()
if err != nil {
return err
}
for {
var (
connectionDetailsFinal []config.ProxyConnection
connectionsResult []byte
)
connectionDetailsFinal, err = getProxyConnections(dataFetcher, serviceName)
if err != nil {
return err
}
if strings.Contains(OutputFormat, constants.JSONPATH) || OutputFormat == constants.JSON {
connectionsResult, err = json.Marshal(connectionDetailsFinal)
if err != nil {
return err
}
if strings.Contains(OutputFormat, constants.JSONPATH) {
result, err := utils.GetJSONPathResults(connectionsResult, OutputFormat)
if err != nil {
return err
}
cmd.Println(result)
} else if OutputFormat == constants.JSON {
cmd.Println(string(connectionsResult))
}
} else {
printWatchHeader(cmd)
cmd.Println(FormatCurrentCluster(connection))
cmd.Println(FormatProxyConnections(connectionDetailsFinal))
}
// check to see if we should exit if we are not watching
if !isWatchEnabled() {
break
}
// we are watching so sleep and then repeat until CTRL-C
time.Sleep(time.Duration(watchDelay) * time.Second)
}
return nil
},
}
func getProxyConnections(dataFetcher fetcher.Fetcher, proxyService string) ([]config.ProxyConnection, error) {
var (
wg sync.WaitGroup
errorSink = createErrorSink()
m = sync.RWMutex{}
err error
connectionDetailsFinal = make([]config.ProxyConnection, 0)
proxyResults []byte
proxiesSummary = config.ProxiesSummary{}
)
proxyResults, err = dataFetcher.GetProxySummaryJSON()
if err != nil {
return connectionDetailsFinal, err
}
err = json.Unmarshal(proxyResults, &proxiesSummary)
if err != nil {
return connectionDetailsFinal, err
}
nodeIds := getProxyNodeIDs(proxyService, proxiesSummary)
nodeIdsLen := len(nodeIds)
if nodeIdsLen == 0 {
return connectionDetailsFinal, fmt.Errorf("%s '%s'", proxyErrorMsg, proxyService)
}
wg.Add(nodeIdsLen)
// retrieve all connection details from JSON
for i := range nodeIds {
go func(nodeID string) {
defer wg.Done()
connectionDetails := config.ProxyConnections{}
data, err1 := dataFetcher.GetProxyConnectionsJSON(proxyService, nodeID)
if err1 != nil {
errorSink.AppendError(err1)
return
}
err1 = json.Unmarshal(data, &connectionDetails)
if err1 != nil {
errorSink.AppendError(err1)
return
}
// protect the slice for update
m.Lock()
defer m.Unlock()
connectionDetailsFinal = append(connectionDetailsFinal, connectionDetails.Proxies...)
}(nodeIds[i])
}
// wait for the results
wg.Wait()
errorList := errorSink.GetErrors()
if len(errorList) > 0 {
return connectionDetailsFinal, utils.GetErrors(errorList)
}
return connectionDetailsFinal, nil
}
func getProxyNodeIDs(selectedService string, proxiesSummary config.ProxiesSummary) []string {
// get a list of node Id's while we search for the service name
nodeIDs := make([]string, 0)
for _, value := range proxiesSummary.Proxies {
if value.ServiceName == selectedService && value.Protocol == "tcp" {
nodeIDs = append(nodeIDs, value.NodeID)
}
}
return nodeIDs
}
func displayProxyDetails(cmd *cobra.Command, dataFetcher fetcher.Fetcher, connection, protocol string, serviceResult, proxyResults []byte) error {
var (
err error
finalResult []byte
result string
details = "PROXY SERVICE DETAILS"
member = "PROXY MEMBER DETAILS"
header = ""
)
if protocol == "http" {
details = "HTTP SERVER SERVICE DETAILS"
member = "HTTP SERVER MEMBER DETAILS"
header = "------"
}
if strings.Contains(OutputFormat, constants.JSONPATH) || OutputFormat == constants.JSON {
finalResult, err = utils.CombineByteArraysForJSON([][]byte{serviceResult, proxyResults}, []string{"services", "members"})
if err != nil {
return err
}
if strings.Contains(OutputFormat, constants.JSONPATH) {
result, err = utils.GetJSONPathResults(finalResult, OutputFormat)
if err != nil {
return err
}
cmd.Println(result)
return nil
}
cmd.Println(string(finalResult))
return nil
}
cmd.Print("\n" + details + "\n")
cmd.Print("---------------------" + header + "\n")
value, err := FormatJSONForDescribe(serviceResult, true, "Name", "Type")
if err != nil {
return err
}
cmd.Println(value)
cmd.Print(member + "\n")
cmd.Print("--------------------" + header + "\n")
value, err = returnGetProxiesDetails(cmd, protocol, dataFetcher, connection)
if err != nil {
return err
}
cmd.Println(value)
return nil
}
func returnGetProxiesDetails(cmd *cobra.Command, protocol string, dataFetcher fetcher.Fetcher, connection string) (string, error) {
var sb strings.Builder
for {
var proxiesSummary = config.ProxiesSummary{}
sb = strings.Builder{}
proxyResults, err := dataFetcher.GetProxySummaryJSON()
if err != nil {
return "", err
}
if len(proxyResults) == 0 {
return "", nil
}
if strings.Contains(OutputFormat, constants.JSONPATH) {
result, err := utils.GetJSONPathResults(proxyResults, OutputFormat)
if err != nil {
return "", err
}
cmd.Println(result)
} else if OutputFormat == constants.JSON {
sb.WriteString(string(proxyResults))
} else {
printWatchHeader(cmd)
sb.WriteString(FormatCurrentCluster(connection) + "\n")
err = json.Unmarshal(proxyResults, &proxiesSummary)
if err != nil {
return "", utils.GetError("unable to unmarshall proxy result", err)
}
sb.WriteString(FormatProxyServers(proxiesSummary.Proxies, protocol))
}
// check to see if we should exit if we are not watching
if !isWatchEnabled() {
break
}
// if we are watching then output the details directly
cmd.Println(sb.String())
// we are watching so sleep and then repeat until CTRL-C
time.Sleep(time.Duration(watchDelay) * time.Second)
}
return sb.String(), nil
}