Browse Source

added node name and status

pull/18639/head
absolutelightning 1 year ago
parent
commit
0130674592
  1. 5
      .changelog/18625.txt
  2. 25
      command/snapshot/save/snapshot_save.go
  3. 13
      command/snapshot/save/snapshot_save_test.go

5
.changelog/18625.txt

@ -1,4 +1,5 @@
```release-note:improvement ```release-note:improvement
Adds flag -append-filename (which works on two values version and dc) to consul snapshot save command. 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 will add consul version and consul datacenter in the file name given in the snapshot save command before the file extension. 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
command/snapshot/save/snapshot_save.go

@ -36,7 +36,7 @@ type cmd struct {
func (c *cmd) getAppendFileNameFlag() *flag.FlagSet { func (c *cmd) getAppendFileNameFlag() *flag.FlagSet {
fs := flag.NewFlagSet("", flag.ContinueOnError) fs := flag.NewFlagSet("", flag.ContinueOnError)
fs.Var(&c.appendFileNameFlag, "append-filename", "Append filename flag takes two possible values. "+ 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 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 //adding extension back
file = fileNameWithoutExt + fileExt file = fileNameWithoutExt + fileExt
} }

13
command/snapshot/save/snapshot_save_test.go

@ -88,12 +88,21 @@ func TestSnapshotSaveCommandWithAppendFileNameFlag(t *testing.T) {
dir := testutil.TempDir(t, "snapshot") dir := testutil.TempDir(t, "snapshot")
file := filepath.Join(dir, "backup.tgz") file := filepath.Join(dir, "backup.tgz")
args := []string{ args := []string{
"-append-filename=version,dc", "-append-filename=version,dc,node,status",
"-http-addr=" + a.HTTPAddr(), "-http-addr=" + a.HTTPAddr(),
file, 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) code := c.Run(args)
if code != 0 { if code != 0 {

Loading…
Cancel
Save