github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/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
    22    the Nomad data directory in JSON format.
    23  
    24    This command requires file system permissions to access the data directory on
    25    disk. The Nomad server locks access to the data directory, so this command
    26    cannot be run on a data directory that is being used by a running Nomad server.
    27  
    28    This is a low-level debugging tool and not subject to Nomad's usual backward
    29    compatibility guarantees.
    30  
    31  Options:
    32  
    33    -last-index=<last_index>
    34      Set the last log index to be applied, to drop spurious log entries not
    35      properly committed. If passed last_index is zero or negative, it's perceived
    36      as an offset from the last index seen in raft.
    37  `
    38  	return strings.TrimSpace(helpText)
    39  }
    40  
    41  func (c *OperatorRaftStateCommand) AutocompleteFlags() complete.Flags {
    42  	return complete.Flags{}
    43  }
    44  
    45  func (c *OperatorRaftStateCommand) AutocompleteArgs() complete.Predictor {
    46  	return complete.PredictNothing
    47  }
    48  
    49  func (c *OperatorRaftStateCommand) Synopsis() string {
    50  	return "Display raft server state"
    51  }
    52  
    53  func (c *OperatorRaftStateCommand) Name() string { return "operator raft state" }
    54  
    55  func (c *OperatorRaftStateCommand) Run(args []string) int {
    56  	var fLastIdx int64
    57  
    58  	flags := c.Meta.FlagSet(c.Name(), 0)
    59  	flags.Usage = func() { fmt.Println(c.Help()) }
    60  	flags.Int64Var(&fLastIdx, "last-index", 0, "")
    61  
    62  	if err := flags.Parse(args); err != nil {
    63  		c.Ui.Error(fmt.Sprintf("Failed to parse args: %v", err))
    64  		return 1
    65  	}
    66  	args = flags.Args()
    67  
    68  	if len(args) != 1 {
    69  		c.Ui.Error("This command takes one argument: <path>")
    70  		c.Ui.Error(commandErrorText(c))
    71  
    72  		return 1
    73  	}
    74  
    75  	// Find raft.db folder
    76  	raftPath, err := raftutil.FindRaftDir(args[0])
    77  	if err != nil {
    78  		c.Ui.Error(err.Error())
    79  		return 1
    80  	}
    81  
    82  	fsm, err := raftutil.NewFSM(raftPath)
    83  	if err != nil {
    84  		c.Ui.Error(err.Error())
    85  		return 1
    86  	}
    87  	defer fsm.Close()
    88  
    89  	_, _, err = fsm.ApplyAll()
    90  	if err != nil {
    91  		c.Ui.Error(err.Error())
    92  		return 1
    93  	}
    94  
    95  	state := fsm.StateAsMap()
    96  	enc := json.NewEncoder(os.Stdout)
    97  	enc.SetIndent("", "  ")
    98  	if err := enc.Encode(state); err != nil {
    99  		c.Ui.Error(fmt.Sprintf("failed to encode output: %v", err))
   100  		return 1
   101  	}
   102  
   103  	return 0
   104  }