forked from tomtruitt/eris-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chains.go
576 lines (473 loc) · 19 KB
/
chains.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
package commands
import (
"fmt"
"strings"
chns "github.com/eris-ltd/eris-cli/chains"
. "github.com/eris-ltd/eris-cli/Godeps/_workspace/src/github.com/eris-ltd/common/go/common"
"github.com/eris-ltd/eris-cli/Godeps/_workspace/src/github.com/spf13/cobra"
)
//----------------------------------------------------------------------
// cli definitions
// Primary Chains Sub-Command
var Chains = &cobra.Command{
Use: "chains",
Short: "Start, Stop, and Manage Blockchains.",
Long: `Start, Stop, and Manage Blockchains.
The chains subcommand is used to work on erisdb smart contract
blockchain networks. The name is not perfect, as eris is able
to operate a wide variety of blockchains out of the box. Most
of those existing blockchains should be ran via the
eris services ...
commands. As they fall under the rubric of "things I just want
to turn on or off". While you can develop against those
blockchains, you generally aren't developing those blockchains
themselves.
Eris chains is built to help you build blockchains. It is our
opinionated gateway to the wonderful world of permissioned
smart contract networks.
Your own baby blockchain/smart contract machine is just an
eris chains new
away!`,
Run: func(cmd *cobra.Command, args []string) { cmd.Help() },
}
// Build the chains subcommand
func buildChainsCommand() {
Chains.AddCommand(chainsNew)
Chains.AddCommand(chainsRegister)
Chains.AddCommand(chainsInstall)
Chains.AddCommand(chainsImport)
Chains.AddCommand(chainsListKnown)
Chains.AddCommand(chainsList)
Chains.AddCommand(chainsCheckout)
Chains.AddCommand(chainsHead)
Chains.AddCommand(chainsPlop)
Chains.AddCommand(chainsPorts)
Chains.AddCommand(chainsEdit)
Chains.AddCommand(chainsExec)
Chains.AddCommand(chainsStart)
Chains.AddCommand(chainsLogs)
Chains.AddCommand(chainsListRunning)
Chains.AddCommand(chainsInspect)
Chains.AddCommand(chainsStop)
Chains.AddCommand(chainsExport)
Chains.AddCommand(chainsRename)
Chains.AddCommand(chainsUpdate)
Chains.AddCommand(chainsRemove)
Chains.AddCommand(chainsGraduate)
Chains.AddCommand(chainsCat)
addChainsFlags()
}
// Chains Sub-sub-Commands
var chainsNew = &cobra.Command{
Use: "new [name]",
Short: "Creates a new blockhain.",
Long: `Creates a new blockchain.
Will use a default genesis.json unless a --genesis flag is passed.
Still a WIP.`,
Run: NewChain,
}
var chainsRegister = &cobra.Command{
Use: "register [name]",
Short: "Registers a blockchain on etcb (a blockchain for registering other blockchains",
Long: "Registers a blockchain on etcb (a blockchain for registering other blockchains",
Run: RegisterChain,
}
var chainsInstall = &cobra.Command{
Use: "install [chainID]",
Short: "Install a blockchain from the etcb registry.",
Long: `Install a blockchain from the etcb registry.
Install an existing erisdb based blockchain for use locally.
Still a WIP.`,
Run: InstallChain,
}
var chainsListKnown = &cobra.Command{
Use: "known",
Short: "List all the blockchains Eris knows about.",
Long: `Lists the blockchains which Eris has installed for you.
To hash a new blockchain, use:
eris chains new
To install and fetch a blockchain from a chain definition
file, use:
eris chains install
Services include all other chain types supported by the
Eris platform.
Services are handled using the [eris services] command.`,
Run: ListKnownChains,
}
var chainsImport = &cobra.Command{
Use: "import [name] [location]",
Short: "Import a chain definition file from Github or IPFS.",
Long: `Import a chain definition for your platform.
By default, Eris will import from ipfs.
To list known chains use: [eris chains known].`,
Example: " eris chains import 2gather QmNUhPtuD9VtntybNqLgTTevUmgqs13eMvo2fkCwLLx5MX",
Run: ImportChain,
}
var chainsList = &cobra.Command{
Use: "ls",
Short: "Lists all known blockchains in the Eris tree.",
Long: `Lists all known blockchains in the Eris tree.
To list the known chains: [eris chains known]
To list the running chains: [eris chains ps]
To start a chain use: [eris chains start chainName].
`,
Run: ListChains,
}
var chainsCheckout = &cobra.Command{
Use: "checkout",
Short: "Checks out a chain.",
Long: `Checks out a chain.
Checkout if a convenience features. For any eris command
which accepts a --chain or $chain variable, the checked
out chain can replace manually passing in a --chain flag.
If a --chain is passed to any command accepting --chain,
the --chain which is passed will overwrite any checked
out chain.
If command is given without arguments it will clear the
head and there will be no chain checked out.
`,
Run: CheckoutChain,
}
var chainsPlop = &cobra.Command{
Use: "plop",
Short: "Plop the genesis or config file",
Long: "Plop the genesis or config file",
Run: PlopChain,
}
var chainsPorts = &cobra.Command{
Use: "ports",
Short: "Print the port mapping",
Long: "Print the port mapping",
Run: PortsChain,
}
var chainsHead = &cobra.Command{
Use: "current",
Short: "The currently checked out chain.",
Long: `The currently checked out chain.
To checkout a new chain use eris chains checkout
`,
Run: CurrentChain,
}
var chainsEdit = &cobra.Command{
Use: "edit [name]",
Short: "Edit a blockchain.",
Long: `Edit a blockchain definition file.
Edit will utilize your default editor.
`,
Run: EditChain,
}
var chainsStart = &cobra.Command{
Use: "start",
Short: "Start a blockchain.",
Long: `Start a blockchain.
[eris chains start name] by default will put the chain into the
background so its logs will not be viewable from the command line.
To stop the chain use: [eris chains stop chainName].
To view a chain's logs use: [eris chains logs chainName].
`,
Run: StartChain,
}
var chainsLogs = &cobra.Command{
Use: "logs",
Short: "Display the logs of a blockchain.",
Long: `Display the logs of a blockchain.`,
Run: LogChain,
}
var chainsExec = &cobra.Command{
Use: "exec [serviceName]",
Short: "Run a command or interactive shell",
Long: `Run a command or interactive shell in a container
with volumes-from the data container`,
Run: ExecChain,
}
var chainsListRunning = &cobra.Command{
Use: "ps",
Short: "List the running blockchains.",
Long: `List the running blockchains.`,
Run: ListRunningChains,
}
var chainsStop = &cobra.Command{
Use: "stop [name]",
Short: "Stop a running blockchain.",
Long: `Stop a running blockchain.`,
Run: KillChain,
}
var chainsInspect = &cobra.Command{
Use: "inspect [chainName] [key]",
Short: "Machine readable chain operation details.",
Long: `Displays machine readable details about running containers.
Information available to the inspect command is provided by the
Docker API. For more information about return values,
see: https://github.com/fsouza/go-dockerclient/blob/master/container.go#L235`,
Example: ` eris chains inspect 2gather -> will display the entire information about 2gather containers
eris chains inspect 2gather name -> will display the name in machine readable format
eris chains inspect 2gather host_config.binds -> will display only that value`,
Run: InspectChain,
}
var chainsExport = &cobra.Command{
Use: "export [chainName]",
Short: "Export a chain definition file to IPFS.",
Long: `Export a chain definition file to IPFS.
Command will return a machine readable version of the IPFS hash
`,
Run: ExportChain,
}
var chainsRename = &cobra.Command{
Use: "rename [old] [new]",
Short: "Rename a blockchain.",
Long: `Rename a blockchain.`,
Run: RenameChain,
}
var chainsRemove = &cobra.Command{
Use: "rm [name]",
Short: "Removes an installed chain.",
Long: `Removes an installed chain.
Command will remove the chain's container but will not
remove the chain definition file.
Use the --force flag to also remove the chain definition file.`,
Run: RmChain,
}
var chainsUpdate = &cobra.Command{
Use: "update [name]",
Short: "Updates an installed chain.",
Long: `Updates an installed chain, or installs it if it has not been installed.
Functionally this command will perform the following sequence:
1. Stop the chain (if it is running)
2. Remove the container which ran the chain
3. Pull the image the container uses from a hub
4. Rebuild the container from the updated image
5. Restart the chain (if it was previously running)
**NOTE**: If the chain uses data containers those will not be affected
by the update command.`,
Run: UpdateChain,
}
var chainsGraduate = &cobra.Command{
Use: "graduate",
Short: "Graduates a chain to a service.",
Long: `Graduates a chain to a service by laying a service definition file with the chain_id`,
Run: GraduateChain,
}
var chainsCat = &cobra.Command{
Use: "cat [name]",
Short: "Displays chains definition file.",
Long: `Displays chains definition file.
Command will cat local chains definition file.`,
Run: CatChain,
}
//----------------------------------------------------------------------
func addChainsFlags() {
chainsNew.PersistentFlags().StringVarP(&do.GenesisFile, "genesis", "g", "", "genesis.json file")
chainsNew.PersistentFlags().StringVarP(&do.ConfigFile, "config", "c", "", "config.toml file")
chainsNew.PersistentFlags().StringSliceVarP(&do.ConfigOpts, "options", "", nil, "space separated <key>=<value> pairs to set in config.toml")
chainsNew.PersistentFlags().StringVarP(&do.Path, "dir", "", "", "a directory whose contents should be copied into the chain's main dir")
chainsNew.PersistentFlags().StringVarP(&do.ServerConf, "serverconf", "", "", "pass in a server_conf.toml file")
chainsNew.PersistentFlags().StringVarP(&do.CSV, "csv", "", "", "render a genesis.json from a csv file")
chainsNew.PersistentFlags().StringVarP(&do.Priv, "priv", "", "", "pass in a priv_validator.json file (dev-only!)")
chainsNew.PersistentFlags().UintVarP(&do.N, "N", "", 1, "make a new genesis.json with this many validators and create data containers for each")
chainsNew.PersistentFlags().BoolVarP(&do.Operations.PublishAllPorts, "publish", "p", false, "publish random ports")
chainsNew.PersistentFlags().BoolVarP(&do.Run, "api", "a", false, "turn the chain on using erisdb's api")
chainsNew.PersistentFlags().StringSliceVarP(&do.Env, "env", "e", nil, "multiple env vars can be passed using the KEY1=val1,KEY2=val1 syntax")
chainsNew.PersistentFlags().StringSliceVarP(&do.Links, "links", "l", nil, "multiple containers can be linked using the KEY1:val1,KEY2:val1 syntax")
chainsRegister.PersistentFlags().StringVarP(&do.Pubkey, "pub", "p", "", "pubkey to use for registering the chain in etcb")
chainsRegister.PersistentFlags().StringSliceVarP(&do.Links, "links", "l", nil, "multiple containers can be linked using the KEY1:val1,KEY2:val1 syntax")
chainsRegister.PersistentFlags().StringSliceVarP(&do.Env, "env", "e", nil, "multiple env vars can be passed using the KEY1:val1,KEY2:val1 syntax")
chainsRegister.PersistentFlags().StringVarP(&do.Gateway, "etcb-host", "", "interblock.io:46657", "set the address of the etcb chain")
chainsRegister.PersistentFlags().StringVarP(&do.ChainID, "etcb-chain", "", "etcb_testnet", "set the chain id of the etcb chain")
chainsInstall.PersistentFlags().StringVarP(&do.ConfigFile, "config", "c", "", "main config file for the chain")
chainsInstall.PersistentFlags().StringVarP(&do.ServerConf, "serverconf", "", "", "pass in a server_conf.toml file")
chainsInstall.PersistentFlags().StringVarP(&do.Path, "dir", "", "", "a directory whose contents should be copied into the chain's main dir")
chainsInstall.PersistentFlags().StringVarP(&do.ChainID, "id", "", "", "id of the chain to fetch")
chainsInstall.PersistentFlags().BoolVarP(&do.Operations.PublishAllPorts, "publish", "p", false, "publish random ports")
chainsInstall.PersistentFlags().StringSliceVarP(&do.Env, "env", "e", nil, "multiple env vars can be passed using the KEY1=val1,KEY2=val1 syntax")
chainsInstall.PersistentFlags().StringSliceVarP(&do.Links, "links", "l", nil, "multiple containers can be linked can be passed using the KEY1:val1,KEY2:val1 syntax")
chainsInstall.PersistentFlags().StringVarP(&do.Gateway, "etcb-host", "", "interblock.io:46657", "set the address of the etcb chain")
chainsInstall.PersistentFlags().IntVarP(&do.Operations.ContainerNumber, "N", "N", 1, "container number")
chainsStart.PersistentFlags().BoolVarP(&do.Operations.PublishAllPorts, "publish", "p", false, "publish random ports")
chainsStart.PersistentFlags().BoolVarP(&do.Run, "api", "a", false, "turn the chain on using erisdb's api")
chainsStart.PersistentFlags().StringSliceVarP(&do.Env, "env", "e", nil, "multiple env vars can be passed using the KEY1=val1,KEY2=val1 syntax")
chainsStart.PersistentFlags().StringSliceVarP(&do.Links, "links", "l", nil, "multiple containers can be linked can be passed using the KEY1:val1,KEY2:val1 syntax")
chainsLogs.Flags().BoolVarP(&do.Follow, "follow", "f", false, "follow logs, like tail -f")
chainsLogs.Flags().StringVarP(&do.Tail, "tail", "t", "150", "number of lines to show from end of logs")
chainsExec.Flags().BoolVarP(&do.Interactive, "interactive", "i", false, "interactive shell")
chainsRemove.Flags().BoolVarP(&do.File, "file", "f", false, "remove chain definition file as well as chain container")
chainsRemove.Flags().BoolVarP(&do.RmD, "data", "x", false, "remove data containers also")
chainsUpdate.Flags().BoolVarP(&do.SkipPull, "pull", "p", true, "pull an updated version of the chain's base service image from docker hub")
chainsUpdate.Flags().UintVarP(&do.Timeout, "timeout", "t", 10, "manually set the timeout; overridden by --force")
chainsUpdate.PersistentFlags().StringSliceVarP(&do.Env, "env", "e", nil, "multiple env vars can be passed using the KEY1=val1,KEY2=val1 syntax")
chainsUpdate.PersistentFlags().StringSliceVarP(&do.Links, "links", "l", nil, "multiple containers can be linked can be passed using the KEY1:val1,KEY2:val1 syntax")
chainsStop.Flags().BoolVarP(&do.Rm, "rm", "r", false, "remove containers after stopping")
chainsStop.Flags().BoolVarP(&do.RmD, "data", "x", false, "remove data containers after stopping")
chainsStop.Flags().BoolVarP(&do.Force, "force", "f", false, "kill the container instantly without waiting to exit")
chainsStop.Flags().UintVarP(&do.Timeout, "timeout", "t", 10, "manually set the timeout; overridden by --force")
chainsList.Flags().BoolVarP(&do.Quiet, "quiet", "q", false, "machine parsable output")
chainsListRunning.Flags().BoolVarP(&do.Quiet, "quiet", "q", false, "machine parsable output")
}
//----------------------------------------------------------------------
// cli command wrappers
func StartChain(cmd *cobra.Command, args []string) {
// [csk]: if no args should we just start the checkedout chain?
IfExit(ArgCheck(1, "ge", cmd, args))
do.Name = args[0]
IfExit(chns.StartChain(do))
}
func LogChain(cmd *cobra.Command, args []string) {
// [csk]: if no args should we just start the checkedout chain?
IfExit(ArgCheck(1, "ge", cmd, args))
do.Name = args[0]
IfExit(chns.LogsChain(do))
}
func ExecChain(cmd *cobra.Command, args []string) {
IfExit(ArgCheck(1, "ge", cmd, args))
do.Name = args[0]
// if interactive, we ignore args. if not, run args as command
if !do.Interactive {
if len(args) < 2 {
Exit(fmt.Errorf("Non-interactive exec sessions must provide arguments to execute"))
}
args = args[1:]
}
if len(args) == 1 {
args = strings.Split(args[0], " ")
}
do.Args = args
IfExit(chns.ExecChain(do))
}
func KillChain(cmd *cobra.Command, args []string) {
// [csk]: if no args should we just start the checkedout chain?
IfExit(ArgCheck(1, "ge", cmd, args))
do.Name = args[0]
IfExit(chns.KillChain(do))
}
// fetch and install a chain
//
// the idea here is you will either specify a chainName as the arg and that will
// double as the chainID, or you want a local reference name for the chain, so you specify
// the chainID with a flag and give your local reference name as the arg
func InstallChain(cmd *cobra.Command, args []string) {
IfExit(ArgCheck(1, "ge", cmd, args))
do.Name = args[0]
IfExit(chns.InstallChain(do))
}
// create a new chain
//
// genesis is either given or a simple single-validator genesis will be laid for you
func NewChain(cmd *cobra.Command, args []string) {
IfExit(ArgCheck(1, "ge", cmd, args))
do.Name = args[0]
IfExit(chns.NewChain(do))
}
// register a chain in the etcb chain registry
func RegisterChain(cmd *cobra.Command, args []string) {
IfExit(ArgCheck(2, "ge", cmd, args))
do.Name = args[0]
do.Args = args[1:]
IfExit(chns.RegisterChain(do))
}
// import a chain definition file
func ImportChain(cmd *cobra.Command, args []string) {
IfExit(ArgCheck(2, "eq", cmd, args))
do.Name = args[0]
do.Path = args[1]
IfExit(chns.ImportChain(do))
}
// checkout a chain
func CheckoutChain(cmd *cobra.Command, args []string) {
if len(args) >= 1 {
do.Name = args[0]
} else {
do.Name = ""
}
IfExit(chns.CheckoutChain(do))
}
func CurrentChain(cmd *cobra.Command, args []string) {
IfExit(chns.CurrentChain(do))
}
func PlopChain(cmd *cobra.Command, args []string) {
IfExit(ArgCheck(2, "eq", cmd, args))
do.ChainID = args[0]
do.Type = args[1]
if len(args) > 2 {
do.Args = args[2:]
}
IfExit(chns.PlopChain(do))
}
func PortsChain(cmd *cobra.Command, args []string) {
IfExit(ArgCheck(2, "ge", cmd, args))
do.Name = args[0]
do.Args = args[1:]
IfExit(chns.PortsChain(do))
}
// edit a chain definition file
func EditChain(cmd *cobra.Command, args []string) {
// [csk]: if no args should we just start the checkedout chain?
IfExit(ArgCheck(1, "ge", cmd, args))
var configVals []string
if len(args) > 1 {
configVals = args[1:]
}
do.Name = args[0]
do.Args = configVals
IfExit(chns.EditChain(do))
}
func InspectChain(cmd *cobra.Command, args []string) {
// [csk]: if no args should we just start the checkedout chain?
IfExit(ArgCheck(1, "ge", cmd, args))
do.Name = args[0]
if len(args) == 1 {
do.Args = []string{"all"}
} else {
do.Args = []string{args[1]}
}
IfExit(chns.InspectChain(do))
}
func ExportChain(cmd *cobra.Command, args []string) {
// [csk]: if no args should we just start the checkedout chain?
IfExit(ArgCheck(1, "ge", cmd, args))
do.Name = args[0]
IfExit(chns.ExportChain(do))
}
func ListKnownChains(cmd *cobra.Command, args []string) {
if err := chns.ListKnown(do); err != nil {
return
}
fmt.Println(do.Result)
}
func ListChains(cmd *cobra.Command, args []string) {
if err := chns.ListExisting(do); err != nil {
return
}
}
func ListRunningChains(cmd *cobra.Command, args []string) {
if err := chns.ListRunning(do); err != nil {
return
}
}
func RenameChain(cmd *cobra.Command, args []string) {
IfExit(ArgCheck(2, "eq", cmd, args))
do.Name = args[0]
do.NewName = args[1]
IfExit(chns.RenameChain(do))
}
func UpdateChain(cmd *cobra.Command, args []string) {
// [csk]: if no args should we just start the checkedout chain?
IfExit(ArgCheck(1, "ge", cmd, args))
do.Name = args[0]
IfExit(chns.UpdateChain(do))
}
func RmChain(cmd *cobra.Command, args []string) {
IfExit(ArgCheck(1, "ge", cmd, args))
do.Name = args[0]
IfExit(chns.RmChain(do))
}
func GraduateChain(cmd *cobra.Command, args []string) {
// [csk]: if no args should we just start the checkedout chain?
IfExit(ArgCheck(1, "ge", cmd, args))
do.Name = args[0]
IfExit(chns.GraduateChain(do))
}
func CatChain(cmd *cobra.Command, args []string) {
// [csk]: if no args should we just start the checkedout chain?
IfExit(ArgCheck(1, "ge", cmd, args))
do.Name = args[0]
IfExit(chns.CatChain(do))
}