2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2021-11-04 14:16:12 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2022-07-11 14:48:00 +00:00
|
|
|
flaghelper "github.com/hashicorp/nomad/helper/flags"
|
2021-11-04 14:16:12 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/raftutil"
|
2022-07-11 14:48:00 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad"
|
2021-11-04 14:16:12 +00:00
|
|
|
"github.com/posener/complete"
|
|
|
|
)
|
|
|
|
|
|
|
|
type OperatorSnapshotStateCommand struct {
|
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *OperatorSnapshotStateCommand) Help() string {
|
|
|
|
helpText := `
|
2022-07-11 14:48:00 +00:00
|
|
|
Usage: nomad operator snapshot state [options] <file>
|
2021-11-04 14:16:12 +00:00
|
|
|
|
|
|
|
Displays a JSON representation of state in the snapshot.
|
|
|
|
|
|
|
|
To inspect the file "backup.snap":
|
|
|
|
|
2021-12-16 15:32:11 +00:00
|
|
|
$ nomad operator snapshot state backup.snap
|
2022-07-11 14:48:00 +00:00
|
|
|
|
|
|
|
Snapshot State Options:
|
|
|
|
|
|
|
|
-filter
|
|
|
|
Specifies an expression used to filter query results.
|
|
|
|
|
2021-11-04 14:16:12 +00:00
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *OperatorSnapshotStateCommand) AutocompleteFlags() complete.Flags {
|
|
|
|
return complete.Flags{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *OperatorSnapshotStateCommand) AutocompleteArgs() complete.Predictor {
|
|
|
|
return complete.PredictNothing
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *OperatorSnapshotStateCommand) Synopsis() string {
|
|
|
|
return "Displays information about a Nomad snapshot file"
|
|
|
|
}
|
|
|
|
|
2021-12-16 15:32:11 +00:00
|
|
|
func (c *OperatorSnapshotStateCommand) Name() string { return "operator snapshot state" }
|
2021-11-04 14:16:12 +00:00
|
|
|
|
|
|
|
func (c *OperatorSnapshotStateCommand) Run(args []string) int {
|
2022-07-11 14:48:00 +00:00
|
|
|
var filterExpr flaghelper.StringFlag
|
|
|
|
|
|
|
|
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
|
|
|
|
flags.Usage = func() { c.Ui.Output(c.Help()) }
|
|
|
|
|
|
|
|
flags.Var(&filterExpr, "filter", "")
|
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Failed to parse args: %v", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
filter, err := nomad.NewFSMFilter(filterExpr.String())
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Invalid filter expression %q: %s", filterExpr, err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2021-11-04 14:16:12 +00:00
|
|
|
// Check that we either got no filename or exactly one.
|
2022-07-11 14:48:00 +00:00
|
|
|
if len(flags.Args()) != 1 {
|
2021-11-04 14:16:12 +00:00
|
|
|
c.Ui.Error("This command takes one argument: <file>")
|
|
|
|
c.Ui.Error(commandErrorText(c))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2022-07-11 14:48:00 +00:00
|
|
|
path := flags.Args()[0]
|
2021-11-04 14:16:12 +00:00
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error opening snapshot file: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2022-07-11 14:48:00 +00:00
|
|
|
state, meta, err := raftutil.RestoreFromArchive(f, filter)
|
2021-11-04 14:16:12 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Failed to read archive file: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
sm := raftutil.StateAsMap(state)
|
|
|
|
sm["SnapshotMeta"] = []interface{}{meta}
|
|
|
|
|
|
|
|
enc := json.NewEncoder(os.Stdout)
|
|
|
|
enc.SetIndent("", " ")
|
|
|
|
if err := enc.Encode(sm); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Failed to encode output: %v", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|