Skip to content

Commit 35846be

Browse files
baytan0720caoxianfei1
authored andcommitted
[feat]tools-v2: add bs create volume snapshot
Signed-off-by: baytan0720 <[email protected]>
1 parent b184f47 commit 35846be

File tree

8 files changed

+229
-12
lines changed

8 files changed

+229
-12
lines changed

tools-v2/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ A tool for CurveFS & CurveBs.
8989
- [create](#create-1)
9090
- [create file](#create-file)
9191
- [create dir](#create-dir)
92+
- [create volume snapshot](#create-volume-snapshot)
9293
- [check](#check-1)
9394
- [check copyset](#check-copyset-1)
9495
- [check chunkserver](#check-chunkserver)
@@ -1875,6 +1876,24 @@ Output:
18751876
+---------+
18761877
```
18771878

1879+
##### create volume snapshot
1880+
1881+
create snapshot
1882+
1883+
Usage:
1884+
```bash
1885+
curve bs create volume snapshot --user root --filename test --snapshotname snap-test
1886+
```
1887+
1888+
Output:
1889+
```
1890+
+------+----------+--------------+
1891+
| USER | FILENAME | SNAPSHOTNAME |
1892+
+------+----------+---------------
1893+
| root | test | snap-test |
1894+
+------+----------+--------------+
1895+
```
1896+
18781897
#### check
18791898

18801899
##### check copyset

tools-v2/internal/utils/row.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ const (
137137
ROW_ISLAZY = "isLazy"
138138
ROW_NEXTSTEP = "nextStep"
139139
ROW_TIME = "time"
140+
ROW_SNAPSHOTNAME = "snapshotName"
140141

141142
ROW_RW_STATUS = "rwStatus"
142143
ROW_DISK_STATE = "diskState"

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const (
3838
QueryStatus = "Status"
3939
QueryType = "Type"
4040
QueryFile = "File"
41+
QueryName = "Name"
4142

4243
ActionClone = "Clone"
4344
ActionRecover = "Recover"

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/create/cluster"
2828
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/create/dir"
2929
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/create/file"
30+
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/create/volume"
3031
"github.com/spf13/cobra"
3132
)
3233

@@ -41,6 +42,7 @@ func (createCmd *CreateCmd) AddSubCommands() {
4142
cluster.NewClusterTopoCmd(),
4243
dir.NewDirectoryCommand(),
4344
file.NewFileCommand(),
45+
volume.NewVolumeCommand(),
4446
)
4547
}
4648

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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+
* Project: CurveCli
18+
* Created Date: 2023-09-23
19+
* Author: baytan0720
20+
*/
21+
22+
package snapshot
23+
24+
import (
25+
"encoding/json"
26+
"fmt"
27+
"time"
28+
29+
cmderror "github.com/opencurve/curve/tools-v2/internal/error"
30+
cobrautil "github.com/opencurve/curve/tools-v2/internal/utils"
31+
snapshotutil "github.com/opencurve/curve/tools-v2/internal/utils/snapshot"
32+
basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command"
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 create volume snapshot --user root --filename test --snapshotname snap-test`
40+
)
41+
42+
type SnapshotCmd struct {
43+
basecmd.FinalCurveCmd
44+
snapshotAddrs []string
45+
timeout time.Duration
46+
47+
user string
48+
fileName string
49+
snapshotName string
50+
}
51+
52+
var _ basecmd.FinalCurveCmdFunc = (*SnapshotCmd)(nil)
53+
54+
func NewCommand() *cobra.Command {
55+
return NewSnapshotCmd().Cmd
56+
}
57+
58+
func NewSnapshotCmd() *SnapshotCmd {
59+
sCmd := &SnapshotCmd{
60+
FinalCurveCmd: basecmd.FinalCurveCmd{
61+
Use: "snapshot",
62+
Short: "create volume snapshot in curvebs cluster",
63+
Example: snapshotExample,
64+
},
65+
}
66+
basecmd.NewFinalCurveCli(&sCmd.FinalCurveCmd, sCmd)
67+
return sCmd
68+
}
69+
70+
func (sCmd *SnapshotCmd) AddFlags() {
71+
config.AddBsSnapshotCloneFlagOption(sCmd.Cmd)
72+
config.AddHttpTimeoutFlag(sCmd.Cmd)
73+
config.AddBsUserRequiredFlag(sCmd.Cmd)
74+
config.AddBsFileNameRequiredFlag(sCmd.Cmd)
75+
config.AddBsSnapshotNameRequiredFlag(sCmd.Cmd)
76+
}
77+
78+
func (sCmd *SnapshotCmd) Init(cmd *cobra.Command, args []string) error {
79+
snapshotAddrs, err := config.GetBsSnapshotAddrSlice(sCmd.Cmd)
80+
if err.TypeCode() != cmderror.CODE_SUCCESS || len(snapshotAddrs) == 0 {
81+
return err.ToError()
82+
}
83+
sCmd.snapshotAddrs = snapshotAddrs
84+
sCmd.timeout = config.GetFlagDuration(sCmd.Cmd, config.HTTPTIMEOUT)
85+
sCmd.user = config.GetBsFlagString(sCmd.Cmd, config.CURVEBS_USER)
86+
sCmd.fileName = config.GetBsFlagString(sCmd.Cmd, config.CURVEBS_FILENAME)
87+
sCmd.snapshotName = config.GetBsFlagString(sCmd.Cmd, config.CURVEBS_SNAPSHOTNAME)
88+
sCmd.SetHeader([]string{cobrautil.ROW_USER, cobrautil.ROW_FILE_NAME, cobrautil.ROW_SNAPSHOTNAME})
89+
return nil
90+
}
91+
92+
func (sCmd *SnapshotCmd) RunCommand(cmd *cobra.Command, args []string) error {
93+
params := map[string]any{
94+
snapshotutil.QueryAction: snapshotutil.ActionCreateSnapshot,
95+
snapshotutil.QueryUser: sCmd.user,
96+
snapshotutil.QueryFile: sCmd.fileName,
97+
snapshotutil.QueryName: sCmd.snapshotName,
98+
}
99+
subUri := snapshotutil.NewQuerySubUri(params)
100+
metric := basecmd.NewMetric(sCmd.snapshotAddrs, subUri, sCmd.timeout)
101+
result, err := basecmd.QueryMetric(metric)
102+
if err.TypeCode() != cmderror.CODE_SUCCESS {
103+
return err.ToError()
104+
}
105+
resp := snapshotutil.Response{}
106+
if err := json.Unmarshal([]byte(result), &resp); err != nil {
107+
return err
108+
}
109+
row := make(map[string]string)
110+
row[cobrautil.ROW_USER] = sCmd.user
111+
row[cobrautil.ROW_FILE_NAME] = sCmd.fileName
112+
row[cobrautil.ROW_SNAPSHOTNAME] = sCmd.snapshotName
113+
114+
if resp.Code != snapshotutil.ResultSuccess {
115+
return fmt.Errorf("create snapshot fail, requestId: %s, code: %s, error: %s", resp.RequestId, resp.Code, resp.Message)
116+
}
117+
118+
sCmd.TableNew.Append(cobrautil.Map2List(row, sCmd.Header))
119+
sCmd.Result = row
120+
sCmd.Error = cmderror.Success()
121+
return nil
122+
}
123+
124+
func (sCmd *SnapshotCmd) Print(cmd *cobra.Command, args []string) error {
125+
return output.FinalCmdOutput(&sCmd.FinalCurveCmd, sCmd)
126+
}
127+
128+
func (sCmd *SnapshotCmd) ResultPlainOutput() error {
129+
return output.FinalCmdOutputPlain(&sCmd.FinalCurveCmd)
130+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
* Project: CurveCli
18+
* Created Date: 2023-09-23
19+
* Author: baytan0720
20+
*/
21+
22+
package volume
23+
24+
import (
25+
basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command"
26+
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/create/volume/snapshot"
27+
"github.com/spf13/cobra"
28+
)
29+
30+
type VolumeCommand struct {
31+
basecmd.MidCurveCmd
32+
}
33+
34+
var _ basecmd.MidCurveCmdFunc = (*VolumeCommand)(nil) // check interface
35+
36+
func (vCmd *VolumeCommand) AddSubCommands() {
37+
vCmd.Cmd.AddCommand(
38+
snapshot.NewCommand(),
39+
)
40+
}
41+
42+
func NewVolumeCommand() *cobra.Command {
43+
vCmd := &VolumeCommand{
44+
basecmd.MidCurveCmd{
45+
Use: "volume",
46+
Short: "create resources in the curvebs",
47+
},
48+
}
49+
return basecmd.NewMidCurveCli(&vCmd.MidCurveCmd, vCmd)
50+
}

tools-v2/pkg/cli/command/curvebs/update/volume/flatten/flatten.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ func NewFlattenCmd() *FlattenCmd {
6969
func (fCmd *FlattenCmd) AddFlags() {
7070
config.AddBsSnapshotCloneFlagOption(fCmd.Cmd)
7171
config.AddHttpTimeoutFlag(fCmd.Cmd)
72-
config.AddBsUserRequireFlag(fCmd.Cmd)
73-
config.AddBsTaskIDRequireFlag(fCmd.Cmd)
72+
config.AddBsUserRequiredFlag(fCmd.Cmd)
73+
config.AddBsTaskIDRequiredFlag(fCmd.Cmd)
7474
}
7575

7676
func (fCmd *FlattenCmd) Init(cmd *cobra.Command, args []string) error {

tools-v2/pkg/config/bs.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,15 @@ const (
147147
CURVEBS_DEFAULT_SNAPSHOT_ID = "*"
148148
CURVEBS_FAILED = "failed"
149149
VIPER_CURVEBS_FAILED = "curvebs.failed"
150-
CURVEBS_CHUNK_SIZE = "chunksize"
151-
VIPER_CURVEBS_CHUNK_SIZE = "curvebs.chunksize"
152-
CURVEBS_CHECK_HASH = "checkhash"
153-
VIPER_CURVEBS_CHECK_HASH = "curvebs.checkhash"
154-
CURVEBS_DEFAULT_CHECK_HASH = false
150+
CURVEBS_CHUNK_SIZE = "chunksize"
151+
VIPER_CURVEBS_CHUNK_SIZE = "curvebs.chunksize"
152+
CURVEBS_CHECK_HASH = "checkhash"
153+
VIPER_CURVEBS_CHECK_HASH = "curvebs.checkhash"
154+
CURVEBS_DEFAULT_CHECK_HASH = false
155+
CURVEBS_FILENAME = "filename"
156+
VIPER_CURVEBS_FILENAME = "curvebs.filename"
157+
CURVEBS_SNAPSHOTNAME = "snapshotname"
158+
VIPER_CURVEBS_SNAPSHOTNAME = "curvebs.snapshotname"
155159
)
156160

157161
var (
@@ -207,8 +211,10 @@ var (
207211
CURVEBS_TASKID: VIPER_CURVEBS_TASKID,
208212
CURVEBS_SNAPSHOT_ID: VIPER_CURVEBS_SNAPSHOT_ID,
209213
CURVEBS_FAILED: VIPER_CURVEBS_FAILED,
210-
CURVEBS_CHUNK_SIZE: VIPER_CURVEBS_CHUNK_SIZE,
211-
CURVEBS_CHECK_HASH: VIPER_CURVEBS_CHECK_HASH,
214+
CURVEBS_CHUNK_SIZE: VIPER_CURVEBS_CHUNK_SIZE,
215+
CURVEBS_CHECK_HASH: VIPER_CURVEBS_CHECK_HASH,
216+
CURVEBS_FILENAME: VIPER_CURVEBS_FILENAME,
217+
CURVEBS_SNAPSHOTNAME: VIPER_CURVEBS_SNAPSHOTNAME,
212218
}
213219

214220
BSFLAG2DEFAULT = map[string]interface{}{
@@ -232,7 +238,7 @@ var (
232238
CURVEBS_ALL: CURVEBS_DEFAULT_ALL,
233239
CURVEBS_LOGIC_POOL_ID: CURVEBS_DEFAULT_LOGIC_POOL_ID,
234240
CURVEBS_COPYSET_ID: CURVEBS_DEFAULT_COPYSET_ID,
235-
CURVEBS_CHECK_HASH: CURVEBS_DEFAULT_CHECK_HASH,
241+
CURVEBS_CHECK_HASH: CURVEBS_DEFAULT_CHECK_HASH,
236242
CURVEBS_SNAPSHOT_ID: CURVEBS_DEFAULT_SNAPSHOT_ID,
237243
}
238244
)
@@ -663,11 +669,11 @@ func AddBsFailedOptionFlag(cmd *cobra.Command) {
663669
AddBsBoolOptionFlag(cmd, CURVEBS_FAILED, "failed")
664670
}
665671

666-
func AddBsUserRequireFlag(cmd *cobra.Command) {
672+
func AddBsUserRequiredFlag(cmd *cobra.Command) {
667673
AddBsStringRequiredFlag(cmd, CURVEBS_USER, "user name")
668674
}
669675

670-
func AddBsTaskIDRequireFlag(cmd *cobra.Command) {
676+
func AddBsTaskIDRequiredFlag(cmd *cobra.Command) {
671677
AddBsStringRequiredFlag(cmd, CURVEBS_TASKID, "task id")
672678
}
673679

@@ -679,6 +685,14 @@ func AddBsTaskTypeOptionFlag(cmd *cobra.Command) {
679685
AddBsStringOptionFlag(cmd, CURVEBS_TYPE, "only query target type (clone or recover)")
680686
}
681687

688+
func AddBsFileNameRequiredFlag(cmd *cobra.Command) {
689+
AddBsStringRequiredFlag(cmd, CURVEBS_FILENAME, "file name")
690+
}
691+
692+
func AddBsSnapshotNameRequiredFlag(cmd *cobra.Command) {
693+
AddBsStringRequiredFlag(cmd, CURVEBS_SNAPSHOTNAME, "snapshot name")
694+
}
695+
682696
// get stingslice flag
683697
func GetBsFlagStringSlice(cmd *cobra.Command, flagName string) []string {
684698
var value []string

0 commit comments

Comments
 (0)