Skip to content

Commit

Permalink
add bs delete snapshot
Browse files Browse the repository at this point in the history
Signed-off-by: ZackSoul <[email protected]>
  • Loading branch information
ZackSoul committed Dec 6, 2023
1 parent bea74e3 commit bcbe08f
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 30 deletions.
23 changes: 23 additions & 0 deletions tools-v2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ A tool for CurveFS & CurveBs.
- [delete volume](#delete-volume)
- [delete volume clone](#delete-volume-clone)
- [delete volume recover](#delete-volume-recover)
- [delete snapshot](#delete-snapshot)
- [update](#update)
- [update peer](#update-peer)
- [update leader](#update-leader)
Expand Down Expand Up @@ -1667,6 +1668,28 @@ Output:
+------+--------------------------------------+--------------------------------------+-------+---------+--------+
```

##### delete snapshot

delete the snapshot in curvebs

Usage:

```shell
curve bs delete snapshot --path /test111/test222 --user root --snapshotFailed=false
```

Output:

```shell
+--------------------------------------+--------------+---------+
| SNAPSHOTID | SNAPSHOTNAME | RESULT |
+--------------------------------------+--------------+---------+
| faf32fa8-cfcf-4ac7-a9a9-458fcad7f995 | testsnap | success |
+--------------------------------------+--------------+---------+
| d23f78ad-4694-45de-a915-d734b6143953 | testsnap1 | success |
+--------------------------------------+--------------+---------+
```

#### update

##### update peer
Expand Down
3 changes: 3 additions & 0 deletions tools-v2/internal/utils/snapshot/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,7 @@ const (
ResultSuccess = "0"
Limit = "100"
Offset = "0"

ErrSnaphshot = "5"
DefaultSnapID = "*"
)
2 changes: 2 additions & 0 deletions tools-v2/pkg/cli/command/curvebs/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/delete/file"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/delete/peer"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/delete/snapshot"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/delete/volume"
)

Expand All @@ -26,6 +27,7 @@ func (dCmd *DeleteCommand) AddSubCommands() {
file.NewFileCommand(),
peer.NewCommand(),
volume.NewVolumeCommand(),
snapshot.NewSnapShotCommand(),
)
}

Expand Down
159 changes: 159 additions & 0 deletions tools-v2/pkg/cli/command/curvebs/delete/snapshot/snapshot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
* 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-10
* Author: ZackSoul
*/
package snapshot

import (
"time"

cmderror "github.com/opencurve/curve/tools-v2/internal/error"
cobrautil "github.com/opencurve/curve/tools-v2/internal/utils"
snapshotutil "github.com/opencurve/curve/tools-v2/internal/utils/snapshot"
basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command"
listSnapshot "github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/list/snapshot"
"github.com/opencurve/curve/tools-v2/pkg/config"
"github.com/opencurve/curve/tools-v2/pkg/output"
"github.com/spf13/cobra"
)

const (
snapshotExample = `$ curve bs delete snapshot --path /test111/test222 --user root --snapshotFailed=false`
)

type SnapShotCommand struct {
basecmd.FinalCurveCmd
snapshotAddrs []string
timeout time.Duration

user string
file string
uuid string
failed bool
}

var _ basecmd.FinalCurveCmdFunc = (*SnapShotCommand)(nil)

func NewSnapShotCommand() *cobra.Command {
return NewDeleteSnapShotCommand().Cmd
}

func NewDeleteSnapShotCommand() *SnapShotCommand {
snapShotCommand := &SnapShotCommand{
FinalCurveCmd: basecmd.FinalCurveCmd{
Use: "snapshot",
Short: "delete snapshot in curvebs",
Example: snapshotExample,
},
}

basecmd.NewFinalCurveCli(&snapShotCommand.FinalCurveCmd, snapShotCommand)
return snapShotCommand
}

func (sCmd *SnapShotCommand) AddFlags() {
config.AddBsSnapshotCloneFlagOption(sCmd.Cmd)
config.AddHttpTimeoutFlag(sCmd.Cmd)
config.AddBsUserOptionFlag(sCmd.Cmd)
config.AddBsSnapshotIDOptionFlag(sCmd.Cmd)
config.AddBsPathOptionFlag(sCmd.Cmd)
config.AddBsSnapshotFailedDOptionFlag(sCmd.Cmd)
}

func (sCmd *SnapShotCommand) Init(cmd *cobra.Command, args []string) error {
snapshotAddrs, err := config.GetBsSnapshotAddrSlice(sCmd.Cmd)
if err.TypeCode() != cmderror.CODE_SUCCESS {
sCmd.Error = err
return err.ToError()
}
sCmd.snapshotAddrs = snapshotAddrs
sCmd.timeout = config.GetFlagDuration(sCmd.Cmd, config.HTTPTIMEOUT)
sCmd.user = config.GetBsFlagString(sCmd.Cmd, config.CURVEBS_USER)
sCmd.file = config.GetBsFlagString(sCmd.Cmd, config.CURVEBS_PATH)
sCmd.uuid = config.GetBsFlagString(sCmd.Cmd, config.CURVEBS_SNAPSHOT_ID)
sCmd.failed = config.GetBsFlagBool(sCmd.Cmd, config.CURVEBS_SNAPSHOT_FAILED)
header := []string{
cobrautil.ROW_SNAPSHOT_ID,
cobrautil.ROW_SNAPSHOT_NAME,
cobrautil.ROW_RESULT,
}
sCmd.SetHeader(header)
return nil
}

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

func (sCmd *SnapShotCommand) RunCommand(cmd *cobra.Command, args []string) error {
params := map[string]any{
snapshotutil.QueryAction: snapshotutil.ActionGetFileSnapshotList,
snapshotutil.QueryUser: sCmd.user,
snapshotutil.QueryFile: sCmd.file,
snapshotutil.QueryLimit: snapshotutil.Limit,
snapshotutil.QueryOffset: snapshotutil.Offset,
}
if sCmd.failed {
params[snapshotutil.QueryStatus] = snapshotutil.ErrSnaphshot
}
if sCmd.uuid != snapshotutil.DefaultSnapID {
params[snapshotutil.QueryUUID] = sCmd.uuid
}
snapshotsList, err := listSnapshot.ListSnapShot(sCmd.snapshotAddrs, sCmd.timeout, params)
if err != nil {
sCmd.Error = err
return sCmd.Error.ToError()
}
rows := make([]map[string]string, 0)
for _, snapshot := range snapshotsList {
row := make(map[string]string)
err := DeleteSnapShot(sCmd.snapshotAddrs, sCmd.timeout, snapshot)
row[cobrautil.ROW_SNAPSHOT_ID] = snapshot.UUID
row[cobrautil.ROW_SNAPSHOT_NAME] = snapshot.Name
if err.TypeCode() == cmderror.CODE_SUCCESS {
row[cobrautil.ROW_RESULT] = cobrautil.ROW_VALUE_SUCCESS
} else {
row[cobrautil.ROW_RESULT] = cobrautil.ROW_VALUE_FAILED
}
rows = append(rows, row)
}
list := cobrautil.ListMap2ListSortByKeys(rows, sCmd.Header, []string{cobrautil.ROW_SNAPSHOT_NAME, cobrautil.ROW_SNAPSHOT_ID})
sCmd.TableNew.AppendBulk(list)
sCmd.Result = rows
sCmd.Error = cmderror.Success()
return nil
}

func (sCmd *SnapShotCommand) ResultPlainOutput() error {
return output.FinalCmdOutputPlain(&sCmd.FinalCurveCmd)
}

func DeleteSnapShot(addrs []string, timeout time.Duration, snapshot *snapshotutil.SnapshotInfo) *cmderror.CmdError {
params := map[string]any{
snapshotutil.QueryAction: snapshotutil.ActionDeleteSnapshot,
snapshotutil.QueryUser: snapshot.User,
snapshotutil.QueryUUID: snapshot.UUID,
snapshotutil.QueryFile: snapshot.File,
}
subUri := snapshotutil.NewQuerySubUri(params)
metric := basecmd.NewMetric(addrs, subUri, timeout)
_, err := basecmd.QueryMetric(metric)
return err
}
5 changes: 3 additions & 2 deletions tools-v2/pkg/cli/command/curvebs/list/snapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package snapshot
import (
"encoding/json"
"fmt"
snapshotutil "github.com/opencurve/curve/tools-v2/internal/utils/snapshot"
"strconv"
"time"

snapshotutil "github.com/opencurve/curve/tools-v2/internal/utils/snapshot"

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"
Expand Down Expand Up @@ -114,7 +115,7 @@ func (sCmd *SnapShotCommand) RunCommand(cmd *cobra.Command, args []string) error
row[cobrautil.ROW_STATUS] = fmt.Sprintf("%d", item.Status)
row[cobrautil.ROW_SNAPSHOT_SEQNUM] = fmt.Sprintf("%d", item.SeqNum)
row[cobrautil.ROW_FILE_LENGTH] = fmt.Sprintf("%d", item.FileLength)
row[cobrautil.ROW_PROGRESS] = fmt.Sprintf("%d", item.Progress)
row[cobrautil.ROW_PROGRESS] = fmt.Sprintf("%.2f", item.Progress)
row[cobrautil.ROW_CREATE_TIME] = time.Unix(int64(item.Time/1000000), 0).Format("2006-01-02 15:04:05")
rows = append(rows, row)
}
Expand Down
65 changes: 37 additions & 28 deletions tools-v2/pkg/config/bs.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,16 @@ const (
CURVEBS_SNAPSHOT_ID = "snapshotid"
VIPER_CURVEBS_SNAPSHOT_ID = "curvebs.snapshotqid"
CURVEBS_DEFAULT_SNAPSHOT_ID = "*"
CURVEBS_SNAPSHOT_FAILED = "snapshotFailed"
VIPER_CURVEBS_SNAPSHOT_FAILED = "curvebs.snapshotFailed"
CURVEBS_DEFAULT_SNAPSHOT_FAILED = false
CURVEBS_FAILED = "failed"
VIPER_CURVEBS_FAILED = "curvebs.failed"
CURVEBS_CHUNK_SIZE = "chunksize"
VIPER_CURVEBS_CHUNK_SIZE = "curvebs.chunksize"
CURVEBS_CHECK_HASH = "checkhash"
VIPER_CURVEBS_CHECK_HASH = "curvebs.checkhash"
CURVEBS_DEFAULT_CHECK_HASH = false
CURVEBS_CHUNK_SIZE = "chunksize"
VIPER_CURVEBS_CHUNK_SIZE = "curvebs.chunksize"
CURVEBS_CHECK_HASH = "checkhash"
VIPER_CURVEBS_CHECK_HASH = "curvebs.checkhash"
CURVEBS_DEFAULT_CHECK_HASH = false
)

var (
Expand Down Expand Up @@ -207,33 +210,35 @@ var (
CURVEBS_TASKID: VIPER_CURVEBS_TASKID,
CURVEBS_SNAPSHOT_ID: VIPER_CURVEBS_SNAPSHOT_ID,
CURVEBS_FAILED: VIPER_CURVEBS_FAILED,
CURVEBS_CHUNK_SIZE: VIPER_CURVEBS_CHUNK_SIZE,
CURVEBS_CHECK_HASH: VIPER_CURVEBS_CHECK_HASH,
CURVEBS_CHUNK_SIZE: VIPER_CURVEBS_CHUNK_SIZE,
CURVEBS_CHECK_HASH: VIPER_CURVEBS_CHECK_HASH,
CURVEBS_SNAPSHOT_FAILED: VIPER_CURVEBS_SNAPSHOT_FAILED,
}

BSFLAG2DEFAULT = map[string]interface{}{
// bs
CURVEBS_USER: CURVEBS_DEFAULT_USER,
CURVEBS_PASSWORD: CURVEBS_DEFAULT_PASSWORD,
CURVEBS_SIZE: CURVEBS_DEFAULT_SIZE,
CURVEBS_STRIPE_UNIT: CURVEBS_DEFAULT_STRIPE_UNIT,
CURVEBS_STRIPE_COUNT: CURVEBS_DEFAULT_STRIPE_COUNT,
CURVEBS_BURST: CURVEBS_DEFAULT_BURST,
CURVEBS_BURST_LENGTH: CURVEBS_DEFAULT_BURST_LENGTH,
CURVEBS_PATH: CURVEBS_DEFAULT_PATH,
CURVEBS_FORCE: CURVEBS_DEFAULT_FORCE,
CURVEBS_MARGIN: CURVEBS_DEFAULT_MARGIN,
CURVEBS_OP: CURVEBS_DEFAULT_OP,
CURVEBS_CHECK_TIME: CURVEBS_DEFAULT_CHECK_TIME,
CURVEBS_SCAN: CURVEBS_DEFAULT_SCAN,
CURVEBS_CHUNKSERVER_ID: CURVEBS_DEFAULT_CHUNKSERVER_ID,
CURVEBS_DRYRUN: CURVEBS_DEFAULT_DRYRUN,
CURVEBS_FIlTER: CURVEBS_DEFAULT_FILTER,
CURVEBS_ALL: CURVEBS_DEFAULT_ALL,
CURVEBS_LOGIC_POOL_ID: CURVEBS_DEFAULT_LOGIC_POOL_ID,
CURVEBS_COPYSET_ID: CURVEBS_DEFAULT_COPYSET_ID,
CURVEBS_CHECK_HASH: CURVEBS_DEFAULT_CHECK_HASH,
CURVEBS_SNAPSHOT_ID: CURVEBS_DEFAULT_SNAPSHOT_ID,
CURVEBS_USER: CURVEBS_DEFAULT_USER,
CURVEBS_PASSWORD: CURVEBS_DEFAULT_PASSWORD,
CURVEBS_SIZE: CURVEBS_DEFAULT_SIZE,
CURVEBS_STRIPE_UNIT: CURVEBS_DEFAULT_STRIPE_UNIT,
CURVEBS_STRIPE_COUNT: CURVEBS_DEFAULT_STRIPE_COUNT,
CURVEBS_BURST: CURVEBS_DEFAULT_BURST,
CURVEBS_BURST_LENGTH: CURVEBS_DEFAULT_BURST_LENGTH,
CURVEBS_PATH: CURVEBS_DEFAULT_PATH,
CURVEBS_FORCE: CURVEBS_DEFAULT_FORCE,
CURVEBS_MARGIN: CURVEBS_DEFAULT_MARGIN,
CURVEBS_OP: CURVEBS_DEFAULT_OP,
CURVEBS_CHECK_TIME: CURVEBS_DEFAULT_CHECK_TIME,
CURVEBS_SCAN: CURVEBS_DEFAULT_SCAN,
CURVEBS_CHUNKSERVER_ID: CURVEBS_DEFAULT_CHUNKSERVER_ID,
CURVEBS_DRYRUN: CURVEBS_DEFAULT_DRYRUN,
CURVEBS_FIlTER: CURVEBS_DEFAULT_FILTER,
CURVEBS_ALL: CURVEBS_DEFAULT_ALL,
CURVEBS_LOGIC_POOL_ID: CURVEBS_DEFAULT_LOGIC_POOL_ID,
CURVEBS_COPYSET_ID: CURVEBS_DEFAULT_COPYSET_ID,
CURVEBS_CHECK_HASH: CURVEBS_DEFAULT_CHECK_HASH,
CURVEBS_SNAPSHOT_ID: CURVEBS_DEFAULT_SNAPSHOT_ID,
CURVEBS_SNAPSHOT_FAILED: CURVEBS_DEFAULT_SNAPSHOT_FAILED,
}
)

Expand Down Expand Up @@ -675,6 +680,10 @@ func AddBsSnapshotIDOptionFlag(cmd *cobra.Command) {
AddBsStringOptionFlag(cmd, CURVEBS_SNAPSHOT_ID, "snapshot seqId")
}

func AddBsSnapshotFailedDOptionFlag(cmd *cobra.Command) {
AddBsBoolOptionFlag(cmd, CURVEBS_SNAPSHOT_FAILED, "snapshot failed(error)")
}

func AddBsTaskTypeOptionFlag(cmd *cobra.Command) {
AddBsStringOptionFlag(cmd, CURVEBS_TYPE, "only query target type (clone or recover)")
}
Expand Down

0 comments on commit bcbe08f

Please sign in to comment.