Skip to content

Commit 82e0ed6

Browse files
committed
add flag to gen config auto backup
1 parent 436acd2 commit 82e0ed6

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

cmd/node/auto_backup_priv_validator_state.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
const (
1919
flagKeep = "keep"
2020
flagBinaryKillByAutoBackup = "binary"
21+
flagGenSetup = "gen-setup"
2122
)
2223

2324
const (
@@ -91,6 +92,43 @@ func GetAutoBackupPrivValidatorStateCmd() *cobra.Command {
9192
_, binaryNameToKill := path.Split(binaryPathToKill)
9293
fmt.Println("INF: binary to kill:", binaryNameToKill, "at", binaryPathToKill)
9394

95+
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")
129+
return
130+
}
131+
94132
privValStateJsonFilePath := path.Join(nodeHomeDirectory, "data", "priv_validator_state.json")
95133
fmt.Println("INF: priv_validator_state.json file path:", privValStateJsonFilePath)
96134

@@ -288,6 +326,7 @@ How to recover:
288326

289327
cmd.Flags().Int(flagKeep, 3, "Keep the last N backups")
290328
cmd.Flags().String(flagBinaryKillByAutoBackup, "", "Absolute path of the chain binary to be killed by process when priv_validator_state.json has problem")
329+
cmd.Flags().Bool(flagGenSetup, false, "Display guide to setup instead of running business logic")
291330

292331
return cmd
293332
}

0 commit comments

Comments
 (0)