Skip to content

Commit

Permalink
resolve the conflict
Browse files Browse the repository at this point in the history
Signed-off-by: ZackSoul <[email protected]>
  • Loading branch information
ZackSoul committed Dec 26, 2023
2 parents 30f1615 + 35846be commit c0af5fe
Show file tree
Hide file tree
Showing 8 changed files with 242 additions and 4 deletions.
19 changes: 19 additions & 0 deletions tools-v2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ A tool for CurveFS & CurveBs.
- [create](#create-1)
- [create file](#create-file)
- [create dir](#create-dir)
- [create volume snapshot](#create-volume-snapshot)
- [check](#check-1)
- [check copyset](#check-copyset-1)
- [check chunkserver](#check-chunkserver)
Expand Down Expand Up @@ -1898,6 +1899,24 @@ Output:
+---------+
```

##### create volume snapshot

create snapshot

Usage:
```bash
curve bs create volume snapshot --user root --filename test --snapshotname snap-test
```

Output:
```
+------+----------+--------------+
| USER | FILENAME | SNAPSHOTNAME |
+------+----------+---------------
| root | test | snap-test |
+------+----------+--------------+
```

#### check

##### check copyset
Expand Down
1 change: 1 addition & 0 deletions tools-v2/internal/utils/row.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ const (
ROW_ISLAZY = "isLazy"
ROW_NEXTSTEP = "nextStep"
ROW_TIME = "time"
ROW_SNAPSHOTNAME = "snapshotName"

ROW_RW_STATUS = "rwStatus"
ROW_DISK_STATE = "diskState"
Expand Down
1 change: 1 addition & 0 deletions tools-v2/internal/utils/snapshot/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const (
QueryStatus = "Status"
QueryType = "Type"
QueryFile = "File"
QueryName = "Name"

ActionClone = "Clone"
ActionRecover = "Recover"
Expand Down
2 changes: 2 additions & 0 deletions tools-v2/pkg/cli/command/curvebs/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"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/opencurve/curve/tools-v2/pkg/cli/command/curvebs/create/volume"
"github.com/spf13/cobra"
)

Expand All @@ -41,6 +42,7 @@ func (createCmd *CreateCmd) AddSubCommands() {
cluster.NewClusterTopoCmd(),
dir.NewDirectoryCommand(),
file.NewFileCommand(),
volume.NewVolumeCommand(),
)
}

Expand Down
130 changes: 130 additions & 0 deletions tools-v2/pkg/cli/command/curvebs/create/volume/snapshot/snapshot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* 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-09-23
* Author: baytan0720
*/

package snapshot

import (
"encoding/json"
"fmt"
"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"
"github.com/opencurve/curve/tools-v2/pkg/config"
"github.com/opencurve/curve/tools-v2/pkg/output"
"github.com/spf13/cobra"
)

const (
snapshotExample = `$ curve bs create volume snapshot --user root --filename test --snapshotname snap-test`
)

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

user string
fileName string
snapshotName string
}

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

func NewCommand() *cobra.Command {
return NewSnapshotCmd().Cmd
}

func NewSnapshotCmd() *SnapshotCmd {
sCmd := &SnapshotCmd{
FinalCurveCmd: basecmd.FinalCurveCmd{
Use: "snapshot",
Short: "create volume snapshot in curvebs cluster",
Example: snapshotExample,
},
}
basecmd.NewFinalCurveCli(&sCmd.FinalCurveCmd, sCmd)
return sCmd
}

func (sCmd *SnapshotCmd) AddFlags() {
config.AddBsSnapshotCloneFlagOption(sCmd.Cmd)
config.AddHttpTimeoutFlag(sCmd.Cmd)
config.AddBsUserRequiredFlag(sCmd.Cmd)
config.AddBsFileNameRequiredFlag(sCmd.Cmd)
config.AddBsSnapshotNameRequiredFlag(sCmd.Cmd)
}

func (sCmd *SnapshotCmd) Init(cmd *cobra.Command, args []string) error {
snapshotAddrs, err := config.GetBsSnapshotAddrSlice(sCmd.Cmd)
if err.TypeCode() != cmderror.CODE_SUCCESS || len(snapshotAddrs) == 0 {
return err.ToError()
}
sCmd.snapshotAddrs = snapshotAddrs
sCmd.timeout = config.GetFlagDuration(sCmd.Cmd, config.HTTPTIMEOUT)
sCmd.user = config.GetBsFlagString(sCmd.Cmd, config.CURVEBS_USER)
sCmd.fileName = config.GetBsFlagString(sCmd.Cmd, config.CURVEBS_FILENAME)
sCmd.snapshotName = config.GetBsFlagString(sCmd.Cmd, config.CURVEBS_SNAPSHOTNAME)
sCmd.SetHeader([]string{cobrautil.ROW_USER, cobrautil.ROW_FILE_NAME, cobrautil.ROW_SNAPSHOTNAME})
return nil
}

func (sCmd *SnapshotCmd) RunCommand(cmd *cobra.Command, args []string) error {
params := map[string]any{
snapshotutil.QueryAction: snapshotutil.ActionCreateSnapshot,
snapshotutil.QueryUser: sCmd.user,
snapshotutil.QueryFile: sCmd.fileName,
snapshotutil.QueryName: sCmd.snapshotName,
}
subUri := snapshotutil.NewQuerySubUri(params)
metric := basecmd.NewMetric(sCmd.snapshotAddrs, subUri, sCmd.timeout)
result, err := basecmd.QueryMetric(metric)
if err.TypeCode() != cmderror.CODE_SUCCESS {
return err.ToError()
}
resp := snapshotutil.Response{}
if err := json.Unmarshal([]byte(result), &resp); err != nil {
return err
}
row := make(map[string]string)
row[cobrautil.ROW_USER] = sCmd.user
row[cobrautil.ROW_FILE_NAME] = sCmd.fileName
row[cobrautil.ROW_SNAPSHOTNAME] = sCmd.snapshotName

if resp.Code != snapshotutil.ResultSuccess {
return fmt.Errorf("create snapshot fail, requestId: %s, code: %s, error: %s", resp.RequestId, resp.Code, resp.Message)
}

sCmd.TableNew.Append(cobrautil.Map2List(row, sCmd.Header))
sCmd.Result = row
sCmd.Error = cmderror.Success()
return nil
}

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

func (sCmd *SnapshotCmd) ResultPlainOutput() error {
return output.FinalCmdOutputPlain(&sCmd.FinalCurveCmd)
}
50 changes: 50 additions & 0 deletions tools-v2/pkg/cli/command/curvebs/create/volume/volume.go
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-09-23
* Author: baytan0720
*/

package volume

import (
basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/create/volume/snapshot"
"github.com/spf13/cobra"
)

type VolumeCommand struct {
basecmd.MidCurveCmd
}

var _ basecmd.MidCurveCmdFunc = (*VolumeCommand)(nil) // check interface

func (vCmd *VolumeCommand) AddSubCommands() {
vCmd.Cmd.AddCommand(
snapshot.NewCommand(),
)
}

func NewVolumeCommand() *cobra.Command {
vCmd := &VolumeCommand{
basecmd.MidCurveCmd{
Use: "volume",
Short: "create resources in the curvebs",
},
}
return basecmd.NewMidCurveCli(&vCmd.MidCurveCmd, vCmd)
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ func NewFlattenCmd() *FlattenCmd {
func (fCmd *FlattenCmd) AddFlags() {
config.AddBsSnapshotCloneFlagOption(fCmd.Cmd)
config.AddHttpTimeoutFlag(fCmd.Cmd)
config.AddBsUserRequireFlag(fCmd.Cmd)
config.AddBsTaskIDRequireFlag(fCmd.Cmd)
config.AddBsUserRequiredFlag(fCmd.Cmd)
config.AddBsTaskIDRequiredFlag(fCmd.Cmd)
}

func (fCmd *FlattenCmd) Init(cmd *cobra.Command, args []string) error {
Expand Down
39 changes: 37 additions & 2 deletions tools-v2/pkg/config/bs.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ const (
CURVEBS_CHECK_HASH = "checkhash"
VIPER_CURVEBS_CHECK_HASH = "curvebs.checkhash"
CURVEBS_DEFAULT_CHECK_HASH = false
CURVEBS_FILENAME = "filename"
VIPER_CURVEBS_FILENAME = "curvebs.filename"
CURVEBS_SNAPSHOTNAME = "snapshotname"
VIPER_CURVEBS_SNAPSHOTNAME = "curvebs.snapshotname"
)

var (
Expand Down Expand Up @@ -213,6 +217,8 @@ var (
CURVEBS_CHUNK_SIZE: VIPER_CURVEBS_CHUNK_SIZE,
CURVEBS_CHECK_HASH: VIPER_CURVEBS_CHECK_HASH,
CURVEBS_SNAPSHOT_FAILED: VIPER_CURVEBS_SNAPSHOT_FAILED,
CURVEBS_FILENAME: VIPER_CURVEBS_FILENAME,
CURVEBS_SNAPSHOTNAME: VIPER_CURVEBS_SNAPSHOTNAME,
}

BSFLAG2DEFAULT = map[string]interface{}{
Expand All @@ -239,6 +245,27 @@ var (
CURVEBS_CHECK_HASH: CURVEBS_DEFAULT_CHECK_HASH,
CURVEBS_SNAPSHOT_ID: CURVEBS_DEFAULT_SNAPSHOT_ID,
CURVEBS_SNAPSHOT_FAILED: CURVEBS_DEFAULT_SNAPSHOT_FAILED,
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,
}
)

Expand Down Expand Up @@ -668,11 +695,11 @@ func AddBsFailedOptionFlag(cmd *cobra.Command) {
AddBsBoolOptionFlag(cmd, CURVEBS_FAILED, "failed")
}

func AddBsUserRequireFlag(cmd *cobra.Command) {
func AddBsUserRequiredFlag(cmd *cobra.Command) {
AddBsStringRequiredFlag(cmd, CURVEBS_USER, "user name")
}

func AddBsTaskIDRequireFlag(cmd *cobra.Command) {
func AddBsTaskIDRequiredFlag(cmd *cobra.Command) {
AddBsStringRequiredFlag(cmd, CURVEBS_TASKID, "task id")
}

Expand All @@ -688,6 +715,14 @@ func AddBsTaskTypeOptionFlag(cmd *cobra.Command) {
AddBsStringOptionFlag(cmd, CURVEBS_TYPE, "only query target type (clone or recover)")
}

func AddBsFileNameRequiredFlag(cmd *cobra.Command) {
AddBsStringRequiredFlag(cmd, CURVEBS_FILENAME, "file name")
}

func AddBsSnapshotNameRequiredFlag(cmd *cobra.Command) {
AddBsStringRequiredFlag(cmd, CURVEBS_SNAPSHOTNAME, "snapshot name")
}

// get stingslice flag
func GetBsFlagStringSlice(cmd *cobra.Command, flagName string) []string {
var value []string
Expand Down

0 comments on commit c0af5fe

Please sign in to comment.