-
Notifications
You must be signed in to change notification settings - Fork 105
/
commands.go
581 lines (490 loc) · 15.6 KB
/
commands.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
577
578
579
580
581
package service
import (
"fmt"
"strings"
"github.com/urfave/cli"
"github.com/rocket-pool/smartnode/shared"
"github.com/rocket-pool/smartnode/shared/services/config"
cfgtypes "github.com/rocket-pool/smartnode/shared/types/config"
cliutils "github.com/rocket-pool/smartnode/shared/utils/cli"
)
// Creates CLI argument flags from the parameters of the configuration struct
func createFlagsFromConfigParams(sectionName string, params []*cfgtypes.Parameter, configFlags []cli.Flag, network cfgtypes.Network) []cli.Flag {
for _, param := range params {
var paramName string
if sectionName == "" {
paramName = param.ID
} else {
paramName = fmt.Sprintf("%s-%s", sectionName, param.ID)
}
defaultVal, err := param.GetDefault(network)
if err != nil {
panic(fmt.Sprintf("Error getting default value for [%s]: %s\n", paramName, err.Error()))
}
switch param.Type {
case cfgtypes.ParameterType_Bool:
configFlags = append(configFlags, cli.BoolFlag{
Name: paramName,
Usage: fmt.Sprintf("%s\n\tType: bool\n", param.Description),
})
case cfgtypes.ParameterType_Int:
configFlags = append(configFlags, cli.IntFlag{
Name: paramName,
Usage: fmt.Sprintf("%s\n\tType: int\n", param.Description),
Value: int(defaultVal.(int64)),
})
case cfgtypes.ParameterType_Float:
configFlags = append(configFlags, cli.Float64Flag{
Name: paramName,
Usage: fmt.Sprintf("%s\n\tType: float\n", param.Description),
Value: defaultVal.(float64),
})
case cfgtypes.ParameterType_String:
configFlags = append(configFlags, cli.StringFlag{
Name: paramName,
Usage: fmt.Sprintf("%s\n\tType: string\n", param.Description),
Value: defaultVal.(string),
})
case cfgtypes.ParameterType_Uint:
configFlags = append(configFlags, cli.UintFlag{
Name: paramName,
Usage: fmt.Sprintf("%s\n\tType: uint\n", param.Description),
Value: uint(defaultVal.(uint64)),
})
case cfgtypes.ParameterType_Uint16:
configFlags = append(configFlags, cli.UintFlag{
Name: paramName,
Usage: fmt.Sprintf("%s\n\tType: uint16\n", param.Description),
Value: uint(defaultVal.(uint16)),
})
case cfgtypes.ParameterType_Choice:
optionStrings := []string{}
for _, option := range param.Options {
optionStrings = append(optionStrings, fmt.Sprint(option.Value))
}
configFlags = append(configFlags, cli.StringFlag{
Name: paramName,
Usage: fmt.Sprintf("%s\n\tType: choice\n\tOptions: %s\n", param.Description, strings.Join(optionStrings, ", ")),
Value: fmt.Sprint(defaultVal),
})
}
}
return configFlags
}
// Register commands
func RegisterCommands(app *cli.App, name string, aliases []string) {
configFlags := []cli.Flag{}
cfgTemplate := config.NewRocketPoolConfig("", false)
network := cfgTemplate.Smartnode.Network.Value.(cfgtypes.Network)
// Root params
configFlags = createFlagsFromConfigParams("", cfgTemplate.GetParameters(), configFlags, network)
// Subconfigs
for sectionName, subconfig := range cfgTemplate.GetSubconfigs() {
configFlags = createFlagsFromConfigParams(sectionName, subconfig.GetParameters(), configFlags, network)
}
app.Commands = append(app.Commands, cli.Command{
Name: name,
Aliases: aliases,
Usage: "Manage Rocket Pool service",
Flags: []cli.Flag{
cli.StringSliceFlag{
Name: "compose-file, f",
Usage: "Optional compose files to override the standard Rocket Pool docker compose YAML files; this flag may be defined multiple times",
},
},
Subcommands: []cli.Command{
{
Name: "install",
Aliases: []string{"i"},
Usage: "Install the Rocket Pool service",
UsageText: "rocketpool service install [options]",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "yes, y",
Usage: "Automatically confirm service installation",
},
cli.BoolFlag{
Name: "verbose, r",
Usage: "Print installation script command output",
},
cli.BoolFlag{
Name: "no-deps, d",
Usage: "Do not install Operating System dependencies",
},
cli.StringFlag{
Name: "path, p",
Usage: "A custom path to install Rocket Pool to",
},
cli.StringFlag{
Name: "version, v",
Usage: "The smart node package version to install",
Value: fmt.Sprintf("v%s", shared.RocketPoolVersion),
},
},
Action: func(c *cli.Context) error {
// Validate args
if err := cliutils.ValidateArgCount(c, 0); err != nil {
return err
}
// Run command
return installService(c)
},
},
{
Name: "config",
Aliases: []string{"c"},
Usage: "Configure the Rocket Pool service",
UsageText: "rocketpool service config",
Flags: configFlags,
Action: func(c *cli.Context) error {
// Validate args
if err := cliutils.ValidateArgCount(c, 0); err != nil {
return err
}
// Run command
return configureService(c)
},
},
{
Name: "status",
Aliases: []string{"u"},
Usage: "View the Rocket Pool service status",
UsageText: "rocketpool service status",
Action: func(c *cli.Context) error {
// Validate args
if err := cliutils.ValidateArgCount(c, 0); err != nil {
return err
}
// Run command
return serviceStatus(c)
},
},
{
Name: "start",
Aliases: []string{"s"},
Usage: "Start the Rocket Pool service",
UsageText: "rocketpool service start",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "ignore-slash-timer",
Usage: "Bypass the safety timer that forces a delay when switching to a new ETH2 client",
},
cli.BoolFlag{
Name: "yes, y",
Usage: "Ignore service config prompt after upgrading",
},
},
Action: func(c *cli.Context) error {
// Validate args
if err := cliutils.ValidateArgCount(c, 0); err != nil {
return err
}
// Run command
return startService(c, false)
},
},
{
Name: "pause",
Aliases: []string{"p"},
Usage: "Pause the Rocket Pool service",
UsageText: "rocketpool service pause [options]",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "yes, y",
Usage: "Automatically confirm service suspension",
},
},
Action: func(c *cli.Context) error {
// Validate args
if err := cliutils.ValidateArgCount(c, 0); err != nil {
return err
}
// Run command
_, err := pauseService(c)
return err
},
},
{
Name: "stop",
Aliases: []string{"o"},
Usage: "Pause the Rocket Pool service (alias of 'rocketpool service pause')",
UsageText: "rocketpool service stop [options]",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "yes, y",
Usage: "Automatically confirm service suspension",
},
},
Action: func(c *cli.Context) error {
// Validate args
if err := cliutils.ValidateArgCount(c, 0); err != nil {
return err
}
// Run command
_, err := pauseService(c)
return err
},
},
{
Name: "reset-docker",
Aliases: []string{"rd"},
Usage: "Cleanup Docker resources, including stopped containers, unused images and networks. Stops and restarts Smartnode.",
UsageText: "rocketpool service reset [options]",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "yes, y",
Usage: "Automatically confirm service suspension",
},
cli.BoolFlag{
Name: "all, a",
Usage: "Removes all Docker images, including those currently used by the Smartnode stack. This will force a full re-download of all images when the Smartnode is restarted.",
},
},
Action: func(c *cli.Context) error {
// Validate args
if err := cliutils.ValidateArgCount(c, 0); err != nil {
return err
}
// Run command
return resetDocker(c)
},
},
{
Name: "prune-docker",
Aliases: []string{"pd"},
Usage: "Cleanup unused Docker resources, including stopped containers, unused images, networks and volumes. Does not restart smartnode, so the running containers and the images and networks they reference will not be pruned.",
UsageText: "rocketpool service prune",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "all, a",
Usage: "Removes all Docker images, including those currently used by the Smartnode stack. This will force a full re-download of all images when the Smartnode is restarted.",
},
},
Action: func(c *cli.Context) error {
// Validate args
if err := cliutils.ValidateArgCount(c, 0); err != nil {
return err
}
// Run command
return pruneDocker(c)
},
},
{
Name: "logs",
Aliases: []string{"l"},
Usage: "View the Rocket Pool service logs",
UsageText: "rocketpool service logs [options] [services...]",
Flags: []cli.Flag{
cli.StringFlag{
Name: "tail, t",
Usage: "The number of lines to show from the end of the logs (number or \"all\")",
Value: "100",
},
},
Action: func(c *cli.Context) error {
// Run command
return serviceLogs(c, c.Args()...)
},
},
{
Name: "stats",
Aliases: []string{"a"},
Usage: "View the Rocket Pool service stats",
UsageText: "rocketpool service stats",
Action: func(c *cli.Context) error {
// Validate args
if err := cliutils.ValidateArgCount(c, 0); err != nil {
return err
}
// Run command
return serviceStats(c)
},
},
{
Name: "compose",
Usage: "View the Rocket Pool service docker compose config",
UsageText: "rocketpool service compose",
Action: func(c *cli.Context) error {
// Validate args
if err := cliutils.ValidateArgCount(c, 0); err != nil {
return err
}
// Run command
return serviceCompose(c)
},
},
{
Name: "version",
Aliases: []string{"v"},
Usage: "View the Rocket Pool service version information",
UsageText: "rocketpool service version",
Action: func(c *cli.Context) error {
// Validate args
if err := cliutils.ValidateArgCount(c, 0); err != nil {
return err
}
// Run command
return serviceVersion(c)
},
},
{
Name: "prune-eth1",
Aliases: []string{"n"},
Usage: "Shuts down the main ETH1 client and prunes its database, freeing up disk space, then restarts it when it's done.",
UsageText: "rocketpool service prune-eth1",
Action: func(c *cli.Context) error {
// Validate args
if err := cliutils.ValidateArgCount(c, 0); err != nil {
return err
}
// Run command
return pruneExecutionClient(c)
},
},
{
Name: "install-update-tracker",
Aliases: []string{"d"},
Usage: "Install the update tracker that provides the available system update count to the metrics dashboard",
UsageText: "rocketpool service install-update-tracker [options]",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "yes, y",
Usage: "Automatically confirm service installation",
},
cli.BoolFlag{
Name: "verbose, r",
Usage: "Print installation script command output",
},
cli.StringFlag{
Name: "version, v",
Usage: "The update tracker package version to install",
Value: fmt.Sprintf("v%s", shared.RocketPoolVersion),
},
},
Action: func(c *cli.Context) error {
// Validate args
if err := cliutils.ValidateArgCount(c, 0); err != nil {
return err
}
// Run command
return installUpdateTracker(c)
},
},
{
Name: "check-cpu-features",
Aliases: []string{"ccf"},
Usage: "Checks if your CPU supports all of the features required by the \"modern\" version of certain client images. If not, it prints what features are missing.",
UsageText: "rocketpool service check-cpu-features",
Action: func(c *cli.Context) error {
// Validate args
if err := cliutils.ValidateArgCount(c, 0); err != nil {
return err
}
// Run command
return checkCpuFeatures()
},
},
{
Name: "get-config-yaml",
Usage: "Generate YAML that shows the current configuration schema, including all of the parameters and their descriptions",
UsageText: "rocketpool service get-config-yaml",
Action: func(c *cli.Context) error {
// Validate args
if err := cliutils.ValidateArgCount(c, 0); err != nil {
return err
}
// Run command
return getConfigYaml(c)
},
},
{
Name: "export-eth1-data",
Usage: "Exports the execution client (eth1) chain data to an external folder. Use this if you want to back up your chain data before switching execution clients.",
UsageText: "rocketpool service export-eth1-data target-folder",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "force",
Usage: "Bypass the free space check on the target folder",
},
cli.BoolFlag{
Name: "dirty",
Usage: "Exports the execution (eth1) chain data without stopping the client. Requires a second pass (much faster) to sync the remaining files without the client running.",
},
cli.BoolFlag{
Name: "yes, y",
Usage: "Automatically confirm",
},
},
Action: func(c *cli.Context) error {
// Validate args
if err := cliutils.ValidateArgCount(c, 1); err != nil {
return err
}
targetDir := c.Args().Get(0)
// Run command
return exportEcData(c, targetDir)
},
},
{
Name: "import-eth1-data",
Usage: "Imports execution client (eth1) chain data from an external folder. Use this if you want to restore the data from an execution client that you previously backed up.",
UsageText: "rocketpool service import-eth1-data source-folder",
Action: func(c *cli.Context) error {
// Validate args
if err := cliutils.ValidateArgCount(c, 1); err != nil {
return err
}
sourceDir := c.Args().Get(0)
// Run command
return importEcData(c, sourceDir)
},
},
{
Name: "resync-eth1",
Usage: fmt.Sprintf("%sDeletes the main ETH1 client's chain data and resyncs it from scratch. Only use this as a last resort!%s", colorRed, colorReset),
UsageText: "rocketpool service resync-eth1",
Action: func(c *cli.Context) error {
// Validate args
if err := cliutils.ValidateArgCount(c, 0); err != nil {
return err
}
// Run command
return resyncEth1(c)
},
},
{
Name: "resync-eth2",
Usage: fmt.Sprintf("%sDeletes the ETH2 client's chain data and resyncs it from scratch. Only use this as a last resort!%s", colorRed, colorReset),
UsageText: "rocketpool service resync-eth2",
Action: func(c *cli.Context) error {
// Validate args
if err := cliutils.ValidateArgCount(c, 0); err != nil {
return err
}
// Run command
return resyncEth2(c)
},
},
{
Name: "terminate",
Aliases: []string{"t"},
Usage: fmt.Sprintf("%sDeletes all of the Rocket Pool Docker containers and volumes, including your ETH1 and ETH2 chain data and your Prometheus database (if metrics are enabled). Also removes your entire `.rocketpool` configuration folder, including your wallet, password, and validator keys. Only use this if you are cleaning up the Smartnode and want to start over!%s", colorRed, colorReset),
UsageText: "rocketpool service terminate [options]",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "yes, y",
Usage: "Automatically confirm service termination",
},
},
Action: func(c *cli.Context) error {
// Validate args
if err := cliutils.ValidateArgCount(c, 0); err != nil {
return err
}
// Run command
return terminateService(c)
},
},
},
})
}