From e8f4bfe7a034e9f187790b7aa5f08f7d197e90ec Mon Sep 17 00:00:00 2001 From: absolutelightning Date: Tue, 5 Sep 2023 22:03:28 +0530 Subject: [PATCH 1/6] init --- command/snapshot/save/snapshot_save.go | 29 +++------------------- website/content/commands/snapshot/save.mdx | 15 ++++++----- 2 files changed, 11 insertions(+), 33 deletions(-) diff --git a/command/snapshot/save/snapshot_save.go b/command/snapshot/save/snapshot_save.go index c774e6d89e1..cbbed801ec2 100644 --- a/command/snapshot/save/snapshot_save.go +++ b/command/snapshot/save/snapshot_save.go @@ -36,7 +36,7 @@ type cmd struct { func (c *cmd) getAppendFileNameFlag() *flag.FlagSet { fs := flag.NewFlagSet("", flag.ContinueOnError) fs.Var(&c.appendFileNameFlag, "append-filename", "Append filename flag supports the following "+ - "comma-separated arguments. 1. version, 2. dc. 3. node 4. status. It appends these values to the filename provided in the command") + "comma-separated arguments. 1. version, 2. dc. It appends these values to the filename provided in the command") return fs } @@ -75,7 +75,9 @@ func (c *cmd) Run(args []string) int { if len(appendFileNameFlags) != 0 && len(c.appendFileNameFlag.String()) > 0 { agentSelfResponse, err := client.Agent().Self() - if err != nil { + operatorHealthResponse, error := client.Operator().AutopilotServerHealth(nil) + + if err != nil && error != nil { c.UI.Error(fmt.Sprintf("Error connecting to Consul agent and fetching datacenter/version: %s", err)) return 1 } @@ -99,29 +101,6 @@ func (c *cmd) Run(args []string) int { } } - if slices.Contains(appendFileNameFlags, "node") { - if config, ok := agentSelfResponse["Config"]; ok { - if nodeName, ok := config["NodeName"]; ok { - fileNameWithoutExt = fileNameWithoutExt + "-" + nodeName.(string) - } - } - } - - if slices.Contains(appendFileNameFlags, "status") { - if status, ok := agentSelfResponse["Stats"]; ok { - if config, ok := status["consul"]; ok { - configMap := config.(map[string]interface{}) - if leader, ok := configMap["leader"]; ok { - if leader == "true" { - fileNameWithoutExt = fileNameWithoutExt + "-" + "leader" - } else { - fileNameWithoutExt = fileNameWithoutExt + "-" + "follower" - } - } - } - } - } - //adding extension back file = fileNameWithoutExt + fileExt } diff --git a/website/content/commands/snapshot/save.mdx b/website/content/commands/snapshot/save.mdx index dfd4d4086d4..e8b780c0267 100644 --- a/website/content/commands/snapshot/save.mdx +++ b/website/content/commands/snapshot/save.mdx @@ -77,20 +77,19 @@ $ consul snapshot save -stale backup.snap # ... ``` -To create snapshot file with consul version, datacenter, node name and leader/follower info, -run +This is useful for situations where a cluster is in a degraded state and no +leader is available. To target a specific server for a snapshot, you can run +the `consul snapshot save` command on that specific server. + +To create snapshot file with consul version and datacenter run ```shell-session -$ consul snapshot save -append-filename node,status,version,dc backup.snap +$ consul snapshot save -append-filename version,dc backup.snap #... ``` -File name created will be like backup-%CONSUL_VERSION%-%DC_NAME%-%NODE_NAME%-%STATUS.snap +File name created will be like backup-%CONSUL_VERSION%-%DC_NAME%.snap example - backup-1.17.0-dc1-local-machine-leader.tgz -This is useful for situations where a cluster is in a degraded state and no -leader is available. To target a specific server for a snapshot, you can run -the `consul snapshot save` command on that specific server. - Please see the [HTTP API](/consul/api-docs/snapshot) documentation for more details about snapshot internals. From 5072958f6394dbf4318df549dec061d8e2ca8a5f Mon Sep 17 00:00:00 2001 From: absolutelightning Date: Wed, 6 Sep 2023 12:37:05 +0530 Subject: [PATCH 2/6] fix tests --- command/snapshot/save/snapshot_save.go | 18 +++++++++----- command/snapshot/save/snapshot_save_test.go | 27 ++++++++++----------- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/command/snapshot/save/snapshot_save.go b/command/snapshot/save/snapshot_save.go index cbbed801ec2..0541ce97c53 100644 --- a/command/snapshot/save/snapshot_save.go +++ b/command/snapshot/save/snapshot_save.go @@ -75,9 +75,7 @@ func (c *cmd) Run(args []string) int { if len(appendFileNameFlags) != 0 && len(c.appendFileNameFlag.String()) > 0 { agentSelfResponse, err := client.Agent().Self() - operatorHealthResponse, error := client.Operator().AutopilotServerHealth(nil) - - if err != nil && error != nil { + if err != nil { c.UI.Error(fmt.Sprintf("Error connecting to Consul agent and fetching datacenter/version: %s", err)) return 1 } @@ -86,11 +84,19 @@ func (c *cmd) Run(args []string) int { fileNameWithoutExt := strings.TrimSuffix(file, fileExt) if slices.Contains(appendFileNameFlags, "version") { - if config, ok := agentSelfResponse["Config"]; ok { - if version, ok := config["Version"]; ok { - fileNameWithoutExt = fileNameWithoutExt + "-" + version.(string) + operatorHealthResponse, err := client.Operator().AutopilotServerHealth(nil) + if err != nil { + c.UI.Error(fmt.Sprintf("Error fetching version of Consul agent Leader: %s", err)) + return 1 + } + version := "" + for _, server := range operatorHealthResponse.Servers { + if server.Leader { + version = server.Version + break } } + fileNameWithoutExt = fileNameWithoutExt + "-" + version } if slices.Contains(appendFileNameFlags, "dc") { diff --git a/command/snapshot/save/snapshot_save_test.go b/command/snapshot/save/snapshot_save_test.go index fb672b9993b..894cadf25d4 100644 --- a/command/snapshot/save/snapshot_save_test.go +++ b/command/snapshot/save/snapshot_save_test.go @@ -88,19 +88,11 @@ func TestSnapshotSaveCommandWithAppendFileNameFlag(t *testing.T) { dir := testutil.TempDir(t, "snapshot") file := filepath.Join(dir, "backup.tgz") args := []string{ - "-append-filename=version,dc,node,status", + "-append-filename=version,dc", "-http-addr=" + a.HTTPAddr(), file, } - stats := a.Stats() - - status := "follower" - - if stats["consul"]["leader"] == "true" { - status = "leader" - } - // We need to use the self endpoint here for ENT, which returns the product suffix (+ent) self, err := client.Agent().Self() require.NoError(t, err) @@ -108,14 +100,21 @@ func TestSnapshotSaveCommandWithAppendFileNameFlag(t *testing.T) { cfg, ok := self["Config"] require.True(t, ok) - versionAny, ok := cfg["Version"] + dc, ok := cfg["Datacenter"] require.True(t, ok) - version, ok := versionAny.(string) - require.True(t, ok) + datacenter := dc.(string) + + operatorHealth, err := client.Operator().AutopilotServerHealth(nil) + + version := "" + for _, server := range operatorHealth.Servers { + if server.Leader { + version = server.Version + } + } - newFilePath := filepath.Join(dir, "backup"+"-"+version+"-"+a.Config.Datacenter+ - "-"+a.Config.NodeName+"-"+status+".tgz") + newFilePath := filepath.Join(dir, "backup"+"-"+version+"-"+datacenter+".tgz") code := c.Run(args) if code != 0 { From 0bc54b181f01c86f62bd4bc361b673fcbdbd0d38 Mon Sep 17 00:00:00 2001 From: absolutelightning Date: Wed, 6 Sep 2023 13:56:58 +0530 Subject: [PATCH 3/6] fix tests lint --- command/snapshot/save/snapshot_save_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/command/snapshot/save/snapshot_save_test.go b/command/snapshot/save/snapshot_save_test.go index 894cadf25d4..4f6b444ac42 100644 --- a/command/snapshot/save/snapshot_save_test.go +++ b/command/snapshot/save/snapshot_save_test.go @@ -105,7 +105,7 @@ func TestSnapshotSaveCommandWithAppendFileNameFlag(t *testing.T) { datacenter := dc.(string) - operatorHealth, err := client.Operator().AutopilotServerHealth(nil) + operatorHealth, _ := client.Operator().AutopilotServerHealth(nil) version := "" for _, server := range operatorHealth.Servers { From fc4fd363985d6453f571bb35964f68fa79d95450 Mon Sep 17 00:00:00 2001 From: absolutelightning Date: Wed, 6 Sep 2023 14:20:44 +0530 Subject: [PATCH 4/6] fix api call inside dc --- command/snapshot/save/snapshot_save.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/command/snapshot/save/snapshot_save.go b/command/snapshot/save/snapshot_save.go index 0541ce97c53..daf962b2e37 100644 --- a/command/snapshot/save/snapshot_save.go +++ b/command/snapshot/save/snapshot_save.go @@ -74,12 +74,6 @@ func (c *cmd) Run(args []string) int { appendFileNameFlags := strings.Split(c.appendFileNameFlag.String(), ",") if len(appendFileNameFlags) != 0 && len(c.appendFileNameFlag.String()) > 0 { - agentSelfResponse, err := client.Agent().Self() - if err != nil { - c.UI.Error(fmt.Sprintf("Error connecting to Consul agent and fetching datacenter/version: %s", err)) - return 1 - } - fileExt := filepath.Ext(file) fileNameWithoutExt := strings.TrimSuffix(file, fileExt) @@ -100,6 +94,12 @@ func (c *cmd) Run(args []string) int { } if slices.Contains(appendFileNameFlags, "dc") { + agentSelfResponse, err := client.Agent().Self() + if err != nil { + c.UI.Error(fmt.Sprintf("Error connecting to Consul agent and fetching datacenter/version: %s", err)) + return 1 + } + if config, ok := agentSelfResponse["Config"]; ok { if datacenter, ok := config["Datacenter"]; ok { fileNameWithoutExt = fileNameWithoutExt + "-" + datacenter.(string) From e31cca38ee486ef0fa54d9f15b98891e7a7bc022 Mon Sep 17 00:00:00 2001 From: absolutelightning Date: Wed, 6 Sep 2023 14:25:17 +0530 Subject: [PATCH 5/6] updated doc --- website/content/commands/snapshot/save.mdx | 1 + 1 file changed, 1 insertion(+) diff --git a/website/content/commands/snapshot/save.mdx b/website/content/commands/snapshot/save.mdx index e8b780c0267..cf77cd48a69 100644 --- a/website/content/commands/snapshot/save.mdx +++ b/website/content/commands/snapshot/save.mdx @@ -90,6 +90,7 @@ $ consul snapshot save -append-filename version,dc backup.snap File name created will be like backup-%CONSUL_VERSION%-%DC_NAME%.snap example - backup-1.17.0-dc1-local-machine-leader.tgz +Note Version is always the leader's consul version Please see the [HTTP API](/consul/api-docs/snapshot) documentation for more details about snapshot internals. From f00f303f545b64342afa58bdadf0c47d3a9439c9 Mon Sep 17 00:00:00 2001 From: absolutelightning Date: Wed, 6 Sep 2023 15:27:35 +0530 Subject: [PATCH 6/6] address comments --- command/snapshot/save/snapshot_save.go | 2 +- command/snapshot/save/snapshot_save_test.go | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/command/snapshot/save/snapshot_save.go b/command/snapshot/save/snapshot_save.go index daf962b2e37..ac333e8ca3d 100644 --- a/command/snapshot/save/snapshot_save.go +++ b/command/snapshot/save/snapshot_save.go @@ -83,7 +83,7 @@ func (c *cmd) Run(args []string) int { c.UI.Error(fmt.Sprintf("Error fetching version of Consul agent Leader: %s", err)) return 1 } - version := "" + var version string for _, server := range operatorHealthResponse.Servers { if server.Leader { version = server.Version diff --git a/command/snapshot/save/snapshot_save_test.go b/command/snapshot/save/snapshot_save_test.go index 4f6b444ac42..44d07aebb88 100644 --- a/command/snapshot/save/snapshot_save_test.go +++ b/command/snapshot/save/snapshot_save_test.go @@ -105,9 +105,10 @@ func TestSnapshotSaveCommandWithAppendFileNameFlag(t *testing.T) { datacenter := dc.(string) - operatorHealth, _ := client.Operator().AutopilotServerHealth(nil) + operatorHealth, error := client.Operator().AutopilotServerHealth(nil) + require.NoError(t, error) - version := "" + var version string for _, server := range operatorHealth.Servers { if server.Leader { version = server.Version