Skip to content

Commit 3310650

Browse files
ZackSoulcaoxianfei1
authored andcommitted
feat(tool):add bs delete snapshot
Signed-off-by: ZackSoul <[email protected]>
1 parent 3a82cb7 commit 3310650

File tree

6 files changed

+277
-38
lines changed

6 files changed

+277
-38
lines changed

tools-v2/README.md

+30-7
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ A tool for CurveFS & CurveBs.
7777
- [delete volume](#delete-volume)
7878
- [delete volume clone](#delete-volume-clone)
7979
- [delete volume recover](#delete-volume-recover)
80+
- [delete snapshot](#delete-snapshot)
8081
- [update](#update)
8182
- [update peer](#update-peer)
8283
- [update leader](#update-leader)
@@ -1693,6 +1694,28 @@ Output:
16931694
+------+--------------------------------------+--------------------------------------+-------+---------+--------+
16941695
```
16951696

1697+
##### delete snapshot
1698+
1699+
delete the snapshot in curvebs
1700+
1701+
Usage:
1702+
1703+
```shell
1704+
curve bs delete snapshot --path /test111/test222 --user root --snapshotFailed=false
1705+
```
1706+
1707+
Output:
1708+
1709+
```shell
1710+
+--------------------------------------+--------------+---------+--------+
1711+
| SNAPSHOTID | SNAPSHOTNAME | RESULT | REASON |
1712+
+--------------------------------------+--------------+---------+--------+
1713+
| 3b9d14c2-79a2-4454-9b0a-6cb7477956db | testsnap1 | success | null |
1714+
+--------------------------------------+--------------+---------+--------+
1715+
| 2a94fb51-e985-4e98-a34c-f02aef8e97b5 | testsnap2 | success | null |
1716+
+--------------------------------------+--------------+---------+--------+
1717+
```
1718+
16961719
#### update
16971720

16981721
##### update peer
@@ -2030,13 +2053,13 @@ curve bs stop snapshot
20302053
Output:
20312054

20322055
```shell
2033-
+--------------------------------------+--------------+---------+
2034-
| SNAPSHOTID | SNAPSHOTNAME | RESULT |
2035-
+--------------------------------------+--------------+---------+
2036-
| 9aa2b4c5-f27b-40a2-82c9-4e0ad6093567 | testsnap | success |
2037-
+--------------------------------------+--------------+---------+
2038-
| 0171a33b-17b7-4215-9f00-6d8de2686f77 | testsnap1 | success |
2039-
+--------------------------------------+--------------+---------+
2056+
+--------------------------------------+--------------+---------+--------+
2057+
| SNAPSHOTID | SNAPSHOTNAME | RESULT | REASON |
2058+
+--------------------------------------+--------------+---------+--------+
2059+
| 3b9d14c2-79a2-4454-9b0a-6cb7477956db | testsnap1 | success | null |
2060+
+--------------------------------------+--------------+---------+--------+
2061+
| 2a94fb51-e985-4e98-a34c-f02aef8e97b5 | testsnap2 | success | null |
2062+
+--------------------------------------+--------------+---------+--------+
20402063
```
20412064

20422065
#### recover

tools-v2/internal/utils/snapshot/const.go

+3
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,7 @@ const (
5555
ResultSuccess = "0"
5656
Limit = "100"
5757
Offset = "0"
58+
59+
ErrSnaphshot = "5"
60+
DefaultSnapID = "*"
5861
)

tools-v2/pkg/cli/command/curvebs/delete/delete.go

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command"
1313
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/delete/file"
1414
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/delete/peer"
15+
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/delete/snapshot"
1516
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/delete/volume"
1617
)
1718

@@ -26,6 +27,7 @@ func (dCmd *DeleteCommand) AddSubCommands() {
2627
file.NewFileCommand(),
2728
peer.NewCommand(),
2829
volume.NewVolumeCommand(),
30+
snapshot.NewSnapShotCommand(),
2931
)
3032
}
3133

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
/*
2+
* Copyright (c) 2023 NetEase Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/*
18+
* Project: CurveCli
19+
* Created Date: 2023-11-10
20+
* Author: ZackSoul
21+
*/
22+
package snapshot
23+
24+
import (
25+
"time"
26+
27+
cmderror "github.com/opencurve/curve/tools-v2/internal/error"
28+
cobrautil "github.com/opencurve/curve/tools-v2/internal/utils"
29+
snapshotutil "github.com/opencurve/curve/tools-v2/internal/utils/snapshot"
30+
basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command"
31+
listSnapshot "github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/list/snapshot"
32+
stopSnapShot "github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/stop/snapshot"
33+
"github.com/opencurve/curve/tools-v2/pkg/config"
34+
"github.com/opencurve/curve/tools-v2/pkg/output"
35+
"github.com/spf13/cobra"
36+
)
37+
38+
const (
39+
snapshotExample = `$ curve bs delete snapshot --path /test111/test222 --user root --snapshotFailed=false`
40+
)
41+
42+
type SnapShotCommand struct {
43+
basecmd.FinalCurveCmd
44+
snapshotAddrs []string
45+
timeout time.Duration
46+
47+
user string
48+
file string
49+
uuid string
50+
failed bool
51+
}
52+
53+
var _ basecmd.FinalCurveCmdFunc = (*SnapShotCommand)(nil)
54+
55+
func NewSnapShotCommand() *cobra.Command {
56+
return NewDeleteSnapShotCommand().Cmd
57+
}
58+
59+
func NewDeleteSnapShotCommand() *SnapShotCommand {
60+
snapShotCommand := &SnapShotCommand{
61+
FinalCurveCmd: basecmd.FinalCurveCmd{
62+
Use: "snapshot",
63+
Short: "delete snapshot in curvebs",
64+
Example: snapshotExample,
65+
},
66+
}
67+
68+
basecmd.NewFinalCurveCli(&snapShotCommand.FinalCurveCmd, snapShotCommand)
69+
return snapShotCommand
70+
}
71+
72+
func (sCmd *SnapShotCommand) AddFlags() {
73+
config.AddBsSnapshotCloneFlagOption(sCmd.Cmd)
74+
config.AddHttpTimeoutFlag(sCmd.Cmd)
75+
config.AddBsUserOptionFlag(sCmd.Cmd)
76+
config.AddBsSnapshotIDOptionFlag(sCmd.Cmd)
77+
config.AddBsPathOptionFlag(sCmd.Cmd)
78+
config.AddBsSnapshotFailedOptionFlag(sCmd.Cmd)
79+
}
80+
81+
func (sCmd *SnapShotCommand) Init(cmd *cobra.Command, args []string) error {
82+
snapshotAddrs, err := config.GetBsSnapshotAddrSlice(sCmd.Cmd)
83+
if err.TypeCode() != cmderror.CODE_SUCCESS {
84+
sCmd.Error = err
85+
return err.ToError()
86+
}
87+
sCmd.snapshotAddrs = snapshotAddrs
88+
sCmd.timeout = config.GetFlagDuration(sCmd.Cmd, config.HTTPTIMEOUT)
89+
sCmd.user = config.GetBsFlagString(sCmd.Cmd, config.CURVEBS_USER)
90+
sCmd.file = config.GetBsFlagString(sCmd.Cmd, config.CURVEBS_PATH)
91+
sCmd.uuid = config.GetBsFlagString(sCmd.Cmd, config.CURVEBS_SNAPSHOT_ID)
92+
sCmd.failed = config.GetBsFlagBool(sCmd.Cmd, config.CURVEBS_SNAPSHOT_FAILED)
93+
header := []string{
94+
cobrautil.ROW_SNAPSHOT_ID,
95+
cobrautil.ROW_SNAPSHOT_NAME,
96+
cobrautil.ROW_RESULT,
97+
cobrautil.ROW_REASON,
98+
}
99+
sCmd.TableNew.SetAutoMergeCellsByColumnIndex(cobrautil.GetIndexSlice(
100+
sCmd.Header, []string{cobrautil.ROW_FILE},
101+
))
102+
sCmd.SetHeader(header)
103+
return nil
104+
}
105+
106+
func (sCmd *SnapShotCommand) Print(cmd *cobra.Command, args []string) error {
107+
return output.FinalCmdOutput(&sCmd.FinalCurveCmd, sCmd)
108+
}
109+
110+
func (sCmd *SnapShotCommand) RunCommand(cmd *cobra.Command, args []string) error {
111+
params := map[string]any{
112+
snapshotutil.QueryAction: snapshotutil.ActionGetFileSnapshotList,
113+
snapshotutil.QueryUser: sCmd.user,
114+
snapshotutil.QueryFile: sCmd.file,
115+
snapshotutil.QueryLimit: snapshotutil.Limit,
116+
snapshotutil.QueryOffset: snapshotutil.Offset,
117+
}
118+
if sCmd.failed {
119+
params[snapshotutil.QueryStatus] = snapshotutil.ErrSnaphshot
120+
}
121+
if sCmd.uuid != snapshotutil.DefaultSnapID {
122+
params[snapshotutil.QueryUUID] = sCmd.uuid
123+
}
124+
snapshotsList, err := listSnapshot.ListSnapShot(sCmd.snapshotAddrs, sCmd.timeout, params)
125+
if err != nil {
126+
sCmd.Error = err
127+
return sCmd.Error.ToError()
128+
}
129+
rows := make([]map[string]string, 0)
130+
if len(snapshotsList) == 0 {
131+
rows = append(rows, stopSnapShot.EmptyOutPut())
132+
} else {
133+
for _, snapshot := range snapshotsList {
134+
row := make(map[string]string)
135+
err := DeleteSnapShot(sCmd.snapshotAddrs, sCmd.timeout, snapshot)
136+
row[cobrautil.ROW_SNAPSHOT_ID] = snapshot.UUID
137+
row[cobrautil.ROW_SNAPSHOT_NAME] = snapshot.Name
138+
if err.TypeCode() == cmderror.CODE_SUCCESS {
139+
row[cobrautil.ROW_RESULT] = cobrautil.ROW_VALUE_SUCCESS
140+
row[cobrautil.ROW_REASON] = "null"
141+
} else {
142+
row[cobrautil.ROW_RESULT] = cobrautil.ROW_VALUE_FAILED
143+
row[cobrautil.ROW_REASON] = err.ToError().Error()
144+
}
145+
rows = append(rows, row)
146+
}
147+
}
148+
list := cobrautil.ListMap2ListSortByKeys(rows, sCmd.Header, []string{cobrautil.ROW_SNAPSHOT_NAME, cobrautil.ROW_SNAPSHOT_ID})
149+
var emptyList [][]string = [][]string{}
150+
if len(snapshotsList) == 0 {
151+
sCmd.TableNew.AppendBulk(emptyList)
152+
sCmd.Result = rows
153+
sCmd.Error = cmderror.Success()
154+
return nil
155+
}
156+
sCmd.TableNew.AppendBulk(list)
157+
sCmd.Result = rows
158+
sCmd.Error = cmderror.Success()
159+
return nil
160+
}
161+
162+
func (sCmd *SnapShotCommand) ResultPlainOutput() error {
163+
return output.FinalCmdOutputPlain(&sCmd.FinalCurveCmd)
164+
}
165+
166+
func DeleteSnapShot(addrs []string, timeout time.Duration, snapshot *snapshotutil.SnapshotInfo) *cmderror.CmdError {
167+
params := map[string]any{
168+
snapshotutil.QueryAction: snapshotutil.ActionDeleteSnapshot,
169+
snapshotutil.QueryUser: snapshot.User,
170+
snapshotutil.QueryUUID: snapshot.UUID,
171+
snapshotutil.QueryFile: snapshot.File,
172+
}
173+
subUri := snapshotutil.NewQuerySubUri(params)
174+
metric := basecmd.NewMetric(addrs, subUri, timeout)
175+
_, err := basecmd.QueryMetric(metric)
176+
return err
177+
}

tools-v2/pkg/cli/command/curvebs/stop/snapshot/snapshot.go

+35-10
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,11 @@ func (sCmd *SnapShotCommand) Init(cmd *cobra.Command, args []string) error {
8989
cobrautil.ROW_SNAPSHOT_ID,
9090
cobrautil.ROW_SNAPSHOT_NAME,
9191
cobrautil.ROW_RESULT,
92+
cobrautil.ROW_REASON,
9293
}
94+
sCmd.TableNew.SetAutoMergeCellsByColumnIndex(cobrautil.GetIndexSlice(
95+
sCmd.Header, []string{cobrautil.ROW_FILE},
96+
))
9397
sCmd.SetHeader(header)
9498
return nil
9599
}
@@ -115,17 +119,23 @@ func (sCmd *SnapShotCommand) RunCommand(cmd *cobra.Command, args []string) error
115119
return sCmd.Error.ToError()
116120
}
117121
rows := make([]map[string]string, 0)
118-
for _, snapshot := range snapshotsList {
119-
row := make(map[string]string)
120-
err := StopSnapShot(sCmd.snapshotAddrs, sCmd.timeout, snapshot)
121-
row[cobrautil.ROW_SNAPSHOT_ID] = snapshot.UUID
122-
row[cobrautil.ROW_SNAPSHOT_NAME] = snapshot.Name
123-
if err.TypeCode() == cmderror.CODE_SUCCESS {
124-
row[cobrautil.ROW_RESULT] = cobrautil.ROW_VALUE_SUCCESS
125-
} else {
126-
row[cobrautil.ROW_RESULT] = cobrautil.ROW_VALUE_FAILED
122+
if len(snapshotsList) == 0 {
123+
rows = append(rows, EmptyOutPut())
124+
} else {
125+
for _, snapshot := range snapshotsList {
126+
row := make(map[string]string)
127+
err := StopSnapShot(sCmd.snapshotAddrs, sCmd.timeout, snapshot)
128+
row[cobrautil.ROW_SNAPSHOT_ID] = snapshot.UUID
129+
row[cobrautil.ROW_SNAPSHOT_NAME] = snapshot.Name
130+
if err.TypeCode() == cmderror.CODE_SUCCESS {
131+
row[cobrautil.ROW_RESULT] = cobrautil.ROW_VALUE_SUCCESS
132+
row[cobrautil.ROW_REASON] = "null"
133+
} else {
134+
row[cobrautil.ROW_RESULT] = cobrautil.ROW_VALUE_FAILED
135+
row[cobrautil.ROW_REASON] = err.ToError().Error()
136+
}
137+
rows = append(rows, row)
127138
}
128-
rows = append(rows, row)
129139
}
130140
list := cobrautil.ListMap2ListSortByKeys(rows, sCmd.Header, []string{cobrautil.ROW_SNAPSHOT_NAME, cobrautil.ROW_SNAPSHOT_ID})
131141
sCmd.TableNew.AppendBulk(list)
@@ -150,3 +160,18 @@ func StopSnapShot(addrs []string, timeout time.Duration, snapshot *snapshotutil.
150160
_, err := basecmd.QueryMetric(metric)
151161
return err
152162
}
163+
164+
func EmptyOutPut() map[string]string {
165+
emptyResult := "-"
166+
keys := []string{
167+
cobrautil.ROW_SNAPSHOT_ID,
168+
cobrautil.ROW_SNAPSHOT_NAME,
169+
cobrautil.ROW_RESULT,
170+
cobrautil.ROW_REASON,
171+
}
172+
row := make(map[string]string)
173+
for _, key := range keys {
174+
row[key] = emptyResult
175+
}
176+
return row
177+
}

0 commit comments

Comments
 (0)