Skip to content

Commit

Permalink
Resolving comments
Browse files Browse the repository at this point in the history
Signed-off-by: Tejaswi Bondila <bondila.venkatatejaswi@progress.com>
  • Loading branch information
bvtejaswi committed Jun 20, 2023
1 parent d483245 commit a85959a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 32 deletions.
2 changes: 1 addition & 1 deletion components/automate-cli/cmd/chef-automate/cmdUtils.go
Expand Up @@ -68,7 +68,7 @@ type MockRemoteCmdExecutor struct {
ExecuteFunc func() (map[string][]*CmdResult, error)
ExecuteWithNodeMapFunc func(nodeMap *NodeTypeAndCmd) (map[string][]*CmdResult, error)
GetSshUtilFunc func() SSHUtil
SetWriterFunc func(cli *cli.Writer)
SetWriterFunc func(cli *cli.Writer)
}

func (m *MockRemoteCmdExecutor) Execute() (map[string][]*CmdResult, error) {
Expand Down
2 changes: 1 addition & 1 deletion components/automate-cli/cmd/chef-automate/cmdUtils_test.go
Expand Up @@ -267,7 +267,7 @@ func TestExecute(t *testing.T) {
c := &remoteCmdExecutor{
NodeMap: testCase.fields.NodeMap,
SshUtil: testCase.fields.SshUtil,
Output: cli.NewWriter(os.Stdout, os.Stderr, os.Stdin),
Output: cli.NewWriter(os.Stdout, os.Stderr, os.Stdin),
}
_, err := c.Execute()
if testCase.wantErr {
Expand Down
53 changes: 28 additions & 25 deletions components/automate-cli/cmd/chef-automate/restart.go
Expand Up @@ -19,7 +19,7 @@ const (
RESTART_FRONTEND_COMMAND = `sudo chef-automate restart-services`
RESTART_BACKEND_COMMAND = `sudo HAB_LICENSE=accept-no-persist systemctl restart hab-sup`
DEFAULT_TIMEOUT_FOR_RESTART = 1200
ERROR_ON_MANAGED_SERVICES = "Restart for externally configured services are not supported."
ERROR_ON_MANAGED_SERVICES = "Restart for externally configured %s is not supported."
CMD_FAILED_MSG = "Command failed on %s node : %s with error:\n %s\n"
)

Expand Down Expand Up @@ -123,73 +123,73 @@ func runRestartFromBastion(flags *RestartCmdFlags, rs RemoteCmdExecutor, nu Node
}
flags.automate = true
flags.chefServer = true
if !isManagedServicesOn() {
if !nu.isManagedServicesOn() {
flags.opensearch = true
flags.postgresql = true
}
}
errChan := make(chan restartCmdResult, 4)
runRestartCmdForFrontEnd(infra, flags, rs, errChan)
restartCmdResults := make(chan restartCmdResult, 4)
runRestartCmdForFrontEnd(infra, flags, rs, restartCmdResults)

if nu.isManagedServicesOn() {
errChan <- restartCmdResult{}
errChan <- restartCmdResult{}
err = getChannelValue(errChan, printRestartOutput)
restartCmdResults <- restartCmdResult{}
restartCmdResults <- restartCmdResult{}
err = getChannelValue(restartCmdResults, printRestartOutput)
if err != nil {
return err
}
return handleManagedServices(flags)
}

runRestartCmdForBackend(infra, flags, rs, errChan)
runRestartCmdForBackend(infra, flags, rs, restartCmdResults)

return getChannelValue(errChan, printRestartOutput)
return getChannelValue(restartCmdResults, printRestartOutput)
}

func runRestartCmdForFrontEnd(infra *AutomateHAInfraDetails, flags *RestartCmdFlags, rs RemoteCmdExecutor, errChan chan restartCmdResult) {
func runRestartCmdForFrontEnd(infra *AutomateHAInfraDetails, flags *RestartCmdFlags, rs RemoteCmdExecutor, restartCmdResults chan restartCmdResult) {
if flags.automate {
restartOnGivenNode(flags, AUTOMATE, infra, rs, errChan)
restartOnGivenNode(flags, AUTOMATE, infra, rs, restartCmdResults)
} else {
errChan <- restartCmdResult{}
restartCmdResults <- restartCmdResult{}
}

if flags.chefServer {
flags.automate = false
restartOnGivenNode(flags, CHEF_SERVER, infra, rs, errChan)
restartOnGivenNode(flags, CHEF_SERVER, infra, rs, restartCmdResults)
} else {
errChan <- restartCmdResult{}
restartCmdResults <- restartCmdResult{}
}
}

func runRestartCmdForBackend(infra *AutomateHAInfraDetails, flags *RestartCmdFlags, rs RemoteCmdExecutor, errChan chan restartCmdResult) {
func runRestartCmdForBackend(infra *AutomateHAInfraDetails, flags *RestartCmdFlags, rs RemoteCmdExecutor, restartCmdResults chan restartCmdResult) {
flags.automate = false
flags.chefServer = false
if flags.postgresql {
restartOnGivenNode(flags, POSTGRESQL, infra, rs, errChan)
restartOnGivenNode(flags, POSTGRESQL, infra, rs, restartCmdResults)
} else {
errChan <- restartCmdResult{}
restartCmdResults <- restartCmdResult{}
}
if flags.opensearch {
flags.postgresql = false
restartOnGivenNode(flags, OPENSEARCH, infra, rs, errChan)
restartOnGivenNode(flags, OPENSEARCH, infra, rs, restartCmdResults)
} else {
errChan <- restartCmdResult{}
restartCmdResults <- restartCmdResult{}
}
}

func restartOnGivenNode(flags *RestartCmdFlags, nodeType string, infra *AutomateHAInfraDetails, rs RemoteCmdExecutor, resultErrChan chan restartCmdResult) {
go func(flags RestartCmdFlags, resultErrChan chan<- restartCmdResult) {
func restartOnGivenNode(flags *RestartCmdFlags, nodeType string, infra *AutomateHAInfraDetails, rs RemoteCmdExecutor, restartCmdResults chan restartCmdResult) {
go func(flags RestartCmdFlags, restartCmdResults chan<- restartCmdResult) {
writer := cli.NewWriter(os.Stdout, os.Stderr, os.Stdin)
rs.SetWriter(writer)
nodeMap := constructNodeMapForAllNodeTypes(&flags, infra)
cmdResult, err := rs.ExecuteWithNodeMap(nodeMap)
resultErrChan <- restartCmdResult{
restartCmdResults <- restartCmdResult{
cmdResult: cmdResult,
writer: writer,
nodeType: nodeType,
err: err,
}
}(*flags, resultErrChan)
}(*flags, restartCmdResults)
}

func printRestartOutput(cmdResult map[string][]*CmdResult, remoteService string, writer *cli.Writer) {
Expand Down Expand Up @@ -234,11 +234,14 @@ func getChannelValue(restartCmdResults chan restartCmdResult, printRestartOutput
}

func handleManagedServices(flags *RestartCmdFlags) error {
if flags.postgresql && flags.opensearch {
return status.Errorf(status.InvalidCommandArgsError, ERROR_ON_MANAGED_SERVICES, "postgresql and opensearch")
}
if flags.postgresql {
return status.Errorf(status.InvalidCommandArgsError, ERROR_ON_MANAGED_SERVICES)
return status.Errorf(status.InvalidCommandArgsError, ERROR_ON_MANAGED_SERVICES, POSTGRESQL)
}
if flags.opensearch {
return status.Errorf(status.InvalidCommandArgsError, ERROR_ON_MANAGED_SERVICES)
return status.Errorf(status.InvalidCommandArgsError, ERROR_ON_MANAGED_SERVICES, OPENSEARCH)
}
return nil
}
Expand Down
14 changes: 9 additions & 5 deletions components/automate-cli/cmd/chef-automate/restart_test.go
Expand Up @@ -34,6 +34,7 @@ func TestConstructNodeMapForAllNodeTypes(t *testing.T) {
ErrorCheckEnableInOutput: true,
NodeIps: []string{""},
NodeType: true,
SkipPrintOutput: true,
HideSSHConnectionMessage: true,
},
},
Expand All @@ -43,6 +44,7 @@ func TestConstructNodeMapForAllNodeTypes(t *testing.T) {
ErrorCheckEnableInOutput: true,
NodeIps: []string{""},
NodeType: false,
SkipPrintOutput: true,
HideSSHConnectionMessage: true,
},
},
Expand All @@ -52,6 +54,7 @@ func TestConstructNodeMapForAllNodeTypes(t *testing.T) {
NodeIps: []string{""},
ErrorCheckEnableInOutput: true,
NodeType: false,
SkipPrintOutput: true,
HideSSHConnectionMessage: true,
},
},
Expand All @@ -61,6 +64,7 @@ func TestConstructNodeMapForAllNodeTypes(t *testing.T) {
NodeIps: []string{""},
ErrorCheckEnableInOutput: true,
NodeType: false,
SkipPrintOutput: true,
HideSSHConnectionMessage: true,
},
},
Expand Down Expand Up @@ -247,7 +251,7 @@ func TestRunRestartFromBastion(t *testing.T) {
},
SetWriterFunc: func(cli *cli.Writer) {},
},
errorWant: status.Errorf(status.InvalidCommandArgsError, ERROR_ON_MANAGED_SERVICES),
errorWant: status.Errorf(status.InvalidCommandArgsError, ERROR_ON_MANAGED_SERVICES, OPENSEARCH),
},
{
description: "Restarting Postgresql with managed services",
Expand All @@ -270,7 +274,7 @@ func TestRunRestartFromBastion(t *testing.T) {
},
SetWriterFunc: func(cli *cli.Writer) {},
},
errorWant: status.Errorf(status.InvalidCommandArgsError, ERROR_ON_MANAGED_SERVICES),
errorWant: status.Errorf(status.InvalidCommandArgsError, ERROR_ON_MANAGED_SERVICES, POSTGRESQL),
},
}

Expand All @@ -296,13 +300,13 @@ func TestHandleManagedServiceError(t *testing.T) {
flags: &RestartCmdFlags{
postgresql: true,
},
errorExepected: status.Errorf(status.InvalidCommandArgsError, ERROR_ON_MANAGED_SERVICES),
errorExepected: status.Errorf(status.InvalidCommandArgsError, ERROR_ON_MANAGED_SERVICES, POSTGRESQL),
},
{
flags: &RestartCmdFlags{
opensearch: true,
},
errorExepected: status.Errorf(status.InvalidCommandArgsError, ERROR_ON_MANAGED_SERVICES),
errorExepected: status.Errorf(status.InvalidCommandArgsError, ERROR_ON_MANAGED_SERVICES, OPENSEARCH),
},
{
flags: &RestartCmdFlags{},
Expand All @@ -313,7 +317,7 @@ func TestHandleManagedServiceError(t *testing.T) {
postgresql: true,
opensearch: true,
},
errorExepected: status.Errorf(status.InvalidCommandArgsError, ERROR_ON_MANAGED_SERVICES),
errorExepected: status.Errorf(status.InvalidCommandArgsError, ERROR_ON_MANAGED_SERVICES, "postgresql and opensearch"),
},
}

Expand Down

0 comments on commit a85959a

Please sign in to comment.