Skip to content

Commit

Permalink
[feat]tools-v2: extend file
Browse files Browse the repository at this point in the history
Signed-off-by: Cyber-SiKu <[email protected]>
  • Loading branch information
Cyber-SiKu authored and wuhongsong committed Apr 14, 2023
1 parent 9ad8cc1 commit b4d23f7
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 1 deletion.
12 changes: 12 additions & 0 deletions tools-v2/internal/error/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -719,4 +719,16 @@ var (
}
return NewRpcReultCmdError(-code, message)
}

ErrExtendFile = func(statusCode nameserver2.StatusCode, path, size string) *CmdError {
var message string
code := int(statusCode)
switch statusCode {
case nameserver2.StatusCode_kOK:
message = "successfully expanded the file"
default:
message = fmt.Sprintf("failed to expand file[%s] to %s, err: %s", path, size, statusCode.String())
}
return NewRpcReultCmdError(code, message)
}
)
154 changes: 154 additions & 0 deletions tools-v2/pkg/cli/command/curvebs/update/file/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* 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-13
* 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"
)

const (
fileExample = `$ curve bs update file`
)

type ExtendFileRpc struct {
Info *basecmd.Rpc
Request *nameserver2.ExtendFileRequest
mdsClient nameserver2.CurveFSServiceClient
}

var _ basecmd.RpcFunc = (*ExtendFileRpc)(nil) // check interface

func (eRpc *ExtendFileRpc) NewRpcClient(cc grpc.ClientConnInterface) {
eRpc.mdsClient = nameserver2.NewCurveFSServiceClient(cc)
}

func (eRpc *ExtendFileRpc) Stub_Func(ctx context.Context) (interface{}, error) {
return eRpc.mdsClient.ExtendFile(ctx, eRpc.Request)
}

type FileCommand struct {
basecmd.FinalCurveCmd
Rpc *ExtendFileRpc
Response *nameserver2.ExtendFileResponse
}

var _ basecmd.FinalCurveCmdFunc = (*FileCommand)(nil) // check interface

func NewUpdateFileCommand() *FileCommand {
fileCmd := &FileCommand{
FinalCurveCmd: basecmd.FinalCurveCmd{
Use: "file",
Short: "extend volume of file to size",
Example: fileExample,
},
}
basecmd.NewFinalCurveCli(&fileCmd.FinalCurveCmd, fileCmd)
return fileCmd
}

func NewFileCommand() *cobra.Command {
return NewUpdateFileCommand().Cmd
}

func (uCmd *FileCommand) AddFlags() {
config.AddRpcRetryTimesFlag(uCmd.Cmd)
config.AddRpcTimeoutFlag(uCmd.Cmd)
config.AddBsMdsFlagOption(uCmd.Cmd)
config.AddBsPathRequiredFlag(uCmd.Cmd)
config.AddBsSizeRequiredFlag(uCmd.Cmd)
config.AddBsUserOptionFlag(uCmd.Cmd)
config.AddBsPasswordOptionFlag(uCmd.Cmd)
}

func (uCmd *FileCommand) Init(cmd *cobra.Command, args []string) error {
fileName := config.GetBsFlagString(uCmd.Cmd, config.CURVEBS_PATH)
sizeStr := config.GetBsFlagString(uCmd.Cmd, config.CURVEBS_SIZE)
newSize, err := humanize.ParseBytes(sizeStr)
if err != nil {
return fmt.Errorf("parse size failed, err: %v", err)
}
date, errDat := cobrautil.GetTimeofDayUs()
owner := config.GetBsFlagString(uCmd.Cmd, config.CURVEBS_USER)
if errDat.TypeCode() != cmderror.CODE_SUCCESS {
return errDat.ToError()
}
request := &nameserver2.ExtendFileRequest{
FileName: &fileName,
NewSize: &newSize,
Date: &date,
Owner: &owner,
}
password := config.GetBsFlagString(uCmd.Cmd, config.CURVEBS_PASSWORD)
if owner == viper.GetString(config.VIPER_CURVEBS_USER) && len(password) != 0 {
strSig := cobrautil.GetString2Signature(date, owner)
sig := cobrautil.CalcString2Signature(strSig, password)
request.Signature = &sig
}
mdsAddrs, errMds := config.GetBsMdsAddrSlice(uCmd.Cmd)
if errMds.TypeCode() != cmderror.CODE_SUCCESS {
return errMds.ToError()
}
timeout := config.GetFlagDuration(uCmd.Cmd, config.RPCTIMEOUT)
retrytimes := config.GetFlagInt32(uCmd.Cmd, config.RPCRETRYTIMES)
uCmd.Rpc = &ExtendFileRpc{
Info: basecmd.NewRpc(mdsAddrs, timeout, retrytimes, "ExtendFile"),
Request: request,
}
uCmd.SetHeader([]string{cobrautil.ROW_RESULT})
return nil
}

func (uCmd *FileCommand) Print(cmd *cobra.Command, args []string) error {
return output.FinalCmdOutput(&uCmd.FinalCurveCmd, uCmd)
}

func (uCmd *FileCommand) RunCommand(cmd *cobra.Command, args []string) error {
result, err := basecmd.GetRpcResponse(uCmd.Rpc.Info, uCmd.Rpc)
if err.TypeCode() != cmderror.CODE_SUCCESS {
return err.ToError()
}
uCmd.Response = result.(*nameserver2.ExtendFileResponse)
uCmd.Result = uCmd.Response
sizeStr := humanize.IBytes(uCmd.Rpc.Request.GetNewSize())
uCmd.Error = cmderror.ErrExtendFile(uCmd.Response.GetStatusCode(), *uCmd.Rpc.Request.FileName, sizeStr)
uCmd.TableNew.Append([]string{uCmd.Error.Message})
if uCmd.Error.TypeCode() != cmderror.CODE_SUCCESS {
return uCmd.Error.ToError()
}
return nil
}

func (uCmd *FileCommand) ResultPlainOutput() error {
return output.FinalCmdOutputPlain(&uCmd.FinalCurveCmd)
}
2 changes: 2 additions & 0 deletions tools-v2/pkg/cli/command/curvebs/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/spf13/cobra"

basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/update/file"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/update/peer"
)

Expand All @@ -38,6 +39,7 @@ var _ basecmd.MidCurveCmdFunc = (*UpdateCommand)(nil)
func (updateCmd *UpdateCommand) AddSubCommands() {
updateCmd.Cmd.AddCommand(
peer.NewPeerCommand(),
file.NewFileCommand(),
)
}

Expand Down
8 changes: 7 additions & 1 deletion tools-v2/pkg/config/bs.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ const (
VIPER_CURVEBS_PEERS_ADDRESS = "curvebs.peers"
CURVEBS_OFFSET = "offset"
VIPER_CURVEBS_OFFSET = "curvebs.offset"
CURVEBS_SIZE = "size"
VIPER_CURVEBS_SIZE = "curvebs.size"
)

var (
Expand All @@ -87,6 +89,7 @@ var (
CURVEBS_PEERS_ADDRESS: VIPER_CURVEBS_PEERS_ADDRESS,
CURVEBS_CLUSTERMAP: VIPER_CURVEBS_CLUSTERMAP,
CURVEBS_OFFSET: VIPER_CURVEBS_OFFSET,
CURVEBS_SIZE: VIPER_CURVEBS_SIZE,
}

BSFLAG2DEFAULT = map[string]interface{}{
Expand Down Expand Up @@ -163,7 +166,6 @@ func AddBsUint32RequiredFlag(cmd *cobra.Command, name string, usage string) {
}
}


func AddBsUint64RequiredFlag(cmd *cobra.Command, name string, usage string) {
cmd.Flags().Uint64(name, uint64(0), usage+color.Red.Sprint("[required]"))
cmd.MarkFlagRequired(name)
Expand Down Expand Up @@ -240,6 +242,10 @@ func AddBsOffsetRequiredFlag(cmd *cobra.Command) {
AddBsUint64RequiredFlag(cmd, CURVEBS_OFFSET, "offset")
}

func AddBsSizeRequiredFlag(cmd *cobra.Command) {
AddBsStringRequiredFlag(cmd, CURVEBS_SIZE, "size, just like: 10GiB")
}

// get stingslice flag
func GetBsFlagStringSlice(cmd *cobra.Command, flagName string) []string {
var value []string
Expand Down

0 comments on commit b4d23f7

Please sign in to comment.