Skip to content

Commit

Permalink
added node name and status
Browse files Browse the repository at this point in the history
  • Loading branch information
absolutelightning committed Aug 31, 2023
1 parent 2c9f48e commit 0130674
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
5 changes: 3 additions & 2 deletions .changelog/18625.txt
Original file line number Diff line number Diff line change
@@ -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.
```
25 changes: 24 additions & 1 deletion command/snapshot/save/snapshot_save.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down
13 changes: 11 additions & 2 deletions command/snapshot/save/snapshot_save_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 0130674

Please sign in to comment.