-
Notifications
You must be signed in to change notification settings - Fork 525
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add curve bs recover volume and its readme.
Signed-off-by: kevin <[email protected]>
- Loading branch information
1 parent
c16555e
commit 5a4c6bd
Showing
6 changed files
with
303 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* 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: CurveCli | ||
* Created Date: 2023-11-15 | ||
* Author: CrystalAnalyst | ||
*/ | ||
package recover | ||
|
||
import ( | ||
basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command" | ||
volume "github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/recover/volume" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type RecoverCommand struct { | ||
basecmd.MidCurveCmd | ||
} | ||
|
||
var _ basecmd.MidCurveCmdFunc = (*RecoverCommand)(nil) | ||
|
||
func (rCmd *RecoverCommand) AddSubCommands() { | ||
rCmd.Cmd.AddCommand( | ||
volume.NewVolumeCommand(), | ||
) | ||
} | ||
|
||
func NewVolumeCommand() *cobra.Command { | ||
rCmd := &RecoverCommand{ | ||
basecmd.MidCurveCmd{ | ||
Use: "recover", | ||
Short: "recover resources in curvebs cluster", | ||
}, | ||
} | ||
return basecmd.NewMidCurveCli(&rCmd.MidCurveCmd, rCmd) | ||
} |
142 changes: 142 additions & 0 deletions
142
tools-v2/pkg/cli/command/curvebs/recover/volume/recover.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package volume | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
"google.golang.org/grpc" | ||
|
||
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" | ||
) | ||
|
||
// NewCommand return the mid cli | ||
func NewRecoverFileCommand() *RecoverFileCommand { | ||
RecoverFileCommand := &RecoverFileCommand{ | ||
FinalCurveCmd: basecmd.FinalCurveCmd{}, | ||
} | ||
basecmd.NewFinalCurveCli(&RecoverFileCommand.FinalCurveCmd, RecoverFileCommand) | ||
return RecoverFileCommand | ||
} | ||
|
||
type RecoverCertainFileRPC struct { | ||
Info *basecmd.Rpc | ||
Request *nameserver2.RecoverFileRequest | ||
mdsClient nameserver2.CurveFSServiceClient | ||
} | ||
|
||
var _ basecmd.RpcFunc = (*RecoverCertainFileRPC)(nil) | ||
|
||
func (gRpc *RecoverCertainFileRPC) NewRpcClient(cc grpc.ClientConnInterface) { | ||
gRpc.mdsClient = nameserver2.NewCurveFSServiceClient(cc) | ||
} | ||
|
||
func (gRpc *RecoverCertainFileRPC) Stub_Func(ctx context.Context) (interface{}, error) { | ||
return gRpc.mdsClient.RecoverFile(ctx, gRpc.Request) | ||
} | ||
|
||
type RecoverFileCommand struct { | ||
basecmd.FinalCurveCmd | ||
Rpc *RecoverCertainFileRPC | ||
Response *nameserver2.RecoverFileResponse | ||
} | ||
|
||
var _ basecmd.FinalCurveCmdFunc = (*RecoverFileCommand)(nil) | ||
|
||
func (recoverCommand *RecoverFileCommand) Init(cmd *cobra.Command, args []string) error { | ||
mdsAddrs, err := config.GetBsMdsAddrSlice(recoverCommand.Cmd) | ||
if err.TypeCode() != cmderror.CODE_SUCCESS { | ||
return err.ToError() | ||
} | ||
//get the default timeout and retrytimes | ||
timeout := config.GetFlagDuration(recoverCommand.Cmd, config.RPCTIMEOUT) | ||
retrytimes := config.GetFlagInt32(recoverCommand.Cmd, config.RPCRETRYTIMES) | ||
//get the params needed from commandline | ||
path := config.GetBsFlagString(recoverCommand.Cmd, config.CURVEBS_PATH) | ||
username := config.GetBsFlagString(recoverCommand.Cmd, config.CURVEBS_USER) | ||
password := config.GetBsFlagString(recoverCommand.Cmd, config.CURVEBS_PASSWORD) | ||
fileId := config.GetBsFlagUint64(recoverCommand.Cmd, config.CURVEBS_FILE_ID) | ||
date, errDat := cobrautil.GetTimeofDayUs() | ||
if errDat.TypeCode() != cmderror.CODE_SUCCESS { | ||
return errDat.ToError() | ||
} | ||
|
||
//add basic info | ||
recoverRequest := nameserver2.RecoverFileRequest{ | ||
FileName: &path, | ||
Owner: &username, | ||
FileId: &fileId, | ||
Date: &date, | ||
} | ||
//add signature | ||
if username == viper.GetString(config.VIPER_CURVEBS_USER) && len(password) != 0 { | ||
strSig := cobrautil.GetString2Signature(date, username) | ||
sig := cobrautil.CalcString2Signature(strSig, password) | ||
recoverRequest.Signature = &sig | ||
} | ||
recoverCommand.Rpc = &RecoverCertainFileRPC{ | ||
Info: basecmd.NewRpc(mdsAddrs, timeout, retrytimes, "recoverFile"), | ||
Request: &recoverRequest, | ||
} | ||
return nil | ||
} | ||
|
||
func (recoverCommand *RecoverFileCommand) AddFlags() { | ||
config.AddRpcTimeoutFlag(recoverCommand.Cmd) | ||
config.AddRpcRetryTimesFlag(recoverCommand.Cmd) | ||
config.AddBsMdsFlagOption(recoverCommand.Cmd) | ||
//file path and user name is required. | ||
config.AddBsPathRequiredFlag(recoverCommand.Cmd) | ||
config.AddBsUserRequireFlag(recoverCommand.Cmd) | ||
//password and fileID is optional. | ||
config.AddBsPasswordOptionFlag(recoverCommand.Cmd) | ||
config.AddBsFileIdOptionFlag(recoverCommand.Cmd) | ||
} | ||
|
||
func (recoverCommand *RecoverFileCommand) RunCommand(cmd *cobra.Command, args []string) error { | ||
result, err := basecmd.GetRpcResponse(recoverCommand.Rpc.Info, recoverCommand.Rpc) | ||
if err.TypeCode() != cmderror.CODE_SUCCESS { | ||
recoverCommand.Error = err | ||
recoverCommand.Result = result | ||
return err.ToError() | ||
} | ||
recoverCommand.Response = result.(*nameserver2.RecoverFileResponse) | ||
if recoverCommand.Response.GetStatusCode() != nameserver2.StatusCode_kOK { | ||
recoverCommand.Error = cmderror.ErrBsRecoverFile() | ||
recoverCommand.Result = result | ||
return recoverCommand.Error.ToError() | ||
} | ||
return nil | ||
} | ||
|
||
func (recoverCommand *RecoverFileCommand) Print(cmd *cobra.Command, args []string) error { | ||
return output.FinalCmdOutput(&recoverCommand.FinalCurveCmd, recoverCommand) | ||
} | ||
|
||
func (recoverCommand *RecoverFileCommand) ResultPlainOutput() error { | ||
return output.FinalCmdOutputPlain(&recoverCommand.FinalCurveCmd) | ||
} | ||
|
||
func RecoverFile(caller *cobra.Command) (*nameserver2.RecoverFileResponse, *cmderror.CmdError) { | ||
rCmd := NewRecoverFileCommand() | ||
config.AlignFlagsValue(caller, rCmd.Cmd, []string{ | ||
config.RPCRETRYTIMES, config.RPCTIMEOUT, config.CURVEBS_MDSADDR, | ||
config.CURVEBS_PATH, config.CURVEBS_USER, | ||
config.CURVEBS_PASSWORD, config.CURVEBS_FILE_ID, | ||
}) | ||
rCmd.Cmd.SilenceErrors = true | ||
rCmd.Cmd.SilenceUsage = true | ||
rCmd.Cmd.SetArgs([]string{"--format", config.FORMAT_NOOUT}) | ||
err := rCmd.Cmd.Execute() | ||
if err != nil { | ||
retErr := cmderror.ErrBsRecoverFile() | ||
retErr.Format(err.Error()) | ||
return rCmd.Response, retErr | ||
} | ||
return rCmd.Response, cmderror.Success() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package volume | ||
|
||
import ( | ||
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 ( | ||
RecoverExample = `curve bs recover volume --path /curvebs-volume-path | ||
--user username [--password password] [--fileid fileid]` | ||
) | ||
|
||
// RecoverCommand | ||
type RecoverCommand struct { | ||
basecmd.FinalCurveCmd | ||
} | ||
|
||
var _ basecmd.FinalCurveCmdFunc = (*RecoverCommand)(nil) // check interface | ||
|
||
// new RecoverCommand function | ||
func NewVolumeCommand() *cobra.Command { | ||
rCmd := &RecoverCommand{ | ||
FinalCurveCmd: basecmd.FinalCurveCmd{ | ||
Use: "curve bs recover volume", | ||
Short: "recover volumes from the RecycleBin", | ||
Example: RecoverExample, | ||
}, | ||
} | ||
return basecmd.NewFinalCurveCli(&rCmd.FinalCurveCmd, rCmd) | ||
} | ||
|
||
func (rCmd *RecoverCommand) Init(cmd *cobra.Command, args []string) error { | ||
header := []string{cobrautil.ROW_RESULT} | ||
rCmd.SetHeader(header) | ||
return nil | ||
} | ||
|
||
func (rCmd *RecoverCommand) RunCommand(cmd *cobra.Command, args []string) error { | ||
rCmd.Result, rCmd.Error = RecoverFile(rCmd.Cmd) | ||
if rCmd.Error.TypeCode() != cmderror.CODE_SUCCESS { | ||
return rCmd.Error.ToError() | ||
} | ||
rCmd.TableNew.Append([]string{rCmd.Error.Message}) | ||
return nil | ||
} | ||
|
||
func (rCmd *RecoverCommand) Print(cmd *cobra.Command, args []string) error { | ||
return output.FinalCmdOutput(&rCmd.FinalCurveCmd, rCmd) | ||
} | ||
|
||
func (rCmd *RecoverCommand) ResultPlainOutput() error { | ||
return output.FinalCmdOutputPlain(&rCmd.FinalCurveCmd) | ||
} | ||
|
||
func (rCmd *RecoverCommand) AddFlags() { | ||
config.AddRpcRetryTimesFlag(rCmd.Cmd) | ||
config.AddRpcTimeoutFlag(rCmd.Cmd) | ||
config.AddBsMdsFlagOption(rCmd.Cmd) | ||
config.AddBsPathRequiredFlag(rCmd.Cmd) | ||
config.AddBsUserRequireFlag(rCmd.Cmd) | ||
config.AddBsPasswordOptionFlag(rCmd.Cmd) | ||
config.AddBsFileIdOptionFlag(rCmd.Cmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters