diff --git a/tools-v2/README.md b/tools-v2/README.md index ae0405f69e..76ed325ed1 100644 --- a/tools-v2/README.md +++ b/tools-v2/README.md @@ -1040,25 +1040,25 @@ Output: | curve_ops_tool etcd-status | curve bs status etcd | | curve_ops_tool mds-status | curve bs status mds | | curve_ops_tool server-list | curve bs list server | +| curve_ops_tool client-list | curve bs list client | +| curve_ops_tool delete | curve bs delete file | +| curve_ops_tool list | curve bs list dir | +| create | curve bs create file/dir | +| seginfo | curve bs query seginfo | +| chunk-location | curve bs query chunk | +| remove-peer | curve bs delete peer | +| reset-peer | curve bs update peer | | space | | | status | | | chunkserver-status | | | client-status | | -| curve_ops_tool client-list | curve bs list client | | snapshot-clone-status | | | copysets-status | | | chunkserver-list | | | cluster-status | | -| curve_ops_tool list | curve bs list dir | -| seginfo | curve bs query seginfo | -| curve_ops_tool delete | curve bs delete file | | clean-recycle | | -| create | | -| chunk-location | curve bs query chunk | | check-consistency | | -| remove-peer | curve bs delete peer | | transfer-leader | | -| reset-peer | curve bs update peer | | do-snapshot | | | do-snapshot-all | | | check-chunkserver | | diff --git a/tools-v2/internal/error/error.go b/tools-v2/internal/error/error.go index b1c737efc7..b1a7721ad9 100644 --- a/tools-v2/internal/error/error.go +++ b/tools-v2/internal/error/error.go @@ -399,6 +399,12 @@ var ( ErrBsChunkServerListInCopySets = func() *CmdError { return NewInternalCmdError(46, "get chunkserver list in copysets fail, err: %s") } + ErrBsUnknownFileType = func() *CmdError { + return NewInternalCmdError(47, "unknown file type[%s], only support: dir, file") + } + ErrBsCreateFileOrDirectoryType = func() *CmdError { + return NewInternalCmdError(48, "create file or directory fail, err: %s") + } // http error ErrHttpUnreadableResult = func() *CmdError { @@ -731,4 +737,15 @@ var ( } return NewRpcReultCmdError(code, message) } + ErrCreateFile = func(statusCode nameserver2.StatusCode, path string) *CmdError { + var message string + code := int(statusCode) + switch statusCode { + case nameserver2.StatusCode_kOK: + message = "Created successfully" + default: + message = fmt.Sprintf("failed to create file[%s], err: %s", path, statusCode.String()) + } + return NewRpcReultCmdError(code, message) + } ) diff --git a/tools-v2/internal/utils/proto.go b/tools-v2/internal/utils/proto.go index 2df2b7ee01..d40f484e34 100644 --- a/tools-v2/internal/utils/proto.go +++ b/tools-v2/internal/utils/proto.go @@ -29,6 +29,7 @@ import ( cmderror "github.com/opencurve/curve/tools-v2/internal/error" "github.com/opencurve/curve/tools-v2/proto/curvefs/proto/common" "github.com/opencurve/curve/tools-v2/proto/curvefs/proto/topology" + "github.com/opencurve/curve/tools-v2/proto/proto/nameserver2" ) func TranslateFsType(fsType string) (common.FSType, *cmderror.CmdError) { @@ -193,3 +194,20 @@ func Topology2Map(topo *topology.ListTopologyResponse) (map[string]interface{}, retErr := cmderror.MergeCmdErrorExceptSuccess(errs) return ret, retErr } + +const ( + TYPE_DIR = "dir" + TYPE_FILE = "file" +) + +func TranslateFileType(fileType string) (nameserver2.FileType, *cmderror.CmdError) { + switch fileType { + case TYPE_DIR: + return nameserver2.FileType_INODE_DIRECTORY, cmderror.ErrSuccess() + case TYPE_FILE: + return nameserver2.FileType_INODE_PAGEFILE, cmderror.ErrSuccess() + } + retErr := cmderror.ErrBsUnknownFileType() + retErr.Format(fileType) + return nameserver2.FileType_INODE_DIRECTORY, retErr +} diff --git a/tools-v2/pkg/cli/command/curvebs/create/create.go b/tools-v2/pkg/cli/command/curvebs/create/create.go index 35e22b9679..947385d113 100644 --- a/tools-v2/pkg/cli/command/curvebs/create/create.go +++ b/tools-v2/pkg/cli/command/curvebs/create/create.go @@ -25,6 +25,8 @@ package create import ( basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command" "github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/create/cluster" + "github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/create/dir" + "github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/create/file" "github.com/spf13/cobra" ) @@ -37,6 +39,8 @@ var _ basecmd.MidCurveCmdFunc = (*CreateCmd)(nil) // check interface func (createCmd *CreateCmd) AddSubCommands() { createCmd.Cmd.AddCommand( cluster.NewClusterTopoCmd(), + dir.NewDirectoryCommand(), + file.NewFileCommand(), ) } diff --git a/tools-v2/pkg/cli/command/curvebs/create/dir/dir.go b/tools-v2/pkg/cli/command/curvebs/create/dir/dir.go new file mode 100644 index 0000000000..1519136f3e --- /dev/null +++ b/tools-v2/pkg/cli/command/curvebs/create/dir/dir.go @@ -0,0 +1,97 @@ +/* +* Copyright (c) 2023 NetEase Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. + */ +/* +* Project: curve +* Created Date: 2023-04-14 +* Author: chengyi01 + */ +package dir + +import ( + "fmt" + + cmderror "github.com/opencurve/curve/tools-v2/internal/error" + cobrautil "github.com/opencurve/curve/tools-v2/internal/utils" + basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command" + "github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/create/file" + "github.com/opencurve/curve/tools-v2/pkg/config" + "github.com/opencurve/curve/tools-v2/pkg/output" + "github.com/spf13/cobra" +) + +const ( + dirExample = `$ curve bs create dir --path /test` + TYOE_DIR +) + +type DirectoryCommand struct { + basecmd.FinalCurveCmd +} + +var _ basecmd.FinalCurveCmdFunc = (*DirectoryCommand)(nil) + + +func (dCmd *DirectoryCommand) Init(cmd *cobra.Command, args []string) error { + config.AddBsFileTypeRequiredFlag(dCmd.Cmd) + dCmd.Cmd.ParseFlags([]string{ + fmt.Sprintf("--%s", config.CURVEBS_TYPE), + fmt.Sprint(cobrautil.TYPE_DIR), + }) + dCmd.SetHeader([]string{cobrautil.ROW_RESULT}) + return nil +} + +func (dCmd *DirectoryCommand) RunCommand(cmd *cobra.Command, args []string) error { + dCmd.Result, dCmd.Error = file.CreateFileOrDirectory(dCmd.Cmd) + if dCmd.Error.TypeCode() != cmderror.CODE_SUCCESS { + return dCmd.Error.ToError() + } + dCmd.TableNew.Append([]string{dCmd.Error.Message}) + return nil +} + +func (dCmd *DirectoryCommand) Print(cmd *cobra.Command, args []string) error { + return output.FinalCmdOutput(&dCmd.FinalCurveCmd, dCmd) +} + +func (dCmd *DirectoryCommand) ResultPlainOutput() error { + return output.FinalCmdOutputPlain(&dCmd.FinalCurveCmd) +} + +func (dCmd *DirectoryCommand) AddFlags() { + config.AddBsMdsFlagOption(dCmd.Cmd) + config.AddRpcTimeoutFlag(dCmd.Cmd) + config.AddRpcRetryTimesFlag(dCmd.Cmd) + config.AddBsUserOptionFlag(dCmd.Cmd) + config.AddBsPasswordOptionFlag(dCmd.Cmd) + config.AddBsPathRequiredFlag(dCmd.Cmd) +} + +func NewCreateDirectoryCommand() *DirectoryCommand { + dCmd := &DirectoryCommand{ + FinalCurveCmd: basecmd.FinalCurveCmd{ + Use: "dir", + Short: "create directory in curvebs cluster", + Example: dirExample, + }, + } + basecmd.NewFinalCurveCli(&dCmd.FinalCurveCmd, dCmd) + return dCmd +} + +func NewDirectoryCommand() *cobra.Command { + return NewCreateDirectoryCommand().Cmd +} diff --git a/tools-v2/pkg/cli/command/curvebs/create/file/create.go b/tools-v2/pkg/cli/command/curvebs/create/file/create.go new file mode 100644 index 0000000000..549cdf37ad --- /dev/null +++ b/tools-v2/pkg/cli/command/curvebs/create/file/create.go @@ -0,0 +1,184 @@ +/* +* Copyright (c) 2023 NetEase Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. + */ +/* +* Project: curve +* Created Date: 2023-04-14 +* Author: chengyi01 + */ + +package file + +import ( + "context" + "fmt" + + "github.com/dustin/go-humanize" + cmderror "github.com/opencurve/curve/tools-v2/internal/error" + cobrautil "github.com/opencurve/curve/tools-v2/internal/utils" + basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command" + "github.com/opencurve/curve/tools-v2/pkg/config" + "github.com/opencurve/curve/tools-v2/pkg/output" + "github.com/opencurve/curve/tools-v2/proto/proto/nameserver2" + "github.com/spf13/cobra" + "github.com/spf13/viper" + "google.golang.org/grpc" +) + +type CreateFileRpc struct { + Info *basecmd.Rpc + Request *nameserver2.CreateFileRequest + mdsClient nameserver2.CurveFSServiceClient +} + +var _ basecmd.RpcFunc = (*CreateFileRpc)(nil) + +// CreateCommand definition +type CreateCommand struct { + basecmd.FinalCurveCmd + Rpc *CreateFileRpc + Response *nameserver2.CreateFileResponse +} + +var _ basecmd.FinalCurveCmdFunc = (*CreateCommand)(nil) + +func (gRpc *CreateFileRpc) NewRpcClient(cc grpc.ClientConnInterface) { + gRpc.mdsClient = nameserver2.NewCurveFSServiceClient(cc) + +} + +func (gRpc *CreateFileRpc) Stub_Func(ctx context.Context) (interface{}, error) { + return gRpc.mdsClient.CreateFile(ctx, gRpc.Request) +} + +func (cCmd *CreateCommand) Init(cmd *cobra.Command, args []string) error { + mdsAddrs, err := config.GetBsMdsAddrSlice(cCmd.Cmd) + if err.TypeCode() != cmderror.CODE_SUCCESS { + return err.ToError() + } + //get the default timeout and retrytimes + timeout := config.GetFlagDuration(cCmd.Cmd, config.RPCTIMEOUT) + retrytimes := config.GetFlagInt32(cCmd.Cmd, config.RPCRETRYTIMES) + filename := config.GetBsFlagString(cCmd.Cmd, config.CURVEBS_PATH) + username := config.GetBsFlagString(cCmd.Cmd, config.CURVEBS_USER) + password := config.GetBsFlagString(cCmd.Cmd, config.CURVEBS_PASSWORD) + date, errDat := cobrautil.GetTimeofDayUs() + if errDat.TypeCode() != cmderror.CODE_SUCCESS { + return errDat.ToError() + } + + fileTypeStr := config.GetBsFlagString(cCmd.Cmd, config.CURVEBS_TYPE) + //fileTypeStr := "dir or file" + fileType, errType := cobrautil.TranslateFileType(fileTypeStr) + if errType.TypeCode() != cmderror.CODE_SUCCESS { + return errType.ToError() + } + createRequest := nameserver2.CreateFileRequest{ + FileName: &filename, + Owner: &username, + Date: &date, + FileType: &fileType, + } + if fileType == nameserver2.FileType_INODE_PAGEFILE { + sizeStr := config.GetBsFlagString(cCmd.Cmd, config.CURVEBS_SIZE) + size, err := humanize.ParseBytes(sizeStr) + if err != nil { + return fmt.Errorf("parse size[%s] failed, err: %v", sizeStr, err) + } + createRequest.FileLength = &size + + stripeCount := config.GetBsFlagUint64(cCmd.Cmd, config.CURVEBS_STRIPE_COUNT) + createRequest.StripeCount = &stripeCount + stripeUnitStr := config.GetBsFlagString(cCmd.Cmd, config.CURVEBS_STRIPE_UNIT) + stripeUnit, errUnit := humanize.ParseBytes(stripeUnitStr) + if errUnit != nil { + return fmt.Errorf("parse stripe unit[%s] failed, err: %v", stripeUnitStr, errUnit) + } + createRequest.StripeUnit = &stripeUnit + } + if username == viper.GetString(config.VIPER_CURVEBS_USER) && len(password) != 0 { + strSig := cobrautil.GetString2Signature(date, username) + sig := cobrautil.CalcString2Signature(strSig, password) + createRequest.Signature = &sig + } + cCmd.Rpc = &CreateFileRpc{ + Info: basecmd.NewRpc(mdsAddrs, timeout, retrytimes, "CreateFile"), + Request: &createRequest, + } + return nil +} + +func (cCmd *CreateCommand) RunCommand(cmd *cobra.Command, args []string) error { + result, err := basecmd.GetRpcResponse(cCmd.Rpc.Info, cCmd.Rpc) + if err.TypeCode() != cmderror.CODE_SUCCESS { + return err.ToError() + } + cCmd.Response = result.(*nameserver2.CreateFileResponse) + if cCmd.Response.GetStatusCode() != nameserver2.StatusCode_kOK { + err = cmderror.ErrCreateFile(cCmd.Response.GetStatusCode(), cCmd.Rpc.Request.GetFileName()) + return err.ToError() + } + return nil +} + +func (cCmd *CreateCommand) Print(cmd *cobra.Command, args []string) error { + return output.FinalCmdOutput(&cCmd.FinalCurveCmd, cCmd) +} + +func (cCmd *CreateCommand) ResultPlainOutput() error { + return output.FinalCmdOutputPlain(&cCmd.FinalCurveCmd) +} + +func (cCmd *CreateCommand) AddFlags() { + config.AddBsMdsFlagOption(cCmd.Cmd) + config.AddRpcTimeoutFlag(cCmd.Cmd) + config.AddRpcRetryTimesFlag(cCmd.Cmd) + config.AddBsPathRequiredFlag(cCmd.Cmd) + config.AddBsUserOptionFlag(cCmd.Cmd) + config.AddBsPasswordOptionFlag(cCmd.Cmd) + config.AddBsSizeOptionFlag(cCmd.Cmd) + config.AddBsFileTypeRequiredFlag(cCmd.Cmd) + config.AddBsStripeUnitOptionFlag(cCmd.Cmd) + config.AddBsStripeCountOptionFlag(cCmd.Cmd) +} + +// NewCreateCommand return the mid cli +func NewCreateCommand() *CreateCommand { + cCmd := &CreateCommand{ + FinalCurveCmd: basecmd.FinalCurveCmd{}, + } + basecmd.NewFinalCurveCli(&cCmd.FinalCurveCmd, cCmd) + return cCmd +} + +func CreateFileOrDirectory(caller *cobra.Command) (*nameserver2.CreateFileResponse, *cmderror.CmdError) { + createCmd := NewCreateCommand() + config.AlignFlagsValue(caller, createCmd.Cmd, []string{ + config.RPCRETRYTIMES, config.RPCTIMEOUT, config.CURVEBS_MDSADDR, + config.CURVEBS_PATH, config.CURVEBS_USER, config.CURVEBS_PASSWORD, + config.CURVEBS_SIZE, config.CURVEBS_TYPE, config.CURVEBS_STRIPE_UNIT, + config.CURVEBS_STRIPE_COUNT, + }) + createCmd.Cmd.SilenceErrors = true + createCmd.Cmd.SilenceUsage = true + createCmd.Cmd.SetArgs([]string{"--format", config.FORMAT_NOOUT}) + err := createCmd.Cmd.Execute() + if err != nil { + retErr := cmderror.ErrBsCreateFileOrDirectoryType() + retErr.Format(err.Error()) + return createCmd.Response, retErr + } + return createCmd.Response, cmderror.Success() +} diff --git a/tools-v2/pkg/cli/command/curvebs/create/file/file.go b/tools-v2/pkg/cli/command/curvebs/create/file/file.go new file mode 100644 index 0000000000..cf9da2eaa1 --- /dev/null +++ b/tools-v2/pkg/cli/command/curvebs/create/file/file.go @@ -0,0 +1,99 @@ +/* +* Copyright (c) 2023 NetEase Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. + */ +/* +* Project: curve +* Created Date: 2023-04-14 +* Author: chengyi01 + */ +package file + +import ( + "fmt" + + cmderror "github.com/opencurve/curve/tools-v2/internal/error" + cobrautil "github.com/opencurve/curve/tools-v2/internal/utils" + basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command" + "github.com/opencurve/curve/tools-v2/pkg/config" + "github.com/opencurve/curve/tools-v2/pkg/output" + "github.com/spf13/cobra" +) + +const ( + dirExample = `$ curve bs create file --path /test` + TYOE_DIR +) + +type fileCommand struct { + basecmd.FinalCurveCmd +} + +var _ basecmd.FinalCurveCmdFunc = (*fileCommand)(nil) + + +func (fCmd *fileCommand) Init(cmd *cobra.Command, args []string) error { + config.AddBsFileTypeRequiredFlag(fCmd.Cmd) + fCmd.Cmd.ParseFlags([]string{ + fmt.Sprintf("--%s", config.CURVEBS_TYPE), + fmt.Sprint(cobrautil.TYPE_FILE), + }) + fCmd.SetHeader([]string{cobrautil.ROW_RESULT}) + return nil +} + +func (fCmd *fileCommand) RunCommand(cmd *cobra.Command, args []string) error { + fCmd.Result, fCmd.Error = CreateFileOrDirectory(fCmd.Cmd) + if fCmd.Error.TypeCode() != cmderror.CODE_SUCCESS { + return fCmd.Error.ToError() + } + fCmd.TableNew.Append([]string{fCmd.Error.Message}) + return nil +} + +func (fCmd *fileCommand) Print(cmd *cobra.Command, args []string) error { + return output.FinalCmdOutput(&fCmd.FinalCurveCmd, fCmd) +} + +func (fCmd *fileCommand) ResultPlainOutput() error { + return output.FinalCmdOutputPlain(&fCmd.FinalCurveCmd) +} + +func (fCmd *fileCommand) AddFlags() { + config.AddBsMdsFlagOption(fCmd.Cmd) + config.AddRpcTimeoutFlag(fCmd.Cmd) + config.AddRpcRetryTimesFlag(fCmd.Cmd) + config.AddBsPathRequiredFlag(fCmd.Cmd) + config.AddBsUserOptionFlag(fCmd.Cmd) + config.AddBsPasswordOptionFlag(fCmd.Cmd) + config.AddBsSizeRequiredFlag(fCmd.Cmd) + config.AddBsStripeUnitOptionFlag(fCmd.Cmd) + config.AddBsStripeCountOptionFlag(fCmd.Cmd) +} + +func NewCreateFileCommand() *fileCommand { + fCmd := &fileCommand{ + FinalCurveCmd: basecmd.FinalCurveCmd{ + Use: "file", + Short: "create page file in curvebs cluster", + Example: dirExample, + }, + } + basecmd.NewFinalCurveCli(&fCmd.FinalCurveCmd, fCmd) + return fCmd +} + +func NewFileCommand() *cobra.Command { + return NewCreateFileCommand().Cmd +} diff --git a/tools-v2/pkg/cli/command/curvebs/query/chunk/chunkserver.go b/tools-v2/pkg/cli/command/curvebs/query/chunk/chunkserver.go index f15ad02771..7d9cc9ff31 100644 --- a/tools-v2/pkg/cli/command/curvebs/query/chunk/chunkserver.go +++ b/tools-v2/pkg/cli/command/curvebs/query/chunk/chunkserver.go @@ -62,9 +62,7 @@ var _ basecmd.FinalCurveCmdFunc = (*ChunkServerListInCoysetCommand)(nil) // chec func NewQueryChunkServerListCommand() *ChunkServerListInCoysetCommand { chunkCmd := &ChunkServerListInCoysetCommand{ - FinalCurveCmd: basecmd.FinalCurveCmd{ - Use: "chunkservers", - }, + FinalCurveCmd: basecmd.FinalCurveCmd{}, } basecmd.NewFinalCurveCli(&chunkCmd.FinalCurveCmd, chunkCmd) diff --git a/tools-v2/pkg/cli/command/curvebs/query/seginfo/segment.go b/tools-v2/pkg/cli/command/curvebs/query/seginfo/segment.go index d370f990dd..8999b4b47a 100644 --- a/tools-v2/pkg/cli/command/curvebs/query/seginfo/segment.go +++ b/tools-v2/pkg/cli/command/curvebs/query/seginfo/segment.go @@ -152,7 +152,7 @@ func GetSegment(caller *cobra.Command) (*nameserver2.GetOrAllocateSegmentRespons getCmd.Cmd.SetArgs([]string{"--format", config.FORMAT_NOOUT}) err := getCmd.Cmd.Execute() if err != nil { - retErr := cmderror.ErrBsGetSegment() + retErr := cmderror.ErrBsCreateFileOrDirectoryType() retErr.Format(err.Error()) return getCmd.Response, retErr } diff --git a/tools-v2/pkg/config/bs.go b/tools-v2/pkg/config/bs.go index 4fdeb06550..97eda2cc7a 100644 --- a/tools-v2/pkg/config/bs.go +++ b/tools-v2/pkg/config/bs.go @@ -36,38 +36,47 @@ import ( const ( // curvebs - CURVEBS_MDSADDR = "mdsaddr" - VIPER_CURVEBS_MDSADDR = "curvebs.mdsAddr" - CURVEBS_MDSDUMMYADDR = "mdsdummyaddr" - VIPER_CURVEBS_MDSDUMMYADDR = "curvebs.mdsDummyAddr" - CURVEBS_ETCDADDR = "etcdaddr" - VIPER_CURVEBS_ETCDADDR = "curvebs.etcdAddr" - CURVEBS_PATH = "path" - VIPER_CURVEBS_PATH = "curvebs.path" - CURVEBS_USER = "user" - VIPER_CURVEBS_USER = "curvebs.root.user" - CURVEBS_DEFAULT_USER = "root" - CURVEBS_PASSWORD = "password" - VIPER_CURVEBS_PASSWORD = "curvebs.root.password" - CURVEBS_DEFAULT_PASSWORD = "root_password" - CURVEBS_CLUSTERMAP = "clustermap" - VIPER_CURVEBS_CLUSTERMAP = "curvebs.clustermap" - CURVEBS_FILENAME = "filename" - VIPER_CURVEBS_FILENAME = "curvebs.filename" - CURVEBS_FORCEDELETE = "forcedelete" - CURVEBS_DEFAULT_FORCEDELETE = false - CURVEBS_DIR = "dir" - VIPER_CURVEBS_DIR = "curvebs.dir" - CURVEBS_LOGIC_POOL_ID = "logicalpoolid" - VIPER_CURVEBS_LOGIC_POOL_ID = "curvebs.logicalpoolid" - CURVEBS_COPYSET_ID = "copysetid" - VIPER_CURVEBS_COPYSET_ID = "curvebs.copysetid" - CURVEBS_PEERS_ADDRESS = "peers" - VIPER_CURVEBS_PEERS_ADDRESS = "curvebs.peers" - CURVEBS_OFFSET = "offset" - VIPER_CURVEBS_OFFSET = "curvebs.offset" - CURVEBS_SIZE = "size" - VIPER_CURVEBS_SIZE = "curvebs.size" + CURVEBS_MDSADDR = "mdsaddr" + VIPER_CURVEBS_MDSADDR = "curvebs.mdsAddr" + CURVEBS_MDSDUMMYADDR = "mdsdummyaddr" + VIPER_CURVEBS_MDSDUMMYADDR = "curvebs.mdsDummyAddr" + CURVEBS_ETCDADDR = "etcdaddr" + VIPER_CURVEBS_ETCDADDR = "curvebs.etcdAddr" + CURVEBS_PATH = "path" + VIPER_CURVEBS_PATH = "curvebs.path" + CURVEBS_USER = "user" + VIPER_CURVEBS_USER = "curvebs.root.user" + CURVEBS_DEFAULT_USER = "root" + CURVEBS_PASSWORD = "password" + VIPER_CURVEBS_PASSWORD = "curvebs.root.password" + CURVEBS_DEFAULT_PASSWORD = "root_password" + CURVEBS_CLUSTERMAP = "clustermap" + VIPER_CURVEBS_CLUSTERMAP = "curvebs.clustermap" + CURVEBS_FILENAME = "filename" + VIPER_CURVEBS_FILENAME = "curvebs.filename" + CURVEBS_FORCEDELETE = "forcedelete" + CURVEBS_DEFAULT_FORCEDELETE = false + CURVEBS_DIR = "dir" + VIPER_CURVEBS_DIR = "curvebs.dir" + CURVEBS_LOGIC_POOL_ID = "logicalpoolid" + VIPER_CURVEBS_LOGIC_POOL_ID = "curvebs.logicalpoolid" + CURVEBS_COPYSET_ID = "copysetid" + VIPER_CURVEBS_COPYSET_ID = "curvebs.copysetid" + CURVEBS_PEERS_ADDRESS = "peers" + VIPER_CURVEBS_PEERS_ADDRESS = "curvebs.peers" + CURVEBS_OFFSET = "offset" + VIPER_CURVEBS_OFFSET = "curvebs.offset" + CURVEBS_SIZE = "size" + VIPER_CURVEBS_SIZE = "curvebs.size" + CURVEBS_DEFAULT_SIZE = "10 GiB" + CURVEBS_TYPE = "type" + VIPER_CURVEBS_TYPE = "curvebs.type" + CURVEBS_STRIPE_UNIT = "stripeunit" + VIPER_CURVEBS_STRIPE_UNIT = "curvebs.stripeunit" + CURVEBS_DEFAULT_STRIPE_UNIT = "32 KiB" + CURVEBS_STRIPE_COUNT = "stripecount" + VIPER_CURVEBS_STRIPE_COUNT = "curvebs.stripecount" + CURVEBS_DEFAULT_STRIPE_COUNT = uint64(32) ) var ( @@ -90,13 +99,18 @@ var ( CURVEBS_CLUSTERMAP: VIPER_CURVEBS_CLUSTERMAP, CURVEBS_OFFSET: VIPER_CURVEBS_OFFSET, CURVEBS_SIZE: VIPER_CURVEBS_SIZE, + CURVEBS_STRIPE_UNIT: VIPER_CURVEBS_STRIPE_UNIT, + CURVEBS_STRIPE_COUNT: VIPER_CURVEBS_STRIPE_COUNT, } BSFLAG2DEFAULT = map[string]interface{}{ // bs - CURVEBS_USER: CURVEBS_DEFAULT_USER, - CURVEBS_PASSWORD: CURVEBS_DEFAULT_PASSWORD, - CURVEBS_FORCEDELETE: CURVEBS_DEFAULT_FORCEDELETE, + CURVEBS_USER: CURVEBS_DEFAULT_USER, + CURVEBS_PASSWORD: CURVEBS_DEFAULT_PASSWORD, + CURVEBS_FORCEDELETE: CURVEBS_DEFAULT_FORCEDELETE, + CURVEBS_SIZE: CURVEBS_DEFAULT_SIZE, + CURVEBS_STRIPE_UNIT: CURVEBS_DEFAULT_STRIPE_UNIT, + CURVEBS_STRIPE_COUNT: CURVEBS_DEFAULT_STRIPE_COUNT, } ) @@ -124,7 +138,7 @@ func AddBsStringSliceRequiredFlag(cmd *cobra.Command, name string, usage string) } func AddBsStringOptionFlag(cmd *cobra.Command, name string, usage string) { - defaultValue := FLAG2DEFAULT[name] + defaultValue := BSFLAG2DEFAULT[name] if defaultValue == nil { defaultValue = "" } @@ -135,6 +149,18 @@ func AddBsStringOptionFlag(cmd *cobra.Command, name string, usage string) { } } +func AddBsUint64OptionFlag(cmd *cobra.Command, name string, usage string) { + defaultValue := BSFLAG2DEFAULT[name] + if defaultValue == nil { + defaultValue = 0 + } + cmd.Flags().Uint64(name, defaultValue.(uint64), usage) + err := viper.BindPFlag(BSFLAG2VIPER[name], cmd.Flags().Lookup(name)) + if err != nil { + cobra.CheckErr(err) + } +} + // add bs required flag func AddBsStringRequiredFlag(cmd *cobra.Command, name string, usage string) { cmd.Flags().String(name, "", usage+color.Red.Sprint("[required]")) @@ -204,6 +230,18 @@ func AddBsEtcdAddrFlag(cmd *cobra.Command) { AddBsStringOptionFlag(cmd, CURVEBS_ETCDADDR, "etcd address, should be like 127.0.0.1:8700,127.0.0.1:8701,127.0.0.1:8702") } +func AddBsSizeOptionFlag(cmd *cobra.Command) { + AddBsStringOptionFlag(cmd, CURVEBS_SIZE, "size, just like: 10GiB") +} + +func AddBsStripeUnitOptionFlag(cmd *cobra.Command) { + AddBsStringOptionFlag(cmd, CURVEBS_STRIPE_UNIT, "stripe volume uint, just like: 32KiB") +} + +func AddBsStripeCountOptionFlag(cmd *cobra.Command) { + AddBsUint64OptionFlag(cmd, CURVEBS_STRIPE_COUNT, "stripe volume count") +} + // add flag required // add path[required] func AddBsPathRequiredFlag(cmd *cobra.Command) { @@ -246,6 +284,10 @@ func AddBsSizeRequiredFlag(cmd *cobra.Command) { AddBsStringRequiredFlag(cmd, CURVEBS_SIZE, "size, just like: 10GiB") } +func AddBsFileTypeRequiredFlag(cmd *cobra.Command) { + AddBsStringRequiredFlag(cmd, CURVEBS_TYPE, "file type, file or dir") +} + // get stingslice flag func GetBsFlagStringSlice(cmd *cobra.Command, flagName string) []string { var value []string @@ -290,7 +332,7 @@ func GetBsFlagUint64(cmd *cobra.Command, flagName string) uint64 { if cmd.Flag(flagName).Changed { value, _ = cmd.Flags().GetUint64(flagName) } else { - value = viper.GetUint64(FLAG2VIPER[flagName]) + value = viper.GetUint64(BSFLAG2VIPER[flagName]) } return value }