Skip to content

Commit

Permalink
tools-v2: add update fs command to change capacity
Browse files Browse the repository at this point in the history
Signed-off-by: h0hmj <[email protected]>
  • Loading branch information
h0hmj committed Oct 20, 2023
1 parent d5fe7de commit 7d8fb70
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 0 deletions.
3 changes: 3 additions & 0 deletions tools-v2/internal/error/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
2 changes: 2 additions & 0 deletions tools-v2/pkg/cli/command/curvefs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -53,6 +54,7 @@ func (fsCmd *CurveFsCommand) AddSubCommands() {
create.NewCreateCommand(),
check.NewCheckCommand(),
warmup.NewWarmupCommand(),
update.NewUpdateCommand(),
)
}

Expand Down
158 changes: 158 additions & 0 deletions tools-v2/pkg/cli/command/curvefs/update/fs/fs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
* 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)
fCmd.Cmd.Flags().String("fsName", "", "curvefs fsname")
fCmd.Cmd.MarkFlagRequired("fsName")
// things can be changed
fCmd.Cmd.Flags().String("capacity", "", "curvefs capacity")
}

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 cmd.Flags().Changed("capacity") {
nothingToChange = false
capacity, _ = cmd.Flags().GetString("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)
}
45 changes: 45 additions & 0 deletions tools-v2/pkg/cli/command/curvefs/update/update.go
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 4 additions & 0 deletions tools-v2/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 7d8fb70

Please sign in to comment.