Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add utils for service under SansShell services #384

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 37 additions & 34 deletions services/service/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,19 +218,22 @@ func (s *statusCmd) Execute(ctx context.Context, f *flag.FlagSet, args ...interf
return subcommands.ExitUsageError
}

req := &pb.StatusRequest{
SystemType: system,
ServiceName: serviceName,
}
c := pb.NewServiceClientProxy(state.Conn)
err = StopManyRemoteService(ctx, state.Conn, system, serviceName)

respChan, err := c.StatusOneMany(ctx, req)
// Comment out the following code temporarily for testing utils
//req := &pb.StatusRequest{
// SystemType: system,
// ServiceName: serviceName,
//}
//
//c := pb.NewServiceClientProxy(state.Conn)
//respChan, err := c.StatusOneMany(ctx, req)

if err != nil {
// Emit this to every error file as it's not specific to a given target.
for _, e := range state.Err {
fmt.Fprintf(e, "All targets - error executing 'status' for service %s: %v\n", serviceName, err)
}
//for _, e := range state.Err {
// fmt.Fprintf(e, "All targets - error executing 'status' for service %s: %v\n", serviceName, err)
//}
return subcommands.ExitFailure
}

Expand All @@ -241,24 +244,24 @@ func (s *statusCmd) Execute(ctx context.Context, f *flag.FlagSet, args ...interf
// return early here.
// Note that this is only the last non-nil error, and previous
// error values may be lost.
var lastErr error
for resp := range respChan {
out := state.Out[resp.Index]
system, status := resp.Resp.GetSystemType(), resp.Resp.GetServiceStatus().GetStatus()
output := fmt.Sprintf("[%s] %s : %s", systemTypeString(system), serviceName, statusString(status))
if resp.Error != nil {
lastErr = fmt.Errorf("target %s [%d] error: %w\n", resp.Target, resp.Index, resp.Error)
fmt.Fprint(state.Err[resp.Index], lastErr)
continue
}
if _, err := fmt.Fprintln(out, output); err != nil {
lastErr = fmt.Errorf("target %s [%d] write error: %w\n", resp.Target, resp.Index, err)
fmt.Fprint(state.Err[resp.Index], lastErr)
}
}
if lastErr != nil {
return subcommands.ExitFailure
}
//var lastErr error
//for resp := range respChan {
// out := state.Out[resp.Index]
// system, status := resp.Resp.GetSystemType(), resp.Resp.GetServiceStatus().GetStatus()
// output := fmt.Sprintf("[%s] %s : %s", systemTypeString(system), serviceName, statusString(status))
// if resp.Error != nil {
// lastErr = fmt.Errorf("target %s [%d] error: %w\n", resp.Target, resp.Index, resp.Error)
// fmt.Fprint(state.Err[resp.Index], lastErr)
// continue
// }
// if _, err := fmt.Fprintln(out, output); err != nil {
// lastErr = fmt.Errorf("target %s [%d] write error: %w\n", resp.Target, resp.Index, err)
// fmt.Fprint(state.Err[resp.Index], lastErr)
// }
//}
//if lastErr != nil {
// return subcommands.ExitFailure
//}
return subcommands.ExitSuccess
}

Expand Down Expand Up @@ -303,13 +306,13 @@ func (l *listCmd) Execute(ctx context.Context, f *flag.FlagSet, args ...interfac
return subcommands.ExitFailure
}

// Error holding the last observed non-nil error, which will
// determine the exit status of the command.
// The contract with the proxy and 'many' functions requires
// that we completely drain the response channel, so we cannot
// return early here.
// Note that this is only the last non-nil error, and previous
// error values may be lost.
// // Error holding the last observed non-nil error, which will
// // determine the exit status of the command.
// // The contract with the proxy and 'many' functions requires
// // that we completely drain the response channel, so we cannot
// // return early here.
// // Note that this is only the last non-nil error, and previous
// // error values may be lost.
var lastErr error
for resp := range respChan {
out := state.Out[resp.Index]
Expand Down
67 changes: 67 additions & 0 deletions services/service/client/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"errors"
"fmt"
"strings"

"github.com/Snowflake-Labs/sansshell/proxy/proxy"
pb "github.com/Snowflake-Labs/sansshell/services/service"
Expand Down Expand Up @@ -96,6 +97,72 @@ func StopRemoteService(ctx context.Context, conn *proxy.Conn, system pb.SystemTy
return nil
}

// StartManyRemoteServices is a helper function for starting a service on multiple remote targets.
func StartManyRemoteServices(ctx context.Context, conn *proxy.Conn, system pb.SystemType, service string) error {
c := pb.NewServiceClientProxy(conn)
respChan, err := c.ActionOneMany(ctx, &pb.ActionRequest{
ServiceName: service,
SystemType: system,
Action: pb.Action_ACTION_START,
})

if err != nil {
return fmt.Errorf("can't start service %s - %v", service, err)
}

var errorList []error
for resp := range respChan {
if resp.Error != nil {
err := fmt.Errorf("can't start service on target %s: %w", resp.Target, resp.Error)
errorList = append(errorList, err)
}
}

if len(errorList) == 0 {
return nil
}

var errorMessages []string
for _, err := range errorList {
errorMessages = append(errorMessages, err.Error())
}

sfc-gh-cachan marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("%s", strings.Join(errorMessages, "\n"))
}

// StopManyRemoteService is a helper function for stopping a service on multiple remote targets.
func StopManyRemoteService(ctx context.Context, conn *proxy.Conn, system pb.SystemType, service string) error {
sfc-gh-cachan marked this conversation as resolved.
Show resolved Hide resolved
c := pb.NewServiceClientProxy(conn)
respChan, err := c.ActionOneMany(ctx, &pb.ActionRequest{
ServiceName: service,
SystemType: system,
Action: pb.Action_ACTION_STOP,
})

if err != nil {
return fmt.Errorf("can't stop service %s - %v", service, err)
}

var errorList []error
for resp := range respChan {
if resp.Error != nil {
err := fmt.Errorf("can't stop service on target %s: %w", resp.Target, resp.Error)
errorList = append(errorList, err)
}
}

if len(errorList) == 0 {
return nil
}

var errorMessages []string
for _, err := range errorList {
errorMessages = append(errorMessages, err.Error())
}

return fmt.Errorf("%s", strings.Join(errorMessages, "\n"))
}

// RestartService was the original exported name for RestartRemoteService and now
// exists for backwards compatibility.
//
Expand Down
Loading