Skip to content

Commit

Permalink
Added new flag for status and service-versions command
Browse files Browse the repository at this point in the history
Signed-off-by: Tejaswi Bondila <[email protected]>
  • Loading branch information
bvtejaswi committed Oct 23, 2023
1 parent b343a44 commit 19e6bfe
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 22 deletions.
36 changes: 26 additions & 10 deletions components/automate-cli/cmd/chef-automate/service_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
37 changes: 31 additions & 6 deletions components/automate-cli/cmd/chef-automate/service_versions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
21 changes: 17 additions & 4 deletions components/automate-cli/cmd/chef-automate/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
69 changes: 67 additions & 2 deletions components/automate-cli/cmd/chef-automate/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func TestConstructNodeMapForStatus(t *testing.T) {
},
Postgresql: &Cmd{
CmdInputs: &CmdInputs{
Cmd: BACKEND_STATUS_CMD,
Cmd: "",
NodeIps: []string{""},
ErrorCheckEnableInOutput: true,
NodeType: false,
Expand All @@ -142,7 +142,7 @@ func TestConstructNodeMapForStatus(t *testing.T) {
},
Opensearch: &Cmd{
CmdInputs: &CmdInputs{
Cmd: BACKEND_STATUS_CMD,
Cmd: "",
NodeIps: []string{""},
ErrorCheckEnableInOutput: true,
NodeType: false,
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 19e6bfe

Please sign in to comment.