mirror of https://github.com/status-im/consul.git
commands: move snapshot save command to separate pkg
This commit is contained in:
parent
fb1f09d447
commit
e2b686b982
|
@ -39,6 +39,7 @@ import (
|
||||||
"github.com/hashicorp/consul/command/snapshot"
|
"github.com/hashicorp/consul/command/snapshot"
|
||||||
"github.com/hashicorp/consul/command/snapshotinspect"
|
"github.com/hashicorp/consul/command/snapshotinspect"
|
||||||
"github.com/hashicorp/consul/command/snapshotrestore"
|
"github.com/hashicorp/consul/command/snapshotrestore"
|
||||||
|
"github.com/hashicorp/consul/command/snapshotsave"
|
||||||
"github.com/hashicorp/consul/command/validate"
|
"github.com/hashicorp/consul/command/validate"
|
||||||
versioncmd "github.com/hashicorp/consul/command/version"
|
versioncmd "github.com/hashicorp/consul/command/version"
|
||||||
"github.com/hashicorp/consul/version"
|
"github.com/hashicorp/consul/version"
|
||||||
|
@ -199,12 +200,7 @@ func init() {
|
||||||
},
|
},
|
||||||
|
|
||||||
"snapshot save": func() (cli.Command, error) {
|
"snapshot save": func() (cli.Command, error) {
|
||||||
return &SnapshotSaveCommand{
|
return snapshotsave.New(ui), nil
|
||||||
BaseCommand: BaseCommand{
|
|
||||||
Flags: FlagSetHTTP,
|
|
||||||
UI: ui,
|
|
||||||
},
|
|
||||||
}, nil
|
|
||||||
},
|
},
|
||||||
|
|
||||||
"snapshot inspect": func() (cli.Command, error) {
|
"snapshot inspect": func() (cli.Command, error) {
|
||||||
|
|
|
@ -1,55 +1,54 @@
|
||||||
package command
|
package snapshotsave
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/hashicorp/consul/api"
|
"github.com/hashicorp/consul/api"
|
||||||
|
"github.com/hashicorp/consul/command/flags"
|
||||||
"github.com/hashicorp/consul/snapshot"
|
"github.com/hashicorp/consul/snapshot"
|
||||||
|
"github.com/mitchellh/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SnapshotSaveCommand is a Command implementation that is used to save the
|
func New(ui cli.Ui) *cmd {
|
||||||
// state of the Consul servers for disaster recovery.
|
c := &cmd{UI: ui}
|
||||||
type SnapshotSaveCommand struct {
|
c.init()
|
||||||
BaseCommand
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *SnapshotSaveCommand) Help() string {
|
type cmd struct {
|
||||||
c.InitFlagSet()
|
UI cli.Ui
|
||||||
return c.HelpCommand(`
|
flags *flag.FlagSet
|
||||||
Usage: consul snapshot save [options] FILE
|
http *flags.HTTPFlags
|
||||||
|
usage string
|
||||||
Retrieves an atomic, point-in-time snapshot of the state of the Consul servers
|
|
||||||
which includes key/value entries, service catalog, prepared queries, sessions,
|
|
||||||
and ACLs.
|
|
||||||
|
|
||||||
If ACLs are enabled, a management token must be supplied in order to perform
|
|
||||||
snapshot operations.
|
|
||||||
|
|
||||||
To create a snapshot from the leader server and save it to "backup.snap":
|
|
||||||
|
|
||||||
$ consul snapshot save backup.snap
|
|
||||||
|
|
||||||
To create a potentially stale snapshot from any available server (useful if no
|
|
||||||
leader is available):
|
|
||||||
|
|
||||||
$ consul snapshot save -stale backup.snap
|
|
||||||
|
|
||||||
For a full list of options and examples, please see the Consul documentation.
|
|
||||||
|
|
||||||
`)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *SnapshotSaveCommand) Run(args []string) int {
|
func (c *cmd) init() {
|
||||||
c.InitFlagSet()
|
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
||||||
if err := c.FlagSet.Parse(args); err != nil {
|
c.http = &flags.HTTPFlags{}
|
||||||
|
flags.Merge(c.flags, c.http.ClientFlags())
|
||||||
|
flags.Merge(c.flags, c.http.ServerFlags())
|
||||||
|
c.usage = flags.Usage(usage, c.flags, c.http.ClientFlags(), c.http.ServerFlags())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *cmd) Synopsis() string {
|
||||||
|
return "Saves snapshot of Consul server state"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *cmd) Help() string {
|
||||||
|
return c.usage
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *cmd) Run(args []string) int {
|
||||||
|
if err := c.flags.Parse(args); err != nil {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
var file string
|
var file string
|
||||||
|
|
||||||
args = c.FlagSet.Args()
|
args = c.flags.Args()
|
||||||
switch len(args) {
|
switch len(args) {
|
||||||
case 0:
|
case 0:
|
||||||
c.UI.Error("Missing FILE argument")
|
c.UI.Error("Missing FILE argument")
|
||||||
|
@ -62,7 +61,7 @@ func (c *SnapshotSaveCommand) Run(args []string) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create and test the HTTP client
|
// Create and test the HTTP client
|
||||||
client, err := c.HTTPClient()
|
client, err := c.http.APIClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
|
c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
|
||||||
return 1
|
return 1
|
||||||
|
@ -70,7 +69,7 @@ func (c *SnapshotSaveCommand) Run(args []string) int {
|
||||||
|
|
||||||
// Take the snapshot.
|
// Take the snapshot.
|
||||||
snap, qm, err := client.Snapshot().Save(&api.QueryOptions{
|
snap, qm, err := client.Snapshot().Save(&api.QueryOptions{
|
||||||
AllowStale: c.HTTPStale(),
|
AllowStale: c.http.Stale(),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.UI.Error(fmt.Sprintf("Error saving snapshot: %s", err))
|
c.UI.Error(fmt.Sprintf("Error saving snapshot: %s", err))
|
||||||
|
@ -114,6 +113,22 @@ func (c *SnapshotSaveCommand) Run(args []string) int {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *SnapshotSaveCommand) Synopsis() string {
|
const usage = `Usage: consul snapshot save [options] FILE
|
||||||
return "Saves snapshot of Consul server state"
|
|
||||||
}
|
Retrieves an atomic, point-in-time snapshot of the state of the Consul servers
|
||||||
|
which includes key/value entries, service catalog, prepared queries, sessions,
|
||||||
|
and ACLs.
|
||||||
|
|
||||||
|
If ACLs are enabled, a management token must be supplied in order to perform
|
||||||
|
snapshot operations.
|
||||||
|
|
||||||
|
To create a snapshot from the leader server and save it to "backup.snap":
|
||||||
|
|
||||||
|
$ consul snapshot save backup.snap
|
||||||
|
|
||||||
|
To create a potentially stale snapshot from any available server (useful if no
|
||||||
|
leader is available):
|
||||||
|
|
||||||
|
$ consul snapshot save -stale backup.snap
|
||||||
|
|
||||||
|
For a full list of options and examples, please see the Consul documentation.`
|
|
@ -1,4 +1,4 @@
|
||||||
package command
|
package snapshotsave
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
@ -11,26 +11,12 @@ import (
|
||||||
"github.com/mitchellh/cli"
|
"github.com/mitchellh/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
func testSnapshotSaveCommand(t *testing.T) (*cli.MockUi, *SnapshotSaveCommand) {
|
|
||||||
ui := cli.NewMockUi()
|
|
||||||
return ui, &SnapshotSaveCommand{
|
|
||||||
BaseCommand: BaseCommand{
|
|
||||||
UI: ui,
|
|
||||||
Flags: FlagSetHTTP,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSnapshotSaveCommand_implements(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
var _ cli.Command = &SnapshotSaveCommand{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSnapshotSaveCommand_noTabs(t *testing.T) {
|
func TestSnapshotSaveCommand_noTabs(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
assertNoTabs(t, new(SnapshotSaveCommand))
|
if strings.ContainsRune(New(cli.NewMockUi()).Help(), '\t') {
|
||||||
|
t.Fatal("usage has tabs")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSnapshotSaveCommand_Validation(t *testing.T) {
|
func TestSnapshotSaveCommand_Validation(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
@ -49,7 +35,8 @@ func TestSnapshotSaveCommand_Validation(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for name, tc := range cases {
|
for name, tc := range cases {
|
||||||
ui, c := testSnapshotSaveCommand(t)
|
ui := cli.NewMockUi()
|
||||||
|
c := New(ui)
|
||||||
|
|
||||||
// Ensure our buffer is always clear
|
// Ensure our buffer is always clear
|
||||||
if ui.ErrorWriter != nil {
|
if ui.ErrorWriter != nil {
|
||||||
|
@ -77,7 +64,8 @@ func TestSnapshotSaveCommand_Run(t *testing.T) {
|
||||||
defer a.Shutdown()
|
defer a.Shutdown()
|
||||||
client := a.Client()
|
client := a.Client()
|
||||||
|
|
||||||
ui, c := testSnapshotSaveCommand(t)
|
ui := cli.NewMockUi()
|
||||||
|
c := New(ui)
|
||||||
|
|
||||||
dir := testutil.TempDir(t, "snapshot")
|
dir := testutil.TempDir(t, "snapshot")
|
||||||
defer os.RemoveAll(dir)
|
defer os.RemoveAll(dir)
|
Loading…
Reference in New Issue