Skip to content

Commit

Permalink
commands: move snapshot save command to separate pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
magiconair committed Oct 17, 2017
1 parent fb1f09d commit e2b686b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 61 deletions.
8 changes: 2 additions & 6 deletions command/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"github.com/hashicorp/consul/command/snapshot"
"github.com/hashicorp/consul/command/snapshotinspect"
"github.com/hashicorp/consul/command/snapshotrestore"
"github.com/hashicorp/consul/command/snapshotsave"
"github.com/hashicorp/consul/command/validate"
versioncmd "github.com/hashicorp/consul/command/version"
"github.com/hashicorp/consul/version"
Expand Down Expand Up @@ -199,12 +200,7 @@ func init() {
},

"snapshot save": func() (cli.Command, error) {
return &SnapshotSaveCommand{
BaseCommand: BaseCommand{
Flags: FlagSetHTTP,
UI: ui,
},
}, nil
return snapshotsave.New(ui), nil
},

"snapshot inspect": func() (cli.Command, error) {
Expand Down
85 changes: 50 additions & 35 deletions command/snapshot_save.go → command/snapshotsave/snapshot_save.go
Original file line number Diff line number Diff line change
@@ -1,55 +1,54 @@
package command
package snapshotsave

import (
"flag"
"fmt"
"io"
"os"

"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/command/flags"
"github.com/hashicorp/consul/snapshot"
"github.com/mitchellh/cli"
)

// SnapshotSaveCommand is a Command implementation that is used to save the
// state of the Consul servers for disaster recovery.
type SnapshotSaveCommand struct {
BaseCommand
func New(ui cli.Ui) *cmd {
c := &cmd{UI: ui}
c.init()
return c
}

func (c *SnapshotSaveCommand) Help() string {
c.InitFlagSet()
return c.HelpCommand(`
Usage: consul snapshot save [options] FILE
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):
type cmd struct {
UI cli.Ui
flags *flag.FlagSet
http *flags.HTTPFlags
usage string
}

$ consul snapshot save -stale backup.snap
func (c *cmd) init() {
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
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())
}

For a full list of options and examples, please see the Consul documentation.
func (c *cmd) Synopsis() string {
return "Saves snapshot of Consul server state"
}

`)
func (c *cmd) Help() string {
return c.usage
}

func (c *SnapshotSaveCommand) Run(args []string) int {
c.InitFlagSet()
if err := c.FlagSet.Parse(args); err != nil {
func (c *cmd) Run(args []string) int {
if err := c.flags.Parse(args); err != nil {
return 1
}

var file string

args = c.FlagSet.Args()
args = c.flags.Args()
switch len(args) {
case 0:
c.UI.Error("Missing FILE argument")
Expand All @@ -62,15 +61,15 @@ func (c *SnapshotSaveCommand) Run(args []string) int {
}

// Create and test the HTTP client
client, err := c.HTTPClient()
client, err := c.http.APIClient()
if err != nil {
c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
return 1
}

// Take the snapshot.
snap, qm, err := client.Snapshot().Save(&api.QueryOptions{
AllowStale: c.HTTPStale(),
AllowStale: c.http.Stale(),
})
if err != nil {
c.UI.Error(fmt.Sprintf("Error saving snapshot: %s", err))
Expand Down Expand Up @@ -114,6 +113,22 @@ func (c *SnapshotSaveCommand) Run(args []string) int {
return 0
}

func (c *SnapshotSaveCommand) Synopsis() string {
return "Saves snapshot of Consul server state"
}
const usage = `Usage: consul snapshot save [options] FILE
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.`
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package command
package snapshotsave

import (
"os"
Expand All @@ -11,26 +11,12 @@ import (
"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) {
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) {
t.Parallel()

Expand All @@ -49,7 +35,8 @@ func TestSnapshotSaveCommand_Validation(t *testing.T) {
}

for name, tc := range cases {
ui, c := testSnapshotSaveCommand(t)
ui := cli.NewMockUi()
c := New(ui)

// Ensure our buffer is always clear
if ui.ErrorWriter != nil {
Expand Down Expand Up @@ -77,7 +64,8 @@ func TestSnapshotSaveCommand_Run(t *testing.T) {
defer a.Shutdown()
client := a.Client()

ui, c := testSnapshotSaveCommand(t)
ui := cli.NewMockUi()
c := New(ui)

dir := testutil.TempDir(t, "snapshot")
defer os.RemoveAll(dir)
Expand Down

0 comments on commit e2b686b

Please sign in to comment.