From 19e6bfe47868378a17a8433ad375b30bbca153ff Mon Sep 17 00:00:00 2001 From: Tejaswi Bondila Date: Wed, 18 Oct 2023 13:37:53 +0530 Subject: [PATCH] Added new flag for status and service-versions command Signed-off-by: Tejaswi Bondila --- .../cmd/chef-automate/service_versions.go | 36 +++++++--- .../chef-automate/service_versions_test.go | 37 ++++++++-- .../automate-cli/cmd/chef-automate/status.go | 21 ++++-- .../cmd/chef-automate/status_test.go | 69 ++++++++++++++++++- 4 files changed, 141 insertions(+), 22 deletions(-) diff --git a/components/automate-cli/cmd/chef-automate/service_versions.go b/components/automate-cli/cmd/chef-automate/service_versions.go index 02c3310c01e..f2f330ce3e2 100644 --- a/components/automate-cli/cmd/chef-automate/service_versions.go +++ b/components/automate-cli/cmd/chef-automate/service_versions.go @@ -17,17 +17,19 @@ import ( ) const ( - SERVICE_VERSIONS_ERROR_ON_SELF_MANAGED = "Showing the service-versions for externally configured %s is not supported." - BACKEND_SERVICE_VERSIONS_CMD = `sudo HAB_LICENSE=accept-no-persist hab svc status | awk 'NR>1 {split($1,a,"/"); printf "%s/%s %s %s\n", a[1], a[2], a[3], a[4]}'` - FRONTEND_SERVICE_VERSIONS_CMD = "sudo chef-automate service-versions" + SERVICE_VERSIONS_ERROR_ON_SELF_MANAGED = "Showing the service-versions for externally configured %s is not supported." + BACKEND_SERVICE_VERSIONS_CMD = `sudo HAB_LICENSE=accept-no-persist hab svc status | awk 'NR>1 {split($1,a,"/"); printf "%s/%s %s %s\n", a[1], a[2], a[3], a[4]}'` + ACCEPTED_LICENENSE_BACKEND_SERVICE_VERSIONS_CMD = `echo yes | sudo hab svc status | awk 'NR>1 {split($1,a,"/"); printf "%s/%s %s %s\n", a[1], a[2], a[3], a[4]}'` + FRONTEND_SERVICE_VERSIONS_CMD = "sudo chef-automate service-versions" ) type ServiceVersionsCmdFlags struct { - automate bool - chefServer bool - postgresql bool - opensearch bool - node string + automate bool + chefServer bool + postgresql bool + opensearch bool + acceptHabLicense bool + node string } var serviceVersionsCmdFlag = ServiceVersionsCmdFlags{} @@ -67,6 +69,8 @@ func init() { serviceVersionsCmd.PersistentFlags().SetAnnotation("os", docs.Compatibility, []string{docs.CompatiblewithHA}) serviceVersionsCmd.PersistentFlags().StringVar(&serviceVersionsCmdFlag.node, "node", "", "Pass this flag to check service-versions of particular node in the cluster") serviceVersionsCmd.PersistentFlags().SetAnnotation("node", docs.Compatibility, []string{docs.CompatiblewithHA}) + serviceVersionsCmd.PersistentFlags().BoolVarP(&serviceVersionsCmdFlag.acceptHabLicense, "accept-hab-license", "", false, "Pass this flag to accept hab license for PostgresQL/OpenSearch nodes and check service-versions") + serviceVersionsCmd.PersistentFlags().SetAnnotation("accept-hab-license", docs.Compatibility, []string{docs.CompatiblewithHA}) RootCmd.AddCommand(serviceVersionsCmd) } @@ -180,6 +184,18 @@ func nodeServiceVersions(flags ServiceVersionsCmdFlags, nodeType string, infra * writer := cli.NewWriter(os.Stdout, os.Stderr, os.Stdin) remoteExe.SetWriter(writer) nodeMap := constructNodeMapForServiceVersions(infra, &flags) + if flags.opensearch && flags.acceptHabLicense { + nodeMap.Opensearch.CmdInputs.Cmd = ACCEPTED_LICENENSE_BACKEND_SERVICE_VERSIONS_CMD + } + if flags.opensearch && !flags.acceptHabLicense { + nodeMap.Opensearch.CmdInputs.Cmd = BACKEND_SERVICE_VERSIONS_CMD + } + if flags.postgresql && flags.acceptHabLicense { + nodeMap.Postgresql.CmdInputs.Cmd = ACCEPTED_LICENENSE_BACKEND_SERVICE_VERSIONS_CMD + } + if flags.postgresql && !flags.acceptHabLicense { + nodeMap.Postgresql.CmdInputs.Cmd = BACKEND_SERVICE_VERSIONS_CMD + } cmdResult, err := remoteExe.ExecuteWithNodeMap(nodeMap) serviceVersionsCmdResults <- ServiceVersionsCmdResult{ cmdResult: cmdResult, @@ -220,7 +236,7 @@ func constructNodeMapForServiceVersions(infra *AutomateHAInfraDetails, flags *Se }, Postgresql: &Cmd{ CmdInputs: &CmdInputs{ - Cmd: BACKEND_SERVICE_VERSIONS_CMD, + Cmd: "", NodeIps: []string{flags.node}, ErrorCheckEnableInOutput: true, NodeType: flags.postgresql, @@ -230,7 +246,7 @@ func constructNodeMapForServiceVersions(infra *AutomateHAInfraDetails, flags *Se }, Opensearch: &Cmd{ CmdInputs: &CmdInputs{ - Cmd: BACKEND_SERVICE_VERSIONS_CMD, + Cmd: "", NodeIps: []string{flags.node}, ErrorCheckEnableInOutput: true, NodeType: flags.opensearch, diff --git a/components/automate-cli/cmd/chef-automate/service_versions_test.go b/components/automate-cli/cmd/chef-automate/service_versions_test.go index 2791fcef69d..2702cd30741 100644 --- a/components/automate-cli/cmd/chef-automate/service_versions_test.go +++ b/components/automate-cli/cmd/chef-automate/service_versions_test.go @@ -59,10 +59,11 @@ func TestRunServiceVersionsFromBastion(t *testing.T) { { description: "Want service_versions of backend services", flags: &ServiceVersionsCmdFlags{ - automate: false, - chefServer: false, - opensearch: true, - postgresql: true, + automate: false, + chefServer: false, + opensearch: true, + postgresql: true, + acceptHabLicense: true, }, mockNodeOpUtils: &MockNodeUtilsImpl{ getHaInfraDetailsfunc: func() (*AutomateHAInfraDetails, *SSHConfig, error) { @@ -179,6 +180,30 @@ func TestRunServiceVersionsFromBastion(t *testing.T) { }, errorWant: errors.New("Some error occured while remote execution"), }, + { + description: "Want service_versions of backend services with --accept-hab-license", + flags: &ServiceVersionsCmdFlags{ + automate: false, + chefServer: false, + opensearch: true, + postgresql: true, + }, + mockNodeOpUtils: &MockNodeUtilsImpl{ + getHaInfraDetailsfunc: func() (*AutomateHAInfraDetails, *SSHConfig, error) { + return &AutomateHAInfraDetails{}, &SSHConfig{}, nil + }, + isManagedServicesOnFunc: func() bool { + return false + }, + }, + mockRemoteCmdExec: &MockRemoteCmdExecutor{ + ExecuteWithNodeMapFunc: func(nodeMap *NodeTypeAndCmd) (map[string][]*CmdResult, error) { + return map[string][]*CmdResult{}, nil + }, + SetWriterFunc: func(cli *cli.Writer) {}, + }, + errorWant: nil, + }, } for _, testCase := range testCases { @@ -281,7 +306,7 @@ func TestConstructNodeMapForServiceVersions(t *testing.T) { }, Postgresql: &Cmd{ CmdInputs: &CmdInputs{ - Cmd: BACKEND_SERVICE_VERSIONS_CMD, + Cmd: "", NodeIps: []string{""}, ErrorCheckEnableInOutput: true, NodeType: false, @@ -291,7 +316,7 @@ func TestConstructNodeMapForServiceVersions(t *testing.T) { }, Opensearch: &Cmd{ CmdInputs: &CmdInputs{ - Cmd: BACKEND_SERVICE_VERSIONS_CMD, + Cmd: "", NodeIps: []string{""}, ErrorCheckEnableInOutput: true, NodeType: false, diff --git a/components/automate-cli/cmd/chef-automate/status.go b/components/automate-cli/cmd/chef-automate/status.go index 4f05328207a..3b2b73ce992 100644 --- a/components/automate-cli/cmd/chef-automate/status.go +++ b/components/automate-cli/cmd/chef-automate/status.go @@ -67,7 +67,7 @@ const ( STATUS_ERROR_ON_SELF_MANAGED = "Showing the status for externally configured %s is not supported." CMD_FAIL_MSG = "Command failed on %s node : %s with error:\n %s\n" BACKEND_STATUS_CMD = "sudo HAB_LICENSE=accept-no-persist hab svc status" - ACCEPTED_LICENENSE_BACKEND_STATUS_CMD = "sudo echo yes | hab svc status" + ACCEPTED_LICENENSE_BACKEND_STATUS_CMD = "echo yes | sudo hab svc status" FRONTEND_STATUS_CMD = "sudo chef-automate status" ) @@ -118,7 +118,8 @@ func newStatusCmd() *cobra.Command { statusCmd.Flags().SetAnnotation("os", docs.Compatibility, []string{docs.CompatiblewithHA}) statusCmd.Flags().StringVar(&statusCmdFlag.node, "node", "", "Pass this flag to check status of perticular node in the cluster") statusCmd.Flags().SetAnnotation("node", docs.Compatibility, []string{docs.CompatiblewithHA}) - statusCmd.Flags().StringVar(&statusCmdFlag.node, "--accept-hab-license", "", "Pass this flag to accept hab license for PostgresQL/OpenSearch nodes") + statusCmd.Flags().BoolVarP(&statusCmdFlag.acceptHabLicense, "accept-hab-license", "", false, "Pass this flag to accept hab license for PostgresQL/OpenSearch nodes and show status") + statusCmd.Flags().SetAnnotation("accept-hab-license", docs.Compatibility, []string{docs.CompatiblewithHA}) statusCmd.AddCommand(newStatusSummaryCmd()) return statusCmd @@ -373,6 +374,18 @@ func nodeStatus(flags StatusCmdFlags, nodeType string, infra *AutomateHAInfraDet writer := cli.NewWriter(os.Stdout, os.Stderr, os.Stdin) remoteExe.SetWriter(writer) nodeMap := constructNodeMapForStatus(&flags, infra) + if flags.opensearch && flags.acceptHabLicense { + nodeMap.Opensearch.CmdInputs.Cmd = ACCEPTED_LICENENSE_BACKEND_STATUS_CMD + } + if flags.opensearch && !flags.acceptHabLicense { + nodeMap.Opensearch.CmdInputs.Cmd = BACKEND_STATUS_CMD + } + if flags.postgresql && flags.acceptHabLicense { + nodeMap.Postgresql.CmdInputs.Cmd = ACCEPTED_LICENENSE_BACKEND_STATUS_CMD + } + if flags.postgresql && !flags.acceptHabLicense { + nodeMap.Postgresql.CmdInputs.Cmd = BACKEND_STATUS_CMD + } cmdResult, err := remoteExe.ExecuteWithNodeMap(nodeMap) statusCmdResults <- StatusCmdResult{ cmdResult: cmdResult, @@ -482,7 +495,7 @@ func constructNodeMapForStatus(flags *StatusCmdFlags, infra *AutomateHAInfraDeta }, Postgresql: &Cmd{ CmdInputs: &CmdInputs{ - Cmd: BACKEND_STATUS_CMD, + Cmd: "", NodeIps: []string{flags.node}, ErrorCheckEnableInOutput: true, NodeType: flags.postgresql, @@ -493,7 +506,7 @@ func constructNodeMapForStatus(flags *StatusCmdFlags, infra *AutomateHAInfraDeta }, Opensearch: &Cmd{ CmdInputs: &CmdInputs{ - Cmd: BACKEND_STATUS_CMD, + Cmd: "", NodeIps: []string{flags.node}, ErrorCheckEnableInOutput: true, NodeType: flags.opensearch, diff --git a/components/automate-cli/cmd/chef-automate/status_test.go b/components/automate-cli/cmd/chef-automate/status_test.go index d569cd5e778..9b102b93ecc 100644 --- a/components/automate-cli/cmd/chef-automate/status_test.go +++ b/components/automate-cli/cmd/chef-automate/status_test.go @@ -131,7 +131,7 @@ func TestConstructNodeMapForStatus(t *testing.T) { }, Postgresql: &Cmd{ CmdInputs: &CmdInputs{ - Cmd: BACKEND_STATUS_CMD, + Cmd: "", NodeIps: []string{""}, ErrorCheckEnableInOutput: true, NodeType: false, @@ -142,7 +142,7 @@ func TestConstructNodeMapForStatus(t *testing.T) { }, Opensearch: &Cmd{ CmdInputs: &CmdInputs{ - Cmd: BACKEND_STATUS_CMD, + Cmd: "", NodeIps: []string{""}, ErrorCheckEnableInOutput: true, NodeType: false, @@ -274,6 +274,71 @@ func TestRunStatusFromBastion(t *testing.T) { }, errorWant: status.Errorf(status.InvalidCommandArgsError, STATUS_ERROR_ON_SELF_MANAGED, POSTGRESQL), }, + { + description: "Want status pg services when --accept-hab-license provided", + flags: &StatusCmdFlags{ + postgresql: true, + acceptHabLicense: true, + }, + mockNodeOpUtils: &MockNodeUtilsImpl{ + getHaInfraDetailsfunc: func() (*AutomateHAInfraDetails, *SSHConfig, error) { + return &AutomateHAInfraDetails{}, &SSHConfig{}, nil + }, + isManagedServicesOnFunc: func() bool { + return false + }, + }, + mockRemoteCmdExec: &MockRemoteCmdExecutor{ + ExecuteWithNodeMapFunc: func(nodeMap *NodeTypeAndCmd) (map[string][]*CmdResult, error) { + return map[string][]*CmdResult{}, nil + }, + SetWriterFunc: func(cli *cli.Writer) {}, + }, + errorWant: nil, + }, + { + description: "Want status os services when --accept-hab-license provided", + flags: &StatusCmdFlags{ + opensearch: true, + acceptHabLicense: true, + }, + mockNodeOpUtils: &MockNodeUtilsImpl{ + getHaInfraDetailsfunc: func() (*AutomateHAInfraDetails, *SSHConfig, error) { + return &AutomateHAInfraDetails{}, &SSHConfig{}, nil + }, + isManagedServicesOnFunc: func() bool { + return false + }, + }, + mockRemoteCmdExec: &MockRemoteCmdExecutor{ + ExecuteWithNodeMapFunc: func(nodeMap *NodeTypeAndCmd) (map[string][]*CmdResult, error) { + return map[string][]*CmdResult{}, nil + }, + SetWriterFunc: func(cli *cli.Writer) {}, + }, + errorWant: nil, + }, + { + description: "Want status all services when --accept-hab-license provided", + flags: &StatusCmdFlags{ + acceptHabLicense: true, + }, + mockNodeOpUtils: &MockNodeUtilsImpl{ + getHaInfraDetailsfunc: func() (*AutomateHAInfraDetails, *SSHConfig, error) { + return &AutomateHAInfraDetails{}, &SSHConfig{}, nil + }, + isManagedServicesOnFunc: func() bool { + return false + }, + }, + mockRemoteCmdExec: &MockRemoteCmdExecutor{ + ExecuteWithNodeMapFunc: func(nodeMap *NodeTypeAndCmd) (map[string][]*CmdResult, error) { + return map[string][]*CmdResult{}, nil + }, + SetWriterFunc: func(cli *cli.Writer) {}, + }, + errorWant: nil, + }, } for _, testCase := range testCases {