Skip to content

Commit

Permalink
add fdbserver version lookup (#264)
Browse files Browse the repository at this point in the history
* add fdbserver version lookup

* address comments: change version from flag to action & typo fix

* set fdbserver binary as a flag for sansshell server

* remove unnecessary lines for fdbserver command join
  • Loading branch information
Peihao Chu authored Jul 11, 2023
1 parent 7b57c83 commit 17b55d7
Show file tree
Hide file tree
Showing 9 changed files with 981 additions and 80 deletions.
1 change: 1 addition & 0 deletions cmd/sansshell-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ var (
)

func init() {
flag.StringVar(&fdbserver.FDBServer, "fdbserver", "/usr/sbin/fdbserver", "Path to fdbserver binary.")
flag.StringVar(&fdbserver.FDBCLI, "fdbcli", "/usr/bin/fdbcli", "Path to fdbcli binary. API assumes version 7.1. Older versions may not implement some commands.")
flag.StringVar(&fdbserver.FDBCLIUser, "fdbcli-user", "", "User to change to when running fdbcli")
flag.StringVar(&fdbserver.FDBCLIGroup, "fdbcli-group", "", "Group to change to when running fdbcli")
Expand Down
93 changes: 93 additions & 0 deletions services/fdb/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func (*fdbCmd) GetSubpackage(f *flag.FlagSet) *subcommands.Commander {
c := client.SetupSubpackage(subPackage, f)
c.Register(&fdbCLICmd{}, "")
c.Register(&fdbConfCmd{}, "")
c.Register(&fdbServerCmd{}, "")

return c
}
Expand Down Expand Up @@ -3340,3 +3341,95 @@ func (d *fdbConfDeleteCmd) Execute(ctx context.Context, f *flag.FlagSet, args ..

return retCode
}

const fdbServerPackage = "fdbserver"

func (*fdbServerCmd) GetSubpackage(f *flag.FlagSet) *subcommands.Commander {
c := client.SetupSubpackage(fdbServerPackage, f)
c.Register(&fdbServerVersionCmd{}, "")
return c
}

type fdbServerCmd struct {
req *pb.FDBServerRequest
}

func (*fdbServerCmd) Name() string { return fdbServerPackage }
func (p *fdbServerCmd) Synopsis() string {
return "Run a fdbserver command on the given host.\n" + client.GenerateSynopsis(p.GetSubpackage(flag.NewFlagSet("", flag.ContinueOnError)), 4)
}
func (p *fdbServerCmd) Usage() string {
return `fdbserver <command>
Run fdbserver for the given command
` + client.GenerateUsage(fdbServerPackage, p.Synopsis())
}

func (r *fdbServerCmd) SetFlags(f *flag.FlagSet) {
r.req = &pb.FDBServerRequest{}
}

func (p *fdbServerCmd) Execute(ctx context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
c := p.GetSubpackage(f)
args = append(args, p.req)
return c.Execute(ctx, args...)
}

type fdbServerVersionCmd struct {
req *pb.FDBServerCommand
}

func (*fdbServerVersionCmd) Name() string { return "version" }

func (p *fdbServerVersionCmd) Synopsis() string {
return "Lookup the fdbserver version"
}

func (p *fdbServerVersionCmd) Usage() string {
return "version\n"
}

func (r *fdbServerVersionCmd) SetFlags(f *flag.FlagSet) {
r.req = &pb.FDBServerCommand{}
}

func (p *fdbServerVersionCmd) Execute(ctx context.Context, f *flag.FlagSet, args ...interface{}) subcommands.ExitStatus {
state := args[0].(*util.ExecuteState)
req := args[1].(*pb.FDBServerRequest)
client := pb.NewServerClientProxy(state.Conn)

req.Commands = append(req.Commands,
&pb.FDBServerCommand{
Command: &pb.FDBServerCommand_Version{},
})
resp, err := client.FDBServerOneMany(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 - fdbcli error: %v\n", err)
}
return subcommands.ExitFailure
}
retCode := subcommands.ExitSuccess

// process version output
for r := range resp {
if r.Error != nil {
fmt.Fprintf(state.Err[r.Index], "fdb server check version error: %v\n", r.Error)
retCode = subcommands.ExitFailure
continue
}
// split results to multiple lines and ident, finally combine them
multiLines := strings.Split(string(r.Resp.Stdout), "\n")
// if the last line is empty, just remove it
if len(multiLines) > 0 && multiLines[len(multiLines)-1] == "" {
multiLines = multiLines[:len(multiLines)-1]
}
for i, line := range multiLines {
multiLines[i] = fmt.Sprintf(" %s", line)
}
combinedLines := strings.Join(multiLines, "\n")
fmt.Fprintf(state.Out[r.Index], "Target %s(%d) FDB server version:\n%s\n", r.Target, r.Index, combinedLines)
}

return retCode
}
Loading

0 comments on commit 17b55d7

Please sign in to comment.