2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2020-08-21 21:16:42 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/helper/raftutil"
|
|
|
|
"github.com/posener/complete"
|
|
|
|
)
|
|
|
|
|
|
|
|
type OperatorRaftStateCommand struct {
|
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *OperatorRaftStateCommand) Help() string {
|
|
|
|
helpText := `
|
2021-12-16 15:32:11 +00:00
|
|
|
Usage: nomad operator raft state <path to nomad data dir>
|
2020-08-21 21:16:42 +00:00
|
|
|
|
2021-12-16 15:32:11 +00:00
|
|
|
Display the server state obtained by replaying raft log entries persisted in
|
|
|
|
the Nomad data directory in JSON format.
|
|
|
|
|
|
|
|
This command requires file system permissions to access the data directory on
|
|
|
|
disk. The Nomad server locks access to the data directory, so this command
|
|
|
|
cannot be run on a data directory that is being used by a running Nomad server.
|
2020-08-31 11:49:45 +00:00
|
|
|
|
|
|
|
This is a low-level debugging tool and not subject to Nomad's usual backward
|
|
|
|
compatibility guarantees.
|
2020-08-21 21:16:42 +00:00
|
|
|
|
|
|
|
Options:
|
|
|
|
|
2020-08-31 11:47:16 +00:00
|
|
|
-last-index=<last_index>
|
2020-08-21 21:16:42 +00:00
|
|
|
Set the last log index to be applied, to drop spurious log entries not
|
|
|
|
properly committed. If passed last_index is zero or negative, it's perceived
|
|
|
|
as an offset from the last index seen in raft.
|
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *OperatorRaftStateCommand) AutocompleteFlags() complete.Flags {
|
2023-10-30 18:00:40 +00:00
|
|
|
return complete.Flags{
|
|
|
|
"-last-index": complete.PredictNothing,
|
|
|
|
}
|
2020-08-21 21:16:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *OperatorRaftStateCommand) AutocompleteArgs() complete.Predictor {
|
2023-10-30 18:00:40 +00:00
|
|
|
return complete.PredictFiles("*")
|
2020-08-21 21:16:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *OperatorRaftStateCommand) Synopsis() string {
|
2020-09-18 01:35:17 +00:00
|
|
|
return "Display raft server state"
|
2020-08-21 21:16:42 +00:00
|
|
|
}
|
|
|
|
|
2021-12-16 15:32:11 +00:00
|
|
|
func (c *OperatorRaftStateCommand) Name() string { return "operator raft state" }
|
2020-08-21 21:16:42 +00:00
|
|
|
|
|
|
|
func (c *OperatorRaftStateCommand) Run(args []string) int {
|
|
|
|
var fLastIdx int64
|
|
|
|
|
|
|
|
flags := c.Meta.FlagSet(c.Name(), 0)
|
|
|
|
flags.Usage = func() { fmt.Println(c.Help()) }
|
|
|
|
flags.Int64Var(&fLastIdx, "last-index", 0, "")
|
|
|
|
|
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Failed to parse args: %v", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
args = flags.Args()
|
|
|
|
|
|
|
|
if len(args) != 1 {
|
|
|
|
c.Ui.Error("This command takes one argument: <path>")
|
|
|
|
c.Ui.Error(commandErrorText(c))
|
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2020-09-18 01:35:17 +00:00
|
|
|
// Find raft.db folder
|
2020-09-24 23:00:53 +00:00
|
|
|
raftPath, err := raftutil.FindRaftDir(args[0])
|
2020-09-18 01:35:17 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(err.Error())
|
2020-08-21 21:16:42 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2021-11-04 14:16:12 +00:00
|
|
|
fsm, err := raftutil.NewFSM(raftPath)
|
2020-08-21 21:16:42 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
2021-11-04 14:16:12 +00:00
|
|
|
defer fsm.Close()
|
2020-08-21 21:16:42 +00:00
|
|
|
|
2021-11-04 14:16:12 +00:00
|
|
|
_, _, err = fsm.ApplyAll()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
state := fsm.StateAsMap()
|
2020-08-21 21:16:42 +00:00
|
|
|
enc := json.NewEncoder(os.Stdout)
|
|
|
|
enc.SetIndent("", " ")
|
|
|
|
if err := enc.Encode(state); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("failed to encode output: %v", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|