Skip to content

Commit 480994d

Browse files
committed
support spdk tgt
Signed-off-by: YLShiJustFly <youseeicanfly@126.com>
1 parent 3b6b0a1 commit 480994d

File tree

21 files changed

+1447
-25
lines changed

21 files changed

+1447
-25
lines changed

Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,3 @@ upload:
7373
lint:
7474
go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(GOLANGCILINT_VERSION)
7575
$(GOBIN_GOLANGCILINT) run -v
76-

cli/command/cmd.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"github.com/opencurve/curveadm/cli/command/pfs"
3939
"github.com/opencurve/curveadm/cli/command/playground"
4040
"github.com/opencurve/curveadm/cli/command/target"
41+
"github.com/opencurve/curveadm/cli/command/spdk_target"
4142
"github.com/opencurve/curveadm/cli/command/website"
4243
"github.com/opencurve/curveadm/internal/errno"
4344
tools "github.com/opencurve/curveadm/internal/tools/upgrade"
@@ -68,6 +69,7 @@ func addSubCommands(cmd *cobra.Command, curveadm *cli.CurveAdm) {
6869
disks.NewDisksCommand(curveadm), // curveadm disks ...
6970
playground.NewPlaygroundCommand(curveadm), // curveadm playground ...
7071
target.NewTargetCommand(curveadm), // curveadm target ...
72+
spdk_target.NewSpdkTargetCommand(curveadm), // curveadm target ...
7173
pfs.NewPFSCommand(curveadm), // curveadm pfs ...
7274
monitor.NewMonitorCommand(curveadm), // curveadm monitor ...
7375
http.NewHttpCommand(curveadm), // curveadm http

cli/command/spdk_target/add.go

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/*
2+
* Copyright (c) 2021 NetEase Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/*
18+
* Project: CurveAdm
19+
* Created Date: 2022-02-08
20+
* Author: Jingli Chen (Wine93)
21+
*/
22+
23+
package spdk_target
24+
25+
import (
26+
"github.com/fatih/color"
27+
"github.com/opencurve/curveadm/cli/cli"
28+
"github.com/opencurve/curveadm/cli/command/client"
29+
comm "github.com/opencurve/curveadm/internal/common"
30+
"github.com/opencurve/curveadm/internal/configure"
31+
"github.com/opencurve/curveadm/internal/configure/topology"
32+
"github.com/opencurve/curveadm/internal/errno"
33+
"github.com/opencurve/curveadm/internal/playbook"
34+
"github.com/opencurve/curveadm/internal/task/task/bs"
35+
cliutil "github.com/opencurve/curveadm/internal/utils"
36+
utils "github.com/opencurve/curveadm/internal/utils"
37+
"github.com/spf13/cobra"
38+
)
39+
40+
var (
41+
ADD_PLAYBOOK_STEPS = []int{
42+
//playbook.CREATE_VOLUME,
43+
playbook.ADD_TARGET,
44+
}
45+
)
46+
47+
type addOptions struct {
48+
image string
49+
host string
50+
size string
51+
create bool
52+
filename string
53+
blocksize string
54+
}
55+
56+
func checkAddOptions(curveadm *cli.CurveAdm, options addOptions) error {
57+
if _, _, err := client.ParseImage(options.image); err != nil {
58+
return err
59+
} else if _, err = client.ParseSize(options.size); err != nil {
60+
return err
61+
} else if _, err = client.ParseBlockSize(options.blocksize); err != nil {
62+
return err
63+
} else if !utils.PathExist(options.filename) {
64+
return errno.ERR_CLIENT_CONFIGURE_FILE_NOT_EXIST.
65+
F("file path: %s", utils.AbsPath(options.filename))
66+
}
67+
return nil
68+
}
69+
70+
func NewAddCommand(curveadm *cli.CurveAdm) *cobra.Command {
71+
var options addOptions
72+
73+
cmd := &cobra.Command{
74+
Use: "add USER:VOLUME [OPTIONS]",
75+
Short: "Add a spdk target of CurveBS",
76+
Args: cliutil.ExactArgs(1),
77+
PreRunE: func(cmd *cobra.Command, args []string) error {
78+
options.image = args[0]
79+
return checkAddOptions(curveadm, options)
80+
},
81+
RunE: func(cmd *cobra.Command, args []string) error {
82+
options.image = args[0]
83+
return runAdd(curveadm, options)
84+
},
85+
DisableFlagsInUseLine: true,
86+
}
87+
88+
flags := cmd.Flags()
89+
flags.StringVar(&options.host, "host", "localhost", "Specify spdk target host")
90+
flags.BoolVar(&options.create, "create", false, "Create volume if not exist")
91+
flags.StringVar(&options.size, "size", "10GiB", "Specify volume size")
92+
flags.StringVarP(&options.filename, "conf", "c", "client.yaml", "Specify client configuration file")
93+
flags.StringVar(&options.blocksize, "blocksize", "4096B", "Specify volume blocksize")
94+
return cmd
95+
}
96+
97+
func genAddPlaybook(curveadm *cli.CurveAdm,
98+
ccs []*configure.ClientConfig,
99+
options addOptions) (*playbook.Playbook, error) {
100+
user, name, _ := client.ParseImage(options.image)
101+
size, _ := client.ParseSize(options.size)
102+
blocksize, _ := client.ParseBlockSize(options.blocksize)
103+
steps := ADD_PLAYBOOK_STEPS
104+
pb := playbook.NewPlaybook(curveadm)
105+
for _, step := range steps {
106+
pb.AddStep(&playbook.PlaybookStep{
107+
Type: step,
108+
Configs: ccs,
109+
Options: map[string]interface{}{
110+
comm.KEY_SPDK_TARGET_OPTIONS: bs.SpdkTargetOption{
111+
Host: options.host,
112+
User: user,
113+
Volume: name,
114+
Size: uint64(size),
115+
Blocksize: blocksize,
116+
Create: options.create,
117+
},
118+
},
119+
})
120+
}
121+
return pb, nil
122+
}
123+
124+
func runAdd(curveadm *cli.CurveAdm, options addOptions) error {
125+
// 1) parse client configure
126+
cc, err := configure.ParseClientConfig(options.filename)
127+
if err != nil {
128+
return err
129+
} else if cc.GetKind() != topology.KIND_CURVEBS {
130+
return errno.ERR_REQUIRE_CURVEBS_KIND_CLIENT_CONFIGURE_FILE.
131+
F("kind: %s", cc.GetKind())
132+
}
133+
134+
// 2) generate map playbook
135+
pb, err := genAddPlaybook(curveadm, []*configure.ClientConfig{cc}, options)
136+
if err != nil {
137+
return err
138+
}
139+
140+
// 3) run playground
141+
err = pb.Run()
142+
if err != nil {
143+
return err
144+
}
145+
146+
// 4) print success prompt
147+
curveadm.WriteOutln("")
148+
curveadm.WriteOutln(color.GreenString("Add target (%s) to %s success ^_^"),
149+
options.image, options.host)
150+
return nil
151+
}

cli/command/spdk_target/cmd.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2021 NetEase Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/*
18+
* Project: CurveAdm
19+
* Created Date: 2022-02-08
20+
* Author: Jingli Chen (Wine93)
21+
*/
22+
23+
package spdk_target
24+
25+
import (
26+
"github.com/opencurve/curveadm/cli/cli"
27+
cliutil "github.com/opencurve/curveadm/internal/utils"
28+
"github.com/spf13/cobra"
29+
)
30+
31+
func NewSpdkTargetCommand(curveadm *cli.CurveAdm) *cobra.Command {
32+
cmd := &cobra.Command{
33+
Use: "spdktarget",
34+
Short: "Manage spdk target of CurveBS",
35+
Args: cliutil.NoArgs,
36+
RunE: cliutil.ShowHelp(curveadm.Err()),
37+
}
38+
39+
cmd.AddCommand(
40+
NewStartCommand(curveadm),
41+
NewStopCommand(curveadm),
42+
NewAddCommand(curveadm),
43+
NewDeleteCommand(curveadm),
44+
NewListCommand(curveadm),
45+
)
46+
return cmd
47+
}

cli/command/spdk_target/delete.go

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright (c) 2021 NetEase Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/*
18+
* Project: CurveAdm
19+
* Created Date: 2022-02-09
20+
* Author: Jingli Chen (Wine93)
21+
*/
22+
23+
package spdk_target
24+
25+
import (
26+
"github.com/fatih/color"
27+
"github.com/opencurve/curveadm/cli/cli"
28+
comm "github.com/opencurve/curveadm/internal/common"
29+
"github.com/opencurve/curveadm/internal/playbook"
30+
"github.com/opencurve/curveadm/internal/task/task/bs"
31+
cliutil "github.com/opencurve/curveadm/internal/utils"
32+
"github.com/spf13/cobra"
33+
)
34+
35+
var (
36+
DELETE_PLAYBOOK_STEPS = []int{
37+
playbook.DELETE_TARGET,
38+
}
39+
)
40+
41+
type deleteOptions struct {
42+
host string
43+
sockname string
44+
rpcpath string
45+
tid string
46+
}
47+
48+
func NewDeleteCommand(curveadm *cli.CurveAdm) *cobra.Command {
49+
var options deleteOptions
50+
51+
cmd := &cobra.Command{
52+
Use: "rm TID [OPTIONS]",
53+
Aliases: []string{"delete"},
54+
Short: "Delete a spdk target of CurveBS",
55+
Args: cliutil.ExactArgs(1),
56+
RunE: func(cmd *cobra.Command, args []string) error {
57+
options.sockname = args[0]
58+
options.rpcpath = args[1]
59+
options.tid = args[2]
60+
return runDelete(curveadm, options)
61+
},
62+
DisableFlagsInUseLine: true,
63+
}
64+
65+
flags := cmd.Flags()
66+
flags.StringVar(&options.host, "host", "localhost", "Specify spdk target host")
67+
68+
return cmd
69+
}
70+
71+
func genDeletePlaybook(curveadm *cli.CurveAdm, options deleteOptions) (*playbook.Playbook, error) {
72+
steps := DELETE_PLAYBOOK_STEPS
73+
pb := playbook.NewPlaybook(curveadm)
74+
for _, step := range steps {
75+
pb.AddStep(&playbook.PlaybookStep{
76+
Type: step,
77+
Configs: nil,
78+
Options: map[string]interface{}{
79+
comm.KEY_SPDK_TARGET_OPTIONS: bs.SpdkTargetOption{
80+
Host: options.host,
81+
Sockname: options.sockname,
82+
RpcPath: options.rpcpath,
83+
Tid: options.tid,
84+
},
85+
},
86+
})
87+
}
88+
return pb, nil
89+
}
90+
91+
func runDelete(curveadm *cli.CurveAdm, options deleteOptions) error {
92+
// 1) generate list playbook
93+
pb, err := genDeletePlaybook(curveadm, options)
94+
if err != nil {
95+
return err
96+
}
97+
98+
// 2) run playground
99+
err = pb.Run()
100+
if err != nil {
101+
return err
102+
}
103+
104+
// 3) print targets
105+
curveadm.WriteOutln("")
106+
curveadm.WriteOutln(color.GreenString("Delete target (tid=%s) on %s success ^_^"),
107+
options.tid, options.host)
108+
return nil
109+
}

0 commit comments

Comments
 (0)