Skip to content

Commit f856f13

Browse files
tsonglewcw123
authored andcommitted
curve/toos-v2: add list client #2037
Signed-off-by: Tsong Lew <tsonglew@gmail.com>
1 parent 81b5714 commit f856f13

File tree

5 files changed

+181
-3
lines changed

5 files changed

+181
-3
lines changed

tools-v2/README.md

+20-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ A tool for CurveFS & CurveBs.
4545
- [list](#list-1)
4646
- [list logical-pool](#list-logical-pool)
4747
- [list server](#list-server)
48+
- [list client](#list-client)
4849
- [query](#query-1)
4950
- [query file](#query-file)
5051
- [status](#status-1)
@@ -842,6 +843,24 @@ Output:
842843
+----+---------------------+------+---------+-------------------+-------------------+
843844
```
844845

846+
##### list client
847+
848+
list all client information in curvebs
849+
850+
```bash
851+
curve bs list client
852+
```
853+
854+
Output:
855+
856+
```bash
857+
+------------+------+
858+
| IP | PORT |
859+
+------------+------+
860+
| 172.17.0.2 | 9000 |
861+
+------------+------+
862+
```
863+
845864
### query
846865

847866
##### query file
@@ -958,7 +977,7 @@ Output:
958977
| status | |
959978
| chunkserver-status | |
960979
| client-status | |
961-
| client-list | |
980+
| client-list | curve bs list client |
962981
| snapshot-clone-status | |
963982
| copysets-status | |
964983
| chunkserver-list | |

tools-v2/internal/utils/row.go

+2
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ const (
9191
ROW_USED = "used"
9292
ROW_VERSION = "version"
9393
ROW_ZONE = "zone"
94+
ROW_IP = "ip"
95+
ROW_PORT = "port"
9496

9597
// s3
9698
ROW_S3CHUNKINFO_CHUNKID = "s3ChunkId"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/*
2+
* Copyright (c) 2022 NetEase Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/*
18+
* Project: CurveCli
19+
* Created Date: 2022-11-10
20+
* Author: Tsonglew
21+
*/
22+
23+
package client
24+
25+
import (
26+
"context"
27+
"fmt"
28+
cmderror "github.com/opencurve/curve/tools-v2/internal/error"
29+
cobrautil "github.com/opencurve/curve/tools-v2/internal/utils"
30+
basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command"
31+
"github.com/opencurve/curve/tools-v2/pkg/config"
32+
"github.com/opencurve/curve/tools-v2/pkg/output"
33+
"github.com/opencurve/curve/tools-v2/proto/proto/nameserver2"
34+
"github.com/spf13/cobra"
35+
"google.golang.org/grpc"
36+
)
37+
38+
const (
39+
clientExample = `$ curve bs list client`
40+
)
41+
42+
type ListClientRpc struct {
43+
Info *basecmd.Rpc
44+
Request *nameserver2.ListClientRequest
45+
curveFSClient nameserver2.CurveFSServiceClient
46+
}
47+
48+
var _ basecmd.RpcFunc = (*ListClientRpc)(nil) // check interface
49+
50+
type ClientCommand struct {
51+
basecmd.FinalCurveCmd
52+
Rpc []*ListClientRpc
53+
}
54+
55+
var _ basecmd.FinalCurveCmdFunc = (*ClientCommand)(nil) // check interface
56+
57+
func (lRpc *ListClientRpc) NewRpcClient(cc grpc.ClientConnInterface) {
58+
lRpc.curveFSClient = nameserver2.NewCurveFSServiceClient(cc)
59+
}
60+
61+
func (lRpc *ListClientRpc) Stub_Func(ctx context.Context) (interface{}, error) {
62+
return lRpc.curveFSClient.ListClient(ctx, lRpc.Request)
63+
}
64+
65+
func NewClientCommand() *cobra.Command {
66+
return NewListCLientCommand().Cmd
67+
}
68+
69+
func NewListCLientCommand() *ClientCommand {
70+
lsCmd := &ClientCommand{
71+
FinalCurveCmd: basecmd.FinalCurveCmd{
72+
Use: "client",
73+
Short: "list all client information in curvebs",
74+
Example: clientExample,
75+
},
76+
}
77+
78+
basecmd.NewFinalCurveCli(&lsCmd.FinalCurveCmd, lsCmd)
79+
return lsCmd
80+
}
81+
82+
// AddFlags implements basecmd.FinalCurveCmdFunc
83+
func (pCmd *ClientCommand) AddFlags() {
84+
config.AddBsMdsFlagOption(pCmd.Cmd)
85+
config.AddRpcRetryTimesFlag(pCmd.Cmd)
86+
config.AddRpcTimeoutFlag(pCmd.Cmd)
87+
}
88+
89+
// Init implements basecmd.FinalCurveCmdFunc
90+
func (pCmd *ClientCommand) Init(cmd *cobra.Command, args []string) error {
91+
mdsAddrs, err := config.GetBsMdsAddrSlice(pCmd.Cmd)
92+
if err.TypeCode() != cmderror.CODE_SUCCESS {
93+
return err.ToError()
94+
}
95+
96+
timeout := config.GetFlagDuration(pCmd.Cmd, config.RPCTIMEOUT)
97+
retrytimes := config.GetFlagInt32(pCmd.Cmd, config.RPCRETRYTIMES)
98+
99+
rpc := &ListClientRpc{
100+
Request: &nameserver2.ListClientRequest{},
101+
Info: basecmd.NewRpc(mdsAddrs, timeout, retrytimes, "ListClient"),
102+
}
103+
pCmd.Rpc = append(pCmd.Rpc, rpc)
104+
105+
header := []string{cobrautil.ROW_IP, cobrautil.ROW_PORT}
106+
pCmd.SetHeader(header)
107+
pCmd.TableNew.SetAutoMergeCellsByColumnIndex(cobrautil.GetIndexSlice(
108+
pCmd.Header, header,
109+
))
110+
return nil
111+
}
112+
113+
// Print implements basecmd.FinalCurveCmdFunc
114+
func (pCmd *ClientCommand) Print(cmd *cobra.Command, args []string) error {
115+
return output.FinalCmdOutput(&pCmd.FinalCurveCmd, pCmd)
116+
}
117+
118+
// RunCommand implements basecmd.FinalCurveCmdFunc
119+
func (pCmd *ClientCommand) RunCommand(cmd *cobra.Command, args []string) error {
120+
var infos []*basecmd.Rpc
121+
var funcs []basecmd.RpcFunc
122+
for _, rpc := range pCmd.Rpc {
123+
infos = append(infos, rpc.Info)
124+
funcs = append(funcs, rpc)
125+
}
126+
results, errs := basecmd.GetRpcListResponse(infos, funcs)
127+
if len(errs) == len(infos) {
128+
mergeErr := cmderror.MergeCmdErrorExceptSuccess(errs)
129+
return mergeErr.ToError()
130+
}
131+
var errors []*cmderror.CmdError
132+
rows := make([]map[string]string, 0)
133+
for _, res := range results {
134+
infos := res.(*nameserver2.ListClientResponse).GetClientInfos()
135+
for _, info := range infos {
136+
row := make(map[string]string)
137+
row[cobrautil.ROW_IP] = info.GetIp()
138+
row[cobrautil.ROW_PORT] = fmt.Sprintf("%d", info.GetPort())
139+
rows = append(rows, row)
140+
}
141+
}
142+
list := cobrautil.ListMap2ListSortByKeys(rows, pCmd.Header, []string{
143+
cobrautil.ROW_IP,
144+
})
145+
pCmd.TableNew.AppendBulk(list)
146+
errRet := cmderror.MergeCmdError(errors)
147+
pCmd.Error = &errRet
148+
pCmd.Result = results
149+
return nil
150+
}
151+
152+
// ResultPlainOutput implements basecmd.FinalCurveCmdFunc
153+
func (pCmd *ClientCommand) ResultPlainOutput() error {
154+
return output.FinalCmdOutputPlain(&pCmd.FinalCurveCmd)
155+
}

tools-v2/pkg/cli/command/curvebs/list/list.go

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ package list
2424

2525
import (
2626
basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command"
27+
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/list/client"
2728
logicalpool "github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/list/logicalPool"
2829
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/list/server"
2930
"github.com/spf13/cobra"
@@ -39,6 +40,7 @@ func (listCmd *ListCommand) AddSubCommands() {
3940
listCmd.Cmd.AddCommand(
4041
logicalpool.NewLogicalPoolCommand(),
4142
server.NewServerCommand(),
43+
client.NewClientCommand(),
4244
)
4345
}
4446

tools-v2/pkg/config/bs.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ func AddBsStringRequiredFlag(cmd *cobra.Command, name string, usage string) {
110110
// add flag option
111111
// bs mds[option]
112112
func AddBsMdsFlagOption(cmd *cobra.Command) {
113-
AddBsStringSliceOptionFlag(cmd, CURVEBS_MDSADDR, "mds address, should be like 127.0.0.1:6700,127.0.0.1:6701,127.0.0.1:6702")
113+
AddBsStringOptionFlag(cmd, CURVEBS_MDSADDR, "mds address, should be like 127.0.0.1:6700,127.0.0.1:6701,127.0.0.1:6702")
114114
}
115115
func AddBsMdsDummyFlagOption(cmd *cobra.Command) {
116-
AddBsStringSliceOptionFlag(cmd, CURVEBS_MDSDUMMYADDR, "mds dummy address, should be like 127.0.0.1:6700,127.0.0.1:6701,127.0.0.1:6702")
116+
AddBsStringOptionFlag(cmd, CURVEBS_MDSDUMMYADDR, "mds dummy address, should be like 127.0.0.1:6700,127.0.0.1:6701,127.0.0.1:6702")
117117
}
118118

119119
// user

0 commit comments

Comments
 (0)