From ebc62eda4859b8b1ab4f117e3ea7bdea52a0446a Mon Sep 17 00:00:00 2001 From: Arvinth C Date: Fri, 25 Nov 2022 17:12:57 +0530 Subject: [PATCH] Config show for each nodes from Bastion server Signed-off-by: Arvinth C --- .../automate-cli/cmd/chef-automate/config.go | 74 ++++++++++++++++--- 1 file changed, 65 insertions(+), 9 deletions(-) diff --git a/components/automate-cli/cmd/chef-automate/config.go b/components/automate-cli/cmd/chef-automate/config.go index a8c2bd03c56..59c98a13010 100644 --- a/components/automate-cli/cmd/chef-automate/config.go +++ b/components/automate-cli/cmd/chef-automate/config.go @@ -57,6 +57,8 @@ const ( automate-backend-ctl applied --svc=automate-ha-%s ` + GET_FRONTEND_CONFIGS = `sudo chef-automate config show` + dateFormat = "%Y%m%d%H%M%S" postgresql = "postgresql" @@ -70,7 +72,12 @@ func init() { configCmd.AddCommand(patchConfigCmd) configCmd.AddCommand(setConfigCmd) - showConfigCmd.Flags().BoolVarP(&configCmdFlags.overwriteFile, "overwrite", "o", false, "Overwrite existing config.toml") + showConfigCmd.Flags().BoolVarP(&configCmdFlags.overwriteFile, "overwrite", "O", false, "Overwrite existing config.toml [Standalone]") + showConfigCmd.PersistentFlags().BoolVarP(&configCmdFlags.automate, "automate", "a", false, "Shows configurations from Automate node(HA)") + showConfigCmd.PersistentFlags().BoolVarP(&configCmdFlags.chef_server, "chef_server", "c", false, "Shows configurations from Chef-server node(HA)") + showConfigCmd.PersistentFlags().BoolVarP(&configCmdFlags.postgresql, "postgresql", "p", false, "Shows configurations from PostgresQL node") + showConfigCmd.PersistentFlags().BoolVarP(&configCmdFlags.opensearch, "opensearch", "o", false, "Shows configurations from OpenSearch node") + patchConfigCmd.PersistentFlags().BoolVarP(&configCmdFlags.frontend, "frontend", "f", false, "Patch toml configuration to the all frontend nodes") patchConfigCmd.PersistentFlags().BoolVar(&configCmdFlags.frontend, "fe", false, "Patch toml configuration to the all frontend nodes[DUPLICATE]") @@ -99,7 +106,7 @@ var showConfigCmd = &cobra.Command{ Short: "show the Chef Automate configuration", Long: "Show the Chef Automate configuration. When given a filepath, the output will be written to the file instead of printed to STDOUT", RunE: runShowCmd, - Args: cobra.RangeArgs(0, 1), + Args: cobra.RangeArgs(0, 2), } var patchConfigCmd = &cobra.Command{ @@ -118,6 +125,61 @@ var setConfigCmd = &cobra.Command{ } func runShowCmd(cmd *cobra.Command, args []string) error { + + if isA2HARBFileExist() { + + infra, err := getAutomateHAInfraDetails() + if err != nil { + return err + } + + sshUser := infra.Outputs.SSHUser.Value + sskKeyFile := infra.Outputs.SSHKeyFile.Value + sshPort := infra.Outputs.SSHPort.Value + + sshConfig := &SSHConfig{ + sshUser: sshUser, + sshKeyFile: sskKeyFile, + sshPort: sshPort, + } + sshUtil := NewSSHUtil(sshConfig) + + writer.Bodyf("%v", configCmdFlags) + if configCmdFlags.overwriteFile { + writer.Error("Overwrite flag is not supported in HA") + // return errors.New("Node not mentioned") + } + + var scriptCommand string + var hostIpArray []string + switch true { + case configCmdFlags.automate: + hostIpArray = append(hostIpArray, infra.Outputs.AutomatePrivateIps.Value[0]) + scriptCommand = GET_FRONTEND_CONFIGS + case configCmdFlags.chef_server: + hostIpArray = append(hostIpArray, infra.Outputs.ChefServerPrivateIps.Value[0]) + scriptCommand = GET_FRONTEND_CONFIGS + case configCmdFlags.postgresql: + hostIpArray = infra.Outputs.PostgresqlPrivateIps.Value + scriptCommand = fmt.Sprintf(GET_CONFIG, "postgresql") + case configCmdFlags.opensearch: + hostIpArray = infra.Outputs.OpensearchPrivateIps.Value + scriptCommand = fmt.Sprintf(GET_CONFIG, "opensearch") + default: + return errors.New("Unsupported flag") + } + for i := 0; i < len(hostIpArray); i++ { + sshUtil.getSSHConfig().hostIP = hostIpArray[i] + output, err := sshUtil.connectAndExecuteCommandOnRemote(scriptCommand, true) + if err != nil { + return err + } + writer.Success("Config on node IP: " + hostIpArray[i] + "\n") + writer.Println(output) + } + + return nil + } res, err := client.GetAutomateConfig(configCmdFlags.timeout) if err != nil { return err @@ -540,17 +602,11 @@ func getConfigForArgsPostgresqlOrOpenSearch(args []string, remoteService string) } return dest, nil } - - return nil, nil - } // isConfigChanged checks if configuration is changed func isConfigChanged(src interface{}, dest interface{}) bool { - if reflect.DeepEqual(src, dest) { - return false - } - return true + return !reflect.DeepEqual(src, dest) } // getExistingAndRequestedConfigForPostgres get requested and existing config for postgresql