diff --git a/tools-v2/internal/error/error.go b/tools-v2/internal/error/error.go index d9676184db..72f8de384d 100644 --- a/tools-v2/internal/error/error.go +++ b/tools-v2/internal/error/error.go @@ -265,6 +265,9 @@ var ( ErrGetClusterFsInfo = func() *CmdError { return NewInternalCmdError(8, "get cluster fs info failed, the error is: \n%s") } + ErrUpdateClusterFsInfo = func() *CmdError { + return NewInternalCmdError(9, "update cluster fs info failed, the error is: \n%s") + } ErrGetAddr = func() *CmdError { return NewInternalCmdError(9, "invalid %s addr is: %s") } diff --git a/tools-v2/pkg/cli/command/curvefs/fs.go b/tools-v2/pkg/cli/command/curvefs/fs.go index 4404c2ca06..da0cdaa19f 100644 --- a/tools-v2/pkg/cli/command/curvefs/fs.go +++ b/tools-v2/pkg/cli/command/curvefs/fs.go @@ -31,6 +31,7 @@ import ( "github.com/opencurve/curve/tools-v2/pkg/cli/command/curvefs/query" status "github.com/opencurve/curve/tools-v2/pkg/cli/command/curvefs/status" umount "github.com/opencurve/curve/tools-v2/pkg/cli/command/curvefs/umount" + update "github.com/opencurve/curve/tools-v2/pkg/cli/command/curvefs/update" usage "github.com/opencurve/curve/tools-v2/pkg/cli/command/curvefs/usage" "github.com/opencurve/curve/tools-v2/pkg/cli/command/curvefs/warmup" "github.com/spf13/cobra" @@ -53,6 +54,7 @@ func (fsCmd *CurveFsCommand) AddSubCommands() { create.NewCreateCommand(), check.NewCheckCommand(), warmup.NewWarmupCommand(), + update.NewUpdateCommand(), ) } diff --git a/tools-v2/pkg/cli/command/curvefs/update/fs/fs.go b/tools-v2/pkg/cli/command/curvefs/update/fs/fs.go new file mode 100644 index 0000000000..f4b5ad2c2d --- /dev/null +++ b/tools-v2/pkg/cli/command/curvefs/update/fs/fs.go @@ -0,0 +1,157 @@ +/* + * 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. + */ + +package fs + +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" + mds "github.com/opencurve/curve/tools-v2/proto/curvefs/proto/mds" + "github.com/spf13/cobra" + "github.com/spf13/viper" + "google.golang.org/grpc" +) + +const ( + fsExample = `$ curve fs update fs --fsname test1` +) + +type UpdateFsRpc struct { + Info *basecmd.Rpc + Request *mds.UpdateFsInfoRequest + mdsClient mds.MdsServiceClient +} + +var _ basecmd.RpcFunc = (*UpdateFsRpc)(nil) // check interface + +type FsCommand struct { + basecmd.FinalCurveCmd + Rpc *UpdateFsRpc +} + +var _ basecmd.FinalCurveCmdFunc = (*FsCommand)(nil) // check interface + +func (ufRp *UpdateFsRpc) NewRpcClient(cc grpc.ClientConnInterface) { + ufRp.mdsClient = mds.NewMdsServiceClient(cc) +} + +func (ufRp *UpdateFsRpc) Stub_Func(ctx context.Context) (interface{}, error) { + return ufRp.mdsClient.UpdateFsInfo(ctx, ufRp.Request) +} + +func NewFsCommand() *cobra.Command { + fsCmd := &FsCommand{ + FinalCurveCmd: basecmd.FinalCurveCmd{ + Use: "fs", + Short: "update fsinfo", + Long: "update fsinfo", + Example: fsExample, + }, + } + basecmd.NewFinalCurveCli(&fsCmd.FinalCurveCmd, fsCmd) + return fsCmd.Cmd +} + +func (fCmd *FsCommand) AddFlags() { + config.AddRpcRetryTimesFlag(fCmd.Cmd) + config.AddRpcTimeoutFlag(fCmd.Cmd) + config.AddFsMdsAddrFlag(fCmd.Cmd) + config.AddFsNameRequiredFlag(fCmd.Cmd) + // things can be changed + config.AddCapacityOptionFlag(fCmd.Cmd) +} + +func (fCmd *FsCommand) Init(cmd *cobra.Command, args []string) error { + // args check + fsName, _ := cmd.Flags().GetString("fsName") + request := &mds.UpdateFsInfoRequest{ + FsName: &fsName, + } + + nothingToChange := true + var capacity string + if config.GetFlagChanged(cmd, config.CURVEFS_CAPACITY) { + nothingToChange = false + capacity = config.GetFlagString(cmd, config.CURVEFS_CAPACITY) + cap_bytes, _ := humanize.ParseBytes(capacity) + request.Capacity = &cap_bytes + } + + if nothingToChange { + return fmt.Errorf("please specify something to change") + } + + addrs, addrErr := config.GetFsMdsAddrSlice(fCmd.Cmd) + if addrErr.TypeCode() != cmderror.CODE_SUCCESS { + return fmt.Errorf(addrErr.Message) + } + timeout := viper.GetDuration(config.VIPER_GLOBALE_RPCTIMEOUT) + retrytimes := viper.GetInt32(config.VIPER_GLOBALE_RPCRETRYTIMES) + + // output format + header := []string{cobrautil.ROW_FS_NAME, cobrautil.ROW_RESULT} + fCmd.SetHeader(header) + + // set rpc + fCmd.Rpc = &UpdateFsRpc{ + Request: request, + } + fCmd.Rpc.Info = basecmd.NewRpc(addrs, timeout, retrytimes, "UpdateFsInfo") + return nil +} + +func (fCmd *FsCommand) Print(cmd *cobra.Command, args []string) error { + return output.FinalCmdOutput(&fCmd.FinalCurveCmd, fCmd) +} + +func (fCmd *FsCommand) RunCommand(cmd *cobra.Command, args []string) error { + result, errCmd := basecmd.GetRpcResponse(fCmd.Rpc.Info, fCmd.Rpc) + if errCmd.TypeCode() != cmderror.CODE_SUCCESS { + return fmt.Errorf(errCmd.Message) + } + + response := result.(*mds.UpdateFsInfoResponse) + errCreate := cmderror.ErrCreateFs(int(response.GetStatusCode())) + row := map[string]string{ + cobrautil.ROW_FS_NAME: fCmd.Rpc.Request.GetFsName(), + cobrautil.ROW_RESULT: errCreate.Message, + } + + fCmd.TableNew.Append(cobrautil.Map2List(row, fCmd.Header)) + + var errs []*cmderror.CmdError + res, errTranslate := output.MarshalProtoJson(response) + if errTranslate != nil { + errMar := cmderror.ErrMarShalProtoJson() + errMar.Format(errTranslate.Error()) + errs = append(errs, errMar) + } + + fCmd.Result = res + fCmd.Error = cmderror.MostImportantCmdError(errs) + return nil +} + +func (fCmd *FsCommand) ResultPlainOutput() error { + return output.FinalCmdOutputPlain(&fCmd.FinalCurveCmd) +} diff --git a/tools-v2/pkg/cli/command/curvefs/update/update.go b/tools-v2/pkg/cli/command/curvefs/update/update.go new file mode 100644 index 0000000000..2b7cf4513f --- /dev/null +++ b/tools-v2/pkg/cli/command/curvefs/update/update.go @@ -0,0 +1,45 @@ +/* + * 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. + */ + +package update + +import ( + basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command" + "github.com/opencurve/curve/tools-v2/pkg/cli/command/curvefs/update/fs" + "github.com/spf13/cobra" +) + +type UpdateCommand struct { + basecmd.MidCurveCmd +} + +var _ basecmd.MidCurveCmdFunc = (*UpdateCommand)(nil) // check interface + +func (updateCmd *UpdateCommand) AddSubCommands() { + updateCmd.Cmd.AddCommand( + fs.NewFsCommand(), + ) +} + +func NewUpdateCommand() *cobra.Command { + updateCmd := &UpdateCommand{ + basecmd.MidCurveCmd{ + Use: "update", + Short: "update resources in the curvefs", + }, + } + return basecmd.NewMidCurveCli(&updateCmd.MidCurveCmd, updateCmd) +} diff --git a/tools-v2/pkg/config/config.go b/tools-v2/pkg/config/config.go index 6a30e6d364..dd37904218 100644 --- a/tools-v2/pkg/config/config.go +++ b/tools-v2/pkg/config/config.go @@ -134,6 +134,10 @@ func AddShowErrorPFlag(cmd *cobra.Command) { } } +func AddFsCapacityFlag(cmd *cobra.Command) { + AddStringOptionFlag(cmd, "capacity", "capacity of the filesystem") +} + // Align the flag (changed) in the caller with the callee func AlignFlagsValue(caller *cobra.Command, callee *cobra.Command, flagNames []string) { callee.Flags().VisitAll(func(flag *pflag.Flag) {