Skip to content

Commit

Permalink
support spdk tgt
Browse files Browse the repository at this point in the history
Signed-off-by: YLShiJustFly <youseeicanfly@126.com>
  • Loading branch information
asignmisspast committed Sep 18, 2023
1 parent 3b6b0a1 commit 39aaf68
Show file tree
Hide file tree
Showing 21 changed files with 1,442 additions and 25 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,3 @@ upload:
lint:
go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANGCILINT_VERSION)
$(GOBIN_GOLANGCILINT) run -v

2 changes: 2 additions & 0 deletions cli/command/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"github.com/opencurve/curveadm/cli/command/pfs"
"github.com/opencurve/curveadm/cli/command/playground"
"github.com/opencurve/curveadm/cli/command/target"
"github.com/opencurve/curveadm/cli/command/spdk_target"
"github.com/opencurve/curveadm/cli/command/website"
"github.com/opencurve/curveadm/internal/errno"
tools "github.com/opencurve/curveadm/internal/tools/upgrade"
Expand Down Expand Up @@ -68,6 +69,7 @@ func addSubCommands(cmd *cobra.Command, curveadm *cli.CurveAdm) {
disks.NewDisksCommand(curveadm), // curveadm disks ...
playground.NewPlaygroundCommand(curveadm), // curveadm playground ...
target.NewTargetCommand(curveadm), // curveadm target ...
spdk_target.NewSpdkTargetCommand(curveadm), // curveadm target ...
pfs.NewPFSCommand(curveadm), // curveadm pfs ...
monitor.NewMonitorCommand(curveadm), // curveadm monitor ...
http.NewHttpCommand(curveadm), // curveadm http
Expand Down
151 changes: 151 additions & 0 deletions cli/command/spdk_target/add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
* Copyright (c) 2021 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: CurveAdm
* Created Date: 2022-02-08
* Author: Jingli Chen (Wine93)
*/

package spdk_target

import (
"github.com/fatih/color"
"github.com/opencurve/curveadm/cli/cli"
"github.com/opencurve/curveadm/cli/command/client"
comm "github.com/opencurve/curveadm/internal/common"
"github.com/opencurve/curveadm/internal/configure"
"github.com/opencurve/curveadm/internal/configure/topology"
"github.com/opencurve/curveadm/internal/errno"
"github.com/opencurve/curveadm/internal/playbook"
"github.com/opencurve/curveadm/internal/task/task/bs"
cliutil "github.com/opencurve/curveadm/internal/utils"
utils "github.com/opencurve/curveadm/internal/utils"
"github.com/spf13/cobra"
)

var (
ADD_PLAYBOOK_STEPS = []int{
//playbook.CREATE_VOLUME,
playbook.ADD_TARGET,
}
)

type addOptions struct {
image string
host string
size string
create bool
filename string
blocksize string
}

func checkAddOptions(curveadm *cli.CurveAdm, options addOptions) error {
if _, _, err := client.ParseImage(options.image); err != nil {
return err
} else if _, err = client.ParseSize(options.size); err != nil {
return err
} else if _, err = client.ParseBlockSize(options.blocksize); err != nil {
return err
} else if !utils.PathExist(options.filename) {
return errno.ERR_CLIENT_CONFIGURE_FILE_NOT_EXIST.
F("file path: %s", utils.AbsPath(options.filename))
}
return nil
}

func NewAddCommand(curveadm *cli.CurveAdm) *cobra.Command {
var options addOptions

cmd := &cobra.Command{
Use: "add USER:VOLUME [OPTIONS]",
Short: "Add a spdk target of CurveBS",
Args: cliutil.ExactArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
options.image = args[0]
return checkAddOptions(curveadm, options)
},
RunE: func(cmd *cobra.Command, args []string) error {
options.image = args[0]
return runAdd(curveadm, options)
},
DisableFlagsInUseLine: true,
}

flags := cmd.Flags()
flags.StringVar(&options.host, "host", "localhost", "Specify spdk target host")
flags.BoolVar(&options.create, "create", false, "Create volume if not exist")
flags.StringVar(&options.size, "size", "10GiB", "Specify volume size")
flags.StringVarP(&options.filename, "conf", "c", "client.yaml", "Specify client configuration file")
flags.StringVar(&options.blocksize, "blocksize", "4096B", "Specify volume blocksize")
return cmd
}

func genAddPlaybook(curveadm *cli.CurveAdm,
ccs []*configure.ClientConfig,
options addOptions) (*playbook.Playbook, error) {
user, name, _ := client.ParseImage(options.image)
size, _ := client.ParseSize(options.size)
blocksize, _ := client.ParseBlockSize(options.blocksize)
steps := ADD_PLAYBOOK_STEPS
pb := playbook.NewPlaybook(curveadm)
for _, step := range steps {
pb.AddStep(&playbook.PlaybookStep{
Type: step,
Configs: ccs,
Options: map[string]interface{}{
comm.KEY_SPDK_TARGET_OPTIONS: bs.SpdkTargetOption{
Host: options.host,
User: user,
Volume: name,
Size: uint64(size),
Blocksize: blocksize,
Create: options.create,
},
},
})
}
return pb, nil
}

func runAdd(curveadm *cli.CurveAdm, options addOptions) error {
// 1) parse client configure
cc, err := configure.ParseClientConfig(options.filename)
if err != nil {
return err
} else if cc.GetKind() != topology.KIND_CURVEBS {
return errno.ERR_REQUIRE_CURVEBS_KIND_CLIENT_CONFIGURE_FILE.
F("kind: %s", cc.GetKind())
}

// 2) generate map playbook
pb, err := genAddPlaybook(curveadm, []*configure.ClientConfig{cc}, options)
if err != nil {
return err
}

// 3) run playground
err = pb.Run()
if err != nil {
return err
}

// 4) print success prompt
curveadm.WriteOutln("")
curveadm.WriteOutln(color.GreenString("Add target (%s) to %s success ^_^"),
options.image, options.host)
return nil
}
47 changes: 47 additions & 0 deletions cli/command/spdk_target/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2021 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: CurveAdm
* Created Date: 2022-02-08
* Author: Jingli Chen (Wine93)
*/

package spdk_target

import (
"github.com/opencurve/curveadm/cli/cli"
cliutil "github.com/opencurve/curveadm/internal/utils"
"github.com/spf13/cobra"
)

func NewSpdkTargetCommand(curveadm *cli.CurveAdm) *cobra.Command {
cmd := &cobra.Command{
Use: "spdktarget",
Short: "Manage spdk target of CurveBS",
Args: cliutil.NoArgs,
RunE: cliutil.ShowHelp(curveadm.Err()),
}

cmd.AddCommand(
NewStartCommand(curveadm),
NewStopCommand(curveadm),
NewAddCommand(curveadm),
NewDeleteCommand(curveadm),
NewListCommand(curveadm),
)
return cmd
}
109 changes: 109 additions & 0 deletions cli/command/spdk_target/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright (c) 2021 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: CurveAdm
* Created Date: 2022-02-09
* Author: Jingli Chen (Wine93)
*/

package spdk_target

import (
"github.com/fatih/color"
"github.com/opencurve/curveadm/cli/cli"
comm "github.com/opencurve/curveadm/internal/common"
"github.com/opencurve/curveadm/internal/playbook"
"github.com/opencurve/curveadm/internal/task/task/bs"
cliutil "github.com/opencurve/curveadm/internal/utils"
"github.com/spf13/cobra"
)

var (
DELETE_PLAYBOOK_STEPS = []int{
playbook.DELETE_TARGET,
}
)

type deleteOptions struct {
host string
sockname string
rpcpath string
tid string
}

func NewDeleteCommand(curveadm *cli.CurveAdm) *cobra.Command {
var options deleteOptions

cmd := &cobra.Command{
Use: "rm TID [OPTIONS]",
Aliases: []string{"delete"},
Short: "Delete a spdk target of CurveBS",
Args: cliutil.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
options.sockname = args[0]
options.rpcpath = args[1]
options.tid = args[2]
return runDelete(curveadm, options)
},
DisableFlagsInUseLine: true,
}

flags := cmd.Flags()
flags.StringVar(&options.host, "host", "localhost", "Specify spdk target host")

return cmd
}

func genDeletePlaybook(curveadm *cli.CurveAdm, options deleteOptions) (*playbook.Playbook, error) {
steps := DELETE_PLAYBOOK_STEPS
pb := playbook.NewPlaybook(curveadm)
for _, step := range steps {
pb.AddStep(&playbook.PlaybookStep{
Type: step,
Configs: nil,
Options: map[string]interface{}{
comm.KEY_SPDK_TARGET_OPTIONS: bs.SpdkTargetOption{
Host: options.host,
Sockname: options.sockname,
RpcPath: options.rpcpath,
Tid: options.tid,
},
},
})
}
return pb, nil
}

func runDelete(curveadm *cli.CurveAdm, options deleteOptions) error {
// 1) generate list playbook
pb, err := genDeletePlaybook(curveadm, options)
if err != nil {
return err
}

// 2) run playground
err = pb.Run()
if err != nil {
return err
}

// 3) print targets
curveadm.WriteOutln("")
curveadm.WriteOutln(color.GreenString("Delete target (tid=%s) on %s success ^_^"),
options.tid, options.host)
return nil
}
Loading

0 comments on commit 39aaf68

Please sign in to comment.