Skip to content

Commit dda5b8c

Browse files
committed
refactor code
1 parent 82e0ed6 commit dda5b8c

File tree

1 file changed

+42
-36
lines changed

1 file changed

+42
-36
lines changed

cmd/node/auto_backup_priv_validator_state.go

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ const (
3232

3333
func GetAutoBackupPrivValidatorStateCmd() *cobra.Command {
3434
var cmd = &cobra.Command{
35-
Use: commandAutoBackupPrivValidatorState + " [node_home]",
36-
Short: "Designed to be run as a service, it will automatically backup the `priv_validator_state.json`, and kill the node process if the content of the file is decreased",
37-
Args: cobra.ExactArgs(1),
35+
Use: commandAutoBackupPrivValidatorState + " [node_home]",
36+
Aliases: []string{"auto-backup-pvs", "auto-backup-priv-validator-state"},
37+
Short: "Designed to be run as a service, it will automatically backup the `priv_validator_state.json`, and kill the node process if the content of the file is decreased",
38+
Args: cobra.ExactArgs(1),
3839
Run: func(cmd *cobra.Command, args []string) {
3940
utils.MustNotUserRoot()
4041

@@ -93,39 +94,7 @@ func GetAutoBackupPrivValidatorStateCmd() *cobra.Command {
9394
fmt.Println("INF: binary to kill:", binaryNameToKill, "at", binaryPathToKill)
9495

9596
if cmd.Flags().Changed(flagGenSetup) {
96-
const serviceFileName = "auto-backup-priv-validator-state"
97-
fmt.Println("Input chain name (eg: Cosmos Hub):")
98-
chainName := utils.ReadText(false)
99-
fmt.Println("Mainnet or Testnet?")
100-
networkType := utils.ReadText(false)
101-
fmt.Println("INF: setup guide:")
102-
fmt.Println("1. Create service file")
103-
fmt.Println("> sudo vi /etc/systemd/system/" + serviceFileName + ".service")
104-
fmt.Printf(`[Unit]
105-
Description=Auto backup priv_validator_state.json for Validator on %s %s
106-
After=network.target
107-
#
108-
[Service]
109-
User=%s
110-
ExecStart=%s/go/bin/%s start %s --%s %s --%s %d
111-
RestartSec=1
112-
Restart=on-failure
113-
LimitNOFILE=1024
114-
#
115-
[Install]
116-
WantedBy=multi-user.target
117-
`, chainName, networkType, currentUser.Username, currentUser.HomeDir, constants.BINARY_NAME, nodeHomeDirectory, flagBinaryKillByAutoBackup, binaryPathToKill, flagKeep, keepRecent)
118-
fmt.Println("2. Setup visudo")
119-
fmt.Println("> sudo visudo")
120-
fmt.Printf(strings.ReplaceAll(strings.ReplaceAll(`# Allow user @USER@ to manage @SVC@ service
121-
@USER@ ALL= NOPASSWD: /usr/bin/systemctl start @SVC@
122-
@USER@ ALL= NOPASSWD: /usr/bin/systemctl stop @SVC@
123-
@USER@ ALL= NOPASSWD: /usr/bin/systemctl restart @SVC@
124-
@USER@ ALL= NOPASSWD: /usr/bin/systemctl enable @SVC@ # Do not allow disable
125-
@USER@ ALL= NOPASSWD: /usr/bin/systemctl status @SVC@
126-
`, "@USER@", currentUser.Username), "@SVC@", serviceFileName))
127-
fmt.Println("3. Enable service to automatically run at startup")
128-
fmt.Println("> sudo systemctl daemon-reload && sudo systemctl enable " + serviceFileName + ".service")
97+
genSetupThenExit(nodeHomeDirectory, binaryPathToKill, keepRecent, currentUser)
12998
return
13099
}
131100

@@ -501,3 +470,40 @@ func loadLatestBackupPrivValidatorStateOrExitWithErr(backupDstPath string) types
501470

502471
return *pvs
503472
}
473+
474+
func genSetupThenExit(nodeHomeDirectory, binaryPathToKill string, keepRecent int, currentUser *user.User) {
475+
const serviceFileName = "auto-backup-pvs"
476+
fmt.Println("Input chain name (eg: Cosmos Hub):")
477+
chainName := utils.ReadText(false)
478+
fmt.Println("Mainnet or Testnet?")
479+
networkType := utils.ReadText(false)
480+
fmt.Println("INF: setup guide:")
481+
fmt.Println("1. Create service file")
482+
fmt.Println("> sudo vi /etc/systemd/system/" + serviceFileName + ".service")
483+
fmt.Printf(`[Unit]
484+
Description=Auto backup priv_validator_state.json for Validator on %s %s
485+
After=network.target
486+
#
487+
[Service]
488+
User=%s
489+
ExecStart=%s/go/bin/%s start %s --%s %s --%s %d
490+
RestartSec=1
491+
Restart=on-failure
492+
LimitNOFILE=1024
493+
#
494+
[Install]
495+
WantedBy=multi-user.target
496+
`, chainName, networkType, currentUser.Username, currentUser.HomeDir, constants.BINARY_NAME, nodeHomeDirectory, flagBinaryKillByAutoBackup, binaryPathToKill, flagKeep, keepRecent)
497+
fmt.Println("2. Setup visudo")
498+
fmt.Println("> sudo visudo")
499+
fmt.Printf(strings.ReplaceAll(strings.ReplaceAll(`# Allow user @USER@ to manage @SVC@ service
500+
@USER@ ALL= NOPASSWD: /usr/bin/systemctl start @SVC@
501+
@USER@ ALL= NOPASSWD: /usr/bin/systemctl stop @SVC@
502+
@USER@ ALL= NOPASSWD: /usr/bin/systemctl restart @SVC@
503+
@USER@ ALL= NOPASSWD: /usr/bin/systemctl enable @SVC@ # Do not allow disable
504+
@USER@ ALL= NOPASSWD: /usr/bin/systemctl status @SVC@
505+
`, "@USER@", currentUser.Username), "@SVC@", serviceFileName))
506+
fmt.Println("3. Enable service to automatically run at startup")
507+
fmt.Println("> sudo systemctl daemon-reload && sudo systemctl enable " + serviceFileName + ".service")
508+
os.Exit(0)
509+
}

0 commit comments

Comments
 (0)