Skip to content

Commit

Permalink
[feat]tools-v2: add snapshot utils
Browse files Browse the repository at this point in the history
Signed-off-by: baytan0720 <[email protected]>
  • Loading branch information
baytan0720 authored and Cyber-SiKu committed Sep 20, 2023
1 parent 8adae43 commit 862d8b0
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 229 deletions.
69 changes: 69 additions & 0 deletions tools-v2/internal/utils/snapshot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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-16
* Author: baytan0720
*/

package cobrautil

import (
"fmt"
"net/url"
)

const (
Version = "0.0.6"
TypeCloneTask = "0"
TypeRecoverTask = "1"

QueryAction = "Action"
QueryVersion = "Version"
QueryUser = "User"
QueryUUID = "UUID"
QuerySource = "Source"
QueryDestination = "Destination"
QueryLimit = "Limit"
QueryOffset = "Offset"
QueryStatus = "Status"
QueryType = "Type"

ActionClone = "Clone"
ActionRecover = "Recover"
ActionFlatten = "Flatten"
ActionCreateSnapshot = "CreateSnapshot"
ActionDeleteSnapshot = "DeleteSnapshot"
ActionCancelSnapshot = "CancelSnapshot"
ActionCleanCloneTask = "CleanCloneTask"
ActionGetCloneTaskList = "GetCloneTaskList"
ActionGetFileSnapshotList = "GetFileSnapshotList"
ActionGetFileSnapshotInfo = "GetFileSnapshotInfo"
)

func NewSnapshotQuerySubUri(params map[string]any) string {
values := url.Values{}

values.Add(QueryVersion, Version)
for key, value := range params {
if value != "" {
values.Add(key, fmt.Sprintf("%s", value))
}
}

return "/SnapshotCloneService?" + values.Encode()
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
* Author: setcy
*/

package volume
package clone

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

"github.com/spf13/cobra"
Expand All @@ -48,6 +51,7 @@ type CloneCmd struct {
taskID string
all bool
failed bool
status string
}

var _ basecmd.FinalCurveCmdFunc = (*CloneCmd)(nil)
Expand All @@ -65,22 +69,75 @@ func (rCmd *CloneCmd) Init(cmd *cobra.Command, args []string) error {
rCmd.taskID = config.GetBsFlagString(rCmd.Cmd, config.CURVEBS_TASKID)
rCmd.all = config.GetBsFlagBool(rCmd.Cmd, config.CURVEBS_ALL)
rCmd.failed = config.GetBsFlagBool(rCmd.Cmd, config.CURVEBS_FAILED)
if rCmd.failed {
rCmd.status = "5"
}
rCmd.SetHeader([]string{cobrautil.ROW_USER, cobrautil.ROW_SRC, cobrautil.ROW_TASK_ID, cobrautil.ROW_FILE, cobrautil.ROW_RESULT})
return nil
}

func (rCmd *CloneCmd) RunCommand(cmd *cobra.Command, args []string) error {
c := newCloneOrRecover(rCmd.snapshotAddrs, rCmd.timeout, rCmd.user, rCmd.src, rCmd.dest, rCmd.taskID, rCmd.all, rCmd.failed)
records := c.queryCloneOrRecoverBy(cloneTaskType)
for _, item := range records {
err := c.cleanCloneOrRecover(item.UUID, item.User)
if err == nil {
item.Result = "success"
params := map[string]any{
cobrautil.QueryAction: cobrautil.ActionGetCloneTaskList,
cobrautil.QueryType: cobrautil.TypeCloneTask,
cobrautil.QueryUser: rCmd.user,
cobrautil.QueryUUID: rCmd.taskID,
cobrautil.QuerySource: rCmd.src,
cobrautil.QueryDestination: rCmd.dest,
cobrautil.QueryStatus: rCmd.status,
cobrautil.QueryLimit: 100,
cobrautil.QueryOffset: 0,
}
records := make([]map[string]string, 0)
for {
subUri := cobrautil.NewSnapshotQuerySubUri(params)
metric := basecmd.NewMetric(rCmd.snapshotAddrs, subUri, rCmd.timeout)
result, err := basecmd.QueryMetric(metric)
if err.TypeCode() != cmderror.CODE_SUCCESS {
return err.ToError()
}

var resp struct {
Code string `json:"Code"`
TaskInfos []map[string]string `json:"TaskInfos"`
TotalCount int `json:"TotalCount"`
}
if err := json.Unmarshal([]byte(result), &resp); err != nil {
return err
}
if resp.Code != "0" {
return fmt.Errorf("get clone list fail, error code: %s", resp.Code)
}
if len(resp.TaskInfos) == 0 {
break
} else {
item.Result = "fail"
records = append(records, resp.TaskInfos...)
params[cobrautil.QueryOffset] = params[cobrautil.QueryOffset].(int) + params[cobrautil.QueryLimit].(int)
}
rCmd.TableNew.Append([]string{item.User, item.Src, item.UUID, item.File, item.Result})
}

wg := sync.WaitGroup{}
for _, item := range records {
wg.Add(1)
go func(item map[string]string) {
params := map[string]any{
cobrautil.QueryAction: cobrautil.ActionCleanCloneTask,
cobrautil.QueryUser: item["User"],
cobrautil.QueryUUID: item["UUID"],
}
subUri := cobrautil.NewSnapshotQuerySubUri(params)
metric := basecmd.NewMetric(rCmd.snapshotAddrs, subUri, rCmd.timeout)
_, err := basecmd.QueryMetric(metric)
if err.TypeCode() != cmderror.CODE_SUCCESS {
item["Result"] = "fail"
} else {
item["Result"] = "success"
}
rCmd.TableNew.Append([]string{item["User"], item["Src"], item["UUID"], item["File"], item["Result"]})
wg.Done()
}(item)
}
wg.Wait()
rCmd.Result = records
rCmd.Error = cmderror.Success()
return nil
Expand Down
205 changes: 0 additions & 205 deletions tools-v2/pkg/cli/command/curvebs/delete/volume/clone_or_recover.go

This file was deleted.

Loading

0 comments on commit 862d8b0

Please sign in to comment.