Skip to content

Commit

Permalink
fix the dco & conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
kai2321 committed Oct 11, 2023
1 parent baa4f94 commit 82e6e7e
Show file tree
Hide file tree
Showing 7 changed files with 337 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tools-v2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,15 @@ A tool for CurveFS & CurveBs.
- [update copyset availflag](#update-copyset-availflag)
- [update leader-schedule](#update-leader-schedule)
- [create](#create-1)

- [create file](#create-file)
- [create dir](#create-dir)
- [check](#check-1)
- [check copyset](#check-copyset-1)
- [check chunkserver](#check-chunkserver)
- [check server](#check-server)
- [stop](#stop)
- [stop volume snapshot](#stop-volume-snapshot)
- [snapshot](#snapshot)
- [snapshot copyset](#snapshot-copyset)
- [Comparison of old and new commands](#comparison-of-old-and-new-commands)
Expand Down Expand Up @@ -1871,6 +1874,29 @@ Output:
+--------+-----------+-------+------------------+
```

#### stop

##### stop volume snapshot

stop volume snapshot

Usage:

```shell
curve bs stop volumeSnapshot
```

Output:

```
+--------------------------------------+--------+
| ID | RESULT |
+--------------------------------------+--------+
| 4f46d5c5-10cb-4542-adea-1062d9c39018 | success |
+--------------------------------------+--------+
| d938db41-fb52-4d53-85ee-d667c9653dfb | success |
```

#### snapshot

##### snapshot copyset
Expand Down
6 changes: 6 additions & 0 deletions tools-v2/internal/error/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,12 @@ var (
ErrListWarmup = func() *CmdError {
return NewInternalCmdError(74, "list warmup progress fail, err: %s")
}
ErrBsGetSnapShotListResult = func() *CmdError {
return NewInternalCmdError(75, "get snapshot list results fail, err: %s")
}
ErrBsEligibleSnapShot = func() *CmdError {
return NewInternalCmdError(76, "no eligible snapshot")
}

// http error
ErrHttpUnreadableResult = func() *CmdError {
Expand Down
2 changes: 2 additions & 0 deletions tools-v2/pkg/cli/command/curvebs/bs.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/query"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/snapshot"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/status"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/stop"
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/update"
)

Expand All @@ -54,6 +55,7 @@ func (bsCmd *CurveBsCommand) AddSubCommands() {
clean_recycle.NewCleanRecycleCommand(),
check.NewCheckCommand(),
snapshot.NewSnapshotCommand(),
stop.NewStopCommand(),
)
}

Expand Down
29 changes: 29 additions & 0 deletions tools-v2/pkg/cli/command/curvebs/stop/stop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package stop

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

type StopCommand struct {
basecmd.MidCurveCmd
}

var _ basecmd.MidCurveCmdFunc = (*StopCommand)(nil)

func (sCmd *StopCommand) AddSubCommands() {
sCmd.Cmd.AddCommand(
snapshot.NewStopVolumeSnapshotCommand(),
)
}

func NewStopCommand() *cobra.Command {
sCmd := &StopCommand{
basecmd.MidCurveCmd{
Use: "stop",
Short: "stop volume snapshot in the curvebs",
},
}
return basecmd.NewMidCurveCli(&sCmd.MidCurveCmd, sCmd)
}
170 changes: 170 additions & 0 deletions tools-v2/pkg/cli/command/curvebs/stop/volumeSnapshot/stop_volume.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package volumeSnapshot

import (
"encoding/json"
"fmt"
"strings"
"time"

cmderror "github.com/opencurve/curve/tools-v2/internal/error"
basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command"
)

const (
VERSION = "0.0.6"
LIMIT = "100"
OFFSET = "0"
)

type Stop struct {
serverAddress []string
timeout time.Duration

User string
FileName string
UUID string

messages []string
}

func newStopSnapShot(serverAddress []string, timeout time.Duration, user, fileName, uuid string) *Stop {
return &Stop{
serverAddress: serverAddress,
timeout: timeout,
User: user,
FileName: fileName,
UUID: uuid,
}
}

type Record struct {
UUID string `json:"UUID"`
User string `json:"User"`
File string `json:"File"`

Result string `json:"Result"`
}

func (s *Stop) queryStopBy() ([]*Record, *cmderror.CmdError) {
records, err := s.getSnapShotListAll()
return records, err
}

func (s *Stop) getSnapShotListAll() ([]*Record, *cmderror.CmdError) {
var receiveRecords []*Record
params := QueryParams{
Action: "GetFileSnapshotList",
Version: VERSION,
User: s.User,
UUID: s.UUID,
File: s.FileName,
Limit: LIMIT,
Offset: OFFSET,
}
for {
records, err := s.getSnapShotList(params)
if err != nil || len(records) == 0 || records == nil {
return receiveRecords, err
}
receiveRecords = append(receiveRecords, records...)

params.Offset = params.Offset + params.Limit
}
}

type QueryParams struct {
Action string `json:"Action"`
Version string `json:"Version"`
User string `json:"User"`
File string `json:"File"`
UUID string `json:"UUID"`
Limit string `json:"limit"`
Offset string `json:"Offset"`
}

func (s *Stop) getSnapShotList(params QueryParams) ([]*Record, *cmderror.CmdError) {
var resp struct {
Code string
Message string
RequestId string
TotalCount int
SnapShots []*Record
}
err := s.query(params, &resp)
if err != nil || resp.Code != "0" {
return resp.SnapShots, err
}
return resp.SnapShots, nil
}

func (s *Stop) query(params QueryParams, data interface{}) *cmderror.CmdError {
encodedParams := s.encodeParam(params)

subUri := fmt.Sprintf("/SnapshotCloneService?%s", encodedParams)

metric := basecmd.NewMetric(s.serverAddress, subUri, s.timeout)

result, err := basecmd.QueryMetric(metric)
if err.TypeCode() != cmderror.CODE_SUCCESS {
return err
}

if err := json.Unmarshal([]byte(result), &data); err != nil {
retErr := cmderror.ErrUnmarshalJson()
retErr.Format(err.Error())
return retErr
}

return nil
}

func (s *Stop) encodeParam(params QueryParams) string {
paramsMap := map[string]string{}
if params.Action == "CancelSnapshot" {
paramsMap = map[string]string{
"Action": params.Action,
"Version": params.Version,
"User": params.User,
"UUID": params.UUID,
"File": params.File,
}
} else {
paramsMap = map[string]string{
"Action": params.Action,
"Version": params.Version,
"User": params.User,
"UUID": params.UUID,
"File": params.File,
"Limit": params.Limit,
"Offset": params.Offset,
}
}

values := strings.Builder{}
for key, value := range paramsMap {
if value != "" {
values.WriteString(key)
values.WriteString("=")
values.WriteString(value)
values.WriteString("&")
}
}
str := values.String()
return str[:len(str)-1]
}

func (s *Stop) stopSnapShot(uuid, user, file string) *cmderror.CmdError {
params := QueryParams{
Action: "CancelSnapshot",
Version: VERSION,
User: user,
UUID: uuid,
File: file,
}

err := s.query(params, nil)
if err != nil {
return err
}
return err
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package volumeSnapshot

import (
"time"

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 (
stopExample = `$ curve bs stop volumeSnapshot`
)

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

user string
fileName string
uuid string
}

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

func (sCmd *StopCmd) 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_PATH)
sCmd.uuid = config.GetBsFlagString(sCmd.Cmd, config.CURVEBS_SNAPSHOTSEQID)
header := []string{cobrautil.ROW_ID, cobrautil.ROW_RESULT}
sCmd.SetHeader(header)
return nil
}

func (sCmd *StopCmd) RunCommand(cmd *cobra.Command, args []string) error {
s := newStopSnapShot(sCmd.snapshotAddrs, sCmd.timeout, sCmd.user, sCmd.fileName, sCmd.uuid)
records, err := s.queryStopBy()
if err != nil {
sCmd.Error = err
return sCmd.Error.ToError()
}
if records == nil || len(records) == 0 {
sCmd.Result = cobrautil.ROW_VALUE_FAILED
sCmd.Error = cmderror.ErrBsEligibleSnapShot()
return sCmd.Error.ToError()
}
for _, item := range records {
err := s.stopSnapShot(item.UUID, item.User, item.File)
if err.TypeCode() == cmderror.CODE_SUCCESS {
item.Result = cobrautil.ROW_VALUE_SUCCESS
} else {
item.Result = cobrautil.ROW_VALUE_FAILED
}
sCmd.TableNew.Append([]string{item.UUID, item.Result})
}
sCmd.Result = records
sCmd.Error = cmderror.Success()
return nil
}

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

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

func (sCmd *StopCmd) AddFlags() {
config.AddBsSnapshotCloneFlagOption(sCmd.Cmd)
config.AddHttpTimeoutFlag(sCmd.Cmd)
config.AddBsUserOptionFlag(sCmd.Cmd)
config.AddBsSnapshotSeqIDOptionFlag(sCmd.Cmd)
config.AddBsPathOptionFlag(sCmd.Cmd)
}

func NewStopVolumeSnapshotCommand() *cobra.Command {
sCmd := &StopCmd{
FinalCurveCmd: basecmd.FinalCurveCmd{
Use: "volumeSnapshot",
Short: "stop volume snapshot in curvebs cluster",
Example: stopExample,
},
}
basecmd.NewFinalCurveCli(&sCmd.FinalCurveCmd, sCmd)
return sCmd.Cmd
}
Loading

0 comments on commit 82e6e7e

Please sign in to comment.