-
Notifications
You must be signed in to change notification settings - Fork 526
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] tools-v2: add bs status volume snapshot command
Signed-off-by: saicaca <[email protected]>
- Loading branch information
Showing
8 changed files
with
308 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
204 changes: 204 additions & 0 deletions
204
tools-v2/pkg/cli/command/curvebs/status/volume/snapshot/snapshot.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
/* | ||
* 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-13 | ||
* Author: saicaca | ||
*/ | ||
|
||
package snapshot | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
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" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
type QueryResponse struct { | ||
Code string `json:"Code"` | ||
Message string `json:"Message"` | ||
RequestId string `json:"RequestId"` | ||
TotalCount int `json:"TotalCount"` | ||
Snapshots []*SnapshotInfoData `json:"Snapshots,omitempty"` | ||
} | ||
|
||
type SnapshotInfoData struct { | ||
UUID string `json:"UUID"` | ||
User string `json:"User"` | ||
FileName string `json:"FileName"` | ||
SnapshotName string `json:"SnapshotName"` | ||
SeqNum uint64 `json:"SeqNum"` | ||
ChunkSize uint32 `json:"ChunkSize"` | ||
SegmentSize uint64 `json:"SegmentSize"` | ||
FileLength uint64 `json:"FileLength"` | ||
Time uint64 `json:"Time"` | ||
Status int32 `json:"Status"` | ||
StripeUnit *uint64 `json:"StripeUnit,omitempty"` | ||
StripeCount *uint64 `json:"StripeCount,omitempty"` | ||
Poolset *string `json:"Poolset,omitempty"` | ||
} | ||
|
||
const ( | ||
SNAPSHOT_QUERY_LIMIT = 100 | ||
SNAPSHOT_QUERY_INITIAL_OFFSET = 0 | ||
SNAPSHOT_QUERY_CODE_SUCCESS = "0" | ||
LENGTH_EMPTY = 0 | ||
) | ||
|
||
const ( | ||
STATUS_DONE = iota | ||
STATUS_IN_PROGRESS | ||
STATUS_DELETING | ||
STATUS_ERROR_DELETING | ||
STATUS_CANCELING | ||
STATUS_ERROR | ||
) | ||
|
||
var statusList = []int{ | ||
STATUS_DONE, | ||
STATUS_IN_PROGRESS, | ||
STATUS_DELETING, | ||
STATUS_ERROR_DELETING, | ||
STATUS_CANCELING, | ||
STATUS_ERROR, | ||
} | ||
|
||
var statusName = map[int]string{ | ||
STATUS_DONE: "done", | ||
STATUS_IN_PROGRESS: "in-progress", | ||
STATUS_DELETING: "deleting", | ||
STATUS_ERROR_DELETING: "errorDeleting", | ||
STATUS_CANCELING: "canceling", | ||
STATUS_ERROR: "error", | ||
} | ||
|
||
type SnapshotCommand struct { | ||
basecmd.FinalCurveCmd | ||
|
||
snapshotAddrs []string | ||
timeout time.Duration | ||
user string | ||
filename string | ||
|
||
errCode int | ||
health cobrautil.ClUSTER_HEALTH_STATUS | ||
} | ||
|
||
var _ basecmd.FinalCurveCmdFunc = (*SnapshotCommand)(nil) // check interface | ||
|
||
func NewSnapshotCommand() *cobra.Command { | ||
return NewVolumeSnapshotCommand().Cmd | ||
} | ||
|
||
func NewVolumeSnapshotCommand() *SnapshotCommand { | ||
vsCmd := &SnapshotCommand{ | ||
FinalCurveCmd: basecmd.FinalCurveCmd{ | ||
Use: "snapshot", | ||
Short: "show the snapshot status", | ||
}, | ||
} | ||
basecmd.NewFinalCurveCli(&vsCmd.FinalCurveCmd, vsCmd) | ||
return vsCmd | ||
} | ||
|
||
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.filename = config.GetBsFlagString(sCmd.Cmd, config.CURVEBS_FILENAME) | ||
sCmd.user = config.GetBsFlagString(sCmd.Cmd, config.CURVEBS_USER) | ||
sCmd.timeout = config.GetFlagDuration(sCmd.Cmd, config.HTTPTIMEOUT) | ||
|
||
header := []string{cobrautil.ROW_STATUS, cobrautil.ROW_COUNT} | ||
sCmd.SetHeader(header) | ||
|
||
return nil | ||
} | ||
|
||
func (sCmd *SnapshotCommand) RunCommand(cmd *cobra.Command, args []string) error { | ||
params := map[string]any{ | ||
cobrautil.QueryAction: cobrautil.ActionGetFileSnapshotList, | ||
cobrautil.QueryUser: sCmd.user, | ||
cobrautil.QueryFile: sCmd.filename, | ||
cobrautil.QueryLimit: SNAPSHOT_QUERY_LIMIT, | ||
cobrautil.QueryOffset: SNAPSHOT_QUERY_INITIAL_OFFSET, | ||
} | ||
count := make(map[int]int) | ||
for { | ||
subUri := cobrautil.NewSnapshotQuerySubUri(params) | ||
|
||
metric := basecmd.NewMetric(sCmd.snapshotAddrs, subUri, sCmd.timeout) | ||
result, err := basecmd.QueryMetric(metric) | ||
if err.TypeCode() != cmderror.CODE_SUCCESS { | ||
return err.ToError() | ||
} | ||
|
||
var resp QueryResponse | ||
if err := json.Unmarshal([]byte(result), &resp); err != nil { | ||
return err | ||
} | ||
if resp.Code != SNAPSHOT_QUERY_CODE_SUCCESS { | ||
return fmt.Errorf("get clone list fail, error code: %s", resp.Code) | ||
} | ||
if len(resp.Snapshots) == LENGTH_EMPTY { | ||
break | ||
} | ||
for _, s := range resp.Snapshots { | ||
count[int(s.Status)] += 1 | ||
} | ||
|
||
params[cobrautil.QueryOffset] = params[cobrautil.QueryOffset].(int) + params[cobrautil.QueryLimit].(int) | ||
} | ||
|
||
rows := make([]map[string]string, 0) | ||
for _, s := range statusList { | ||
rows = append(rows, map[string]string{ | ||
cobrautil.ROW_STATUS: statusName[s], | ||
cobrautil.ROW_COUNT: strconv.Itoa(count[s]), | ||
}) | ||
} | ||
list := cobrautil.ListMap2ListSortByKeys(rows, sCmd.Header, []string{}) | ||
sCmd.TableNew.AppendBulk(list) | ||
sCmd.Result = rows | ||
sCmd.Error = cmderror.ErrSuccess() | ||
|
||
return nil | ||
} | ||
|
||
func (sCmd *SnapshotCommand) Print(cmd *cobra.Command, args []string) error { | ||
return output.FinalCmdOutput(&sCmd.FinalCurveCmd, sCmd) | ||
} | ||
|
||
func (sCmd *SnapshotCommand) ResultPlainOutput() error { | ||
return output.FinalCmdOutputPlain(&sCmd.FinalCurveCmd) | ||
} | ||
|
||
func (sCmd *SnapshotCommand) AddFlags() { | ||
config.AddBsUserOptionFlag(sCmd.Cmd) | ||
config.AddBsSnapshotFilenameFlag(sCmd.Cmd) | ||
config.AddBsSnapshotCloneFlagOption(sCmd.Cmd) | ||
config.AddHttpTimeoutFlag(sCmd.Cmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* 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-13 | ||
* Author: saicaca | ||
*/ | ||
|
||
package volume | ||
|
||
import ( | ||
basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command" | ||
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/status/volume/snapshot" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type VolumeCommand struct { | ||
basecmd.MidCurveCmd | ||
} | ||
|
||
var _ basecmd.MidCurveCmdFunc = (*VolumeCommand)(nil) // check interface | ||
|
||
func (volumeCmd *VolumeCommand) AddSubCommands() { | ||
volumeCmd.Cmd.AddCommand( | ||
snapshot.NewSnapshotCommand(), | ||
) | ||
} | ||
|
||
func NewVolumeCommand() *cobra.Command { | ||
volumeCmd := &VolumeCommand{ | ||
basecmd.MidCurveCmd{ | ||
Use: "volume", | ||
Short: "get the status of curvebs volume", | ||
}, | ||
} | ||
return basecmd.NewMidCurveCli(&volumeCmd.MidCurveCmd, volumeCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters