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 OperatorRaftLogsCommand struct {
|
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *OperatorRaftLogsCommand) Help() string {
|
|
|
|
helpText := `
|
2021-12-16 15:32:11 +00:00
|
|
|
Usage: nomad operator raft logs <path to nomad data dir>
|
2020-08-21 21:16:42 +00:00
|
|
|
|
2021-12-16 15:32:11 +00:00
|
|
|
Display the 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-11-19 21:38:08 +00:00
|
|
|
|
2021-12-16 18:38:58 +00:00
|
|
|
Raft Logs Options:
|
|
|
|
|
|
|
|
-pretty
|
|
|
|
By default this command outputs newline delimited JSON. If the -pretty flag
|
|
|
|
is passed, each entry will be pretty-printed.
|
2020-08-21 21:16:42 +00:00
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *OperatorRaftLogsCommand) AutocompleteFlags() complete.Flags {
|
|
|
|
return complete.Flags{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *OperatorRaftLogsCommand) AutocompleteArgs() complete.Predictor {
|
|
|
|
return complete.PredictNothing
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *OperatorRaftLogsCommand) Synopsis() string {
|
|
|
|
return "Display raft log content"
|
|
|
|
}
|
|
|
|
|
2021-12-16 15:32:11 +00:00
|
|
|
func (c *OperatorRaftLogsCommand) Name() string { return "operator raft logs" }
|
2020-08-21 21:16:42 +00:00
|
|
|
|
|
|
|
func (c *OperatorRaftLogsCommand) Run(args []string) int {
|
2021-12-16 18:38:58 +00:00
|
|
|
|
|
|
|
var pretty bool
|
|
|
|
flagSet := c.Meta.FlagSet(c.Name(), FlagSetClient)
|
|
|
|
flagSet.Usage = func() { c.Ui.Output(c.Help()) }
|
|
|
|
flagSet.BoolVar(&pretty, "pretty", false, "")
|
|
|
|
|
|
|
|
if err := flagSet.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
args = flagSet.Args()
|
|
|
|
if l := len(args); l != 1 {
|
2020-08-21 21:16:42 +00:00
|
|
|
c.Ui.Error("This command takes one argument: <path>")
|
|
|
|
c.Ui.Error(commandErrorText(c))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2020-09-24 23:00:53 +00:00
|
|
|
raftPath, err := raftutil.FindRaftFile(args[0])
|
2020-09-18 01:35:17 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(err.Error())
|
|
|
|
return 1
|
2020-08-21 21:16:42 +00:00
|
|
|
}
|
|
|
|
|
2021-12-16 18:38:58 +00:00
|
|
|
enc := json.NewEncoder(os.Stdout)
|
|
|
|
if pretty {
|
|
|
|
enc.SetIndent("", " ")
|
|
|
|
}
|
|
|
|
|
|
|
|
logChan, warningsChan, err := raftutil.LogEntries(raftPath)
|
2020-08-21 21:16:42 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2021-12-16 18:38:58 +00:00
|
|
|
// so that the warnings don't end up mixed into the JSON stream,
|
|
|
|
// collect them and print them once we're done
|
|
|
|
warnings := []error{}
|
|
|
|
|
|
|
|
DONE:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case log := <-logChan:
|
|
|
|
if log == nil {
|
|
|
|
break DONE // no more logs, but break to print warnings
|
|
|
|
}
|
|
|
|
if err := enc.Encode(log); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("failed to encode output: %v", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
case warning := <-warningsChan:
|
|
|
|
warnings = append(warnings, warning)
|
|
|
|
}
|
2020-08-21 21:16:42 +00:00
|
|
|
}
|
|
|
|
|
2021-12-16 18:38:58 +00:00
|
|
|
for _, warning := range warnings {
|
|
|
|
c.Ui.Error(warning.Error())
|
2020-08-21 21:16:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|