github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/command/operator_raft_state.go (about) 1 package command 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "os" 7 "strings" 8 9 "github.com/hashicorp/nomad/helper/raftutil" 10 "github.com/posener/complete" 11 ) 12 13 type OperatorRaftStateCommand struct { 14 Meta 15 } 16 17 func (c *OperatorRaftStateCommand) Help() string { 18 helpText := ` 19 Usage: nomad operator raft _state <path to nomad data dir> 20 21 Display the server state obtained by replaying raft log entries persisted in data dir in json form. 22 23 This is a low-level debugging tool and not subject to Nomad's usual backward 24 compatibility guarantees. 25 26 If ACLs are enabled, this command requires a management token. 27 28 Options: 29 30 -last-index=<last_index> 31 Set the last log index to be applied, to drop spurious log entries not 32 properly committed. If passed last_index is zero or negative, it's perceived 33 as an offset from the last index seen in raft. 34 ` 35 return strings.TrimSpace(helpText) 36 } 37 38 func (c *OperatorRaftStateCommand) AutocompleteFlags() complete.Flags { 39 return complete.Flags{} 40 } 41 42 func (c *OperatorRaftStateCommand) AutocompleteArgs() complete.Predictor { 43 return complete.PredictNothing 44 } 45 46 func (c *OperatorRaftStateCommand) Synopsis() string { 47 return "Display raft server state" 48 } 49 50 func (c *OperatorRaftStateCommand) Name() string { return "operator raft _state" } 51 52 func (c *OperatorRaftStateCommand) Run(args []string) int { 53 var fLastIdx int64 54 55 flags := c.Meta.FlagSet(c.Name(), 0) 56 flags.Usage = func() { fmt.Println(c.Help()) } 57 flags.Int64Var(&fLastIdx, "last-index", 0, "") 58 59 if err := flags.Parse(args); err != nil { 60 c.Ui.Error(fmt.Sprintf("Failed to parse args: %v", err)) 61 return 1 62 } 63 args = flags.Args() 64 65 if len(args) != 1 { 66 c.Ui.Error("This command takes one argument: <path>") 67 c.Ui.Error(commandErrorText(c)) 68 69 return 1 70 } 71 72 // Find raft.db folder 73 raftPath, err := raftutil.FindRaftDir(args[0]) 74 if err != nil { 75 c.Ui.Error(err.Error()) 76 return 1 77 } 78 79 state, err := raftutil.FSMState(raftPath, fLastIdx) 80 if err != nil { 81 c.Ui.Error(err.Error()) 82 return 1 83 } 84 85 enc := json.NewEncoder(os.Stdout) 86 enc.SetIndent("", " ") 87 if err := enc.Encode(state); err != nil { 88 c.Ui.Error(fmt.Sprintf("failed to encode output: %v", err)) 89 return 1 90 } 91 92 return 0 93 }