This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
utils.go
367 lines (332 loc) · 9.75 KB
/
utils.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
package main
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"strings"
"time"
"github.com/fatih/color"
)
var (
red = color.New(color.FgRed)
green = color.New(color.FgGreen)
DefaultQuorumVersion = "21.7.1"
DefaultTmName = "tessera"
DefaultTesseraVersion = "21.7.2"
DefaultConstellationVersion = "0.3.2"
DefaultConsensus = "istanbul"
DefaultNodeNumber = 4
DefaultChainId = "1000"
DefaultGethPort = "8545"
DefaultTesseraPort = "9001"
DefaultP2PPort = "30303"
DefaultPrometheusClusterPort = "9090"
DefaultPrometheusNodePort = "31323"
ServiceTypeNodePort = "NodePort"
ServiceTypeClusterIP = "ClusterIP"
RaftConsensus = "raft"
IstanbulConsensus = "istanbul"
QubernetesContainer = "quorumengineering/qubernetes" // "username/qubernetes-local"
)
func podNameFromPrefix(prefix string, namespace string) string {
//log.Printf("connecting to node [%v] ", prefix)
//TODO: extract this into a utils function
c1 := exec.Command("kubectl", "--namespace="+namespace, "get", "pods")
//fmt.Println(c1.String())
c2 := exec.Command("grep", prefix)
r, w := io.Pipe()
c1.Stdout = w
c2.Stdin = r
var b2 bytes.Buffer
c2.Stdout = &b2
c1.Start()
c2.Start()
c1.Wait()
w.Close()
c2.Wait()
//var out bytes.Buffer
//cmd.Stdout = &out
//err := cmd.Run()
//if err != nil {
// log.Fatal(err)
//}
podOutput := b2.String()
//io.Copy(os.Stdout, &b2)
//fmt.Printf(podOutput)
podName := strings.Split(podOutput, " ")[0]
return podName
}
func serviceNamesFromPrefix(prefix string, namespace string, info bool) []string {
//log.Printf("connecting to node [%v] ", prefix)
//TODO: extract this into a utils function
c1 := exec.Command("kubectl", "--namespace="+namespace, "get", "service")
if info {
fmt.Println(c1.String())
}
c2 := exec.Command("grep", prefix)
r, w := io.Pipe()
c1.Stdout = w
c2.Stdin = r
var b2 bytes.Buffer
c2.Stdout = &b2
c1.Start()
c2.Start()
c1.Wait()
w.Close()
c2.Wait()
srvOutput := b2.String()
//fmt.Println("srvOutput", srvOutput)
// split output on new line, this will add an extra empty entry in the array, e.g. if 1 item is returned, there
// will be 2 items in the array.
srvNames := strings.Split(srvOutput, "\n")
srvNames = srvNames[:len(srvNames)-1]
//fmt.Println("srvNames", srvNames)
//fmt.Println("There are ", len(srvNames))
//serviceCt := len(srvNames)
var names []string
for _, s := range srvNames {
name := strings.Split(s, " ")[0]
//fmt.Println("name: ", name)
names = append(names, name)
}
return names
// if the result returns more than one service entry this will only return the first one.
//srvName := strings.Split(srvOutput, " ")[0]
//return srvName
}
func serviceForPrefix(prefix string, namespace string, info bool) string {
c1 := exec.Command("kubectl", "--namespace="+namespace, "get", "service")
if info {
fmt.Println(c1.String())
}
c2 := exec.Command("grep", prefix)
r, w := io.Pipe()
c1.Stdout = w
c2.Stdin = r
var b2 bytes.Buffer
c2.Stdout = &b2
c1.Start()
c2.Start()
c1.Wait()
w.Close()
c2.Wait()
srvOutput := b2.String()
return srvOutput
}
// get the clusterIP for the given service
// serviceOutput is the output of kubectl get service for a single service.
func clusterIpForService(serviceOutputStr string) string {
c1 := exec.Command("echo", serviceOutputStr)
c2 := exec.Command("awk", `{print $3}`)
//fmt.Println(c1.String())
//fmt.Println(c2.String())
r, w := io.Pipe()
c1.Stdout = w
c2.Stdin = r
var out bytes.Buffer
c2.Stdout = &out
c1.Start()
c2.Start()
c1.Wait()
w.Close()
c2.Wait()
clusterIp := out.String()
//fmt.Println("" + clusterIp)
// if err := c2.Run(); err != nil {
// log.Fatal(err)
// }
return strings.TrimSpace(clusterIp)
}
// get the NodePort for the given service output and the clusterPort (internal K8s port)
// serviceOutput is the output of kubectl get service for a single service.
// TODO slice awk output on ','
func nodePortFormClusterPort(serviceOutputStr string, clusterPort string) string {
c1 := exec.Command("echo", serviceOutputStr)
c2 := exec.Command("awk", `{print $5}`)
//fmt.Println(c1.String())
//fmt.Println(c2.String())
r, w := io.Pipe()
c1.Stdout = w
c2.Stdin = r
var out bytes.Buffer
c2.Stdout = &out
c1.Start()
c2.Start()
c1.Wait()
w.Close()
c2.Wait()
// out contains all nodeportsi, e.g. 9001:30589/TCP,9080:30151/TCP,8545:32119/TCP,8546:30510/TCP,30303:32238/TCP
nodePortOutput := strings.TrimSpace(out.String())
// example nodePort output: 9080:31973/TCP,8545:32734/TCP
nodePorts := strings.Split(nodePortOutput, ",")
//fmt.Println(fmt.Sprintf("nodePorts [%v]", nodePorts))
// only one nodePort no filtering, just return the nodePort, e.g. cakeshop
var nodePort string
if len(nodePorts) == 1 {
// get 8545:32734/TCP
internalAndNodePort := strings.Split(nodePorts[0], ":")
nodePortAndProtocol := strings.Split(internalAndNodePort[1], "/")
nodePort = nodePortAndProtocol[0]
} else if clusterPort != "" {
for _, nodePortEntry := range nodePorts {
internalAndNodePort := strings.Split(nodePortEntry, ":")
_clusterPort := internalAndNodePort[0]
// obtain the nodePort associated with the clusterIp
if clusterPort == _clusterPort {
nodePortAndProtocol := strings.Split(internalAndNodePort[1], "/")
nodePort = nodePortAndProtocol[0]
}
}
}
return nodePort
}
func nodePortForService(serviceOutputStr string) string {
return nodePortFormClusterPort(serviceOutputStr, "")
}
func showPods(namespace string) {
cmd := exec.Command("kubectl", "--namespace="+namespace, "get", "pods")
var b bytes.Buffer
cmd.Stdout = &b
fmt.Print(b.String())
}
// https://www.reddit.com/r/golang/comments/2nd4pq/how_can_i_open_an_interactive_subprogram_from/
// runs a subcommand in interactive mode.
func dropIntoCmd(cmd *exec.Cmd) error {
//log.Printf(cmd.String())
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
err := cmd.Run()
return err
}
func dropIntoCmdQuiet(cmd *exec.Cmd) error {
// cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
err := cmd.Run()
return err
}
// do not display the command output
func runCmdQuiet(cmd *exec.Cmd) error {
err := cmd.Run()
if err != nil {
return err
}
return nil
}
func runCmd(cmd *exec.Cmd) (bytes.Buffer, error) {
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
return out, err
}
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
func waitForPodsReadyState(qconfigYaml QConfig, namespace string) {
nodeNames := getNodeNames(qconfigYaml)
allContainersReady := false
nodeNotDeployed := false
var nodeNotDeployedName string
for {
if allContainersReady {
break
}
allContainersReady = true
if nodeNotDeployed {
fmt.Println()
red.Println(fmt.Sprintf(" node [%s] found in config, but not deploy to k8s, try running:", nodeNotDeployedName))
fmt.Println()
red.Println(" > qctl generate network --update ")
red.Println(" > qctl deploy network ")
fmt.Println()
break
}
// loop through this till 2/2 RUNNING is true for all nodes in the config file.
// if any pod doesn't have both containers running break, set to false, wait and loop again.
for i := 0; i < len(nodeNames); i++ {
podName := podNameFromPrefix(nodeNames[i], namespace)
// get the full pod status for the current pod, e.g.:
// NAME READY STATUS RESTARTS AGE
// node5-deployment-54d7d99575-ztgbv 2/2 Running 0 9h
c1 := exec.Command("kubectl", "--namespace="+namespace, "get", "pod", podName)
// get the pod status part only ignoring the header, e.g.:
// node5-deployment-54d7d99575-ztgbv 2/2 Running 0 9h
c2 := exec.Command("grep", podName)
r, w := io.Pipe()
c1.Stdout = w
c2.Stdin = r
var b2 bytes.Buffer
c2.Stdout = &b2
c1.Start()
c2.Start()
c1.Wait()
w.Close()
c2.Wait()
b2.String()
podOutput := b2.String()
podParts := strings.Fields(podOutput)
isRunning := false
if len(podParts) < 1 {
allContainersReady = false
nodeNotDeployed = true
nodeNotDeployedName = nodeNames[i]
break
}
status := podParts[2]
if strings.ToUpper(status) == "RUNNING" {
isRunning = true
} else { // break
allContainersReady = false
fmt.Println(podOutput)
red.Println("not in running state")
red.Println(strings.ToUpper(status))
red.Println("wait for 5 seconds before checking the network again")
time.Sleep(5 * time.Second)
break
}
if isRunning {
// make sure both container are ready, e.g. 2/2 Running:
// node5-deployment-54d7d99575-ztgbv 2/2 Running
numContainersRunning := podParts[1]
if numContainersRunning == "2/2" {
allContainersReady = true
} else { // break
fmt.Println(podOutput)
red.Println("not all containers are running")
red.Println(fmt.Sprintf("num container running: [%s]", numContainersRunning))
red.Println("wait for 5 seconds before checking the network again")
allContainersReady = false
time.Sleep(5 * time.Second)
break
}
}
}
}
fmt.Println()
firstNode := nodeNames[0]
green.Println(" Your Quorum network is ready to go!")
fmt.Println()
green.Println(" To run the test contract, check the block number and")
green.Println(" geth attach to a node in the network, run: ")
fmt.Println()
green.Println(fmt.Sprintf(" > qctl test contract %s", firstNode))
green.Println(fmt.Sprintf(" > qctl geth exec %s 'eth.blockNumber'", firstNode))
green.Println(fmt.Sprintf(" > qctl geth attach %s", firstNode))
fmt.Println()
fmt.Println()
return
}
func getNodeNames(configFileYaml QConfig) []string {
var names []string
for i := 0; i < len(configFileYaml.Nodes); i++ {
names = append(names, configFileYaml.Nodes[i].NodeUserIdent)
}
return names
}