From 0130674592727129bf247815d5b7236c693278e3 Mon Sep 17 00:00:00 2001 From: absolutelightning Date: Thu, 31 Aug 2023 22:02:32 +0530 Subject: [PATCH] added node name and status --- .changelog/18625.txt | 5 +++-- command/snapshot/save/snapshot_save.go | 25 ++++++++++++++++++++- command/snapshot/save/snapshot_save_test.go | 13 +++++++++-- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/.changelog/18625.txt b/.changelog/18625.txt index 763b47c1eb..8474cac8dc 100644 --- a/.changelog/18625.txt +++ b/.changelog/18625.txt @@ -1,4 +1,5 @@ ```release-note:improvement -Adds flag -append-filename (which works on two values version and dc) to consul snapshot save command. -Adding the flag -append-filename version,dc will add consul version and consul datacenter in the file name given in the snapshot save command before the file extension. +Adds flag -append-filename (which works on values version, dc, node and status) to consul snapshot save command. +Adding the flag -append-filename version,dc,node,status will add consul version, consul datacenter, node name and leader/follower +(status) in the file name given in the snapshot save command before the file extension. ``` diff --git a/command/snapshot/save/snapshot_save.go b/command/snapshot/save/snapshot_save.go index 99350a947b..c9ed61b281 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 takes two possible values. "+ - "1. version, 2. dc. It appends consul version and datacenter to filename given in command") + "1. version, 2. dc. 3. node 5. status. It appends consul version and datacenter to filename given in command") return fs } @@ -99,6 +99,29 @@ 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/command/snapshot/save/snapshot_save_test.go b/command/snapshot/save/snapshot_save_test.go index c7d7367e41..8e339cd51f 100644 --- a/command/snapshot/save/snapshot_save_test.go +++ b/command/snapshot/save/snapshot_save_test.go @@ -88,12 +88,21 @@ func TestSnapshotSaveCommandWithAppendFileNameFlag(t *testing.T) { dir := testutil.TempDir(t, "snapshot") file := filepath.Join(dir, "backup.tgz") args := []string{ - "-append-filename=version,dc", + "-append-filename=version,dc,node,status", "-http-addr=" + a.HTTPAddr(), file, } - newFilePath := filepath.Join(dir, "backup"+"-"+a.Config.Version+"-"+a.Config.Datacenter+".tgz") + stats := a.Stats() + + status := "follower" + + if stats["consul"]["leader"] == "true" { + status = "leader" + } + + newFilePath := filepath.Join(dir, "backup"+"-"+a.Config.Version+"-"+a.Config.Datacenter+ + "-"+a.Config.NodeName+"-"+status+".tgz") code := c.Run(args) if code != 0 {