Skip to content

Commit

Permalink
[feta]tools-v2:create
Browse files Browse the repository at this point in the history
1. create file
2. create dir

Signed-off-by: Cyber-SiKu <[email protected]>
  • Loading branch information
Cyber-SiKu committed Apr 17, 2023
1 parent 6f63fdf commit efcbe1c
Show file tree
Hide file tree
Showing 10 changed files with 508 additions and 49 deletions.
16 changes: 8 additions & 8 deletions tools-v2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | |
Expand Down
17 changes: 17 additions & 0 deletions tools-v2/internal/error/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
)
18 changes: 18 additions & 0 deletions tools-v2/internal/utils/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions tools-v2/pkg/cli/command/curvebs/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -37,6 +39,8 @@ var _ basecmd.MidCurveCmdFunc = (*CreateCmd)(nil) // check interface
func (createCmd *CreateCmd) AddSubCommands() {
createCmd.Cmd.AddCommand(
cluster.NewClusterTopoCmd(),
dir.NewDirectoryCommand(),
file.NewFileCommand(),
)
}

Expand Down
97 changes: 97 additions & 0 deletions tools-v2/pkg/cli/command/curvebs/create/dir/dir.go
Original file line number Diff line number Diff line change
@@ -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
}
184 changes: 184 additions & 0 deletions tools-v2/pkg/cli/command/curvebs/create/file/create.go
Original file line number Diff line number Diff line change
@@ -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()
}
Loading

0 comments on commit efcbe1c

Please sign in to comment.