From b134e8821222c41a03098fe3ca4d7b46ce3e5fae Mon Sep 17 00:00:00 2001 From: ZackSoul <2269713951@qq.com> Date: Fri, 3 Nov 2023 00:30:16 +0800 Subject: [PATCH] add bs stop snapshot Signed-off-by: ZackSoul <2269713951@qq.com> add bs stop snapshot Signed-off-by: ZackSoul <2269713951@qq.com> add bs stop snapshot Signed-off-by: ZackSoul <2269713951@qq.com> add bs stop snapshot Signed-off-by: ZackSoul <2269713951@qq.com> add bs stop snapshot Signed-off-by: ZackSoul <2269713951@qq.com> add bs stop snapshot Signed-off-by: ZackSoul <2269713951@qq.com> add bs stop snapshot Signed-off-by: ZackSoul <2269713951@qq.com> add bs stop snapshot Signed-off-by: ZackSoul <2269713951@qq.com> --- tools-v2/README.md | 26 +++ tools-v2/pkg/cli/command/curvebs/bs.go | 2 + .../command/curvebs/stop/snapshot/snapshot.go | 152 ++++++++++++++++++ tools-v2/pkg/cli/command/curvebs/stop/stop.go | 50 ++++++ 4 files changed, 230 insertions(+) create mode 100644 tools-v2/pkg/cli/command/curvebs/stop/snapshot/snapshot.go create mode 100644 tools-v2/pkg/cli/command/curvebs/stop/stop.go diff --git a/tools-v2/README.md b/tools-v2/README.md index cc24dc284f..309ed57734 100644 --- a/tools-v2/README.md +++ b/tools-v2/README.md @@ -95,6 +95,8 @@ A tool for CurveFS & CurveBs. - [check consistency](#check-consistency) - [snapshot](#snapshot) - [snapshot copyset](#snapshot-copyset) + - [stop](#stop) + - [stop snapshot](#stop-snapshot) - [Comparison of old and new commands](#comparison-of-old-and-new-commands) - [curve fs](#curve-fs) - [curve bs](#curve-bs) @@ -1971,6 +1973,30 @@ Output: +----------------+---------+ ``` +#### stop + +##### stop snapshot + +stop snapshot in curve bs + +Usage: + +```shell +curve bs stop snapshot +``` + +Output: + +```shell ++--------------------------------------+--------------+---------+ +| SNAPSHOTID | SNAPSHOTNAME | RESULT | ++--------------------------------------+--------------+---------+ +| 9aa2b4c5-f27b-40a2-82c9-4e0ad6093567 | testsnap | success | ++--------------------------------------+--------------+---------+ +| 0171a33b-17b7-4215-9f00-6d8de2686f77 | testsnap1 | success | ++--------------------------------------+--------------+---------+ +``` + ## Comparison of old and new commands ### curve fs diff --git a/tools-v2/pkg/cli/command/curvebs/bs.go b/tools-v2/pkg/cli/command/curvebs/bs.go index 3fa7fa86c6..c82ca69528 100644 --- a/tools-v2/pkg/cli/command/curvebs/bs.go +++ b/tools-v2/pkg/cli/command/curvebs/bs.go @@ -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" ) @@ -54,6 +55,7 @@ func (bsCmd *CurveBsCommand) AddSubCommands() { clean_recycle.NewCleanRecycleCommand(), check.NewCheckCommand(), snapshot.NewSnapshotCommand(), + stop.NewStopCommand(), ) } diff --git a/tools-v2/pkg/cli/command/curvebs/stop/snapshot/snapshot.go b/tools-v2/pkg/cli/command/curvebs/stop/snapshot/snapshot.go new file mode 100644 index 0000000000..7dd826f392 --- /dev/null +++ b/tools-v2/pkg/cli/command/curvebs/stop/snapshot/snapshot.go @@ -0,0 +1,152 @@ +/* + * 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-02 + * 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 stop snapshot` +) + +type SnapShotCommand struct { + basecmd.FinalCurveCmd + snapshotAddrs []string + timeout time.Duration + + user string + file string + uuid string +} + +var _ basecmd.FinalCurveCmdFunc = (*SnapShotCommand)(nil) + +func NewSnapShotCommand() *cobra.Command { + return NewStopSnapShotCommand().Cmd +} + +func NewStopSnapShotCommand() *SnapShotCommand { + snapShotCommand := &SnapShotCommand{ + FinalCurveCmd: basecmd.FinalCurveCmd{ + Use: "snapshot", + Short: "stop 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) +} + +func (sCmd *SnapShotCommand) 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.file = config.GetBsFlagString(sCmd.Cmd, config.CURVEBS_PATH) + sCmd.uuid = config.GetBsFlagString(sCmd.Cmd, config.CURVEBS_SNAPSHOT_ID) + 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.uuid != "*" { + 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 := StopSnapShot(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 StopSnapShot(addrs []string, timeout time.Duration, snapshot *snapshotutil.SnapshotInfo) *cmderror.CmdError { + params := map[string]any{ + snapshotutil.QueryAction: snapshotutil.ActionCancelSnapshot, + 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 +} diff --git a/tools-v2/pkg/cli/command/curvebs/stop/stop.go b/tools-v2/pkg/cli/command/curvebs/stop/stop.go new file mode 100644 index 0000000000..d4bbb01a93 --- /dev/null +++ b/tools-v2/pkg/cli/command/curvebs/stop/stop.go @@ -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-11-02 + * Author: ZackSoul + */ +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/snapshot" + "github.com/spf13/cobra" +) + +type StopCommand struct { + basecmd.MidCurveCmd +} + +var _ basecmd.MidCurveCmdFunc = (*StopCommand)(nil) + +func (sCmd *StopCommand) AddSubCommands() { + sCmd.Cmd.AddCommand( + snapshot.NewSnapShotCommand(), + ) +} + +func NewStopCommand() *cobra.Command { + sCmd := &StopCommand{ + basecmd.MidCurveCmd{ + Use: "stop", + Short: "stop resources in the curvebs", + }, + } + return basecmd.NewMidCurveCli(&sCmd.MidCurveCmd, sCmd) +}