|
|
@ -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 supports the following "+
|
|
|
|
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
|
|
|
|
return fs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -74,50 +74,35 @@ func (c *cmd) Run(args []string) int {
|
|
|
|
appendFileNameFlags := strings.Split(c.appendFileNameFlag.String(), ",")
|
|
|
|
appendFileNameFlags := strings.Split(c.appendFileNameFlag.String(), ",")
|
|
|
|
|
|
|
|
|
|
|
|
if len(appendFileNameFlags) != 0 && len(c.appendFileNameFlag.String()) > 0 {
|
|
|
|
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)
|
|
|
|
fileExt := filepath.Ext(file)
|
|
|
|
fileNameWithoutExt := strings.TrimSuffix(file, fileExt)
|
|
|
|
fileNameWithoutExt := strings.TrimSuffix(file, fileExt)
|
|
|
|
|
|
|
|
|
|
|
|
if slices.Contains(appendFileNameFlags, "version") {
|
|
|
|
if slices.Contains(appendFileNameFlags, "version") {
|
|
|
|
if config, ok := agentSelfResponse["Config"]; ok {
|
|
|
|
operatorHealthResponse, err := client.Operator().AutopilotServerHealth(nil)
|
|
|
|
if version, ok := config["Version"]; ok {
|
|
|
|
if err != nil {
|
|
|
|
fileNameWithoutExt = fileNameWithoutExt + "-" + version.(string)
|
|
|
|
c.UI.Error(fmt.Sprintf("Error fetching version of Consul agent Leader: %s", err))
|
|
|
|
|
|
|
|
return 1
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
var version string
|
|
|
|
|
|
|
|
for _, server := range operatorHealthResponse.Servers {
|
|
|
|
|
|
|
|
if server.Leader {
|
|
|
|
|
|
|
|
version = server.Version
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fileNameWithoutExt = fileNameWithoutExt + "-" + version
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if slices.Contains(appendFileNameFlags, "dc") {
|
|
|
|
if slices.Contains(appendFileNameFlags, "dc") {
|
|
|
|
if config, ok := agentSelfResponse["Config"]; ok {
|
|
|
|
agentSelfResponse, err := client.Agent().Self()
|
|
|
|
if datacenter, ok := config["Datacenter"]; ok {
|
|
|
|
if err != nil {
|
|
|
|
fileNameWithoutExt = fileNameWithoutExt + "-" + datacenter.(string)
|
|
|
|
c.UI.Error(fmt.Sprintf("Error connecting to Consul agent and fetching datacenter/version: %s", err))
|
|
|
|
}
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if slices.Contains(appendFileNameFlags, "node") {
|
|
|
|
|
|
|
|
if config, ok := agentSelfResponse["Config"]; ok {
|
|
|
|
if config, ok := agentSelfResponse["Config"]; ok {
|
|
|
|
if nodeName, ok := config["NodeName"]; ok {
|
|
|
|
if datacenter, ok := config["Datacenter"]; ok {
|
|
|
|
fileNameWithoutExt = fileNameWithoutExt + "-" + nodeName.(string)
|
|
|
|
fileNameWithoutExt = fileNameWithoutExt + "-" + datacenter.(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"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|