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
/
Copy pathconfigcmd.go
359 lines (335 loc) · 12.6 KB
/
configcmd.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
package main
import (
"fmt"
"io/ioutil"
"os/exec"
"strings"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
var (
// qctl generate config --qversion=21.7.1 --consensus=istanbul --tmversion=21.7.2 --tm=tessera --num=4
// qctl generate config --num=5 --qversion=21.7.1
initConfigCommand = cli.Command{
Name: "init",
Usage: "creates a base qubernetes.yaml file which can be used to create a Quorum network.",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config, c",
Usage: "Load configuration from `FULL_PATH_FILE`",
EnvVars: []string{"QUBE_CONFIG"},
},
&cli.IntFlag{
Name: "num",
Usage: "Number of nodes in the network.",
Value: DefaultNodeNumber,
},
&cli.StringFlag{
Name: "consensus",
Usage: "Consensus to use raft | istanbul | clique | qbft.",
Value: DefaultConsensus,
},
&cli.StringFlag{
Name: "testqbftblock",
Usage: "Blocknumber at which the network will switch over from ibft to qbft.",
Value: "0",
},
&cli.StringFlag{
Name: "qversion",
Aliases: []string{"qv"},
Usage: "Quorum Version.",
Value: DefaultQuorumVersion,
},
&cli.StringFlag{
Name: "tmversion",
Aliases: []string{"tmv"},
Usage: "transaction Manager Version.",
Value: DefaultTesseraVersion,
},
&cli.StringFlag{
Name: "tm",
Usage: "transaction Manager to user: tessera | constellation.",
Value: DefaultTmName,
},
&cli.StringFlag{
Name: "chainid",
Usage: "The chain id for the network.",
Value: DefaultChainId,
},
&cli.StringFlag{
Name: "qimagefull",
Usage: "The full repo + image name of the quorum image.",
},
&cli.StringFlag{
Name: "tmimagefull",
Usage: "The full repo + image name of the tm image.",
},
&cli.StringFlag{
Name: "gethparams",
Usage: "additional geth startup params to run on the node.",
},
&cli.BoolFlag{
Name: "monitor",
Aliases: []string{"monit"},
Usage: "enable monitoring on the geth / quorum node (prometheus).",
Value: false,
},
&cli.BoolFlag{
Name: "cakeshop",
Aliases: []string{"cake"},
Usage: "deploy cakeshop with the Quorum network.",
Value: false,
},
&cli.BoolFlag{
Name: "ingress",
Usage: "include a K8s ingress with the Quorum network.",
Value: false,
},
},
Action: func(c *cli.Context) error {
pwdCmd := exec.Command("pwd")
b, _ := runCmd(pwdCmd)
pwd := strings.TrimSpace(b.String())
// If the QUBE_CONFIG env is set or the flag passed in, use this file path and generate the config there.
// this is helpful when creating, deleting, networks repeatedly so that the config dirs can be set once and
// will be generated to the same place.
configFile := c.String("config")
if configFile == "" {
// no configuration file provided, check for flags and use the default.
configFile = pwd + "/qubernetes.generate.yaml"
}
// TODO: it might be nice to allow these to override the config file, load the config then set any additional
// params that were passed in.
numberNodes := c.Int("num")
quorumVersion := c.String("qversion")
tmVersion := c.String("tmversion")
transactionManager := c.String("tm")
consensus := c.String("consensus")
testqbftblock := c.String("testqbftblock")
chainId := c.String("chainid")
qimagefull := c.String("qimagefull")
tmImageFull := c.String("tmimagefull")
gethparams := c.String("gethparams")
isMonitoring := c.Bool("monitor")
isCakeshop := c.Bool("cakeshop")
// k8s specific
isIngress := c.Bool("ingress")
configYaml := QConfig{}
for i := 1; i <= numberNodes; i++ {
quorum := Quorum{
Consensus: consensus,
QuorumVersion: quorumVersion,
DockerRepoFull: qimagefull,
}
tm := Tm{
Name: transactionManager,
TmVersion: tmVersion,
DockerRepoFull: tmImageFull,
}
quorumEntry := QuorumEntry{
Quorum: quorum,
Tm: tm,
}
// for now we need to replace =" with =\" and *" with *\"
// this cleans the params for inserting them into the pod, as " must be escaped.
if gethparams != "" {
gethparams = strings.ReplaceAll(gethparams, "=\"", "=\\\"")
gethparams = strings.ReplaceAll(gethparams, "*\"", "*\\\"")
}
gethEntry := GethEntry{
GetStartupParams: gethparams,
}
nodeEntry := NodeEntry{
NodeUserIdent: fmt.Sprintf("quorum-node%d", i),
KeyDir: fmt.Sprintf("key%d", i),
QuorumEntry: quorumEntry,
GethEntry: gethEntry,
}
configYaml.Nodes = append(configYaml.Nodes, nodeEntry)
}
configYaml.Genesis.QuorumVersion = quorumVersion
configYaml.Genesis.TmVersion = tmVersion
configYaml.Genesis.Consensus = consensus
if consensus == "qbft" { // if --testqbftblock=BLOCK_NUM not specified set to default block 0, else the user can specify --testqbftblock=BLOCK_NUM
configYaml.Genesis.TestQBFTBlock = testqbftblock
}
configYaml.Genesis.Chain_Id = chainId
if isMonitoring {
//configYaml.Prometheus.NodePort = DefaultPrometheusNodePort
configYaml.Prometheus.Enabled = true
}
if isCakeshop {
configYaml.Cakeshop.Version = "latest"
}
if isIngress {
configYaml.K8s.Service.Type = ServiceTypeNodePort
configYaml.K8s.Service.Ingress.Strategy = "OneToMany"
}
configBytes := []byte(configYaml.ToString())
// try to write the generated file out to disk this file will be used to initialize the network.
err := ioutil.WriteFile(configFile, configBytes, 0644)
if err != nil {
log.Fatal("error writing configFile to [%v]. err: [%v]", configFile, err)
}
// TODO: check the config file was properly generated
// Set the configfile to the full path
if fileExists(configFile) {
// check if config file is full path or relative path.
if !strings.HasPrefix(configFile, "/") {
configFile = pwd + "/" + configFile
}
}
fmt.Println()
green.Println("=======================================================================================")
fmt.Println()
fmt.Println("Your Qubernetes config has been generated see:")
fmt.Println()
fmt.Println(" " + configFile)
fmt.Println()
fmt.Println("The Quorum network values are:")
fmt.Println()
// tell the default
green.Println(fmt.Sprintf(" num nodes = %d", numberNodes))
green.Println(fmt.Sprintf(" consensus = %s", consensus))
green.Println(fmt.Sprintf(" quorumVersion = %s", quorumVersion))
green.Println(fmt.Sprintf(" tmVersion = %s", tmVersion))
green.Println(fmt.Sprintf(" transactionManager = %s", transactionManager))
green.Println(fmt.Sprintf(" chainId = %s", chainId))
fmt.Println()
fmt.Println("To set this as your default config for future commands, run: ")
fmt.Println()
fmt.Println("**********************************************************************************************")
fmt.Println()
green.Println(fmt.Sprintf(" $> export QUBE_CONFIG=%s", configFile))
fmt.Println()
green.Println(fmt.Sprintf(" $> qctl generate network --create"))
fmt.Println()
fmt.Println("**********************************************************************************************")
return nil
},
}
describeConfigCommand = cli.Command{
Name: "config",
Usage: "displays info about the quberentes config.",
//#ArgsUsage: "[pod_substring]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config, c",
Usage: "Load configuration from `FULL_PATH_FILE`",
EnvVars: []string{"QUBE_CONFIG"},
//Required: true,
},
&cli.StringFlag{ // this is only required to get the enodeurl
Name: "k8sdir",
Usage: "The k8sdir (usually out) containing the output k8s resources",
EnvVars: []string{"QUBE_K8S_DIR"},
},
&cli.BoolFlag{
Name: "long, l",
Usage: "Display all relavent information from the config",
//Required: true,
},
},
Action: func(c *cli.Context) error {
pwdCmd := exec.Command("pwd")
b, _ := runCmd(pwdCmd)
pwd := strings.TrimSpace(b.String())
configFile := c.String("config")
outputLong := c.Bool("long")
k8sdir := c.String("k8sdir")
if configFile == "" {
c.App.Run([]string{"qctl", "help", "init"})
// QUBE_CONFIG or flag
fmt.Println()
fmt.Println()
red.Println(" --config flag must be provided.")
red.Println(" or ")
red.Println(" QUBE_CONFIG environment variable needs to be set to your config file.")
fmt.Println()
red.Println(" If you need to generate a qubernetes.yaml config use the command: ")
fmt.Println()
green.Println(" qctl generate config")
fmt.Println()
return cli.Exit("--config flag must be set to the fullpath of your config file.", 3)
}
// the config file must exist or this is an error.
if fileExists(configFile) {
// check if config file is full path or relative path.
if !strings.HasPrefix(configFile, "/") {
configFile = pwd + "/" + configFile
}
} else {
c.App.Run([]string{"qctl", "help", "init"})
return cli.Exit(fmt.Sprintf("ConfigFile must exist! Given configFile [%v]", configFile), 3)
}
configFileYaml, err := LoadYamlConfig(configFile)
if err != nil {
log.Fatal("config file [%v] could not be loaded into the valid qubernetes yaml. err: [%v]", configFile, err)
}
//TODO: get the global or passed in k8s dir.
fmt.Println()
fmt.Println("=======================================================================================")
fmt.Println()
green.Println(" Using qubernetes config file:")
fmt.Println()
fmt.Println(" " + configFile)
fmt.Println()
green.Println(" Using k8sdir:")
fmt.Println()
if k8sdir != "" {
fmt.Println(" " + k8sdir)
} else {
fmt.Println(" NOT SET")
}
fmt.Println()
fmt.Println()
fmt.Println(" To export:")
fmt.Println()
green.Println(" export QUBE_CONFIG=" + configFile)
if k8sdir != "" {
green.Println(" export QUBE_K8S_DIR=" + k8sdir)
}
fmt.Println()
fmt.Println("=======================================================================================")
fmt.Println()
// display the config contents
fmt.Println(" Network Configuration: ")
fmt.Println()
if outputLong {
displayConfigLong(configFileYaml)
} else {
fmt.Println("only display the first node")
fmt.Println("to display all nodes, run: qctl ls config --long")
displayConfigShort(configFileYaml)
}
fmt.Println()
return nil
},
}
)
// TODO: could be smarter here and only display nodes that differ from eachother, e.g. diff versions of quorum / tessera.
func displayConfigLong(configFileYaml QConfig) {
green.Println(fmt.Sprintf(" num nodes = %d", len(configFileYaml.Nodes)))
green.Println(fmt.Sprintf(" consensus = %s", configFileYaml.Genesis.Consensus))
green.Println(fmt.Sprintf(" quorumVersion = %s", configFileYaml.Genesis.QuorumVersion))
for i := 0; i < len(configFileYaml.Nodes); i++ {
fmt.Println()
green.Println(fmt.Sprintf(" [%s] transactionManager = %s", configFileYaml.Nodes[i].NodeUserIdent, configFileYaml.Nodes[i].QuorumEntry.Tm.Name))
green.Println(fmt.Sprintf(" [%s] tmVersion = %s", configFileYaml.Nodes[i].NodeUserIdent, configFileYaml.Nodes[i].QuorumEntry.Tm.TmVersion))
green.Println(fmt.Sprintf(" [%s] quorumVersion = %s", configFileYaml.Nodes[i].NodeUserIdent, configFileYaml.Nodes[i].QuorumEntry.Quorum.QuorumVersion))
green.Println(fmt.Sprintf(" [%s] consensus = %s", configFileYaml.Nodes[i].NodeUserIdent, configFileYaml.Nodes[i].QuorumEntry.Quorum.Consensus))
green.Println(fmt.Sprintf(" [%s] chainId = %s", configFileYaml.Nodes[i].NodeUserIdent, configFileYaml.Genesis.Chain_Id))
fmt.Println()
}
}
func displayConfigShort(configFileYaml QConfig) {
green.Println(fmt.Sprintf(" num nodes = %d", len(configFileYaml.Nodes)))
green.Println(fmt.Sprintf(" consensus = %s", configFileYaml.Genesis.Consensus))
green.Println(fmt.Sprintf(" quorumVersion = %s", configFileYaml.Genesis.QuorumVersion))
fmt.Println()
green.Println(fmt.Sprintf(" [%s] transactionManager = %s", configFileYaml.Nodes[0].NodeUserIdent, configFileYaml.Nodes[0].QuorumEntry.Tm.Name))
green.Println(fmt.Sprintf(" [%s] tmVersion = %s", configFileYaml.Nodes[0].NodeUserIdent, configFileYaml.Nodes[0].QuorumEntry.Tm.TmVersion))
green.Println(fmt.Sprintf(" [%s] quorumVersion = %s", configFileYaml.Nodes[0].NodeUserIdent, configFileYaml.Nodes[0].QuorumEntry.Quorum.QuorumVersion))
green.Println(fmt.Sprintf(" [%s] consensus = %s", configFileYaml.Nodes[0].NodeUserIdent, configFileYaml.Nodes[0].QuorumEntry.Quorum.Consensus))
green.Println(fmt.Sprintf(" [%s] chainId = %s", configFileYaml.Nodes[0].NodeUserIdent, configFileYaml.Genesis.Chain_Id))
}